Skip to content

Commit

Permalink
*: Handle paths starting with ~Username
Browse files Browse the repository at this point in the history
Signed-off-by: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com>
  • Loading branch information
AriehSchneier committed Jul 9, 2023
1 parent dc17aae commit 4a7b487
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 26 deletions.
29 changes: 29 additions & 0 deletions internal/path_util/path_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package path_util

import (
"os"
"os/user"
"strings"
)

func ReplaceTildeWithHome(path string) (string, error) {
if strings.HasPrefix(path, "~") {
firstSlash := strings.Index(path, "/")
if firstSlash == 1 {
home, err := os.UserHomeDir()
if err != nil {
return path, err
}
return strings.Replace(path, "~", home, 1), nil
} else if firstSlash > 1 {
username := path[1:firstSlash]
userAccount, err := user.Lookup(username)
if err != nil {
return path, err
}
return strings.Replace(path, path[:firstSlash], userAccount.HomeDir, 1), nil
}
}

return "", nil
}
18 changes: 2 additions & 16 deletions plumbing/format/gitignore/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"bytes"
"io"
"os"
"os/user"
"strings"

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-git/v5/internal/path_util"
"github.com/go-git/go-git/v5/plumbing/format/config"
gioutil "github.com/go-git/go-git/v5/utils/ioutil"
)
Expand All @@ -27,21 +27,7 @@ const (
// readIgnoreFile reads a specific git ignore file.
func readIgnoreFile(fs billy.Filesystem, path []string, ignoreFile string) (ps []Pattern, err error) {

if strings.HasPrefix(ignoreFile, "~") {
firstSlash := strings.Index(ignoreFile, "/")
if firstSlash == 1 {
home, err := os.UserHomeDir()
if err == nil {
ignoreFile = strings.Replace(ignoreFile, "~", home, 1)
}
} else if firstSlash > 1 {
username := ignoreFile[1:firstSlash]
userAccount, err := user.Lookup(username)
if err == nil {
ignoreFile = strings.Replace(ignoreFile, ignoreFile[:firstSlash], userAccount.HomeDir, 1)
}
}
}
ignoreFile, _ = path_util.ReplaceTildeWithHome(ignoreFile)

f, err := fs.Open(fs.Join(append(path, ignoreFile)...))
if err == nil {
Expand Down
18 changes: 8 additions & 10 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-billy/v5/util"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/internal/path_util"
"github.com/go-git/go-git/v5/internal/revision"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/cache"
Expand Down Expand Up @@ -322,16 +323,13 @@ func PlainOpenWithOptions(path string, o *PlainOpenOptions) (*Repository, error)
}

func dotGitToOSFilesystems(path string, detect bool) (dot, wt billy.Filesystem, err error) {
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
}
path, err = path_util.ReplaceTildeWithHome(path)
if err != nil {
return nil, nil, err
}

if path, err = filepath.Abs(path); err != nil {
return nil, nil, err
}

var fs billy.Filesystem
Expand Down
21 changes: 21 additions & 0 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"os"
"os/exec"
"os/user"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -558,6 +559,26 @@ func (s *RepositorySuite) TestPlainOpenTildePath(c *C) {
c.Assert(r, NotNil)
}

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

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

currentUser, err := user.Current()
c.Assert(err, IsNil)
// remove domain for windows
username := currentUser.Username[strings.Index(currentUser.Username, "\\")+1:]

path := strings.Replace(dir, strings.Split(dir, ".tmp")[0], "~"+username+"/", 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

0 comments on commit 4a7b487

Please sign in to comment.