@@ -5,26 +5,17 @@ package git
5
5
6
6
import (
7
7
"crypto/sha1"
8
- "fmt"
9
8
"regexp"
10
- "strings"
11
- )
12
-
13
- type ObjectFormatID int
14
-
15
- const (
16
- Sha1 ObjectFormatID = iota
17
9
)
18
10
19
11
// sha1Pattern can be used to determine if a string is an valid sha
20
12
var sha1Pattern = regexp .MustCompile (`^[0-9a-f]{4,40}$` )
21
13
22
14
type 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
28
19
// EmptyTree is the hash of an empty tree
29
20
EmptyTree () ObjectID
30
21
// FullLength is the length of the hash's hex string
@@ -35,67 +26,71 @@ type ObjectFormat interface {
35
26
MustIDFromString (s string ) ObjectID
36
27
NewID (b []byte ) (ObjectID , error )
37
28
NewIDFromString (s string ) (ObjectID , error )
38
- NewEmptyID () ObjectID
39
29
40
30
NewHasher () HasherInterface
41
31
}
42
32
43
- type Sha1ObjectFormat struct {}
33
+ type Sha1ObjectFormatImpl struct {}
44
34
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 {
50
38
0x4b , 0x82 , 0x5d , 0xc6 , 0x42 , 0xcb , 0x6e , 0xb9 , 0xa0 , 0x60 ,
51
39
0xe5 , 0x4b , 0xf8 , 0xd6 , 0x92 , 0x88 , 0xfb , 0xee , 0x49 , 0x04 ,
52
40
}
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
53
50
}
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 {
56
53
return sha1Pattern .MatchString (input )
57
54
}
58
55
59
- func (* Sha1ObjectFormat ) MustID (b []byte ) ObjectID {
56
+ func (Sha1ObjectFormatImpl ) MustID (b []byte ) ObjectID {
60
57
var id Sha1Hash
61
58
copy (id [0 :20 ], b )
62
59
return & id
63
60
}
64
61
65
- func (h * Sha1ObjectFormat ) MustIDFromString (s string ) ObjectID {
62
+ func (h Sha1ObjectFormatImpl ) MustIDFromString (s string ) ObjectID {
66
63
return MustIDFromString (h , s )
67
64
}
68
65
69
- func (h * Sha1ObjectFormat ) NewID (b []byte ) (ObjectID , error ) {
66
+ func (h Sha1ObjectFormatImpl ) NewID (b []byte ) (ObjectID , error ) {
70
67
return IDFromRaw (h , b )
71
68
}
72
69
73
- func (h * Sha1ObjectFormat ) NewIDFromString (s string ) (ObjectID , error ) {
70
+ func (h Sha1ObjectFormatImpl ) NewIDFromString (s string ) (ObjectID , error ) {
74
71
return genericIDFromString (h , s )
75
72
}
76
73
77
- func (* Sha1ObjectFormat ) NewEmptyID () ObjectID {
78
- return NewSha1 ()
79
- }
80
-
81
- func (h * Sha1ObjectFormat ) NewHasher () HasherInterface {
74
+ func (h Sha1ObjectFormatImpl ) NewHasher () HasherInterface {
82
75
return & Sha1Hasher {sha1 .New ()}
83
76
}
84
77
85
- func ObjectFormatFromID (id ObjectFormatID ) ObjectFormat {
86
- switch id {
87
- case Sha1 :
88
- return & Sha1ObjectFormat {}
89
- }
78
+ var Sha1ObjectFormat ObjectFormat = Sha1ObjectFormatImpl {}
90
79
91
- return nil
80
+ var SupportedObjectFormats = []ObjectFormat {
81
+ Sha1ObjectFormat ,
82
+ // TODO: add sha256
92
83
}
93
84
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
+ }
98
90
}
91
+ return nil
92
+ }
99
93
100
- return nil , fmt .Errorf ("unknown hash type: %s" , hash )
94
+ func IsValidObjectFormat (name string ) bool {
95
+ return ObjectFormatFromName (name ) != nil
101
96
}
0 commit comments