| 1 | package gitrepo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestBranchOps(t *testing.T) { |
| 12 | if _, err := exec.LookPath("git"); err != nil { |
| 13 | t.Skip("git ontbreekt") |
| 14 | } |
| 15 | dir := t.TempDir() |
| 16 | git := func(args ...string) { |
| 17 | t.Helper() |
| 18 | cmd := exec.Command("git", append([]string{"-C", dir}, args...)...) |
| 19 | cmd.Env = append(os.Environ(), "GIT_AUTHOR_NAME=tim", "GIT_AUTHOR_EMAIL=tim@example.com", "GIT_COMMITTER_NAME=tim", "GIT_COMMITTER_EMAIL=tim@example.com") |
| 20 | if out, err := cmd.CombinedOutput(); err != nil { |
| 21 | t.Fatalf("git %v: %v\n%s", args, err, out) |
| 22 | } |
| 23 | } |
| 24 | git("init", "-b", "master") |
| 25 | git("config", "commit.gpgsign", "false") |
| 26 | if err := os.WriteFile(filepath.Join(dir, "hello.txt"), []byte("een\n"), 0o644); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | git("add", "hello.txt") |
| 30 | git("commit", "-m", "start") |
| 31 | git("checkout", "-b", "topic") |
| 32 | if err := os.WriteFile(filepath.Join(dir, "hello.txt"), []byte("twee\n"), 0o644); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | git("add", "hello.txt") |
| 36 | git("commit", "-m", "onderwerp-xyz") |
| 37 | |
| 38 | ctx := context.Background() |
| 39 | tips, err := BranchTips(ctx, "git", dir) |
| 40 | if err != nil || len(tips) != 2 { |
| 41 | t.Fatalf("tips %v %v", tips, err) |
| 42 | } |
| 43 | ahead, behind, err := AheadBehind(ctx, "git", dir, "master", "topic") |
| 44 | if err != nil || ahead != 1 || behind != 0 { |
| 45 | t.Fatalf("voor/achter %d %d %v", ahead, behind, err) |
| 46 | } |
| 47 | if err := CreateBranch(ctx, "git", dir, "wip", "topic"); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | if err := RenameBranch(ctx, "git", dir, "wip", "wip2"); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | if err := DeleteBranch(ctx, "git", dir, "wip2"); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | names, err := Branches(ctx, "git", dir) |
| 57 | if err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | for _, name := range names { |
| 61 | if name == "wip" || name == "wip2" { |
| 62 | t.Fatalf("branch bleef bestaan: %v", names) |
| 63 | } |
| 64 | } |
| 65 | files, err := ComparePatch(ctx, "git", dir, "master", "topic") |
| 66 | if err != nil || len(files) != 1 || files[0].Path != "hello.txt" { |
| 67 | t.Fatalf("vergelijk %v %v", files, err) |
| 68 | } |
| 69 | subjects, err := Subjects(ctx, "git", dir, "master..topic", 20) |
| 70 | if err != nil || len(subjects) != 1 || subjects[0] != "onderwerp-xyz" { |
| 71 | t.Fatalf("onderwerpen %v %v", subjects, err) |
| 72 | } |
| 73 | } |