Fix empty tree hash for SHA-256 repos

This commit is contained in:
GDS K S 2026-04-11 22:46:44 -05:00
parent 38dd035e28
commit 4c43104537
No known key found for this signature in database
2 changed files with 17 additions and 3 deletions

View file

@ -8,8 +8,9 @@ import (
"github.com/stefanhaller/git-todo-parser/todo"
)
// Special commit hash for empty tree object
// Empty tree object hashes for SHA-1 and SHA-256 repos.
const EmptyTreeCommitHash = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
const EmptyTreeCommitHashSHA256 = "6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321"
type CommitStatus uint8
@ -122,11 +123,20 @@ func (c *Commit) ShortRefName() string {
func (c *Commit) ParentRefName() string {
if c.IsFirstCommit() {
return EmptyTreeCommitHash
return c.emptyTreeHash()
}
return c.RefName() + "^"
}
// emptyTreeHash returns the empty tree object hash matching this repo's
// object format. SHA-256 repos produce 64-char hashes; SHA-1 repos 40-char.
func (c *Commit) emptyTreeHash() string {
if len(c.Hash()) > 40 {
return EmptyTreeCommitHashSHA256
}
return EmptyTreeCommitHash
}
func (c *Commit) Parents() []string {
return lo.Map(c.parents, func(s *string, _ int) string { return *s })
}

View file

@ -142,7 +142,11 @@ func getNextPipes(prevPipes []Pipe, commit *models.Commit, getStyle func(c *mode
var toHash *string
if commit.IsFirstCommit() {
toHash = &EmptyTreeCommitHash
emptyHash := EmptyTreeCommitHash
if len(commit.Hash()) > 40 {
emptyHash = models.EmptyTreeCommitHashSHA256
}
toHash = &emptyHash
} else {
toHash = commit.ParentPtrs()[0]
}