Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: Handle paths starting with tilde #808

Merged
merged 2 commits into from
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,26 @@ func (s *BaseSuite) TemporalDir() (path string, clean func()) {
return
}

func (s *BaseSuite) TemporalHomeDir() (path string, clean func()) {
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}

fs := osfs.New(home)
relPath, err := util.TempDir(fs, "", "")
if err != nil {
panic(err)
}

path = fs.Join(fs.Root(), relPath)
clean = func() {
_ = util.RemoveAll(fs, relPath)
}

return
}

func (s *BaseSuite) TemporalFilesystem() (fs billy.Filesystem, clean func()) {
fs = osfs.New(os.TempDir())
path, err := util.TempDir(fs, "", "")
Expand Down
12 changes: 10 additions & 2 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,16 @@ func PlainOpenWithOptions(path string, o *PlainOpenOptions) (*Repository, error)
}

func dotGitToOSFilesystems(path string, detect bool) (dot, wt billy.Filesystem, err error) {
if path, err = filepath.Abs(path); err != nil {
return nil, nil, err
if strings.HasPrefix(path, "~/") {
home, err := os.UserHomeDir()
if err != nil {
return nil, nil, err
}
path = filepath.Join(home, path[2:])
} else {
if path, err = filepath.Abs(path); err != nil {
return nil, nil, err
}
}

var fs billy.Filesystem
Expand Down
15 changes: 15 additions & 0 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,21 @@ func (s *RepositorySuite) TestPlainOpen(c *C) {
c.Assert(r, NotNil)
}

func (s *RepositorySuite) TestPlainOpenTildePath(c *C) {
dir, clean := s.TemporalHomeDir()
defer clean()

r, err := PlainInit(dir, false)
c.Assert(err, IsNil)
c.Assert(r, NotNil)

path := strings.Replace(dir, strings.Split(dir, ".tmp")[0], "~/", 1)

r, err = PlainOpen(path)
c.Assert(err, IsNil)
c.Assert(r, NotNil)
}

func (s *RepositorySuite) TestPlainOpenBare(c *C) {
dir, clean := s.TemporalDir()
defer clean()
Expand Down