Skip to content

Commit 020ae41

Browse files
committed
more optimaztion for ObjectFormat interface
1 parent 47fedfa commit 020ae41

File tree

17 files changed

+124
-99
lines changed

17 files changed

+124
-99
lines changed

models/git/branch_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ func TestAddDeletedBranch(t *testing.T) {
3030
assert.True(t, secondBranch.IsDeleted)
3131

3232
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
33+
objectID, err := objectFormat.NewIDFromString(secondBranch.CommitID)
34+
assert.NoError(t, err)
3335
commit := &git.Commit{
34-
ID: objectFormat.MustIDFromString(secondBranch.CommitID),
36+
ID: objectID,
3537
CommitMessage: secondBranch.CommitMessage,
3638
Committer: &git.Signature{
3739
When: secondBranch.CommitTime.AsLocalTime(),
3840
},
3941
}
4042

41-
_, err := git_model.UpdateBranch(db.DefaultContext, repo.ID, secondBranch.PusherID, secondBranch.Name, commit)
43+
_, err = git_model.UpdateBranch(db.DefaultContext, repo.ID, secondBranch.PusherID, secondBranch.Name, commit)
4244
assert.NoError(t, err)
4345
}
4446

modules/git/commit_info_nogogit.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ func GetLastCommitForPaths(ctx context.Context, commit *Commit, treePath string,
153153
if typ != "commit" {
154154
return nil, fmt.Errorf("unexpected type: %s for commit id: %s", typ, commitID)
155155
}
156-
c, err = CommitFromReader(commit.repo, commit.ID.Type().MustIDFromString(commitID), io.LimitReader(batchReader, size))
156+
objectID, err := commit.ID.Type().NewIDFromString(commitID)
157+
if err != nil {
158+
return nil, err
159+
}
160+
c, err = CommitFromReader(commit.repo, objectID, io.LimitReader(batchReader, size))
157161
if err != nil {
158162
return nil, err
159163
}

modules/git/commit_reader.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,18 @@ readLoop:
7171

7272
switch string(split[0]) {
7373
case "tree":
74-
commit.Tree = *NewTree(gitRepo, objectID.Type().MustIDFromString(string(data)))
74+
newObjectID, err := objectID.Type().NewIDFromString(string(data))
75+
if err != nil {
76+
return nil, err
77+
}
78+
commit.Tree = *NewTree(gitRepo, newObjectID)
7579
_, _ = payloadSB.Write(line)
7680
case "parent":
77-
commit.Parents = append(commit.Parents, objectID.Type().MustIDFromString(string(data)))
81+
parentObjectID, err := objectID.Type().NewIDFromString(string(data))
82+
if err != nil {
83+
return nil, err
84+
}
85+
commit.Parents = append(commit.Parents, parentObjectID)
7886
_, _ = payloadSB.Write(line)
7987
case "author":
8088
commit.Author = &Signature{}

modules/git/commit_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ func TestHasPreviousCommit(t *testing.T) {
135135
commit, err := repo.GetCommit("8006ff9adbf0cb94da7dad9e537e53817f9fa5c0")
136136
assert.NoError(t, err)
137137

138-
parentSHA := repo.objectFormat.MustIDFromString("8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2")
139-
notParentSHA := repo.objectFormat.MustIDFromString("2839944139e0de9737a044f78b0e4b40d989a9e3")
138+
parentSHA := MustIDFromString(repo.objectFormat, "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2")
139+
notParentSHA := MustIDFromString(repo.objectFormat, "2839944139e0de9737a044f78b0e4b40d989a9e3")
140140

141141
haz, err := commit.HasPreviousCommit(parentSHA)
142142
assert.NoError(t, err)

modules/git/object_format.go

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package git
66
import (
77
"crypto/sha1"
88
"regexp"
9+
"strconv"
910
)
1011

1112
// sha1Pattern can be used to determine if a string is an valid sha
@@ -20,14 +21,12 @@ type ObjectFormat interface {
2021
EmptyTree() ObjectID
2122
// FullLength is the length of the hash's hex string
2223
FullLength() int
23-
24-
IsValid(input string) bool
25-
MustID(b []byte) ObjectID
26-
MustIDFromString(s string) ObjectID
27-
NewID(b []byte) (ObjectID, error)
28-
NewIDFromString(s string) (ObjectID, error)
29-
30-
NewHasher() HasherInterface
24+
// IsValid returns true if the input is a valid hash
25+
IsValid(objIDStr string) bool
26+
// NewIDFromString create ObjectID from object id string
27+
NewIDFromString(objIDStr string) (ObjectID, error)
28+
// ComputeHash computes and generate object id
29+
ComputeHash(t ObjectType, content []byte) (ObjectID, error)
3130
}
3231

3332
type Sha1ObjectFormatImpl struct{}
@@ -49,30 +48,25 @@ func (Sha1ObjectFormatImpl) EmptyTree() ObjectID {
4948
return emptyTree
5049
}
5150
func (Sha1ObjectFormatImpl) FullLength() int { return 40 }
52-
func (Sha1ObjectFormatImpl) IsValid(input string) bool {
53-
return sha1Pattern.MatchString(input)
54-
}
55-
56-
func (Sha1ObjectFormatImpl) MustID(b []byte) ObjectID {
57-
var id Sha1Hash
58-
copy(id[0:20], b)
59-
return &id
60-
}
61-
62-
func (h Sha1ObjectFormatImpl) MustIDFromString(s string) ObjectID {
63-
return MustIDFromString(h, s)
64-
}
65-
66-
func (h Sha1ObjectFormatImpl) NewID(b []byte) (ObjectID, error) {
67-
return IDFromRaw(h, b)
51+
func (Sha1ObjectFormatImpl) IsValid(objIDStr string) bool {
52+
return sha1Pattern.MatchString(objIDStr)
6853
}
6954

70-
func (h Sha1ObjectFormatImpl) NewIDFromString(s string) (ObjectID, error) {
71-
return genericIDFromString(h, s)
55+
func (h Sha1ObjectFormatImpl) NewIDFromString(objIDStr string) (ObjectID, error) {
56+
return genericIDFromString(h, objIDStr)
7257
}
7358

74-
func (h Sha1ObjectFormatImpl) NewHasher() HasherInterface {
75-
return &Sha1Hasher{sha1.New()}
59+
// ComputeHash compute the hash for a given ObjectType and content
60+
func (h Sha1ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) (ObjectID, error) {
61+
hasher := sha1.New()
62+
_, _ = hasher.Write(t.Bytes())
63+
_, _ = hasher.Write([]byte(" "))
64+
_, _ = hasher.Write([]byte(strconv.FormatInt(int64(len(content)), 10)))
65+
_, _ = hasher.Write([]byte{0})
66+
67+
var sha1 Sha1Hash
68+
copy(sha1[:], hasher.Sum(nil))
69+
return &sha1, nil
7670
}
7771

7872
var Sha1ObjectFormat ObjectFormat = Sha1ObjectFormatImpl{}

modules/git/object_id.go

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ import (
88
"encoding/hex"
99
"errors"
1010
"fmt"
11-
"hash"
12-
"strconv"
1311
"strings"
1412
)
1513

1614
type ObjectID interface {
1715
String() string
1816
IsZero() bool
1917
RawValue() []byte
18+
SetRawValue([]byte)
2019
Type() ObjectFormat
2120
}
2221

@@ -31,6 +30,9 @@ func (h *Sha1Hash) IsZero() bool {
3130
return bytes.Equal(empty[:], h[:])
3231
}
3332
func (h *Sha1Hash) RawValue() []byte { return h[:] }
33+
func (h *Sha1Hash) SetRawValue(b []byte) {
34+
copy(h[0:20], b)
35+
}
3436
func (*Sha1Hash) Type() ObjectFormat { return Sha1ObjectFormat }
3537

3638
var _ ObjectID = &Sha1Hash{}
@@ -45,16 +47,22 @@ func EmptyObjectID(objectFormatName string) (ObjectID, error) {
4547
return nil, errors.New("unsupported hash type")
4648
}
4749

50+
func MustID(objectFormat ObjectFormat, b []byte) ObjectID {
51+
objectID, _ := EmptyObjectID(objectFormat.Name())
52+
objectID.SetRawValue(b)
53+
return objectID
54+
}
55+
4856
func IDFromRaw(h ObjectFormat, b []byte) (ObjectID, error) {
4957
if len(b) != h.FullLength()/2 {
5058
return h.EmptyObjectID(), fmt.Errorf("length must be %d: %v", h.FullLength(), b)
5159
}
52-
return h.MustID(b), nil
60+
return MustID(h, b), nil
5361
}
5462

5563
func MustIDFromString(h ObjectFormat, s string) ObjectID {
5664
b, _ := hex.DecodeString(s)
57-
return h.MustID(b)
65+
return MustID(h, b)
5866
}
5967

6068
func genericIDFromString(h ObjectFormat, s string) (ObjectID, error) {
@@ -66,7 +74,7 @@ func genericIDFromString(h ObjectFormat, s string) (ObjectID, error) {
6674
if err != nil {
6775
return h.EmptyObjectID(), err
6876
}
69-
return h.NewID(b)
77+
return IDFromRaw(h, b)
7078
}
7179

7280
func IDFromString(hexHash string) (ObjectID, error) {
@@ -92,37 +100,9 @@ func IsEmptyCommitID(commitID string) bool {
92100
return id.IsZero()
93101
}
94102

95-
// HasherInterface is a struct that will generate a Hash
96-
type HasherInterface interface {
97-
hash.Hash
98-
99-
HashSum() ObjectID
100-
}
101-
102-
type Sha1Hasher struct {
103-
hash.Hash
104-
}
105-
106103
// ComputeBlobHash compute the hash for a given blob content
107-
func ComputeBlobHash(hashType ObjectFormat, content []byte) ObjectID {
108-
return ComputeHash(hashType, ObjectBlob, content)
109-
}
110-
111-
// ComputeHash compute the hash for a given ObjectType and content
112-
func ComputeHash(hashType ObjectFormat, t ObjectType, content []byte) ObjectID {
113-
h := hashType.NewHasher()
114-
_, _ = h.Write(t.Bytes())
115-
_, _ = h.Write([]byte(" "))
116-
_, _ = h.Write([]byte(strconv.FormatInt(int64(len(content)), 10)))
117-
_, _ = h.Write([]byte{0})
118-
return h.HashSum()
119-
}
120-
121-
// HashSum generates a SHA1 for the provided hash
122-
func (h *Sha1Hasher) HashSum() ObjectID {
123-
var sha1 Sha1Hash
124-
copy(sha1[:], h.Hash.Sum(nil))
125-
return &sha1
104+
func ComputeBlobHash(hashType ObjectFormat, content []byte) (ObjectID, error) {
105+
return hashType.ComputeHash(ObjectBlob, content)
126106
}
127107

128108
type ErrInvalidSHA struct {

modules/git/parse_gogit_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ func TestParseTreeEntries(t *testing.T) {
2828
Input: "100644 blob 61ab7345a1a3bbc590068ccae37b8515cfc5843c 1022\texample/file2.txt\n",
2929
Expected: []*TreeEntry{
3030
{
31-
ID: Sha1ObjectFormat.MustIDFromString("61ab7345a1a3bbc590068ccae37b8515cfc5843c"),
31+
ID: MustIDFromString(Sha1ObjectFormat, "61ab7345a1a3bbc590068ccae37b8515cfc5843c"),
3232
gogitTreeEntry: &object.TreeEntry{
33-
Hash: plumbing.Hash(Sha1ObjectFormat.MustIDFromString("61ab7345a1a3bbc590068ccae37b8515cfc5843c").RawValue()),
33+
Hash: plumbing.Hash(MustIDFromString(Sha1ObjectFormat, "61ab7345a1a3bbc590068ccae37b8515cfc5843c").RawValue()),
3434
Name: "example/file2.txt",
3535
Mode: filemode.Regular,
3636
},
@@ -44,20 +44,20 @@ func TestParseTreeEntries(t *testing.T) {
4444
"040000 tree 1d01fb729fb0db5881daaa6030f9f2d3cd3d5ae8 -\texample\n",
4545
Expected: []*TreeEntry{
4646
{
47-
ID: Sha1ObjectFormat.MustIDFromString("61ab7345a1a3bbc590068ccae37b8515cfc5843c"),
47+
ID: MustIDFromString(Sha1ObjectFormat, "61ab7345a1a3bbc590068ccae37b8515cfc5843c"),
4848
gogitTreeEntry: &object.TreeEntry{
49-
Hash: plumbing.Hash(Sha1ObjectFormat.MustIDFromString("61ab7345a1a3bbc590068ccae37b8515cfc5843c").RawValue()),
49+
Hash: plumbing.Hash(MustIDFromString(Sha1ObjectFormat, "61ab7345a1a3bbc590068ccae37b8515cfc5843c").RawValue()),
5050
Name: "example/\n.txt",
5151
Mode: filemode.Symlink,
5252
},
5353
size: 234131,
5454
sized: true,
5555
},
5656
{
57-
ID: Sha1ObjectFormat.MustIDFromString("1d01fb729fb0db5881daaa6030f9f2d3cd3d5ae8"),
57+
ID: MustIDFromString(Sha1ObjectFormat, "1d01fb729fb0db5881daaa6030f9f2d3cd3d5ae8"),
5858
sized: true,
5959
gogitTreeEntry: &object.TreeEntry{
60-
Hash: plumbing.Hash(Sha1ObjectFormat.MustIDFromString("1d01fb729fb0db5881daaa6030f9f2d3cd3d5ae8").RawValue()),
60+
Hash: plumbing.Hash(MustIDFromString(Sha1ObjectFormat, "1d01fb729fb0db5881daaa6030f9f2d3cd3d5ae8").RawValue()),
6161
Name: "example",
6262
Mode: filemode.Dir,
6363
},

modules/git/parse_nogogit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ loop:
127127
return nil, fmt.Errorf("unknown mode: %v", string(mode))
128128
}
129129

130-
entry.ID = objectFormat.MustID(sha)
130+
entry.ID = MustID(objectFormat, sha)
131131
entry.name = string(fname)
132132
entries = append(entries, entry)
133133
}

modules/git/parse_nogogit_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,28 @@ func TestParseTreeEntriesLong(t *testing.T) {
2626
`,
2727
Expected: []*TreeEntry{
2828
{
29-
ID: objectFormat.MustIDFromString("ea0d83c9081af9500ac9f804101b3fd0a5c293af"),
29+
ID: MustIDFromString(objectFormat, "ea0d83c9081af9500ac9f804101b3fd0a5c293af"),
3030
name: "README.md",
3131
entryMode: EntryModeBlob,
3232
size: 8218,
3333
sized: true,
3434
},
3535
{
36-
ID: objectFormat.MustIDFromString("037f27dc9d353ae4fd50f0474b2194c593914e35"),
36+
ID: MustIDFromString(objectFormat, "037f27dc9d353ae4fd50f0474b2194c593914e35"),
3737
name: "README_ZH.md",
3838
entryMode: EntryModeBlob,
3939
size: 4681,
4040
sized: true,
4141
},
4242
{
43-
ID: objectFormat.MustIDFromString("9846a94f7e8350a916632929d0fda38c90dd2ca8"),
43+
ID: MustIDFromString(objectFormat, "9846a94f7e8350a916632929d0fda38c90dd2ca8"),
4444
name: "SECURITY.md",
4545
entryMode: EntryModeBlob,
4646
size: 429,
4747
sized: true,
4848
},
4949
{
50-
ID: objectFormat.MustIDFromString("84b90550547016f73c5dd3f50dea662389e67b6d"),
50+
ID: MustIDFromString(objectFormat, "84b90550547016f73c5dd3f50dea662389e67b6d"),
5151
name: "assets",
5252
entryMode: EntryModeTree,
5353
sized: true,
@@ -78,12 +78,12 @@ func TestParseTreeEntriesShort(t *testing.T) {
7878
`,
7979
Expected: []*TreeEntry{
8080
{
81-
ID: objectFormat.MustIDFromString("ea0d83c9081af9500ac9f804101b3fd0a5c293af"),
81+
ID: MustIDFromString(objectFormat, "ea0d83c9081af9500ac9f804101b3fd0a5c293af"),
8282
name: "README.md",
8383
entryMode: EntryModeBlob,
8484
},
8585
{
86-
ID: objectFormat.MustIDFromString("84b90550547016f73c5dd3f50dea662389e67b6d"),
86+
ID: MustIDFromString(objectFormat, "84b90550547016f73c5dd3f50dea662389e67b6d"),
8787
name: "assets",
8888
entryMode: EntryModeTree,
8989
},

modules/git/pipeline/lfs_nogogit.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ func FindLFSFile(repo *git.Repository, objectID git.ObjectID) ([]*LFSResult, err
119119
if err != nil {
120120
return nil, err
121121
}
122-
curCommit, err = git.CommitFromReader(repo, objectFormat.MustIDFromString(string(commitID)), io.LimitReader(batchReader, size))
122+
objectID, err := objectFormat.NewIDFromString(string(commitID))
123+
if err != nil {
124+
return nil, err
125+
}
126+
curCommit, err = git.CommitFromReader(repo, objectID, io.LimitReader(batchReader, size))
123127
if err != nil {
124128
return nil, err
125129
}

modules/git/repo_commit_nogogit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,5 @@ func (repo *Repository) ConvertToGitID(commitID string) (ObjectID, error) {
155155
return nil, err
156156
}
157157

158-
return repo.objectFormat.MustIDFromString(string(sha)), nil
158+
return repo.objectFormat.NewIDFromString(string(sha))
159159
}

modules/git/repo_ref_nogogit.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ func (repo *Repository) GetRefsFiltered(pattern string) ([]*Reference, error) {
7373
}
7474

7575
if pattern == "" || strings.HasPrefix(refName, pattern) {
76+
objectID, err := repo.objectFormat.NewIDFromString(sha)
77+
if err != nil {
78+
return nil, err
79+
}
7680
r := &Reference{
7781
Name: refName,
78-
Object: repo.objectFormat.MustIDFromString(sha),
82+
Object: objectID,
7983
Type: typ,
8084
repo: repo,
8185
}

0 commit comments

Comments
 (0)