Skip to content

Commit df6ac1a

Browse files
committed
fix lint
1 parent 0918e75 commit df6ac1a

File tree

4 files changed

+80
-65
lines changed

4 files changed

+80
-65
lines changed

modules/git/parse.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package git
5+
6+
import (
7+
"bytes"
8+
"fmt"
9+
"strconv"
10+
"strings"
11+
)
12+
13+
var sepSpace = []byte{' '}
14+
15+
func parseLsTreeLine(line []byte) (*TreeEntry, error) {
16+
// expect line to be of the form:
17+
// <mode> <type> <sha> <space-padded-size>\t<filename>
18+
// <mode> <type> <sha>\t<filename>
19+
20+
var err error
21+
posTab := bytes.IndexByte(line, '\t')
22+
if posTab == -1 {
23+
return nil, fmt.Errorf("invalid ls-tree output (no tab): %q", line)
24+
}
25+
26+
entry := new(TreeEntry)
27+
28+
entryAttrs := line[:posTab]
29+
entryName := line[posTab+1:]
30+
31+
entryMode, entryAttrs, _ := bytes.Cut(entryAttrs, sepSpace)
32+
_ /* entryType */, entryAttrs, _ = bytes.Cut(entryAttrs, sepSpace) // the type is not used, the mode is enough to determine the type
33+
entryObjectID, entryAttrs, _ := bytes.Cut(entryAttrs, sepSpace)
34+
if len(entryAttrs) > 0 {
35+
entrySize := entryAttrs // the last field is the space-padded-size
36+
entry.size, _ = strconv.ParseInt(strings.TrimSpace(string(entrySize)), 10, 64)
37+
entry.sized = true
38+
}
39+
40+
switch string(entryMode) {
41+
case "100644":
42+
entry.entryMode = EntryModeBlob
43+
case "100755":
44+
entry.entryMode = EntryModeExec
45+
case "120000":
46+
entry.entryMode = EntryModeSymlink
47+
case "160000":
48+
entry.entryMode = EntryModeCommit
49+
case "040000", "040755": // git uses 040000 for tree object, but some users may get 040755 for unknown reasons
50+
entry.entryMode = EntryModeTree
51+
default:
52+
return nil, fmt.Errorf("unknown type: %v", string(entryMode))
53+
}
54+
55+
entry.ID, err = NewIDFromString(string(entryObjectID))
56+
if err != nil {
57+
return nil, fmt.Errorf("invalid ls-tree output (invalid object id): %q, err: %w", line, err)
58+
}
59+
60+
if len(entryName) > 0 && entryName[0] == '"' {
61+
entry.name, err = strconv.Unquote(string(entryName))
62+
if err != nil {
63+
return nil, fmt.Errorf("invalid ls-tree output (invalid name): %q, err: %w", line, err)
64+
}
65+
} else {
66+
entry.name = string(entryName)
67+
}
68+
return entry, nil
69+
}

modules/git/parse_nogogit.go

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"bytes"
1111
"fmt"
1212
"io"
13-
"strconv"
14-
"strings"
1513

1614
"code.gitea.io/gitea/modules/log"
1715
)
@@ -21,64 +19,6 @@ func ParseTreeEntries(data []byte) ([]*TreeEntry, error) {
2119
return parseTreeEntries(data, nil)
2220
}
2321

24-
var sepSpace = []byte{' '}
25-
26-
func parseLsTreeLine(line []byte) (*TreeEntry, error) {
27-
// expect line to be of the form:
28-
// <mode> <type> <sha> <space-padded-size>\t<filename>
29-
// <mode> <type> <sha>\t<filename>
30-
31-
var err error
32-
posTab := bytes.IndexByte(line, '\t')
33-
if posTab == -1 {
34-
return nil, fmt.Errorf("invalid ls-tree output (no tab): %q", line)
35-
}
36-
37-
entry := new(TreeEntry)
38-
39-
entryAttrs := line[:posTab]
40-
entryName := line[posTab+1:]
41-
42-
entryMode, entryAttrs, _ := bytes.Cut(entryAttrs, sepSpace)
43-
_ /* entryType */, entryAttrs, _ = bytes.Cut(entryAttrs, sepSpace) // the type is not used, the mode is enough to determine the type
44-
entryObjectID, entryAttrs, _ := bytes.Cut(entryAttrs, sepSpace)
45-
if len(entryAttrs) > 0 {
46-
entrySize := entryAttrs // the last field is the space-padded-size
47-
entry.size, _ = strconv.ParseInt(strings.TrimSpace(string(entrySize)), 10, 64)
48-
entry.sized = true
49-
}
50-
51-
switch string(entryMode) {
52-
case "100644":
53-
entry.entryMode = EntryModeBlob
54-
case "100755":
55-
entry.entryMode = EntryModeExec
56-
case "120000":
57-
entry.entryMode = EntryModeSymlink
58-
case "160000":
59-
entry.entryMode = EntryModeCommit
60-
case "040000", "040755": // git uses 040000 for tree object, but some users may get 040755 for unknown reasons
61-
entry.entryMode = EntryModeTree
62-
default:
63-
return nil, fmt.Errorf("unknown type: %v", string(entryMode))
64-
}
65-
66-
entry.ID, err = NewIDFromString(string(entryObjectID))
67-
if err != nil {
68-
return nil, fmt.Errorf("invalid ls-tree output (invalid object id): %q, err: %w", line, err)
69-
}
70-
71-
if len(entryName) > 0 && entryName[0] == '"' {
72-
entry.name, err = strconv.Unquote(string(entryName))
73-
if err != nil {
74-
return nil, fmt.Errorf("invalid ls-tree output (invalid name): %q, err: %w", line, err)
75-
}
76-
} else {
77-
entry.name = string(entryName)
78-
}
79-
return entry, nil
80-
}
81-
8222
// parseTreeEntries FIXME this function's design is not right, it should make the caller read all data into memory
8323
func parseTreeEntries(data []byte, ptree *Tree) ([]*TreeEntry, error) {
8424
entries := make([]*TreeEntry, 0, bytes.Count(data, []byte{'\n'})+1)

modules/git/submodule.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
14
package git
25

36
import (

modules/git/submodule_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
14
package git
25

36
import (
@@ -13,11 +16,11 @@ func TestRepository_GetSubmoduleCommits(t *testing.T) {
1316
submodules, err := GetTemplateSubmoduleCommits(DefaultContext, testRepoPath)
1417
require.NoError(t, err)
1518

16-
assert.EqualValues(t, len(submodules), 2)
19+
assert.Len(t, submodules, 2)
1720

18-
assert.EqualValues(t, submodules[0].Path, "<°)))><")
19-
assert.EqualValues(t, submodules[0].Commit, "d2932de67963f23d43e1c7ecf20173e92ee6c43c")
21+
assert.EqualValues(t, "<°)))><", submodules[0].Path)
22+
assert.EqualValues(t, "d2932de67963f23d43e1c7ecf20173e92ee6c43c", submodules[0].Commit)
2023

21-
assert.EqualValues(t, submodules[1].Path, "libtest")
22-
assert.EqualValues(t, submodules[1].Commit, "1234567890123456789012345678901234567890")
24+
assert.EqualValues(t, "libtest", submodules[1].Path)
25+
assert.EqualValues(t, "1234567890123456789012345678901234567890", submodules[1].Commit)
2326
}

0 commit comments

Comments
 (0)