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:
parent
8f4ebab023
commit
1cfd5e0b98
1 changed files with 34 additions and 31 deletions
|
@ -10,8 +10,6 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"forgejo.org/modules/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ParseTreeEntries parses the output of a `git ls-tree -l` command.
|
// 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
|
entry.sized = true
|
||||||
}
|
}
|
||||||
|
|
||||||
switch string(entryMode) {
|
entry.entryMode, err = parseMode(string(entryMode))
|
||||||
case "100644":
|
if err != nil {
|
||||||
entry.entryMode = EntryModeBlob
|
return nil, err
|
||||||
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.ID, err = NewIDFromString(string(entryObjectID))
|
entry.ID, err = NewIDFromString(string(entryObjectID))
|
||||||
|
@ -108,23 +96,10 @@ loop:
|
||||||
sz -= int64(count)
|
sz -= int64(count)
|
||||||
entry := new(TreeEntry)
|
entry := new(TreeEntry)
|
||||||
entry.ptree = ptree
|
entry.ptree = ptree
|
||||||
|
entry.entryMode, err = parseMode(string(mode))
|
||||||
switch string(mode) {
|
if err != nil {
|
||||||
case "100644":
|
return nil, err
|
||||||
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.ID = objectFormat.MustID(sha)
|
entry.ID = objectFormat.MustID(sha)
|
||||||
entry.name = string(fname)
|
entry.name = string(fname)
|
||||||
entries = append(entries, entry)
|
entries = append(entries, entry)
|
||||||
|
@ -135,3 +110,31 @@ loop:
|
||||||
|
|
||||||
return entries, nil
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue