1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-09-30 19:22:08 +00:00

fix: do better parsing of file modes

- No longer hardcode the file modes we expect, parse them as numbers and
do bitmask tricks that Git does so we allow a more variety of _weird_
file modes that can happen in the wild.
- Ref: https://codeberg.org/forgejo/forgejo/pulls/8900#issuecomment-6429175
- Resolves Codeberg/Community#2111
This commit is contained in:
Gusted 2025-09-04 02:04:25 +02:00
parent 8f4ebab023
commit 1cfd5e0b98
No known key found for this signature in database
GPG key ID: FD821B732837125F

View file

@ -10,8 +10,6 @@ import (
"io"
"strconv"
"strings"
"forgejo.org/modules/log"
)
// ParseTreeEntries parses the output of a `git ls-tree -l` command.
@ -55,19 +53,9 @@ func parseTreeEntries(data []byte, ptree *Tree) ([]*TreeEntry, error) {
entry.sized = true
}
switch string(entryMode) {
case "100644":
entry.entryMode = EntryModeBlob
case "100755":
entry.entryMode = EntryModeExec
case "120000":
entry.entryMode = EntryModeSymlink
case "160000":
entry.entryMode = EntryModeCommit
case "040000", "040755", "040775": // git uses 040000 for tree object, but some users may get 040755 or 040775 for unknown reasons
entry.entryMode = EntryModeTree
default:
return nil, fmt.Errorf("unknown type: %v", string(entryMode))
entry.entryMode, err = parseMode(string(entryMode))
if err != nil {
return nil, err
}
entry.ID, err = NewIDFromString(string(entryObjectID))
@ -108,23 +96,10 @@ loop:
sz -= int64(count)
entry := new(TreeEntry)
entry.ptree = ptree
switch string(mode) {
case "100644":
entry.entryMode = EntryModeBlob
case "100755":
entry.entryMode = EntryModeExec
case "120000":
entry.entryMode = EntryModeSymlink
case "160000":
entry.entryMode = EntryModeCommit
case "40000", "40755", "40775": // git uses 40000 for tree object, but some users may get 40755 or 40775 for unknown reasons
entry.entryMode = EntryModeTree
default:
log.Debug("Unknown mode: %v", string(mode))
return nil, fmt.Errorf("unknown mode: %v", string(mode))
entry.entryMode, err = parseMode(string(mode))
if err != nil {
return nil, err
}
entry.ID = objectFormat.MustID(sha)
entry.name = string(fname)
entries = append(entries, entry)
@ -135,3 +110,31 @@ loop:
return entries, nil
}
// Parse the file mode, we cannot hardcode the modes that we expect for
// a variety of reasons (that is not known to us) the permissions bits
// of files can vary, usually the result because of tooling that uses Git in
// a funny way. So we have to parse the mode as a integer and do bit tricks.
func parseMode(modeStr string) (EntryMode, error) {
mode, err := strconv.ParseUint(modeStr, 8, 64)
if err != nil {
return 0, fmt.Errorf("cannot parse mode: %v", err)
}
switch mode & 0o170000 {
case 0o040000:
return EntryModeTree, nil
case 0o120000:
return EntryModeSymlink, nil
case 0o160000:
return EntryModeCommit, nil
case 0o100000:
// Check for the permission bit on the owner.
if mode&0o100 == 0o100 {
return EntryModeExec, nil
}
return EntryModeBlob, nil
}
return 0, fmt.Errorf("unknown mode: %o", mode)
}