fix: hook post-receive for sha256 repos

This commit is contained in:
oliverpool 2024-05-15 15:42:29 +02:00
parent 2ac3dcbd43
commit 5e73c67d67
8 changed files with 50 additions and 22 deletions

View file

@ -316,12 +316,12 @@ func runHookUpdate(c *cli.Context) error {
return nil return nil
} }
// Deletion of the ref means that the new commit ID is only composed of '0'. // Empty new commit ID means deletion.
if strings.ContainsFunc(newCommitID, func(e rune) bool { return e != '0' }) { if git.IsEmptyCommitID(newCommitID, nil) {
return nil return fail(ctx, fmt.Sprintf("The deletion of %s is skipped as it's an internal reference.", refFullName), "")
} }
return fail(ctx, fmt.Sprintf("The deletion of %s is skipped as it's an internal reference.", refFullName), "") return nil
} }
func runHookPostReceive(c *cli.Context) error { func runHookPostReceive(c *cli.Context) error {
@ -405,8 +405,7 @@ Forgejo or set your environment appropriately.`, "")
newCommitIDs[count] = string(fields[1]) newCommitIDs[count] = string(fields[1])
refFullNames[count] = git.RefName(fields[2]) refFullNames[count] = git.RefName(fields[2])
commitID, _ := git.NewIDFromString(newCommitIDs[count]) if refFullNames[count] == git.BranchPrefix+"master" && !git.IsEmptyCommitID(newCommitIDs[count], nil) && count == total {
if refFullNames[count] == git.BranchPrefix+"master" && !commitID.IsZero() && count == total {
masterPushed = true masterPushed = true
} }
count++ count++
@ -697,8 +696,7 @@ Forgejo or set your environment appropriately.`, "")
if err != nil { if err != nil {
return err return err
} }
commitID, _ := git.NewIDFromString(rs.OldOID) if !git.IsEmptyCommitID(rs.OldOID, nil) {
if !commitID.IsZero() {
err = writeDataPktLine(ctx, os.Stdout, []byte("option old-oid "+rs.OldOID)) err = writeDataPktLine(ctx, os.Stdout, []byte("option old-oid "+rs.OldOID))
if err != nil { if err != nil {
return err return err

View file

@ -329,7 +329,7 @@ func (repo *Repository) HTMLURL() string {
// CommitLink make link to by commit full ID // CommitLink make link to by commit full ID
// note: won't check whether it's an right id // note: won't check whether it's an right id
func (repo *Repository) CommitLink(commitID string) (result string) { func (repo *Repository) CommitLink(commitID string) (result string) {
if git.IsEmptyCommitID(commitID) { if git.IsEmptyCommitID(commitID, nil) {
result = "" result = ""
} else { } else {
result = repo.Link() + "/commit/" + url.PathEscape(commitID) result = repo.Link() + "/commit/" + url.PathEscape(commitID)

View file

@ -122,6 +122,7 @@ func (h Sha256ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) Object
var ( var (
Sha1ObjectFormat ObjectFormat = Sha1ObjectFormatImpl{} Sha1ObjectFormat ObjectFormat = Sha1ObjectFormatImpl{}
Sha256ObjectFormat ObjectFormat = Sha256ObjectFormatImpl{} Sha256ObjectFormat ObjectFormat = Sha256ObjectFormatImpl{}
// any addition must be reflected in IsEmptyCommitID
) )
var SupportedObjectFormats = []ObjectFormat{ var SupportedObjectFormats = []ObjectFormat{

View file

@ -79,17 +79,25 @@ func NewIDFromString(hexHash string) (ObjectID, error) {
return theObjectFormat.MustID(b), nil return theObjectFormat.MustID(b), nil
} }
func IsEmptyCommitID(commitID string) bool { // IsEmptyCommitID checks if an hexadecimal string represents an empty commit according to git (only '0').
// If objectFormat is not nil, the length will be checked as well (otherwise the lenght must match the sha1 or sha256 length).
func IsEmptyCommitID(commitID string, objectFormat ObjectFormat) bool {
if commitID == "" { if commitID == "" {
return true return true
} }
if objectFormat == nil {
id, err := NewIDFromString(commitID) if Sha1ObjectFormat.FullLength() != len(commitID) && Sha256ObjectFormat.FullLength() != len(commitID) {
if err != nil { return false
}
} else if objectFormat.FullLength() != len(commitID) {
return false return false
} }
for _, c := range commitID {
return id.IsZero() if c != '0' {
return false
}
}
return true
} }
// ComputeBlobHash compute the hash for a given blob content // ComputeBlobHash compute the hash for a given blob content

View file

@ -23,3 +23,27 @@ func TestIsValidSHAPattern(t *testing.T) {
assert.Equal(t, "d5c6407415d85df49592672aa421aed39b9db5e3", ComputeBlobHash(Sha1ObjectFormat, []byte("same length blob")).String()) assert.Equal(t, "d5c6407415d85df49592672aa421aed39b9db5e3", ComputeBlobHash(Sha1ObjectFormat, []byte("same length blob")).String())
assert.Equal(t, "df0b5174ed06ae65aea40d43316bcbc21d82c9e3158ce2661df2ad28d7931dd6", ComputeBlobHash(Sha256ObjectFormat, []byte("some random blob")).String()) assert.Equal(t, "df0b5174ed06ae65aea40d43316bcbc21d82c9e3158ce2661df2ad28d7931dd6", ComputeBlobHash(Sha256ObjectFormat, []byte("some random blob")).String())
} }
func TestIsEmptyCommitID(t *testing.T) {
assert.True(t, IsEmptyCommitID("", nil))
assert.True(t, IsEmptyCommitID("", Sha1ObjectFormat))
assert.True(t, IsEmptyCommitID("", Sha256ObjectFormat))
assert.False(t, IsEmptyCommitID("79ee38a6416c1ede423ec7ee0a8639ceea4aad20", Sha1ObjectFormat))
assert.True(t, IsEmptyCommitID("0000000000000000000000000000000000000000", nil))
assert.True(t, IsEmptyCommitID("0000000000000000000000000000000000000000", Sha1ObjectFormat))
assert.False(t, IsEmptyCommitID("0000000000000000000000000000000000000000", Sha256ObjectFormat))
assert.False(t, IsEmptyCommitID("00000000000000000000000000000000000000000", nil))
assert.False(t, IsEmptyCommitID("0f0b5174ed06ae65aea40d43316bcbc21d82c9e3158ce2661df2ad28d7931dd6", nil))
assert.True(t, IsEmptyCommitID("0000000000000000000000000000000000000000000000000000000000000000", nil))
assert.False(t, IsEmptyCommitID("0000000000000000000000000000000000000000000000000000000000000000", Sha1ObjectFormat))
assert.True(t, IsEmptyCommitID("0000000000000000000000000000000000000000000000000000000000000000", Sha256ObjectFormat))
assert.False(t, IsEmptyCommitID("1", nil))
assert.False(t, IsEmptyCommitID("0", nil))
assert.False(t, IsEmptyCommitID("010", nil))
assert.False(t, IsEmptyCommitID("0 0", nil))
}

View file

@ -21,14 +21,12 @@ type PushUpdateOptions struct {
// IsNewRef return true if it's a first-time push to a branch, tag or etc. // IsNewRef return true if it's a first-time push to a branch, tag or etc.
func (opts *PushUpdateOptions) IsNewRef() bool { func (opts *PushUpdateOptions) IsNewRef() bool {
commitID, err := git.NewIDFromString(opts.OldCommitID) return git.IsEmptyCommitID(opts.OldCommitID, nil)
return err == nil && commitID.IsZero()
} }
// IsDelRef return true if it's a deletion to a branch or tag // IsDelRef return true if it's a deletion to a branch or tag
func (opts *PushUpdateOptions) IsDelRef() bool { func (opts *PushUpdateOptions) IsDelRef() bool {
commitID, err := git.NewIDFromString(opts.NewCommitID) return git.IsEmptyCommitID(opts.NewCommitID, nil)
return err == nil && commitID.IsZero()
} }
// IsUpdateRef return true if it's an update operation // IsUpdateRef return true if it's an update operation

View file

@ -239,7 +239,7 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
} }
// If we've pushed a branch (and not deleted it) // If we've pushed a branch (and not deleted it)
if !git.IsEmptyCommitID(newCommitID) && refFullName.IsBranch() { if !git.IsEmptyCommitID(newCommitID, nil) && refFullName.IsBranch() {
// First ensure we have the repository loaded, we're allowed pulls requests and we can get the base repo // First ensure we have the repository loaded, we're allowed pulls requests and we can get the base repo
if repo == nil { if repo == nil {
repo = loadRepository(ctx, ownerName, repoName) repo = loadRepository(ctx, ownerName, repoName)

View file

@ -515,8 +515,7 @@ func (*actionsNotifier) MergePullRequest(ctx context.Context, doer *user_model.U
} }
func (n *actionsNotifier) PushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { func (n *actionsNotifier) PushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
commitID, _ := git.NewIDFromString(opts.NewCommitID) if git.IsEmptyCommitID(opts.NewCommitID, nil) {
if commitID.IsZero() {
log.Trace("new commitID is empty") log.Trace("new commitID is empty")
return return
} }