File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package git
22
33import (
44 "fmt"
5+ "regexp"
56)
67
78const (
@@ -10,12 +11,14 @@ const (
1011 fullShaLen = 40
1112)
1213
13- type ErrGitShaTooShort struct {
14+ var shaRegex = regexp .MustCompile (`^[\da-f]{40}$` )
15+
16+ type ErrInvalidGitSha struct {
1417 sha string
1518}
1619
17- func (e ErrGitShaTooShort ) Error () string {
18- return fmt .Sprintf ("git sha %s was too short " , e .sha )
20+ func (e ErrInvalidGitSha ) Error () string {
21+ return fmt .Sprintf ("git sha %s was invalid " , e .sha )
1922}
2023
2124type SHA struct {
@@ -24,8 +27,8 @@ type SHA struct {
2427}
2528
2629func NewSha (rawSha string ) (* SHA , error ) {
27- if len ( rawSha ) < fullShaLen {
28- return nil , ErrGitShaTooShort {sha : rawSha }
30+ if ! shaRegex . Match ([] byte ( rawSha )) {
31+ return nil , ErrInvalidGitSha {sha : rawSha }
2932 }
3033 return & SHA {Full : rawSha , Short : rawSha [0 :8 ]}, nil
3134}
You can’t perform that action at this time.
0 commit comments