Skip to content

Commit 605326f

Browse files
committed
Adjust object format interface
1 parent 52046b9 commit 605326f

20 files changed

+39
-68
lines changed

models/repo/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func (repo *Repository) AfterLoad() {
277277
repo.NumOpenProjects = repo.NumProjects - repo.NumClosedProjects
278278
repo.NumOpenActionRuns = repo.NumActionRuns - repo.NumClosedActionRuns
279279

280-
repo.ObjectFormat = git.ObjectFormatFromID(git.Sha1)
280+
repo.ObjectFormat = git.Sha1ObjectFormat{}
281281
}
282282

283283
// LoadAttributes loads attributes of the repository.

modules/git/blame_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestReadingBlameOutput(t *testing.T) {
3939
}
4040

4141
for _, bypass := range []bool{false, true} {
42-
blameReader, err := CreateBlameReader(ctx, &Sha1ObjectFormat{}, "./tests/repos/repo5_pulls", commit, "README.md", bypass)
42+
blameReader, err := CreateBlameReader(ctx, Sha1ObjectFormat{}, "./tests/repos/repo5_pulls", commit, "README.md", bypass)
4343
assert.NoError(t, err)
4444
assert.NotNil(t, blameReader)
4545
defer blameReader.Close()

modules/git/object_format.go

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,13 @@ package git
55

66
import (
77
"crypto/sha1"
8-
"fmt"
98
"regexp"
10-
"strings"
11-
)
12-
13-
type ObjectFormatID int
14-
15-
const (
16-
Sha1 ObjectFormatID = iota
179
)
1810

1911
// sha1Pattern can be used to determine if a string is an valid sha
2012
var sha1Pattern = regexp.MustCompile(`^[0-9a-f]{4,40}$`)
2113

2214
type ObjectFormat interface {
23-
ID() ObjectFormatID
2415
String() string
2516

2617
// Empty is the hash of empty git
@@ -43,61 +34,41 @@ type ObjectFormat interface {
4334
/* SHA1 Type */
4435
type Sha1ObjectFormat struct{}
4536

46-
func (*Sha1ObjectFormat) ID() ObjectFormatID { return Sha1 }
47-
func (*Sha1ObjectFormat) String() string { return "sha1" }
48-
func (*Sha1ObjectFormat) Empty() ObjectID { return &Sha1Hash{} }
49-
func (*Sha1ObjectFormat) EmptyTree() ObjectID {
37+
func (Sha1ObjectFormat) String() string { return "sha1" }
38+
func (Sha1ObjectFormat) Empty() ObjectID { return &Sha1Hash{} }
39+
func (Sha1ObjectFormat) EmptyTree() ObjectID {
5040
return &Sha1Hash{
5141
0x4b, 0x82, 0x5d, 0xc6, 0x42, 0xcb, 0x6e, 0xb9, 0xa0, 0x60,
5242
0xe5, 0x4b, 0xf8, 0xd6, 0x92, 0x88, 0xfb, 0xee, 0x49, 0x04,
5343
}
5444
}
55-
func (*Sha1ObjectFormat) FullLength() int { return 40 }
56-
func (*Sha1ObjectFormat) IsValid(input string) bool {
45+
func (Sha1ObjectFormat) FullLength() int { return 40 }
46+
func (Sha1ObjectFormat) IsValid(input string) bool {
5747
return sha1Pattern.MatchString(input)
5848
}
5949

60-
func (*Sha1ObjectFormat) MustID(b []byte) ObjectID {
50+
func (Sha1ObjectFormat) MustID(b []byte) ObjectID {
6151
var id Sha1Hash
6252
copy(id[0:20], b)
6353
return &id
6454
}
6555

66-
func (h *Sha1ObjectFormat) MustIDFromString(s string) ObjectID {
56+
func (h Sha1ObjectFormat) MustIDFromString(s string) ObjectID {
6757
return MustIDFromString(h, s)
6858
}
6959

70-
func (h *Sha1ObjectFormat) NewID(b []byte) (ObjectID, error) {
60+
func (h Sha1ObjectFormat) NewID(b []byte) (ObjectID, error) {
7161
return IDFromRaw(h, b)
7262
}
7363

74-
func (h *Sha1ObjectFormat) NewIDFromString(s string) (ObjectID, error) {
64+
func (h Sha1ObjectFormat) NewIDFromString(s string) (ObjectID, error) {
7565
return genericIDFromString(h, s)
7666
}
7767

78-
func (*Sha1ObjectFormat) NewEmptyID() ObjectID {
68+
func (Sha1ObjectFormat) NewEmptyID() ObjectID {
7969
return NewSha1()
8070
}
8171

82-
func (h *Sha1ObjectFormat) NewHasher() HasherInterface {
72+
func (h Sha1ObjectFormat) NewHasher() HasherInterface {
8373
return &Sha1Hasher{sha1.New()}
8474
}
85-
86-
// utils
87-
func ObjectFormatFromID(id ObjectFormatID) ObjectFormat {
88-
switch id {
89-
case Sha1:
90-
return &Sha1ObjectFormat{}
91-
}
92-
93-
return nil
94-
}
95-
96-
func ObjectFormatFromString(hash string) (ObjectFormat, error) {
97-
switch strings.ToLower(hash) {
98-
case "sha1":
99-
return &Sha1ObjectFormat{}, nil
100-
}
101-
102-
return nil, fmt.Errorf("unknown hash type: %s", hash)
103-
}

modules/git/object_id.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (h *Sha1Hash) IsZero() bool {
3232
return bytes.Equal(empty[:], h[:])
3333
}
3434
func (h *Sha1Hash) RawValue() []byte { return h[:] }
35-
func (*Sha1Hash) Type() ObjectFormat { return &Sha1ObjectFormat{} }
35+
func (*Sha1Hash) Type() ObjectFormat { return Sha1ObjectFormat{} }
3636

3737
func NewSha1() *Sha1Hash {
3838
return &Sha1Hash{}

modules/git/object_id_gogit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
func ParseGogitHash(h plumbing.Hash) ObjectID {
1313
switch hash.Size {
1414
case 20:
15-
return ObjectFormatFromID(Sha1).MustID(h[:])
15+
return Sha1ObjectFormat{}.MustID(h[:])
1616
}
1717

1818
return nil

modules/git/parse_gogit_test.go

Lines changed: 7 additions & 7 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: ObjectFormatFromID(Sha1).MustIDFromString("61ab7345a1a3bbc590068ccae37b8515cfc5843c"),
31+
ID: Sha1ObjectFormat{}.MustIDFromString("61ab7345a1a3bbc590068ccae37b8515cfc5843c"),
3232
gogitTreeEntry: &object.TreeEntry{
33-
Hash: plumbing.Hash(ObjectFormatFromID(Sha1).MustIDFromString("61ab7345a1a3bbc590068ccae37b8515cfc5843c").RawValue()),
33+
Hash: plumbing.Hash(Sha1ObjectFormat{}.MustIDFromString("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: ObjectFormatFromID(Sha1).MustIDFromString("61ab7345a1a3bbc590068ccae37b8515cfc5843c"),
47+
ID: Sha1ObjectFormat{}.MustIDFromString("61ab7345a1a3bbc590068ccae37b8515cfc5843c"),
4848
gogitTreeEntry: &object.TreeEntry{
49-
Hash: plumbing.Hash(ObjectFormatFromID(Sha1).MustIDFromString("61ab7345a1a3bbc590068ccae37b8515cfc5843c").RawValue()),
49+
Hash: plumbing.Hash(Sha1ObjectFormat{}.MustIDFromString("61ab7345a1a3bbc590068ccae37b8515cfc5843c").RawValue()),
5050
Name: "example/\n.txt",
5151
Mode: filemode.Symlink,
5252
},
5353
size: 234131,
5454
sized: true,
5555
},
5656
{
57-
ID: ObjectFormatFromID(Sha1).MustIDFromString("1d01fb729fb0db5881daaa6030f9f2d3cd3d5ae8"),
57+
ID: Sha1ObjectFormat{}.MustIDFromString("1d01fb729fb0db5881daaa6030f9f2d3cd3d5ae8"),
5858
sized: true,
5959
gogitTreeEntry: &object.TreeEntry{
60-
Hash: plumbing.Hash(ObjectFormatFromID(Sha1).MustIDFromString("1d01fb729fb0db5881daaa6030f9f2d3cd3d5ae8").RawValue()),
60+
Hash: plumbing.Hash(Sha1ObjectFormat{}.MustIDFromString("1d01fb729fb0db5881daaa6030f9f2d3cd3d5ae8").RawValue()),
6161
Name: "example",
6262
Mode: filemode.Dir,
6363
},
@@ -67,7 +67,7 @@ func TestParseTreeEntries(t *testing.T) {
6767
}
6868

6969
for _, testCase := range testCases {
70-
entries, err := ParseTreeEntries(ObjectFormatFromID(Sha1), []byte(testCase.Input))
70+
entries, err := ParseTreeEntries(Sha1ObjectFormat{}, []byte(testCase.Input))
7171
assert.NoError(t, err)
7272
if len(entries) > 1 {
7373
fmt.Println(testCase.Expected[0].ID)

modules/git/parse_nogogit_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
func TestParseTreeEntriesLong(t *testing.T) {
15-
objectFormat := ObjectFormatFromID(Sha1)
15+
objectFormat := Sha1ObjectFormat{}
1616

1717
testCases := []struct {
1818
Input string
@@ -66,7 +66,7 @@ func TestParseTreeEntriesLong(t *testing.T) {
6666
}
6767

6868
func TestParseTreeEntriesShort(t *testing.T) {
69-
objectFormat := ObjectFormatFromID(Sha1)
69+
objectFormat := Sha1ObjectFormat{}
7070

7171
testCases := []struct {
7272
Input string
@@ -102,7 +102,7 @@ func TestParseTreeEntriesShort(t *testing.T) {
102102

103103
func TestParseTreeEntriesInvalid(t *testing.T) {
104104
// there was a panic: "runtime error: slice bounds out of range" when the input was invalid: #20315
105-
entries, err := ParseTreeEntries(ObjectFormatFromID(Sha1), []byte("100644 blob ea0d83c9081af9500ac9f804101b3fd0a5c293af"))
105+
entries, err := ParseTreeEntries(Sha1ObjectFormat{}, []byte("100644 blob ea0d83c9081af9500ac9f804101b3fd0a5c293af"))
106106
assert.Error(t, err)
107107
assert.Len(t, entries, 0)
108108
}

modules/git/ref.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func RefURL(repoURL, ref string) string {
205205
return repoURL + "/src/branch/" + refName
206206
case refFullName.IsTag():
207207
return repoURL + "/src/tag/" + refName
208-
case !ObjectFormatFromID(Sha1).IsValid(ref):
208+
case !Sha1ObjectFormat{}.IsValid(ref):
209209
// assume they mean a branch
210210
return repoURL + "/src/branch/" + refName
211211
default:

modules/git/repo_tag_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func TestRepository_GetAnnotatedTag(t *testing.T) {
194194
}
195195

196196
func TestRepository_parseTagRef(t *testing.T) {
197-
sha1 := ObjectFormatFromID(Sha1)
197+
sha1 := Sha1ObjectFormat{}
198198
tests := []struct {
199199
name string
200200

modules/git/tag_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ ono`), tag: Tag{
4949
}
5050

5151
for _, test := range testData {
52-
tag, err := parseTagData(ObjectFormatFromID(Sha1), test.data)
52+
tag, err := parseTagData(Sha1ObjectFormat{}, test.data)
5353
assert.NoError(t, err)
5454
assert.EqualValues(t, test.tag.ID, tag.ID)
5555
assert.EqualValues(t, test.tag.Object, tag.Object)

modules/repository/commits_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func TestListToPushCommits(t *testing.T) {
169169
When: now,
170170
}
171171

172-
hashType := git.ObjectFormatFromID(git.Sha1)
172+
hashType := git.Sha1ObjectFormat{}
173173
const hexString1 = "0123456789abcdef0123456789abcdef01234567"
174174
hash1, err := hashType.NewIDFromString(hexString1)
175175
assert.NoError(t, err)

modules/repository/generate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r
224224
}
225225

226226
// FIXME: fix the hash
227-
if err := git.InitRepository(ctx, tmpDir, false, git.ObjectFormatFromID(git.Sha1)); err != nil {
227+
if err := git.InitRepository(ctx, tmpDir, false, git.Sha1ObjectFormat{}); err != nil {
228228
return err
229229
}
230230

@@ -358,7 +358,7 @@ func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templ
358358
}
359359

360360
// FIXME - fix the hash
361-
if err = CheckInitRepository(ctx, owner.Name, generateRepo.Name, git.ObjectFormatFromID(git.Sha1)); err != nil {
361+
if err = CheckInitRepository(ctx, owner.Name, generateRepo.Name, git.Sha1ObjectFormat{}); err != nil {
362362
return generateRepo, err
363363
}
364364

routers/api/v1/repo/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func CreateUserRepo(ctx *context.APIContext, owner *user_model.User, opt api.Cre
253253
DefaultBranch: opt.DefaultBranch,
254254
TrustModel: repo_model.ToTrustModel(opt.TrustModel),
255255
IsTemplate: opt.Template,
256-
ObjectFormat: git.ObjectFormatFromID(git.Sha1),
256+
ObjectFormat: git.Sha1ObjectFormat{},
257257
})
258258
if err != nil {
259259
if repo_model.IsErrRepoAlreadyExist(err) {

routers/web/repo/githttp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ func dummyInfoRefs(ctx *context.Context) {
329329
}
330330
}()
331331

332-
if err := git.InitRepository(ctx, tmpDir, true, git.ObjectFormatFromID(git.Sha1)); err != nil {
332+
if err := git.InitRepository(ctx, tmpDir, true, git.Sha1ObjectFormat{}); err != nil {
333333
log.Error("Failed to init bare repo for git-receive-pack cache: %v", err)
334334
return
335335
}

services/convert/git_commit_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
func TestToCommitMeta(t *testing.T) {
2020
assert.NoError(t, unittest.PrepareTestDatabase())
2121
headRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
22-
sha1 := git.ObjectFormatFromID(git.Sha1)
22+
sha1 := git.Sha1ObjectFormat{}
2323
signature := &git.Signature{Name: "Test Signature", Email: "[email protected]", When: time.Unix(0, 0)}
2424
tag := &git.Tag{
2525
Name: "Test Tag",

services/migrations/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func CheckAndEnsureSafePR(pr *base.PullRequest, commonCloneBaseURL string, g bas
4949

5050
// SECURITY: SHAs Must be a SHA
5151
// FIXME: hash only a SHA1
52-
CommitType := git.ObjectFormatFromID(git.Sha1)
52+
CommitType := git.Sha1ObjectFormat{}
5353
if pr.MergeCommitSHA != "" && !CommitType.IsValid(pr.MergeCommitSHA) {
5454
WarnAndNotice("PR #%d in %s has invalid MergeCommitSHA: %s", pr.Number, g, pr.MergeCommitSHA)
5555
pr.MergeCommitSHA = ""

services/repository/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func CreateRepositoryDirectly(ctx context.Context, doer, u *user_model.User, opt
211211
}
212212

213213
if opts.ObjectFormat == nil {
214-
opts.ObjectFormat = git.ObjectFormatFromID(git.Sha1)
214+
opts.ObjectFormat = git.Sha1ObjectFormat{}
215215
}
216216

217217
// Check if label template exist

services/wiki/wiki.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error {
3636
return nil
3737
}
3838

39-
if err := git.InitRepository(ctx, repo.WikiPath(), true, git.ObjectFormatFromID(git.Sha1)); err != nil {
39+
if err := git.InitRepository(ctx, repo.WikiPath(), true, git.Sha1ObjectFormat{}); err != nil {
4040
return fmt.Errorf("InitRepository: %w", err)
4141
} else if err = repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil {
4242
return fmt.Errorf("createDelegateHooks: %w", err)

services/wiki/wiki_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func TestPrepareWikiFileName_FirstPage(t *testing.T) {
302302
// Now create a temporaryDirectory
303303
tmpDir := t.TempDir()
304304

305-
err := git.InitRepository(git.DefaultContext, tmpDir, true, git.ObjectFormatFromID(git.Sha1))
305+
err := git.InitRepository(git.DefaultContext, tmpDir, true, git.Sha1ObjectFormat{})
306306
assert.NoError(t, err)
307307

308308
gitRepo, err := git.OpenRepository(git.DefaultContext, tmpDir)

tests/integration/git_helper_for_declarative_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func doGitCloneFail(u *url.URL) func(*testing.T) {
120120
func doGitInitTestRepository(dstPath string) func(*testing.T) {
121121
return func(t *testing.T) {
122122
// Init repository in dstPath
123-
assert.NoError(t, git.InitRepository(git.DefaultContext, dstPath, false, git.ObjectFormatFromID(git.Sha1)))
123+
assert.NoError(t, git.InitRepository(git.DefaultContext, dstPath, false, git.Sha1ObjectFormat{}))
124124
// forcibly set default branch to master
125125
_, _, err := git.NewCommand(git.DefaultContext, "symbolic-ref", "HEAD", git.BranchPrefix+"master").RunStdString(&git.RunOpts{Dir: dstPath})
126126
assert.NoError(t, err)

0 commit comments

Comments
 (0)