Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Dec 31, 2024
1 parent df6ac1a commit 764931a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 26 deletions.
31 changes: 20 additions & 11 deletions modules/git/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ import (
"fmt"
"strconv"
"strings"

"code.gitea.io/gitea/modules/optional"
)

var sepSpace = []byte{' '}

func parseLsTreeLine(line []byte) (*TreeEntry, error) {
type LsTreeEntry struct {
ID ObjectID
EntryMode EntryMode
Name string
Size optional.Option[int64]
}

func parseLsTreeLine(line []byte) (*LsTreeEntry, error) {
// expect line to be of the form:
// <mode> <type> <sha> <space-padded-size>\t<filename>
// <mode> <type> <sha>\t<filename>
Expand All @@ -23,7 +32,7 @@ func parseLsTreeLine(line []byte) (*TreeEntry, error) {
return nil, fmt.Errorf("invalid ls-tree output (no tab): %q", line)
}

entry := new(TreeEntry)
entry := new(LsTreeEntry)

entryAttrs := line[:posTab]
entryName := line[posTab+1:]
Expand All @@ -33,21 +42,21 @@ func parseLsTreeLine(line []byte) (*TreeEntry, error) {
entryObjectID, entryAttrs, _ := bytes.Cut(entryAttrs, sepSpace)
if len(entryAttrs) > 0 {
entrySize := entryAttrs // the last field is the space-padded-size
entry.size, _ = strconv.ParseInt(strings.TrimSpace(string(entrySize)), 10, 64)
entry.sized = true
size, _ := strconv.ParseInt(strings.TrimSpace(string(entrySize)), 10, 64)
entry.Size = optional.Some(size)
}

switch string(entryMode) {
case "100644":
entry.entryMode = EntryModeBlob
entry.EntryMode = EntryModeBlob
case "100755":
entry.entryMode = EntryModeExec
entry.EntryMode = EntryModeExec
case "120000":
entry.entryMode = EntryModeSymlink
entry.EntryMode = EntryModeSymlink
case "160000":
entry.entryMode = EntryModeCommit
entry.EntryMode = EntryModeCommit
case "040000", "040755": // git uses 040000 for tree object, but some users may get 040755 for unknown reasons
entry.entryMode = EntryModeTree
entry.EntryMode = EntryModeTree
default:
return nil, fmt.Errorf("unknown type: %v", string(entryMode))
}
Expand All @@ -58,12 +67,12 @@ func parseLsTreeLine(line []byte) (*TreeEntry, error) {
}

if len(entryName) > 0 && entryName[0] == '"' {
entry.name, err = strconv.Unquote(string(entryName))
entry.Name, err = strconv.Unquote(string(entryName))
if err != nil {
return nil, fmt.Errorf("invalid ls-tree output (invalid name): %q, err: %w", line, err)
}
} else {
entry.name = string(entryName)
entry.Name = string(entryName)
}
return entry, nil
}
12 changes: 9 additions & 3 deletions modules/git/parse_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ func parseTreeEntries(data []byte, ptree *Tree) ([]*TreeEntry, error) {
}

line := data[pos:posEnd]
entry, err := parseLsTreeLine(line)
lsTreeLine, err := parseLsTreeLine(line)
if err != nil {
return nil, err
}
entry.ptree = ptree

entry := &TreeEntry{
ptree: ptree,
ID: lsTreeLine.ID,
entryMode: lsTreeLine.EntryMode,
name: lsTreeLine.Name,
size: lsTreeLine.Size.Value(),
sized: lsTreeLine.Size.Has(),
}
pos = posEnd + 1
entries = append(entries, entry)
}
Expand Down
4 changes: 2 additions & 2 deletions modules/git/submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func GetTemplateSubmoduleCommits(ctx context.Context, repoPath string) (submodul
cancel()
return err
}
if entry.IsSubModule() {
submoduleCommits = append(submoduleCommits, TemplateSubmoduleCommit{Path: entry.Name(), Commit: entry.ID.String()})
if entry.EntryMode == EntryModeCommit {
submoduleCommits = append(submoduleCommits, TemplateSubmoduleCommit{Path: entry.Name, Commit: entry.ID.String()})
}
}
return scanner.Err()
Expand Down
1 change: 0 additions & 1 deletion modules/git/tree_blob_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func (t *Tree) GetTreeEntryByPath(relpath string) (*TreeEntry, error) {
ptree: t,
ID: t.ID,
name: "",
fullName: "",
entryMode: EntryModeTree,
}, nil
}
Expand Down
12 changes: 3 additions & 9 deletions modules/git/tree_entry_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,17 @@ import "code.gitea.io/gitea/modules/log"

// TreeEntry the leaf in the git tree
type TreeEntry struct {
ID ObjectID

ID ObjectID
ptree *Tree

entryMode EntryMode
name string

size int64
sized bool
fullName string
size int64
sized bool
}

// Name returns the name of the entry
func (te *TreeEntry) Name() string {
if te.fullName != "" {
return te.fullName
}
return te.name
}

Expand Down

0 comments on commit 764931a

Please sign in to comment.