2018-02-10 18:19:26 +00:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-02-10 18:19:26 +00:00
|
|
|
|
2021-08-24 16:47:09 +00:00
|
|
|
//go:build !gogit
|
2020-12-17 14:00:47 +00:00
|
|
|
|
2018-02-10 18:19:26 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2021-05-10 01:27:03 +00:00
|
|
|
"bufio"
|
2018-02-10 18:19:26 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2021-05-10 01:27:03 +00:00
|
|
|
"io"
|
2021-06-25 16:54:08 +00:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2018-02-10 18:19:26 +00:00
|
|
|
)
|
|
|
|
|
2021-02-17 21:32:25 +00:00
|
|
|
// ParseTreeEntries parses the output of a `git ls-tree -l` command.
|
2024-04-29 08:47:56 +00:00
|
|
|
func ParseTreeEntries(data []byte) ([]*TreeEntry, error) {
|
|
|
|
return parseTreeEntries(data, nil)
|
2018-02-10 18:19:26 +00:00
|
|
|
}
|
|
|
|
|
2025-01-02 04:17:05 +00:00
|
|
|
// parseTreeEntries FIXME this function's design is not right, it should make the caller read all data into memory
|
2024-04-29 08:47:56 +00:00
|
|
|
func parseTreeEntries(data []byte, ptree *Tree) ([]*TreeEntry, error) {
|
2022-10-07 17:20:53 +00:00
|
|
|
entries := make([]*TreeEntry, 0, bytes.Count(data, []byte{'\n'})+1)
|
2018-02-10 18:19:26 +00:00
|
|
|
for pos := 0; pos < len(data); {
|
2022-10-07 17:20:53 +00:00
|
|
|
posEnd := bytes.IndexByte(data[pos:], '\n')
|
|
|
|
if posEnd == -1 {
|
|
|
|
posEnd = len(data)
|
|
|
|
} else {
|
|
|
|
posEnd += pos
|
|
|
|
}
|
|
|
|
|
2025-01-02 04:17:05 +00:00
|
|
|
line := data[pos:posEnd]
|
|
|
|
lsTreeLine, err := parseLsTreeLine(line)
|
2018-02-10 18:19:26 +00:00
|
|
|
if err != nil {
|
2025-01-02 04:17:05 +00:00
|
|
|
return nil, err
|
2018-02-10 18:19:26 +00:00
|
|
|
}
|
2025-01-02 04:17:05 +00:00
|
|
|
entry := &TreeEntry{
|
|
|
|
ptree: ptree,
|
|
|
|
ID: lsTreeLine.ID,
|
|
|
|
entryMode: lsTreeLine.EntryMode,
|
|
|
|
name: lsTreeLine.Name,
|
|
|
|
size: lsTreeLine.Size.Value(),
|
|
|
|
sized: lsTreeLine.Size.Has(),
|
2018-02-10 18:19:26 +00:00
|
|
|
}
|
2022-10-07 17:20:53 +00:00
|
|
|
pos = posEnd + 1
|
2018-02-10 18:19:26 +00:00
|
|
|
entries = append(entries, entry)
|
|
|
|
}
|
|
|
|
return entries, nil
|
|
|
|
}
|
2021-05-10 01:27:03 +00:00
|
|
|
|
2023-12-13 21:02:00 +00:00
|
|
|
func catBatchParseTreeEntries(objectFormat ObjectFormat, ptree *Tree, rd *bufio.Reader, sz int64) ([]*TreeEntry, error) {
|
2021-05-10 01:27:03 +00:00
|
|
|
fnameBuf := make([]byte, 4096)
|
|
|
|
modeBuf := make([]byte, 40)
|
2023-12-13 21:02:00 +00:00
|
|
|
shaBuf := make([]byte, objectFormat.FullLength())
|
2021-05-10 01:27:03 +00:00
|
|
|
entries := make([]*TreeEntry, 0, 10)
|
|
|
|
|
|
|
|
loop:
|
|
|
|
for sz > 0 {
|
2025-01-02 04:17:05 +00:00
|
|
|
mode, fname, sha, count, err := ParseCatFileTreeLine(objectFormat, rd, modeBuf, fnameBuf, shaBuf)
|
2021-05-10 01:27:03 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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
|
2022-09-18 01:31:20 +00:00
|
|
|
case "40000", "40755": // git uses 40000 for tree object, but some users may get 40755 for unknown reasons
|
2021-05-10 01:27:03 +00:00
|
|
|
entry.entryMode = EntryModeTree
|
|
|
|
default:
|
2021-06-25 16:54:08 +00:00
|
|
|
log.Debug("Unknown mode: %v", string(mode))
|
2021-05-10 01:27:03 +00:00
|
|
|
return nil, fmt.Errorf("unknown mode: %v", string(mode))
|
|
|
|
}
|
|
|
|
|
2023-12-13 21:02:00 +00:00
|
|
|
entry.ID = objectFormat.MustID(sha)
|
2021-05-10 01:27:03 +00:00
|
|
|
entry.name = string(fname)
|
|
|
|
entries = append(entries, entry)
|
|
|
|
}
|
|
|
|
if _, err := rd.Discard(1); err != nil {
|
|
|
|
return entries, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries, nil
|
|
|
|
}
|