Skip to content

Commit 29250ba

Browse files
committed
Refactor empty objectID detection
1 parent ecfd075 commit 29250ba

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

models/repo/repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ func (repo *Repository) HTMLURL() string {
317317
// CommitLink make link to by commit full ID
318318
// note: won't check whether it's an right id
319319
func (repo *Repository) CommitLink(commitID string) (result string) {
320-
if commitID == "" || commitID == repo.ObjectFormat.Empty().String() {
320+
if git.IsEmptyCommitID(commitID) {
321321
result = ""
322322
} else {
323323
result = repo.Link() + "/commit/" + url.PathEscape(commitID)

modules/git/hash.go

+13
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ func IDFromString(hexHash string) (ObjectID, error) {
181181
return nil, fmt.Errorf("invalid hash hex string: '%s' len: %d", hexHash, len(hexHash))
182182
}
183183

184+
func IsEmptyCommitID(commitID string) bool {
185+
if commitID == "" {
186+
return true
187+
}
188+
189+
id, err := IDFromString(commitID)
190+
if err != nil {
191+
return false
192+
}
193+
194+
return id.IsZero()
195+
}
196+
184197
// HashInterface is a struct that will generate a Hash
185198
type HasherInterface interface {
186199
hash.Hash

routers/private/hook_post_receive.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,8 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
158158
continue
159159
}
160160

161-
commitID, err := git.IDFromString(newCommitID)
162-
if err != nil {
163-
commitID = &git.Sha1Hash{}
164-
}
165-
166161
// If we've pushed a branch (and not deleted it)
167-
if newCommitID != commitID.Type().Empty().String() && refFullName.IsBranch() {
162+
if git.IsEmptyCommitID(newCommitID) && refFullName.IsBranch() {
168163
// First ensure we have the repository loaded, we're allowed pulls requests and we can get the base repo
169164
if repo == nil {
170165
repo = loadRepository(ctx, ownerName, repoName)

0 commit comments

Comments
 (0)