fork of go-git with some jj specific features
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #809 from AriehSchneier/tilde-user-path

*: Handle paths starting with ~Username

authored by

Paulo Gomes and committed by
GitHub
56c4bf4c dc17aae6

+52 -30
+29
internal/path_util/path_util.go
··· 1 + package path_util 2 + 3 + import ( 4 + "os" 5 + "os/user" 6 + "strings" 7 + ) 8 + 9 + func ReplaceTildeWithHome(path string) (string, error) { 10 + if strings.HasPrefix(path, "~") { 11 + firstSlash := strings.Index(path, "/") 12 + if firstSlash == 1 { 13 + home, err := os.UserHomeDir() 14 + if err != nil { 15 + return path, err 16 + } 17 + return strings.Replace(path, "~", home, 1), nil 18 + } else if firstSlash > 1 { 19 + username := path[1:firstSlash] 20 + userAccount, err := user.Lookup(username) 21 + if err != nil { 22 + return path, err 23 + } 24 + return strings.Replace(path, path[:firstSlash], userAccount.HomeDir, 1), nil 25 + } 26 + } 27 + 28 + return path, nil 29 + }
+2 -16
plumbing/format/gitignore/dir.go
··· 5 5 "bytes" 6 6 "io" 7 7 "os" 8 - "os/user" 9 8 "strings" 10 9 11 10 "github.com/go-git/go-billy/v5" 11 + "github.com/go-git/go-git/v5/internal/path_util" 12 12 "github.com/go-git/go-git/v5/plumbing/format/config" 13 13 gioutil "github.com/go-git/go-git/v5/utils/ioutil" 14 14 ) ··· 27 27 // readIgnoreFile reads a specific git ignore file. 28 28 func readIgnoreFile(fs billy.Filesystem, path []string, ignoreFile string) (ps []Pattern, err error) { 29 29 30 - if strings.HasPrefix(ignoreFile, "~") { 31 - firstSlash := strings.Index(ignoreFile, "/") 32 - if firstSlash == 1 { 33 - home, err := os.UserHomeDir() 34 - if err == nil { 35 - ignoreFile = strings.Replace(ignoreFile, "~", home, 1) 36 - } 37 - } else if firstSlash > 1 { 38 - username := ignoreFile[1:firstSlash] 39 - userAccount, err := user.Lookup(username) 40 - if err == nil { 41 - ignoreFile = strings.Replace(ignoreFile, ignoreFile[:firstSlash], userAccount.HomeDir, 1) 42 - } 43 - } 44 - } 30 + ignoreFile, _ = path_util.ReplaceTildeWithHome(ignoreFile) 45 31 46 32 f, err := fs.Open(fs.Join(append(path, ignoreFile)...)) 47 33 if err == nil {
+8 -10
repository.go
··· 19 19 "github.com/go-git/go-billy/v5/osfs" 20 20 "github.com/go-git/go-billy/v5/util" 21 21 "github.com/go-git/go-git/v5/config" 22 + "github.com/go-git/go-git/v5/internal/path_util" 22 23 "github.com/go-git/go-git/v5/internal/revision" 23 24 "github.com/go-git/go-git/v5/plumbing" 24 25 "github.com/go-git/go-git/v5/plumbing/cache" ··· 322 323 } 323 324 324 325 func dotGitToOSFilesystems(path string, detect bool) (dot, wt billy.Filesystem, err error) { 325 - if strings.HasPrefix(path, "~/") { 326 - home, err := os.UserHomeDir() 327 - if err != nil { 328 - return nil, nil, err 329 - } 330 - path = filepath.Join(home, path[2:]) 331 - } else { 332 - if path, err = filepath.Abs(path); err != nil { 333 - return nil, nil, err 334 - } 326 + path, err = path_util.ReplaceTildeWithHome(path) 327 + if err != nil { 328 + return nil, nil, err 329 + } 330 + 331 + if path, err = filepath.Abs(path); err != nil { 332 + return nil, nil, err 335 333 } 336 334 337 335 var fs billy.Filesystem
+13 -4
repository_test.go
··· 8 8 "io" 9 9 "os" 10 10 "os/exec" 11 + "os/user" 11 12 "path/filepath" 12 13 "regexp" 13 14 "strings" ··· 551 552 c.Assert(err, IsNil) 552 553 c.Assert(r, NotNil) 553 554 554 - path := strings.Replace(dir, strings.Split(dir, ".tmp")[0], "~/", 1) 555 - 556 - r, err = PlainOpen(path) 555 + currentUser, err := user.Current() 557 556 c.Assert(err, IsNil) 558 - c.Assert(r, NotNil) 557 + // remove domain for windows 558 + username := currentUser.Username[strings.Index(currentUser.Username, "\\")+1:] 559 + 560 + homes := []string{"~/", "~" + username + "/"} 561 + for _, home := range homes { 562 + path := strings.Replace(dir, strings.Split(dir, ".tmp")[0], home, 1) 563 + 564 + r, err = PlainOpen(path) 565 + c.Assert(err, IsNil) 566 + c.Assert(r, NotNil) 567 + } 559 568 } 560 569 561 570 func (s *RepositorySuite) TestPlainOpenBare(c *C) {