forked from deis/builder
/
sha.go
41 lines (32 loc) · 1.01 KB
/
sha.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package git
import (
"fmt"
"regexp"
)
// this constant represents the length of a shortened git sha - 8 characters long
const shortShaIdx = 8
var shaRegex = regexp.MustCompile(`^[\da-f]{40}$`)
// ErrInvalidGitSha is returned by NewSha if the given raw sha is invalid for any reason.
type ErrInvalidGitSha struct {
sha string
}
// Error is the error interface implementation.
func (e ErrInvalidGitSha) Error() string {
return fmt.Sprintf("git sha %s was invalid", e.sha)
}
// SHA is the representaton of a git sha
type SHA struct {
full string
short string
}
// NewSha creates a raw string to a SHA. Returns ErrInvalidGitSha if the sha was invalid.
func NewSha(rawSha string) (*SHA, error) {
if !shaRegex.MatchString(rawSha) {
return nil, ErrInvalidGitSha{sha: rawSha}
}
return &SHA{full: rawSha, short: rawSha[0:shortShaIdx]}, nil
}
// Full returns the full git sha.
func (s SHA) Full() string { return s.full }
// Short returns the first 8 characters of the sha.
func (s SHA) Short() string { return s.short }