Skip to content

Commit 0953dd5

Browse files
committed
feat: refactor relative path function for git walker
Signed-off-by: Brian McGee <[email protected]>
1 parent 0a8ffe0 commit 0953dd5

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

walk/git.go

+33-18
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ import (
1313
)
1414

1515
type gitWalker struct {
16-
root string
17-
paths chan string
18-
repo *git.Repository
16+
root string
17+
paths chan string
18+
repo *git.Repository
19+
relPathOffset int
1920
}
2021

21-
func (g *gitWalker) Root() string {
22+
func (g gitWalker) Root() string {
2223
return g.root
2324
}
2425

25-
func (g *gitWalker) Walk(ctx context.Context, fn WalkFunc) error {
26-
// for quick relative paths
27-
relPathOffset := len(g.root) + 1
28-
29-
relPathFn := func(path string) (relPath string) {
30-
if len(path) >= relPathOffset {
31-
relPath = path[relPathOffset:]
32-
}
33-
return
26+
func (g gitWalker) relPath(path string) (string, error) {
27+
// quick optimization for the majority of use cases
28+
if len(path) >= g.relPathOffset && path[:len(g.root)] == g.root {
29+
return path[g.relPathOffset:], nil
3430
}
31+
// fallback to proper relative path resolution
32+
return filepath.Rel(g.root, path)
33+
}
3534

35+
func (g gitWalker) Walk(ctx context.Context, fn WalkFunc) error {
3636
idx, err := g.repo.Storer.Index()
3737
if err != nil {
3838
return fmt.Errorf("failed to open git index: %w", err)
@@ -57,11 +57,21 @@ func (g *gitWalker) Walk(ctx context.Context, fn WalkFunc) error {
5757

5858
// stat the file
5959
path := filepath.Join(g.root, entry.Name)
60+
6061
info, err := os.Lstat(path)
62+
if err != nil {
63+
return fmt.Errorf("failed to stat %s: %w", path, err)
64+
}
65+
66+
// determine a relative path
67+
relPath, err := g.relPath(path)
68+
if err != nil {
69+
return fmt.Errorf("failed to determine a relative path for %s: %w", path, err)
70+
}
6171

6272
file := File{
6373
Path: path,
64-
RelPath: relPathFn(path),
74+
RelPath: relPath,
6575
Info: info,
6676
}
6777

@@ -97,9 +107,9 @@ func (g *gitWalker) Walk(ctx context.Context, fn WalkFunc) error {
97107
return nil
98108
}
99109

100-
relPath, err := filepath.Rel(g.root, path)
110+
relPath, err := g.relPath(path)
101111
if err != nil {
102-
return err
112+
return fmt.Errorf("failed to determine a relative path for %s: %w", path, err)
103113
}
104114

105115
if _, ok := cache[relPath]; !ok {
@@ -109,7 +119,7 @@ func (g *gitWalker) Walk(ctx context.Context, fn WalkFunc) error {
109119

110120
file := File{
111121
Path: path,
112-
RelPath: relPathFn(path),
122+
RelPath: relPath,
113123
Info: info,
114124
}
115125

@@ -125,5 +135,10 @@ func NewGit(root string, paths chan string) (Walker, error) {
125135
if err != nil {
126136
return nil, fmt.Errorf("failed to open git repo: %w", err)
127137
}
128-
return &gitWalker{root, paths, repo}, nil
138+
return &gitWalker{
139+
root: root,
140+
paths: paths,
141+
repo: repo,
142+
relPathOffset: len(root) + 1,
143+
}, nil
129144
}

0 commit comments

Comments
 (0)