| 1 | package gitrepo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestAncestorAndConflict(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", args...) |
| 19 | cmd.Dir = dir |
| 20 | cmd.Env = append(os.Environ(), "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@example.com", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@example.com") |
| 21 | if out, err := cmd.CombinedOutput(); err != nil { |
| 22 | t.Fatalf("git %v: %v\n%s", args, err, out) |
| 23 | } |
| 24 | } |
| 25 | git("init", "-b", "master") |
| 26 | git("config", "user.email", "t@example.com") |
| 27 | git("config", "user.name", "t") |
| 28 | if err := os.WriteFile(filepath.Join(dir, "a.txt"), []byte("a\n"), 0o644); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | git("add", "a.txt") |
| 32 | git("commit", "-m", "start") |
| 33 | git("checkout", "-b", "topic") |
| 34 | if err := os.WriteFile(filepath.Join(dir, "b.txt"), []byte("b\n"), 0o644); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | git("add", "b.txt") |
| 38 | git("commit", "-m", "topic") |
| 39 | ctx := context.Background() |
| 40 | if !IsAncestor(ctx, "git", dir, "master", "topic") { |
| 41 | t.Fatal("master hoort in topic te zitten") |
| 42 | } |
| 43 | if IsAncestor(ctx, "git", dir, "topic", "master") { |
| 44 | t.Fatal("topic zit nog niet in master") |
| 45 | } |
| 46 | conflict, err := Conflicts(ctx, "git", dir, "master", "topic") |
| 47 | if err != nil || conflict { |
| 48 | t.Fatalf("schone merge: %v %v", conflict, err) |
| 49 | } |
| 50 | git("checkout", "master") |
| 51 | git("merge", "--no-edit", "topic") |
| 52 | if !IsAncestor(ctx, "git", dir, "topic", "master") { |
| 53 | t.Fatal("na de merge hoort topic in master te zitten") |
| 54 | } |
| 55 | |
| 56 | other := t.TempDir() |
| 57 | gitc := func(args ...string) { |
| 58 | t.Helper() |
| 59 | cmd := exec.Command("git", args...) |
| 60 | cmd.Dir = other |
| 61 | cmd.Env = append(os.Environ(), "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@example.com", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@example.com") |
| 62 | if out, err := cmd.CombinedOutput(); err != nil { |
| 63 | t.Fatalf("git %v: %v\n%s", args, err, out) |
| 64 | } |
| 65 | } |
| 66 | gitc("init", "-b", "master") |
| 67 | gitc("config", "user.email", "t@example.com") |
| 68 | gitc("config", "user.name", "t") |
| 69 | if err := os.WriteFile(filepath.Join(other, "a.txt"), []byte("a\n"), 0o644); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | gitc("add", "a.txt") |
| 73 | gitc("commit", "-m", "start") |
| 74 | gitc("checkout", "-b", "topic") |
| 75 | if err := os.WriteFile(filepath.Join(other, "a.txt"), []byte("links\n"), 0o644); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | gitc("commit", "-am", "links") |
| 79 | gitc("checkout", "master") |
| 80 | if err := os.WriteFile(filepath.Join(other, "a.txt"), []byte("rechts\n"), 0o644); err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | gitc("commit", "-am", "rechts") |
| 84 | conflict, err = Conflicts(ctx, "git", other, "master", "topic") |
| 85 | if err != nil || !conflict { |
| 86 | t.Fatalf("conflict verwacht: %v %v", conflict, err) |
| 87 | } |
| 88 | } |