Debian Codegraph @

fixtures.go

raw symbols SCIP
1 package fixtures
2
3 import (
4 "embed"
5 "fmt"
6 "io"
7 "os"
8 "slices"
9 "strings"
10 "testing"
11
12 "github.com/go-git/go-billy/v6"
13 "github.com/go-git/go-billy/v6/embedfs"
14 "github.com/go-git/go-git-fixtures/v6/internal/tgz"
15 )
16
17 //nolint:gochecknoglobals
18 var Filesystem = embedfs.New(&data)
19
20 //go:embed data
21 var data embed.FS
22
23 //nolint:gochecknoglobals
24 var fixtures = Fixtures{{
25 Tags: []string{
26 "packfile", "pack-v2", "idx-v2", "index-v2", "ofs-delta",
27 ".git", "root-reference", "index-ext-tree",
28 },
29 URL: "https://github.com/git-fixtures/root-references.git",
30 Head: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
31 PackfileHash: "135fe3d1ad828afe68706f1d481aedbcfa7a86d2",
32 DotGitHash: "78c5fb882e76286d8201016cffee63ea7060a0c2",
33 ObjectsCount: 68,
34 ObjectFormat: "sha1",
35 }, {
36 Tags: []string{
37 "packfile", "packfile-entries", "scanner-entries", "pack-v2", "idx-v2", "index-v2", "ofs-delta",
38 ".git", "index-ext-tree",
39 },
40 URL: "https://github.com/git-fixtures/basic.git",
41 Head: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
42 PackfileHash: "a3fed42da1e8189a077c0e6846c040dcf73fc9dd",
43 DotGitHash: "7a725350b88b05ca03541b59dd0649fda7f521f2",
44 ObjectsCount: 31,
45 ObjectFormat: "sha1",
46 }, {
47 Tags: []string{
48 ".git", "index-ext-tree", "reftable", "index-v2",
49 },
50 URL: "https://github.com/git-fixtures/basic.git",
51 Head: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
52 DotGitHash: "5f620e4b3194c0c4a77fbd17f501030a441f54d4",
53 ObjectsCount: 31,
54 ObjectFormat: "sha1",
55 }, {
56 Tags: []string{
57 "packfile", "packfile-entries", "scanner-entries", "pack-v2", "idx-v2", "index-v2", "ref-delta",
58 ".git", "rev-v1",
59 },
60 URL: "https://github.com/git-fixtures/basic.git",
61 Head: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
62 PackfileHash: "c544593473465e6315ad4182d04d366c4592b829",
63 DotGitHash: "7cbde0ca02f13aedd5ec8b358ca17b1c0bf5ee64",
64 ObjectsCount: 31,
65 ObjectFormat: "sha1",
66 }, {
67 Tags: []string{
68 "packfile", "pack-v2", "idx-v2", "index-v2", "ofs-delta",
69 ".git", "single-branch", "rev-v1", "index-ext-tree",
70 },
71 URL: "https://github.com/git-fixtures/basic.git",
72 Head: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
73 PackfileHash: "61f0ee9c75af1f9678e6f76ff39fbe372b6f1c45",
74 DotGitHash: "21504f6d2cc2ef0c9d6ebb8802c7b49abae40c1a",
75 ObjectsCount: 28,
76 ObjectFormat: "sha1",
77 }, {
78 Tags: []string{".git", "merge-conflict", "index-v2", "index-ext-none"},
79 URL: "https://github.com/git-fixtures/basic.git",
80 DotGitHash: "4870d54b5b04e43da8cf99ceec179d9675494af8",
81 ObjectFormat: "sha1",
82 }, {
83 Tags: []string{".git", "resolve-undo", "index-v2", "index-ext-reuc"},
84 URL: "https://github.com/git-fixtures/basic.git",
85 DotGitHash: "df6781fd40b8f4911d70ce71f8387b991615cd6d",
86 ObjectFormat: "sha1",
87 }, {
88 Tags: []string{".git", "intent-to-add", "index-v3", "index-ext-tree"},
89 URL: "https://github.com/git-fixtures/basic.git",
90 DotGitHash: "4e7600af05c3356e8b142263e127b76f010facfc",
91 ObjectFormat: "sha1",
92 }, {
93 Tags: []string{".git", "index-v4", "index-ext-tree"},
94 URL: "https://github.com/git-fixtures/basic.git",
95 DotGitHash: "935e5ac17c41c309c356639816ea0694a568c484",
96 ObjectFormat: "sha1",
97 }, {
98 Tags: []string{
99 ".git", "end-of-index-entry", "index-v2", "index-ext-eoie",
100 "index-ext-tree",
101 },
102 URL: "https://github.com/git-fixtures/basic.git",
103 DotGitHash: "ab06771a67110b976953d34400d4dbc465ccd2d9",
104 ObjectFormat: "sha1",
105 }, {
106 Tags: []string{"worktree"},
107 URL: "https://github.com/git-fixtures/basic.git",
108 WorktreeHash: "d2e42ddd68eacbb6034e7724e0dd4117ff1f01ee",
109 ObjectFormat: "sha1",
110 }, {
111 Tags: []string{"worktree", "submodule"},
112 URL: "https://github.com/git-fixtures/submodule.git",
113 WorktreeHash: "8b4d55c85677b6b94bef2e46832ed2174ed6ecaf",
114 ObjectFormat: "sha1",
115 }, {
116 Tags: []string{
117 "packfile", "pack-v2", "idx-v2", "index-v2", ".git", "unpacked",
118 "multi-packfile", "index-ext-tree",
119 },
120 URL: "https://github.com/src-d/go-git.git",
121 Head: "e8788ad9165781196e917292d6055cba1d78664e",
122 PackfileHash: "3559b3b47e695b33b0913237a4df3357e739831c",
123 DotGitHash: "174be6bd4292c18160542ae6dc6704b877b8a01a",
124 ObjectsCount: 2133,
125 ObjectFormat: "sha1",
126 }, {
127 Tags: []string{
128 "packfile", "pack-v2", "idx-v2", "index-v2", ".git", "tags",
129 "index-ext-tree",
130 },
131 URL: "https://github.com/git-fixtures/tags.git",
132 Head: "f7b877701fbf855b44c0a9e86f3fdce2c298b07f",
133 DotGitHash: "c0c7c57ab1753ddbd26cc45322299ddd12842794",
134 PackfileHash: "b68617dd8637fe6409d9842825a843a1d9a6e484",
135 ObjectsCount: 7,
136 ObjectFormat: "sha1",
137 }, {
138 Tags: []string{"packfile", "pack-v2", "idx-v2", "rev-v1"},
139 URL: "https://github.com/spinnaker/spinnaker.git",
140 Head: "06ce06d0fc49646c4de733c45b7788aabad98a6f",
141 PackfileHash: "f2e0a8889a746f7600e07d2246a2e29a72f696be",
142 ObjectFormat: "sha1",
143 }, {
144 Tags: []string{"packfile", "pack-v2", "idx-v2", "rev-v1"},
145 URL: "https://github.com/jamesob/desk.git",
146 Head: "d2313db6e7ca7bac79b819d767b2a1449abb0a5d",
147 PackfileHash: "4ec6344877f494690fc800aceaf2ca0e86786acb",
148 ObjectFormat: "sha1",
149 }, {
150 Tags: []string{
151 "packfile", "pack-v2", "idx-v2", "index-v2", "empty-folder",
152 "rev-v1", "index-ext-tree",
153 },
154 URL: "https://github.com/cpcs499/Final_Pres_P.git",
155 Head: "70bade703ce556c2c7391a8065c45c943e8b6bc3",
156 PackfileHash: "29f304662fd64f102d94722cf5bd8802d9a9472c",
157 DotGitHash: "e1580a78f7d36791249df76df8a2a2613d629902",
158 ObjectFormat: "sha1",
159 }, {
160 Tags: []string{"packfile", "pack-v2", "idx-v2", "diff-tree", "rev-v1"},
161 URL: "https://github.com/github/gem-builder.git",
162 PackfileHash: "1ea0b3971fd64fdcdf3282bfb58e8cf10095e4e6",
163 ObjectFormat: "sha1",
164 }, {
165 Tags: []string{"packfile", "pack-v2", "idx-v2", "diff-tree"},
166 URL: "https://github.com/githubtraining/example-branches.git",
167 PackfileHash: "bb8ee94710d3fa39379a630f76812c187217b312",
168 ObjectFormat: "sha1",
169 }, {
170 Tags: []string{"packfile", "pack-v2", "idx-v2", "diff-tree"},
171 URL: "https://github.com/rumpkernel/rumprun-xen.git",
172 PackfileHash: "7861f2632868833a35fe5e4ab94f99638ec5129b",
173 ObjectFormat: "sha1",
174 }, {
175 Tags: []string{"packfile", "pack-v2", "idx-v2", "diff-tree", "rev-v1"},
176 URL: "https://github.com/mcuadros/skeetr.git",
177 PackfileHash: "36ef7a2296bfd526020340d27c5e1faa805d8d38",
178 ObjectFormat: "sha1",
179 }, {
180 Tags: []string{"packfile", "pack-v2", "idx-v2", "diff-tree", "rev-v1"},
181 URL: "https://github.com/dezfowler/LiteMock.git",
182 PackfileHash: "0d9b6cfc261785837939aaede5986d7a7c212518",
183 ObjectFormat: "sha1",
184 }, {
185 Tags: []string{"packfile", "pack-v2", "idx-v2", "diff-tree", "rev-v1"},
186 URL: "https://github.com/tyba/storable.git",
187 PackfileHash: "0d3d824fb5c930e7e7e1f0f399f2976847d31fd3",
188 ObjectFormat: "sha1",
189 }, {
190 Tags: []string{"packfile", "pack-v2", "idx-v2", "diff-tree", "rev-v1"},
191 URL: "https://github.com/toqueteos/ts3.git",
192 PackfileHash: "21b33a26eb7ffbd35261149fe5d886b9debab7cb",
193 ObjectFormat: "sha1",
194 }, {
195 Tags: []string{"empty", ".git", "index-v2"},
196 URL: "https://github.com/git-fixtures/empty.git",
197 DotGitHash: "bf3fedcc8e20fd0dec9172987ceea0038d17b516",
198 ObjectsCount: 0,
199 ObjectFormat: "sha1",
200 }, {
201 Tags: []string{"worktree", "alternates"},
202 WorktreeHash: "a6b6ff89c593f042347113203ead1c14ab5733ce",
203 ObjectFormat: "sha1",
204 }, {
205 Tags: []string{"worktree", "dirty"},
206 WorktreeHash: "7203669c66103305e56b9dcdf940a7fbeb515f28",
207 ObjectFormat: "sha1",
208 }, {
209 // standalone packfile that does not have any dependencies nor is part of any other fixture repo.
210 Tags: []string{"packfile", "pack-v2", "idx-v2", "standalone"},
211 PackfileHash: "3638209d310e10ea8d90c362d568be65dd5e03a6",
212 ObjectFormat: "sha1",
213 }, {
214 // adds commit on top of spinnaker fixture 06ce06d0fc49646c4de733c45b7788aabad98a6f via a thin pack.
215 Tags: []string{"thinpack"},
216 PackfileHash: "ee4fef0ef8be5053ebae4ce75acf062ddf3031fb",
217 Head: "ee372bb08322c1e6e7c6c4f953cc6bf72784e7fb", // the thin pack adds this commit.
218 ObjectFormat: "sha1",
219 }, {
220 Tags: []string{"merge-base", "index-v2", "index-ext-reuc", "index-ext-tree"},
221 DotGitHash: "26baa505b9f6fb2024b9999c140b75514718c988",
222 ObjectFormat: "sha1",
223 }, {
224 Tags: []string{"commit-graph", "index-v2", "index-ext-tree"},
225 Head: "b9d69064b190e7aedccf84731ca1d917871f8a1c",
226 PackfileHash: "769137af7784db501bca677fbd56fef8b52515b7",
227 DotGitHash: "cf717ccadce761d60bb4a8557a7b9a2efd23816a",
228 ObjectsCount: 31,
229 ObjectFormat: "sha1",
230 }, {
231 Tags: []string{"commit-graph-chain", "index-v2", "index-ext-tree"},
232 Head: "b9d69064b190e7aedccf84731ca1d917871f8a1c",
233 PackfileHash: "769137af7784db501bca677fbd56fef8b52515b7",
234 DotGitHash: "00a1fc100787506f842e55511994f08df2c2cd66",
235 ObjectsCount: 31,
236 ObjectFormat: "sha1",
237 }, {
238 Tags: []string{"commit-graph-chain-2", "rev-v1", "index-v2", "index-ext-tree"},
239 Head: "ec6f456c0e8c7058a29611429965aa05c190b54b",
240 PackfileHash: "06ede69e9eba9f1af36eeee184402dc3ad705cd7",
241 DotGitHash: "77b6511a6e67c99162ebcecd2763a9a19a7ad429",
242 ObjectFormat: "sha1",
243 }, {
244 Tags: []string{"worktree", "linked-worktree"},
245 WorktreeHash: "363d996b02d9c3b598f0176619f5c6a44a82480a",
246 ObjectFormat: "sha1",
247 }, {
248 Tags: []string{"worktree", "main-branch", "no-master-head"},
249 WorktreeHash: "e3b91f99d8d050cac81d84fbef89172f58eeb745",
250 ObjectFormat: "sha1",
251 }, {
252 Tags: []string{"packfile", "pack-v2", "idx-v2", "codecommit"},
253 PackfileHash: "9733763ae7ee6efcf452d373d6fff77424fb1dcc",
254 ObjectFormat: "sha1",
255 }, {
256 Tags: []string{"packfile", "pack-v2", "idx-v2", "delta-before-base"},
257 PackfileHash: "90fedc00729b64ea0d0406db861be081cda25bbf",
258 ObjectFormat: "sha1",
259 }, {
260 Tags: []string{"packfile", "scanner-entries", "rev-v1"},
261 PackfileHash: "407497645643e18a7ba56c6132603f167fe9c51c00361ee0c81d74a8f55d0ee2",
262 ObjectsCount: 5,
263 ObjectFormat: "sha256",
264 }, {
265 Tags: []string{"packfile", "pack-v2", "idx-v2", "notes"},
266 PackfileHash: "bc4b855a55cae7703c023d4e36e3a7c9f5d84491",
267 ObjectFormat: "sha1",
268 }, {
269 Tags: []string{".git", "index-v2", "index-ext-tree"},
270 URL: "https://gitlab.com/pjbgf/sha256.git",
271 DotGitHash: "40143428b59fe03546fabba0603268bba3b3c58b",
272 ObjectFormat: "sha256",
273 }, {
274 Tags: []string{"packfile", "packfile-entries", ".git"},
275 URL: "https://github.com/git-fixtures/basic.git",
276 Head: "4fef4adac3be863b9b94613016bdd8e53f67f6d7577234e028bc9d24c5a6a27c",
277 PackfileHash: "c88dfe1663bd216e278d5bb3c8decd0a4bb174a6204585dc44b7c7a05fceed55",
278 DotGitHash: "c20badf43d2495f93b42cb3ea98ed04651510617da9b56d4e07c5837ec08f93d",
279 ObjectsCount: 31,
280 ObjectFormat: "sha256",
281 }, {
282 Tags: []string{"worktree", "submodule"},
283 URL: "https://gitlab.com/go-git-fixtures/sha256-submodule.git",
284 WorktreeHash: "df8b2731b8978b5efca95cc2eef5ee43915e0945e3310653b0ef0c00453d67c4",
285 ObjectFormat: "sha256",
286 }}
287
288 func All() Fixtures {
289 all := make(Fixtures, 0, len(fixtures))
290 for _, f := range fixtures {
291 all = append(all, f.Clone())
292 }
293
294 return all
295 }
296
297 func Basic() Fixtures {
298 return ByURL("https://github.com/git-fixtures/basic.git").
299 Exclude("single-branch")
300 }
301
302 func ByURL(url string) Fixtures {
303 return fixtures.ByURL(url)
304 }
305
306 func ByTag(tag string) Fixtures {
307 return fixtures.ByTag(tag)
308 }
309
310 func ByObjectFormat(format string) Fixtures {
311 return fixtures.ByObjectFormat(format)
312 }
313
314 // Fixture represents a Git repository test fixture that can be used for testing
315 // Git operations. Each fixture contains metadata about a repository and may
316 // provide access to some of its files, such as packfile, index, and/or .git
317 // directory contents.
318 type Fixture struct {
319 // URL is the original repository URL from which this fixture was created.
320 URL string
321 // Tags are labels used to categorize and filter fixtures (e.g., "packfile", ".git", "worktree").
322 Tags []string
323 // Head is the commit hash that HEAD points to in this fixture.
324 Head string
325 // PackfileHash is the hash identifier for the fixture's packfile data.
326 PackfileHash string
327 // DotGitHash is the hash identifier for the archived .git directory.
328 DotGitHash string
329 // WorktreeHash is the hash identifier for the archived worktree.
330 WorktreeHash string
331 // ObjectsCount is the number of git objects in this fixture.
332 ObjectsCount int32
333 // ObjectFormat specifies the object hash algorithm (e.g., "sha1" or "sha256").
334 ObjectFormat string
335 }
336
337 func (f *Fixture) Is(tag string) bool {
338 return slices.Contains(f.Tags, tag)
339 }
340
341 func (f *Fixture) Packfile() (billy.File, error) {
342 file, err := Filesystem.Open(fmt.Sprintf("data/pack-%s.pack", f.PackfileHash))
343 if err != nil {
344 return nil, fmt.Errorf("%w: %w", os.ErrNotExist, err)
345 }
346
347 return file, nil
348 }
349
350 func (f *Fixture) Idx() (billy.File, error) {
351 file, err := Filesystem.Open(fmt.Sprintf("data/pack-%s.idx", f.PackfileHash))
352 if err != nil {
353 return nil, fmt.Errorf("%w: %w", os.ErrNotExist, err)
354 }
355
356 return file, nil
357 }
358
359 func (f *Fixture) Rev() (billy.File, error) {
360 file, err := Filesystem.Open(fmt.Sprintf("data/pack-%s.rev", f.PackfileHash))
361 if err != nil {
362 return nil, fmt.Errorf("%w: %w", os.ErrNotExist, err)
363 }
364
365 return file, nil
366 }
367
368 // DotGit creates a new temporary directory and unpacks the repository .git
369 // directory into it. Multiple calls to DotGit returns different directories.
370 func (f *Fixture) DotGit(opts ...Option) (billy.Filesystem, error) {
371 o := newOptions()
372 for _, opt := range opts {
373 opt(o)
374 }
375
376 if f.DotGitHash == "" && f.WorktreeHash != "" {
377 fs, err := f.Worktree(opts...)
378 if err != nil {
379 return nil, err
380 }
381
382 return fs.Chroot(".git")
383 }
384
385 file, err := Filesystem.Open(fmt.Sprintf("data/git-%s.tgz", f.DotGitHash))
386 if err != nil {
387 return nil, err
388 }
389
390 fs, err := o.fsFactory()
391 if err != nil {
392 return nil, err
393 }
394
395 err = tgz.Extract(file, fs)
396 if err != nil {
397 return nil, err
398 }
399
400 return fs, nil
401 }
402
403 func (f *Fixture) Clone() *Fixture {
404 nf := &Fixture{
405 URL: f.URL,
406 DotGitHash: f.DotGitHash,
407 Head: f.Head,
408 PackfileHash: f.PackfileHash,
409 WorktreeHash: f.WorktreeHash,
410 ObjectsCount: f.ObjectsCount,
411 Tags: slices.Clone(f.Tags),
412 ObjectFormat: f.ObjectFormat,
413 }
414
415 return nf
416 }
417
418 // EnsureIsBare overrides the config file with one where bare is true.
419 func EnsureIsBare(fs billy.Filesystem) error {
420 _, err := fs.Stat("config")
421 if err != nil {
422 return fmt.Errorf("not .git folder: %w", err)
423 }
424
425 cfg, err := fs.OpenFile("config", os.O_TRUNC|os.O_WRONLY, 0)
426 if err != nil {
427 return err
428 }
429
430 defer cfg.Close()
431
432 content := strings.NewReader("" +
433 "[core]\n" +
434 "repositoryformatversion = 0\n" +
435 "filemode = true\n" +
436 "bare = true\n" +
437 "[http]\n" +
438 "receivepack = true\n",
439 )
440
441 _, err = io.Copy(cfg, content)
442
443 return err
444 }
445
446 func (f *Fixture) Worktree(opts ...Option) (billy.Filesystem, error) {
447 o := newOptions()
448 for _, opt := range opts {
449 opt(o)
450 }
451
452 file, err := Filesystem.Open(fmt.Sprintf("data/worktree-%s.tgz", f.WorktreeHash))
453 if err != nil {
454 return nil, err
455 }
456
457 fs, err := o.fsFactory()
458 if err != nil {
459 return nil, err
460 }
461
462 err = tgz.Extract(file, fs)
463 if err != nil {
464 return nil, err
465 }
466
467 return fs, nil
468 }
469
470 type Fixtures []*Fixture
471
472 // Run calls test within a t.Run for each fixture in g.
473 func (g Fixtures) Run(t *testing.T, test func(*testing.T, *Fixture)) {
474 t.Helper()
475
476 for _, f := range g {
477 name := fmt.Sprintf("fixture run (%q, %q)", f.URL, f.Tags)
478 t.Run(name, func(t *testing.T) {
479 test(t, f)
480 })
481 }
482 }
483
484 func (g Fixtures) One() *Fixture {
485 if len(g) == 0 {
486 return nil
487 }
488
489 return g[0].Clone()
490 }
491
492 func (g Fixtures) ByTag(tag string) Fixtures {
493 r := make(Fixtures, 0, len(g))
494 for _, f := range g {
495 if f.Is(tag) {
496 r = append(r, f.Clone())
497 }
498 }
499
500 return r
501 }
502
503 func (g Fixtures) ByObjectFormat(format string) Fixtures {
504 r := make(Fixtures, 0, len(g))
505 for _, f := range g {
506 if f.ObjectFormat == format {
507 r = append(r, f.Clone())
508 }
509 }
510
511 return r
512 }
513
514 func (g Fixtures) ByURL(url string) Fixtures {
515 r := make(Fixtures, 0, len(g))
516 for _, f := range g {
517 if f.URL == url {
518 r = append(r, f.Clone())
519 }
520 }
521
522 return r
523 }
524
525 func (g Fixtures) Exclude(tag string) Fixtures {
526 r := make(Fixtures, 0, len(g))
527 for _, f := range g {
528 if !f.Is(tag) {
529 r = append(r, f.Clone())
530 }
531 }
532
533 return r
534 }