| 1 | package barerepo |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "path/filepath" |
| 7 | "reflect" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestRoots(t *testing.T) { |
| 12 | got := Roots([]string{ |
| 13 | "data/repos/tim/gepackt.git/HEAD", |
| 14 | "data/repos/tim/gepackt.git/packed-refs", |
| 15 | "data/repos/tim/a.git/HEAD", |
| 16 | "data/repos/tim/a.git/refs/heads/HEAD", // een branch die HEAD heet |
| 17 | "data/repos/tim/geen-repo/HEAD", |
| 18 | "data/releases/x.git/HEAD", |
| 19 | "state/bluegit.db", |
| 20 | }, "data/repos/") |
| 21 | want := []string{"data/repos/tim/a.git", "data/repos/tim/gepackt.git"} |
| 22 | if !reflect.DeepEqual(got, want) { |
| 23 | t.Fatalf("%v", got) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // Een volledig gepackte repo zonder refs/ wordt weer een geldige repo. |
| 28 | func TestRepairPackedOnlyRepo(t *testing.T) { |
| 29 | root := t.TempDir() |
| 30 | repo := filepath.Join(root, "tim", "gepackt.git") |
| 31 | _ = os.MkdirAll(filepath.Join(repo, "objects", "pack"), 0o755) |
| 32 | _ = os.WriteFile(filepath.Join(repo, "HEAD"), []byte("ref: refs/heads/master\n"), 0o644) |
| 33 | _ = os.WriteFile(filepath.Join(repo, "packed-refs"), []byte("# pack-refs with: peeled fully-peeled sorted \n"), 0o644) |
| 34 | // Een gewone map met HEAD zonder .git blijft ongemoeid. |
| 35 | other := filepath.Join(root, "tim", "los") |
| 36 | _ = os.MkdirAll(other, 0o755) |
| 37 | _ = os.WriteFile(filepath.Join(other, "HEAD"), []byte("x"), 0o644) |
| 38 | |
| 39 | n, err := Repair(root) |
| 40 | if err != nil || n != 1 { |
| 41 | t.Fatal(n, err) |
| 42 | } |
| 43 | for _, d := range Dirs { |
| 44 | if fi, err := os.Stat(filepath.Join(repo, d)); err != nil || !fi.IsDir() { |
| 45 | t.Fatalf("%s ontbreekt: %v", d, err) |
| 46 | } |
| 47 | } |
| 48 | if _, err := os.Stat(filepath.Join(other, "refs")); err == nil { |
| 49 | t.Fatal("map zonder .git aangepast") |
| 50 | } |
| 51 | if n, err := Repair(root); err != nil || n != 0 { |
| 52 | t.Fatalf("tweede keer: %d %v", n, err) |
| 53 | } |
| 54 | if n, err := Repair(filepath.Join(root, "bestaat-niet")); err != nil || n != 0 { |
| 55 | t.Fatalf("ontbrekende root: %d %v", n, err) |
| 56 | } |
| 57 | if _, err := exec.LookPath("git"); err == nil { |
| 58 | if out, err := exec.Command("git", "--git-dir", repo, "rev-parse", "--git-dir").CombinedOutput(); err != nil { |
| 59 | t.Fatalf("geen geldige repo: %v %s", err, out) |
| 60 | } |
| 61 | } |
| 62 | } |