Skip to content

Commit

Permalink
ExpandPath fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
fcharlie committed Dec 25, 2024
1 parent bcf4847 commit 37c98d1
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 49 deletions.
28 changes: 3 additions & 25 deletions modules/plumbing/format/ignore/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package ignore
import (
"bufio"
"os"
"os/user"
"path/filepath"
"strings"

"github.com/antgroup/hugescm/modules/strengthen"
"github.com/antgroup/hugescm/modules/vfs"
)

Expand All @@ -18,31 +18,9 @@ const (
infoExcludeFile = zetaDir + "/info/exclude"
)

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
}

// readIgnoreFile reads a specific git ignore file.
func readIgnoreFile(fs vfs.VFS, path []string, ignoreFile string) (ps []Pattern, err error) {
ignoreFile, _ = ReplaceTildeWithHome(ignoreFile)
ignoreFile = strengthen.ExpandPath(ignoreFile)
f, err := os.Open(fs.Join(append(path, ignoreFile)...))
if err == nil {
defer f.Close()
Expand All @@ -61,7 +39,7 @@ func readIgnoreFile(fs vfs.VFS, path []string, ignoreFile string) (ps []Pattern,
return
}

// ReadPatterns reads the .git/info/exclude and then the gitignore patterns
// ReadPatterns reads the .zeta/info/exclude and then the zetaignore patterns
// recursively traversing through the directory structure. The result is in
// the ascending order of priority (last higher).
func ReadPatterns(fs vfs.VFS, path []string) (ps []Pattern, err error) {
Expand Down
13 changes: 13 additions & 0 deletions modules/plumbing/format/ignore/ignore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ignore

import (
"fmt"
"os"
"testing"
)

func TestMatch(t *testing.T) {
p := ParsePattern("**/*lue/vol?ano", nil)
r := p.Match([]string{"head", "value", "volcano", "tail"}, false)
fmt.Fprintf(os.Stderr, "%v\n", r)
}
43 changes: 25 additions & 18 deletions modules/plumbing/format/ignore/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,28 @@ func (p *pattern) Match(path []string, isDir bool) MatchResult {
}

path = path[len(p.domain):]
if p.isGlob && !p.globMatch(path, isDir) {
return NoMatch
} else if !p.isGlob && !p.simpleNameMatch(path, isDir) {
return NoMatch
if p.isGlob {
if !p.globMatch(path, isDir) {
return NoMatch
}
} else {
if !p.simpleNameMatch(path, isDir) {
return NoMatch
}
}

if p.inclusion {
return Include
} else {
return Exclude
}
return Exclude
}

func (p *pattern) simpleNameMatch(path []string, isDir bool) bool {
for i, name := range path {
if match, err := filepath.Match(p.pattern[0], name); err != nil {
match, err := filepath.Match(p.pattern[0], name)
if err != nil {
return false
} else if !match {
}
if !match {
continue
}
if p.dirOnly && !isDir && i == len(path)-1 {
Expand Down Expand Up @@ -130,23 +134,26 @@ func (p *pattern) globMatch(path []string, isDir bool) bool {
for len(path) > 0 {
e := path[0]
path = path[1:]
if match, err := filepath.Match(pattern, e); err != nil {
match, err := filepath.Match(pattern, e)
if err != nil {
return false
} else if match {
}
if match {
matched = true
break
} else if len(path) == 0 {
}
if len(path) == 0 {
// if nothing left then fail
matched = false
}
}
} else {
if match, err := filepath.Match(pattern, path[0]); err != nil || !match {
return false
}
matched = true
path = path[1:]
continue
}
if match, err := filepath.Match(pattern, path[0]); err != nil || !match {
return false
}
matched = true
path = path[1:]
}
if matched && p.dirOnly && !isDir && len(path) == 0 {
matched = false
Expand Down
27 changes: 21 additions & 6 deletions modules/strengthen/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package strengthen

import (
"errors"
"os"
"os/user"
"path/filepath"
"strings"
Expand All @@ -13,17 +14,31 @@ var (

// ExpandPath is a helper function to expand a relative or home-relative path to an absolute path.
//
// eg. ~/.someconf -> /home/alec/.someconf
// eg.
//
// ~/.someconf -> /home/alec/.someconf
// ~alec/.someconf -> /home/alec/.someconf
func ExpandPath(path string) string {
if filepath.IsAbs(path) {
return path
}
if strings.HasPrefix(path, "~/") {
user, err := user.Current()
if err != nil {
return path
if strings.HasPrefix(path, "~") {
// For Windows systems, please replace the path separator first
pos := strings.IndexByte(path, '/')
switch {
case pos == 1:
if homeDir, err := os.UserHomeDir(); err == nil {
return filepath.Join(homeDir, path[2:])
}
case pos > 1:
// https://github.com/golang/go/issues/24383
// macOS may not produce correct results
username := path[1:pos]
if userAccount, err := user.Lookup(username); err == nil {
return filepath.Join(userAccount.HomeDir, path[pos+1:])
}
default:
}
return filepath.Join(user.HomeDir, path[2:])
}
abspath, err := filepath.Abs(path)
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions modules/strengthen/path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package strengthen

import (
"fmt"
"os"
"os/user"
"testing"
)

func TestExpandPath(t *testing.T) {
u, err := user.Current()
if err != nil {
return
}
dirs := []string{
"~/.zetaignore", "~" + u.Username + "/jacksone", "/tmp/jock", "~root/downloads",
}
for _, d := range dirs {
fmt.Fprintf(os.Stderr, "%s --> %s\n", d, ExpandPath(d))
}
}

0 comments on commit 37c98d1

Please sign in to comment.