Merge pull request #207 from mattn/fix/git-cat-backslash-path

git: fix 'cat' returning the wrong file for paths with a backslash
This commit is contained in:
mattn
2026-08-28 15:53:25 +00:00
committed by GitHub
+31 -7
View File
@@ -717,19 +717,43 @@ aside from those, there is also:
continue continue
} }
entry, err := gitnaturalapi.GetObjectByPath(url, commit, path) // the path is walked here instead of with GetObjectByPath()
// because that also treats '\' as a separator, while in git
// path names a backslash is just a regular character
segments := strings.Split(strings.Trim(path, "/"), "/")
name := segments[len(segments)-1]
depth := len(segments)
tree, err := gitnaturalapi.GetDirectoryTreeAt(url, commit, &depth)
if err != nil { if err != nil {
lastErr = err lastErr = err
continue continue
} }
if entry == nil {
return fmt.Errorf("path '%s' not found", path) if len(segments) > 1 {
} tree, err = gitTreeAtPath(tree, strings.Join(segments[:len(segments)-1], "/"))
if entry.IsDir { if err != nil {
return fmt.Errorf("path '%s' is a directory", path) return err
}
} }
obj, err := gitnaturalapi.GetObject(url, entry.Hash) hash := ""
for _, file := range tree.Files {
if file.Name == name {
hash = file.Hash
break
}
}
if hash == "" {
for _, dir := range tree.Directories {
if dir.Name == name {
return fmt.Errorf("path '%s' is a directory", path)
}
}
return fmt.Errorf("path '%s' not found", path)
}
obj, err := gitnaturalapi.GetObject(url, hash)
if err != nil { if err != nil {
lastErr = fmt.Errorf("download error: %s", err) lastErr = fmt.Errorf("download error: %s", err)
continue continue