Fast implementation of Git in pure Go codeberg.org/lindenii/furgit
git go
6
fork

Configure Feed

Select the types of activity you want to include in your feed.

object/id: Empty tree

Runxi Yu 27c7fdd5 588fd130

+56 -1
+11 -1
object/id/algorithms.go
··· 24 24 packHashID uint32 25 25 sum func([]byte) ObjectID 26 26 new func() hash.Hash 27 + emptyTree ObjectID 27 28 } 28 29 29 30 //nolint:gochecknoglobals ··· 69 70 ) 70 71 71 72 func init() { //nolint:gochecknoinits 73 + emptyTreeInput := []byte("tree 0\x00") 74 + 72 75 for algo := Algorithm(0); int(algo) < len(algorithmTable); algo++ { 73 - info := algorithmTable[algo] 76 + info := &algorithmTable[algo] 74 77 if info.name == "" { 75 78 continue 76 79 } 77 80 81 + info.emptyTree = info.sum(emptyTreeInput) 78 82 algorithmByName[info.name] = algo 79 83 supportedAlgorithms = append(supportedAlgorithms, algo) 80 84 } ··· 133 137 } 134 138 135 139 return newFn(), nil 140 + } 141 + 142 + // EmptyTree returns the object ID of an empty tree ("tree 0\x00") for this 143 + // algorithm. 144 + func (algo Algorithm) EmptyTree() ObjectID { 145 + return algo.info().emptyTree 136 146 } 137 147 138 148 func (algo Algorithm) info() algorithmDetails {
+45
object/id/objectid_test.go
··· 182 182 t.Fatalf("sha1 and sha256 should differ") 183 183 } 184 184 } 185 + 186 + func TestAlgorithmEmptyTree(t *testing.T) { 187 + t.Parallel() 188 + 189 + tests := []struct { 190 + name string 191 + algo objectid.Algorithm 192 + want string 193 + }{ 194 + { 195 + name: "sha1", 196 + algo: objectid.AlgorithmSHA1, 197 + want: "4b825dc642cb6eb9a060e54bf8d69288fbee4904", 198 + }, 199 + { 200 + name: "sha256", 201 + algo: objectid.AlgorithmSHA256, 202 + want: "6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321", 203 + }, 204 + } 205 + 206 + for _, tt := range tests { 207 + t.Run(tt.name, func(t *testing.T) { 208 + t.Parallel() 209 + 210 + got := tt.algo.EmptyTree() 211 + if got.Algorithm() != tt.algo { 212 + t.Fatalf("EmptyTree() algorithm = %v, want %v", got.Algorithm(), tt.algo) 213 + } 214 + 215 + if got.String() != tt.want { 216 + t.Fatalf("EmptyTree() = %q, want %q", got.String(), tt.want) 217 + } 218 + }) 219 + } 220 + } 221 + 222 + func TestUnknownAlgorithmEmptyTree(t *testing.T) { 223 + t.Parallel() 224 + 225 + got := objectid.AlgorithmUnknown.EmptyTree() 226 + if got != (objectid.ObjectID{}) { 227 + t.Fatalf("EmptyTree() for unknown algorithm = %#v, want zero value", got) 228 + } 229 + }