loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: hook post-receive for sha256 repos

+50 -22
+6 -8
cmd/hook.go
··· 316 316 return nil 317 317 } 318 318 319 - // Deletion of the ref means that the new commit ID is only composed of '0'. 320 - if strings.ContainsFunc(newCommitID, func(e rune) bool { return e != '0' }) { 321 - return nil 319 + // Empty new commit ID means deletion. 320 + if git.IsEmptyCommitID(newCommitID, nil) { 321 + return fail(ctx, fmt.Sprintf("The deletion of %s is skipped as it's an internal reference.", refFullName), "") 322 322 } 323 323 324 - return fail(ctx, fmt.Sprintf("The deletion of %s is skipped as it's an internal reference.", refFullName), "") 324 + return nil 325 325 } 326 326 327 327 func runHookPostReceive(c *cli.Context) error { ··· 405 405 newCommitIDs[count] = string(fields[1]) 406 406 refFullNames[count] = git.RefName(fields[2]) 407 407 408 - commitID, _ := git.NewIDFromString(newCommitIDs[count]) 409 - if refFullNames[count] == git.BranchPrefix+"master" && !commitID.IsZero() && count == total { 408 + if refFullNames[count] == git.BranchPrefix+"master" && !git.IsEmptyCommitID(newCommitIDs[count], nil) && count == total { 410 409 masterPushed = true 411 410 } 412 411 count++ ··· 697 696 if err != nil { 698 697 return err 699 698 } 700 - commitID, _ := git.NewIDFromString(rs.OldOID) 701 - if !commitID.IsZero() { 699 + if !git.IsEmptyCommitID(rs.OldOID, nil) { 702 700 err = writeDataPktLine(ctx, os.Stdout, []byte("option old-oid "+rs.OldOID)) 703 701 if err != nil { 704 702 return err
+1 -1
models/repo/repo.go
··· 329 329 // CommitLink make link to by commit full ID 330 330 // note: won't check whether it's an right id 331 331 func (repo *Repository) CommitLink(commitID string) (result string) { 332 - if git.IsEmptyCommitID(commitID) { 332 + if git.IsEmptyCommitID(commitID, nil) { 333 333 result = "" 334 334 } else { 335 335 result = repo.Link() + "/commit/" + url.PathEscape(commitID)
+1
modules/git/object_format.go
··· 122 122 var ( 123 123 Sha1ObjectFormat ObjectFormat = Sha1ObjectFormatImpl{} 124 124 Sha256ObjectFormat ObjectFormat = Sha256ObjectFormatImpl{} 125 + // any addition must be reflected in IsEmptyCommitID 125 126 ) 126 127 127 128 var SupportedObjectFormats = []ObjectFormat{
+14 -6
modules/git/object_id.go
··· 79 79 return theObjectFormat.MustID(b), nil 80 80 } 81 81 82 - func IsEmptyCommitID(commitID string) bool { 82 + // IsEmptyCommitID checks if an hexadecimal string represents an empty commit according to git (only '0'). 83 + // If objectFormat is not nil, the length will be checked as well (otherwise the lenght must match the sha1 or sha256 length). 84 + func IsEmptyCommitID(commitID string, objectFormat ObjectFormat) bool { 83 85 if commitID == "" { 84 86 return true 85 87 } 86 - 87 - id, err := NewIDFromString(commitID) 88 - if err != nil { 88 + if objectFormat == nil { 89 + if Sha1ObjectFormat.FullLength() != len(commitID) && Sha256ObjectFormat.FullLength() != len(commitID) { 90 + return false 91 + } 92 + } else if objectFormat.FullLength() != len(commitID) { 89 93 return false 90 94 } 91 - 92 - return id.IsZero() 95 + for _, c := range commitID { 96 + if c != '0' { 97 + return false 98 + } 99 + } 100 + return true 93 101 } 94 102 95 103 // ComputeBlobHash compute the hash for a given blob content
+24
modules/git/object_id_test.go
··· 23 23 assert.Equal(t, "d5c6407415d85df49592672aa421aed39b9db5e3", ComputeBlobHash(Sha1ObjectFormat, []byte("same length blob")).String()) 24 24 assert.Equal(t, "df0b5174ed06ae65aea40d43316bcbc21d82c9e3158ce2661df2ad28d7931dd6", ComputeBlobHash(Sha256ObjectFormat, []byte("some random blob")).String()) 25 25 } 26 + 27 + func TestIsEmptyCommitID(t *testing.T) { 28 + assert.True(t, IsEmptyCommitID("", nil)) 29 + assert.True(t, IsEmptyCommitID("", Sha1ObjectFormat)) 30 + assert.True(t, IsEmptyCommitID("", Sha256ObjectFormat)) 31 + 32 + assert.False(t, IsEmptyCommitID("79ee38a6416c1ede423ec7ee0a8639ceea4aad20", Sha1ObjectFormat)) 33 + assert.True(t, IsEmptyCommitID("0000000000000000000000000000000000000000", nil)) 34 + assert.True(t, IsEmptyCommitID("0000000000000000000000000000000000000000", Sha1ObjectFormat)) 35 + assert.False(t, IsEmptyCommitID("0000000000000000000000000000000000000000", Sha256ObjectFormat)) 36 + 37 + assert.False(t, IsEmptyCommitID("00000000000000000000000000000000000000000", nil)) 38 + 39 + assert.False(t, IsEmptyCommitID("0f0b5174ed06ae65aea40d43316bcbc21d82c9e3158ce2661df2ad28d7931dd6", nil)) 40 + assert.True(t, IsEmptyCommitID("0000000000000000000000000000000000000000000000000000000000000000", nil)) 41 + assert.False(t, IsEmptyCommitID("0000000000000000000000000000000000000000000000000000000000000000", Sha1ObjectFormat)) 42 + assert.True(t, IsEmptyCommitID("0000000000000000000000000000000000000000000000000000000000000000", Sha256ObjectFormat)) 43 + 44 + assert.False(t, IsEmptyCommitID("1", nil)) 45 + assert.False(t, IsEmptyCommitID("0", nil)) 46 + 47 + assert.False(t, IsEmptyCommitID("010", nil)) 48 + assert.False(t, IsEmptyCommitID("0 0", nil)) 49 + }
+2 -4
modules/repository/push.go
··· 21 21 22 22 // IsNewRef return true if it's a first-time push to a branch, tag or etc. 23 23 func (opts *PushUpdateOptions) IsNewRef() bool { 24 - commitID, err := git.NewIDFromString(opts.OldCommitID) 25 - return err == nil && commitID.IsZero() 24 + return git.IsEmptyCommitID(opts.OldCommitID, nil) 26 25 } 27 26 28 27 // IsDelRef return true if it's a deletion to a branch or tag 29 28 func (opts *PushUpdateOptions) IsDelRef() bool { 30 - commitID, err := git.NewIDFromString(opts.NewCommitID) 31 - return err == nil && commitID.IsZero() 29 + return git.IsEmptyCommitID(opts.NewCommitID, nil) 32 30 } 33 31 34 32 // IsUpdateRef return true if it's an update operation
+1 -1
routers/private/hook_post_receive.go
··· 239 239 } 240 240 241 241 // If we've pushed a branch (and not deleted it) 242 - if !git.IsEmptyCommitID(newCommitID) && refFullName.IsBranch() { 242 + if !git.IsEmptyCommitID(newCommitID, nil) && refFullName.IsBranch() { 243 243 // First ensure we have the repository loaded, we're allowed pulls requests and we can get the base repo 244 244 if repo == nil { 245 245 repo = loadRepository(ctx, ownerName, repoName)
+1 -2
services/actions/notifier.go
··· 515 515 } 516 516 517 517 func (n *actionsNotifier) PushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { 518 - commitID, _ := git.NewIDFromString(opts.NewCommitID) 519 - if commitID.IsZero() { 518 + if git.IsEmptyCommitID(opts.NewCommitID, nil) { 520 519 log.Trace("new commitID is empty") 521 520 return 522 521 }