Skip to content

Commit

Permalink
Add support for SHA-256 Git commit IDs
Browse files Browse the repository at this point in the history
This also adds a few (mostly edge-case) tests for the `gitutil.IsCommitSHA` function.

Signed-off-by: Tianon Gravi <[email protected]>
  • Loading branch information
tianon committed Oct 30, 2024
1 parent fd61877 commit 80215f6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion util/gitutil/git_commit.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gitutil

func IsCommitSHA(str string) bool {
if len(str) != 40 {
if l := len(str); l != 40 && l != 64 {
return false
}

Expand Down
35 changes: 35 additions & 0 deletions util/gitutil/git_commit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package gitutil

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestIsCommitSHA(t *testing.T) {
for truthy, commits := range map[bool][]string{
true: {
"01234567890abcdef01234567890abcdef012345", // 40 valid characters (SHA-1)
"01234567890abcdef01234567890abcdef01234567890abcdef01234567890ab", // 64 valid characters (SHA-256)
},
false: {
"", // empty string
"abcdef", // too short

"123456789012345678901234567890123456789", // 39 valid characters
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", // 40 invalid characters
"12345678901234567890123456789012345678901", // 41 valid characters

"01234567890abcdef01234567890abcdef01234567890abcdef01234567890a", // 63 valid characters
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", // 64 invalid characters
"01234567890abcdef01234567890abcdef01234567890abcdef01234567890abc", // 65 valid characters
},
} {
for _, commit := range commits {
t.Run(fmt.Sprintf("%t/%q", truthy, commit), func(t *testing.T) {
assert.Equal(t, truthy, IsCommitSHA(commit))
})
}
}
}

0 comments on commit 80215f6

Please sign in to comment.