Skip to content

Commit 449eb48

Browse files
committed
Add "path.Clean" and more comments in gitfs's "resolveSymlink" function
1 parent c9bc568 commit 449eb48

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

pkg/gitfs/fs.go

+6
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,18 @@ func resolveSymlink(f *goGitPlumbingObject.File) (target string, err error) {
5252
return "", fmt.Errorf("unexpected: empty symlink %q", f.Name)
5353
}
5454

55+
// we *could* implement this as absolute symlinks being relative to the root of the Git repository, but that wouldn't match the behavior of a normal repository that's been "git clone"'d on disk, so I think that would be a mistake and erroring out is saner here
5556
if path.IsAbs(target) {
5657
return "", fmt.Errorf("unsupported: %q is an absolute symlink (%q)", f.Name, target)
5758
}
5859

60+
// symlinks are relative to the path they're in, so we need to prepend that
5961
target = path.Join(path.Dir(f.Name), target)
6062

63+
// now let's use path.Clean to get rid of any excess ".." or "." entries in our end result
64+
target = path.Clean(target)
65+
66+
// once we're cleaned, we should have a full path that's relative to the root of the Git repository, so if it still starts with "../", that's a problem that will error later when we try to read it, so let's error out now to bail earlier
6167
if strings.HasPrefix(target, "../") {
6268
return "", fmt.Errorf("unsupported: %q is a relative symlink outside the tree (%q)", f.Name, target)
6369
}

0 commit comments

Comments
 (0)