···316316 return nil
317317 }
318318319319- // Deletion of the ref means that the new commit ID is only composed of '0'.
320320- if strings.ContainsFunc(newCommitID, func(e rune) bool { return e != '0' }) {
321321- return nil
319319+ // Empty new commit ID means deletion.
320320+ if git.IsEmptyCommitID(newCommitID, nil) {
321321+ return fail(ctx, fmt.Sprintf("The deletion of %s is skipped as it's an internal reference.", refFullName), "")
322322 }
323323324324- return fail(ctx, fmt.Sprintf("The deletion of %s is skipped as it's an internal reference.", refFullName), "")
324324+ return nil
325325}
326326327327func runHookPostReceive(c *cli.Context) error {
···405405 newCommitIDs[count] = string(fields[1])
406406 refFullNames[count] = git.RefName(fields[2])
407407408408- commitID, _ := git.NewIDFromString(newCommitIDs[count])
409409- if refFullNames[count] == git.BranchPrefix+"master" && !commitID.IsZero() && count == total {
408408+ if refFullNames[count] == git.BranchPrefix+"master" && !git.IsEmptyCommitID(newCommitIDs[count], nil) && count == total {
410409 masterPushed = true
411410 }
412411 count++
···697696 if err != nil {
698697 return err
699698 }
700700- commitID, _ := git.NewIDFromString(rs.OldOID)
701701- if !commitID.IsZero() {
699699+ if !git.IsEmptyCommitID(rs.OldOID, nil) {
702700 err = writeDataPktLine(ctx, os.Stdout, []byte("option old-oid "+rs.OldOID))
703701 if err != nil {
704702 return err
+1-1
models/repo/repo.go
···329329// CommitLink make link to by commit full ID
330330// note: won't check whether it's an right id
331331func (repo *Repository) CommitLink(commitID string) (result string) {
332332- if git.IsEmptyCommitID(commitID) {
332332+ if git.IsEmptyCommitID(commitID, nil) {
333333 result = ""
334334 } else {
335335 result = repo.Link() + "/commit/" + url.PathEscape(commitID)
+1
modules/git/object_format.go
···122122var (
123123 Sha1ObjectFormat ObjectFormat = Sha1ObjectFormatImpl{}
124124 Sha256ObjectFormat ObjectFormat = Sha256ObjectFormatImpl{}
125125+ // any addition must be reflected in IsEmptyCommitID
125126)
126127127128var SupportedObjectFormats = []ObjectFormat{
+14-6
modules/git/object_id.go
···7979 return theObjectFormat.MustID(b), nil
8080}
81818282-func IsEmptyCommitID(commitID string) bool {
8282+// IsEmptyCommitID checks if an hexadecimal string represents an empty commit according to git (only '0').
8383+// If objectFormat is not nil, the length will be checked as well (otherwise the lenght must match the sha1 or sha256 length).
8484+func IsEmptyCommitID(commitID string, objectFormat ObjectFormat) bool {
8385 if commitID == "" {
8486 return true
8587 }
8686-8787- id, err := NewIDFromString(commitID)
8888- if err != nil {
8888+ if objectFormat == nil {
8989+ if Sha1ObjectFormat.FullLength() != len(commitID) && Sha256ObjectFormat.FullLength() != len(commitID) {
9090+ return false
9191+ }
9292+ } else if objectFormat.FullLength() != len(commitID) {
8993 return false
9094 }
9191-9292- return id.IsZero()
9595+ for _, c := range commitID {
9696+ if c != '0' {
9797+ return false
9898+ }
9999+ }
100100+ return true
93101}
9410295103// ComputeBlobHash compute the hash for a given blob content
···21212222// IsNewRef return true if it's a first-time push to a branch, tag or etc.
2323func (opts *PushUpdateOptions) IsNewRef() bool {
2424- commitID, err := git.NewIDFromString(opts.OldCommitID)
2525- return err == nil && commitID.IsZero()
2424+ return git.IsEmptyCommitID(opts.OldCommitID, nil)
2625}
27262827// IsDelRef return true if it's a deletion to a branch or tag
2928func (opts *PushUpdateOptions) IsDelRef() bool {
3030- commitID, err := git.NewIDFromString(opts.NewCommitID)
3131- return err == nil && commitID.IsZero()
2929+ return git.IsEmptyCommitID(opts.NewCommitID, nil)
3230}
33313432// IsUpdateRef return true if it's an update operation
+1-1
routers/private/hook_post_receive.go
···239239 }
240240241241 // If we've pushed a branch (and not deleted it)
242242- if !git.IsEmptyCommitID(newCommitID) && refFullName.IsBranch() {
242242+ if !git.IsEmptyCommitID(newCommitID, nil) && refFullName.IsBranch() {
243243 // First ensure we have the repository loaded, we're allowed pulls requests and we can get the base repo
244244 if repo == nil {
245245 repo = loadRepository(ctx, ownerName, repoName)