@@ -5,26 +5,17 @@ package git
55
66import (
77 "crypto/sha1"
8- "fmt"
98 "regexp"
10- "strings"
11- )
12-
13- type ObjectFormatID int
14-
15- const (
16- Sha1 ObjectFormatID = iota
179)
1810
1911// sha1Pattern can be used to determine if a string is an valid sha
2012var sha1Pattern = regexp .MustCompile (`^[0-9a-f]{4,40}$` )
2113
2214type ObjectFormat interface {
23- ID () ObjectFormatID
24- String () string
25-
26- // Empty is the hash of empty git
27- Empty () ObjectID
15+ // Name returns the name of the object format
16+ Name () string
17+ // EmptyObjectID creates a new empty ObjectID from an object format hash name
18+ EmptyObjectID () ObjectID
2819 // EmptyTree is the hash of an empty tree
2920 EmptyTree () ObjectID
3021 // FullLength is the length of the hash's hex string
@@ -35,67 +26,71 @@ type ObjectFormat interface {
3526 MustIDFromString (s string ) ObjectID
3627 NewID (b []byte ) (ObjectID , error )
3728 NewIDFromString (s string ) (ObjectID , error )
38- NewEmptyID () ObjectID
3929
4030 NewHasher () HasherInterface
4131}
4232
43- type Sha1ObjectFormat struct {}
33+ type Sha1ObjectFormatImpl struct {}
4434
45- func (* Sha1ObjectFormat ) ID () ObjectFormatID { return Sha1 }
46- func (* Sha1ObjectFormat ) String () string { return "sha1" }
47- func (* Sha1ObjectFormat ) Empty () ObjectID { return & Sha1Hash {} }
48- func (* Sha1ObjectFormat ) EmptyTree () ObjectID {
49- return & Sha1Hash {
35+ var (
36+ emptyObjectID = & Sha1Hash {}
37+ emptyTree = & Sha1Hash {
5038 0x4b , 0x82 , 0x5d , 0xc6 , 0x42 , 0xcb , 0x6e , 0xb9 , 0xa0 , 0x60 ,
5139 0xe5 , 0x4b , 0xf8 , 0xd6 , 0x92 , 0x88 , 0xfb , 0xee , 0x49 , 0x04 ,
5240 }
41+ )
42+
43+ func (Sha1ObjectFormatImpl ) Name () string { return "sha1" }
44+ func (Sha1ObjectFormatImpl ) EmptyObjectID () ObjectID {
45+ return emptyObjectID
46+ }
47+
48+ func (Sha1ObjectFormatImpl ) EmptyTree () ObjectID {
49+ return emptyTree
5350}
54- func (* Sha1ObjectFormat ) FullLength () int { return 40 }
55- func (* Sha1ObjectFormat ) IsValid (input string ) bool {
51+ func (Sha1ObjectFormatImpl ) FullLength () int { return 40 }
52+ func (Sha1ObjectFormatImpl ) IsValid (input string ) bool {
5653 return sha1Pattern .MatchString (input )
5754}
5855
59- func (* Sha1ObjectFormat ) MustID (b []byte ) ObjectID {
56+ func (Sha1ObjectFormatImpl ) MustID (b []byte ) ObjectID {
6057 var id Sha1Hash
6158 copy (id [0 :20 ], b )
6259 return & id
6360}
6461
65- func (h * Sha1ObjectFormat ) MustIDFromString (s string ) ObjectID {
62+ func (h Sha1ObjectFormatImpl ) MustIDFromString (s string ) ObjectID {
6663 return MustIDFromString (h , s )
6764}
6865
69- func (h * Sha1ObjectFormat ) NewID (b []byte ) (ObjectID , error ) {
66+ func (h Sha1ObjectFormatImpl ) NewID (b []byte ) (ObjectID , error ) {
7067 return IDFromRaw (h , b )
7168}
7269
73- func (h * Sha1ObjectFormat ) NewIDFromString (s string ) (ObjectID , error ) {
70+ func (h Sha1ObjectFormatImpl ) NewIDFromString (s string ) (ObjectID , error ) {
7471 return genericIDFromString (h , s )
7572}
7673
77- func (* Sha1ObjectFormat ) NewEmptyID () ObjectID {
78- return NewSha1 ()
79- }
80-
81- func (h * Sha1ObjectFormat ) NewHasher () HasherInterface {
74+ func (h Sha1ObjectFormatImpl ) NewHasher () HasherInterface {
8275 return & Sha1Hasher {sha1 .New ()}
8376}
8477
85- func ObjectFormatFromID (id ObjectFormatID ) ObjectFormat {
86- switch id {
87- case Sha1 :
88- return & Sha1ObjectFormat {}
89- }
78+ var Sha1ObjectFormat ObjectFormat = Sha1ObjectFormatImpl {}
9079
91- return nil
80+ var SupportedObjectFormats = []ObjectFormat {
81+ Sha1ObjectFormat ,
82+ // TODO: add sha256
9283}
9384
94- func ObjectFormatFromString (hash string ) (ObjectFormat , error ) {
95- switch strings .ToLower (hash ) {
96- case "sha1" :
97- return & Sha1ObjectFormat {}, nil
85+ func ObjectFormatFromName (name string ) ObjectFormat {
86+ for _ , objectFormat := range SupportedObjectFormats {
87+ if name == objectFormat .Name () {
88+ return objectFormat
89+ }
9890 }
91+ return nil
92+ }
9993
100- return nil , fmt .Errorf ("unknown hash type: %s" , hash )
94+ func IsValidObjectFormat (name string ) bool {
95+ return ObjectFormatFromName (name ) != nil
10196}
0 commit comments