aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaulo Gomes <pjbgf@linux.com>2023-07-11 08:53:03 +0100
committerGitHub <noreply@github.com>2023-07-11 08:53:03 +0100
commit56c4bf4ca9789505db7a6eefb910a7259c1fcb79 (patch)
tree3049fbf24af25a3e52bf7a391e959e837e032e3a
parentdc17aae6503560777a665c9cfb0d2fcb3a9a9274 (diff)
parent5dad9b23030e344a4fd1458df0c50e6ada55a01a (diff)
downloadgo-git-56c4bf4ca9789505db7a6eefb910a7259c1fcb79.tar.gz
Merge pull request #809 from AriehSchneier/tilde-user-path
*: Handle paths starting with ~Username
-rw-r--r--internal/path_util/path_util.go29
-rw-r--r--plumbing/format/gitignore/dir.go18
-rw-r--r--repository.go18
-rw-r--r--repository_test.go17
4 files changed, 52 insertions, 30 deletions
diff --git a/internal/path_util/path_util.go b/internal/path_util/path_util.go
new file mode 100644
index 0000000..48e4a3d
--- /dev/null
+++ b/internal/path_util/path_util.go
@@ -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 path, nil
+}
diff --git a/plumbing/format/gitignore/dir.go b/plumbing/format/gitignore/dir.go
index 3c4469a..d8fb30c 100644
--- a/plumbing/format/gitignore/dir.go
+++ b/plumbing/format/gitignore/dir.go
@@ -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"
)
@@ -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 {
diff --git a/repository.go b/repository.go
index 168303f..02edb66 100644
--- a/repository.go
+++ b/repository.go
@@ -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"
@@ -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
diff --git a/repository_test.go b/repository_test.go
index 50384e7..9e000a3 100644
--- a/repository_test.go
+++ b/repository_test.go
@@ -8,6 +8,7 @@ import (
"io"
"os"
"os/exec"
+ "os/user"
"path/filepath"
"regexp"
"strings"
@@ -551,11 +552,19 @@ func (s *RepositorySuite) TestPlainOpenTildePath(c *C) {
c.Assert(err, IsNil)
c.Assert(r, NotNil)
- path := strings.Replace(dir, strings.Split(dir, ".tmp")[0], "~/", 1)
-
- r, err = PlainOpen(path)
+ currentUser, err := user.Current()
c.Assert(err, IsNil)
- c.Assert(r, NotNil)
+ // remove domain for windows
+ username := currentUser.Username[strings.Index(currentUser.Username, "\\")+1:]
+
+ homes := []string{"~/", "~" + username + "/"}
+ for _, home := range homes {
+ path := strings.Replace(dir, strings.Split(dir, ".tmp")[0], home, 1)
+
+ r, err = PlainOpen(path)
+ c.Assert(err, IsNil)
+ c.Assert(r, NotNil)
+ }
}
func (s *RepositorySuite) TestPlainOpenBare(c *C) {