aboutsummaryrefslogtreecommitdiff
path: root/object
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-02-21 13:38:02 +0800
committerGravatar Runxi Yu2026-02-21 14:28:15 +0800
commit94482cb2c97aa215f83940643c5d4c0933727dcb (patch)
treebee22fa113542abd1b863ee251fdcf0f9bd409b5 /object
parentdiff: Add package-level doc comment (diff)
signatureNo signature
*: Modernize and lint; add CI v0.1.17
Diffstat (limited to 'object')
-rw-r--r--object/blob_parse_test.go3
-rw-r--r--object/blob_serialize_test.go3
-rw-r--r--object/commit_parse_test.go3
-rw-r--r--object/commit_serialize_test.go3
-rw-r--r--object/ident.go8
-rw-r--r--object/parse.go2
-rw-r--r--object/tag_parse_test.go3
-rw-r--r--object/tag_serialize_test.go3
-rw-r--r--object/tree.go44
-rw-r--r--object/tree_helpers_test.go19
-rw-r--r--object/tree_parse_test.go3
-rw-r--r--object/tree_serialize_test.go3
12 files changed, 46 insertions, 51 deletions
diff --git a/object/blob_parse_test.go b/object/blob_parse_test.go
index 0b8c22ca..7b242ef7 100644
--- a/object/blob_parse_test.go
+++ b/object/blob_parse_test.go
@@ -10,7 +10,8 @@ import (
)
func TestBlobParseFromGit(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
body := []byte("hello\nblob\n")
blobID := testRepo.HashObject(t, "blob", body)
diff --git a/object/blob_serialize_test.go b/object/blob_serialize_test.go
index 041851ec..c49815da 100644
--- a/object/blob_serialize_test.go
+++ b/object/blob_serialize_test.go
@@ -9,7 +9,8 @@ import (
)
func TestBlobSerialize(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
body := []byte("hello\nblob\n")
wantID := testRepo.HashObject(t, "blob", body)
diff --git a/object/commit_parse_test.go b/object/commit_parse_test.go
index 77a570af..f01052ac 100644
--- a/object/commit_parse_test.go
+++ b/object/commit_parse_test.go
@@ -10,7 +10,8 @@ import (
)
func TestCommitParseFromGit(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
_, treeID, commitID := testRepo.MakeCommit(t, "subject\n\nbody")
diff --git a/object/commit_serialize_test.go b/object/commit_serialize_test.go
index 6db48143..4f9856b0 100644
--- a/object/commit_serialize_test.go
+++ b/object/commit_serialize_test.go
@@ -9,7 +9,8 @@ import (
)
func TestCommitSerialize(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
_, _, commitID := testRepo.MakeCommit(t, "subject\n\nbody")
diff --git a/object/ident.go b/object/ident.go
index 432a0977..83503c89 100644
--- a/object/ident.go
+++ b/object/ident.go
@@ -4,10 +4,11 @@ import (
"bytes"
"errors"
"fmt"
- "math"
"strconv"
"strings"
"time"
+
+ "codeberg.org/lindenii/furgit/internal/intconv"
)
// Ident represents a Git identity (author/committer/tagger).
@@ -76,11 +77,10 @@ func ParseIdent(line []byte) (*Ident, error) {
return nil, errors.New("object: ident: invalid timezone minutes range")
}
total := int64(hh)*60 + int64(mm)
- if total > math.MaxInt32 {
+ offset, err := intconv.Int64ToInt32(total)
+ if err != nil {
return nil, errors.New("object: ident: timezone overflow")
}
-
- offset := int32(total)
if sign < 0 {
offset = -offset
}
diff --git a/object/parse.go b/object/parse.go
index ad9750fe..1b15f377 100644
--- a/object/parse.go
+++ b/object/parse.go
@@ -19,6 +19,8 @@ func ParseObjectWithoutHeader(ty objecttype.Type, body []byte, algo objectid.Alg
return ParseCommit(body, algo)
case objecttype.TypeTag:
return ParseTag(body, algo)
+ case objecttype.TypeInvalid, objecttype.TypeFuture, objecttype.TypeOfsDelta, objecttype.TypeRefDelta:
+ return nil, fmt.Errorf("object: unsupported object type %d", ty)
default:
return nil, fmt.Errorf("object: unsupported object type %d", ty)
}
diff --git a/object/tag_parse_test.go b/object/tag_parse_test.go
index 0009338e..7ddb60e9 100644
--- a/object/tag_parse_test.go
+++ b/object/tag_parse_test.go
@@ -11,7 +11,8 @@ import (
)
func TestTagParseFromGit(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
_, _, commitID := testRepo.MakeCommit(t, "subject\n\nbody")
tagID := testRepo.TagAnnotated(t, "v1", commitID, "tag message")
diff --git a/object/tag_serialize_test.go b/object/tag_serialize_test.go
index 6437977b..1b3ea2f8 100644
--- a/object/tag_serialize_test.go
+++ b/object/tag_serialize_test.go
@@ -9,7 +9,8 @@ import (
)
func TestTagSerialize(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
_, _, commitID := testRepo.MakeCommit(t, "subject\n\nbody")
tagID := testRepo.TagAnnotated(t, "v1", commitID, "tag message")
diff --git a/object/tree.go b/object/tree.go
index e6a586cb..4bb459be 100644
--- a/object/tree.go
+++ b/object/tree.go
@@ -49,27 +49,6 @@ func (tree *Tree) Entry(name []byte) *TreeEntry {
return tree.entry(name, false)
}
-func (tree *Tree) entry(name []byte, searchIsTree bool) *TreeEntry {
- low, high := 0, len(tree.Entries)-1
- for low <= high {
- mid := low + (high-low)/2
- entry := &tree.Entries[mid]
- cmp := TreeEntryNameCompare(entry.Name, entry.Mode, name, searchIsTree)
- if cmp == 0 {
- if bytes.Equal(entry.Name, name) {
- return entry
- }
- return nil
- }
- if cmp < 0 {
- low = mid + 1
- } else {
- high = mid - 1
- }
- }
- return nil
-}
-
// InsertEntry inserts a tree entry while preserving Git ordering.
func (tree *Tree) InsertEntry(newEntry TreeEntry) error {
if tree.entry(newEntry.Name, true) != nil || tree.entry(newEntry.Name, false) != nil {
@@ -100,6 +79,27 @@ func (tree *Tree) RemoveEntry(name []byte) error {
return fmt.Errorf("object: tree: entry %q not found", name)
}
+func (tree *Tree) entry(name []byte, searchIsTree bool) *TreeEntry {
+ low, high := 0, len(tree.Entries)-1
+ for low <= high {
+ mid := low + (high-low)/2
+ entry := &tree.Entries[mid]
+ cmp := TreeEntryNameCompare(entry.Name, entry.Mode, name, searchIsTree)
+ if cmp == 0 {
+ if bytes.Equal(entry.Name, name) {
+ return entry
+ }
+ return nil
+ }
+ if cmp < 0 {
+ low = mid + 1
+ } else {
+ high = mid - 1
+ }
+ }
+ return nil
+}
+
// TreeEntryNameCompare compares names using Git tree ordering rules.
func TreeEntryNameCompare(entryName []byte, entryMode FileMode, searchName []byte, searchIsTree bool) int {
isEntryTree := entryMode == FileModeDir
@@ -115,7 +115,7 @@ func TreeEntryNameCompare(entryName []byte, entryMode FileMode, searchName []byt
n := min(searchLen, entryLen)
- for i := 0; i < n; i++ {
+ for i := range n {
var ec, sc byte
if i < len(entryName) {
ec = entryName[i]
diff --git a/object/tree_helpers_test.go b/object/tree_helpers_test.go
index 539d7542..4727e1c7 100644
--- a/object/tree_helpers_test.go
+++ b/object/tree_helpers_test.go
@@ -10,30 +10,15 @@ import (
"codeberg.org/lindenii/furgit/object"
)
-func mktreeTypeFromMode(t *testing.T, mode object.FileMode) string {
- t.Helper()
- switch mode {
- case object.FileModeDir:
- return "tree"
- case object.FileModeRegular, object.FileModeExecutable, object.FileModeSymlink:
- return "blob"
- case object.FileModeGitlink:
- return "commit"
- default:
- t.Fatalf("unsupported file mode: %o", mode)
- return ""
- }
-}
-
func buildGitMktreeInput(entries []object.TreeEntry) string {
var b strings.Builder
for _, e := range entries {
- fmt.Fprintf(&b, "%o %s %s\t%s\n", e.Mode, mktreeTypeFromModeNoTB(e.Mode), e.ID.String(), e.Name)
+ fmt.Fprintf(&b, "%o %s %s\t%s\n", e.Mode, mktreeTypeFromMode(e.Mode), e.ID.String(), e.Name)
}
return b.String()
}
-func mktreeTypeFromModeNoTB(mode object.FileMode) string {
+func mktreeTypeFromMode(mode object.FileMode) string {
switch mode {
case object.FileModeDir:
return "tree"
diff --git a/object/tree_parse_test.go b/object/tree_parse_test.go
index f653f068..5af9c677 100644
--- a/object/tree_parse_test.go
+++ b/object/tree_parse_test.go
@@ -10,7 +10,8 @@ import (
)
func TestTreeParseFromGit(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
entries := adversarialRootEntries(t, testRepo)
inserted := &object.Tree{}
diff --git a/object/tree_serialize_test.go b/object/tree_serialize_test.go
index f40b13a4..e8ebb140 100644
--- a/object/tree_serialize_test.go
+++ b/object/tree_serialize_test.go
@@ -9,7 +9,8 @@ import (
)
func TestTreeSerialize(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
entries := adversarialRootEntries(t, testRepo)
tree := &object.Tree{}