Skip to content

Commit

Permalink
Refactor empty objectID detection
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamMajer committed Nov 24, 2023
1 parent 475e947 commit bf9dc4c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion models/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (repo *Repository) HTMLURL() string {
// CommitLink make link to by commit full ID
// note: won't check whether it's an right id
func (repo *Repository) CommitLink(commitID string) (result string) {
if commitID == "" || commitID == repo.ObjectFormat.Empty().String() {
if git.IsEmptyCommitID(commitID) {
result = ""
} else {
result = repo.Link() + "/commit/" + url.PathEscape(commitID)
Expand Down
13 changes: 13 additions & 0 deletions modules/git/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ func IDFromString(hexHash string) (ObjectID, error) {
return nil, fmt.Errorf("invalid hash hex string: '%s' len: %d", hexHash, len(hexHash))
}

func IsEmptyCommitID(commitID string) bool {
if commitID == "" {
return true
}

id, err := IDFromString(commitID)
if err != nil {
return false
}

return id.IsZero()
}

// HashInterface is a struct that will generate a Hash
type HasherInterface interface {
hash.Hash
Expand Down
7 changes: 1 addition & 6 deletions routers/private/hook_post_receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,8 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
continue
}

commitID, err := git.IDFromString(newCommitID)
if err != nil {
commitID = &git.Sha1Hash{}
}

// If we've pushed a branch (and not deleted it)
if newCommitID != commitID.Type().Empty().String() && refFullName.IsBranch() {
if git.IsEmptyCommitID(newCommitID) && refFullName.IsBranch() {
// First ensure we have the repository loaded, we're allowed pulls requests and we can get the base repo
if repo == nil {
repo = loadRepository(ctx, ownerName, repoName)
Expand Down

0 comments on commit bf9dc4c

Please sign in to comment.