| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | // runInRepo is a tiny helper used by these tests to run raw git commands. |
| 12 | func runInRepo(t *testing.T, dir string, args ...string) string { |
| 13 | t.Helper() |
| 14 | cmd := exec.Command("git", append([]string{"-C", dir}, args...)...) |
| 15 | out, err := cmd.CombinedOutput() |
| 16 | if err != nil { |
| 17 | t.Fatalf("git %v: %v\n%s", args, err, out) |
| 18 | } |
| 19 | return string(out) |
| 20 | } |
| 21 | |
| 22 | // setupRepoWithRemote creates two bare-ish repos: `origin` (bare) and `repo` |
| 23 | // (a working clone of origin). It returns the clone path. |
| 24 | func setupRepoWithRemote(t *testing.T) string { |
| 25 | t.Helper() |
| 26 | |
| 27 | upstream := initTestRepo(t) |
| 28 | bareDir := filepath.Join(t.TempDir(), "origin.git") |
| 29 | if out, err := exec.Command("git", "clone", "--bare", upstream, bareDir).CombinedOutput(); err != nil { |
| 30 | t.Fatalf("bare clone: %v\n%s", err, out) |
| 31 | } |
| 32 | |
| 33 | cloneDir := filepath.Join(t.TempDir(), "clone") |
| 34 | if out, err := exec.Command("git", "clone", bareDir, cloneDir).CombinedOutput(); err != nil { |
| 35 | t.Fatalf("clone: %v\n%s", err, out) |
| 36 | } |
| 37 | runInRepo(t, cloneDir, "config", "user.email", "test@test.com") |
| 38 | runInRepo(t, cloneDir, "config", "user.name", "Test") |
| 39 | |
| 40 | return cloneDir |
| 41 | } |
| 42 | |
| 43 | func TestIsClean(t *testing.T) { |
| 44 | ctx := context.Background() |
| 45 | repo := initTestRepo(t) |
| 46 | |
| 47 | clean, err := IsClean(ctx, repo) |
| 48 | if err != nil { |
| 49 | t.Fatalf("IsClean on fresh repo: %v", err) |
| 50 | } |
| 51 | if !clean { |
| 52 | t.Errorf("fresh repo should be clean") |
| 53 | } |
| 54 | |
| 55 | // Add an untracked file โ still counts as dirty |
| 56 | if err := os.WriteFile(filepath.Join(repo, "new.txt"), []byte("hello"), 0o644); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | clean, err = IsClean(ctx, repo) |
| 60 | if err != nil { |
| 61 | t.Fatalf("IsClean with untracked file: %v", err) |
| 62 | } |
| 63 | if clean { |
| 64 | t.Errorf("repo with untracked file should not be clean") |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestCurrentBranch(t *testing.T) { |
| 69 | ctx := context.Background() |
| 70 | repo := initTestRepo(t) |
| 71 | |
| 72 | branch, err := CurrentBranch(ctx, repo) |
| 73 | if err != nil { |
| 74 | t.Fatalf("CurrentBranch: %v", err) |
| 75 | } |
| 76 | if branch != "main" { |
| 77 | t.Errorf("CurrentBranch = %q, want main", branch) |
| 78 | } |
| 79 | |
| 80 | // Detached HEAD returns empty string without error |
| 81 | runInRepo(t, repo, "checkout", "--detach") |
| 82 | branch, err = CurrentBranch(ctx, repo) |
| 83 | if err != nil { |
| 84 | t.Fatalf("CurrentBranch detached: %v", err) |
| 85 | } |
| 86 | if branch != "" { |
| 87 | t.Errorf("detached HEAD should return empty branch, got %q", branch) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func TestHasUpstream(t *testing.T) { |
| 92 | ctx := context.Background() |
| 93 | |
| 94 | // Plain repo, no remote โ no upstream |
| 95 | repo := initTestRepo(t) |
| 96 | up, err := HasUpstream(ctx, repo) |
| 97 | if err != nil { |
| 98 | t.Fatalf("HasUpstream no-remote: %v", err) |
| 99 | } |
| 100 | if up { |
| 101 | t.Errorf("repo without remote should have no upstream") |
| 102 | } |
| 103 | |
| 104 | // Repo cloned from origin โ main tracks origin/main |
| 105 | clone := setupRepoWithRemote(t) |
| 106 | up, err = HasUpstream(ctx, clone) |
| 107 | if err != nil { |
| 108 | t.Fatalf("HasUpstream with upstream: %v", err) |
| 109 | } |
| 110 | if !up { |
| 111 | t.Errorf("cloned repo's main should have an upstream") |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestMergeFastForward(t *testing.T) { |
| 116 | ctx := context.Background() |
| 117 | clone := setupRepoWithRemote(t) |
| 118 | |
| 119 | // Advance origin by committing to another clone, then pushing. |
| 120 | // Easiest path: make a commit in clone, push, then hard-reset clone back |
| 121 | // one commit to simulate being behind origin. |
| 122 | if err := os.WriteFile(filepath.Join(clone, "extra.txt"), []byte("x"), 0o644); err != nil { |
| 123 | t.Fatal(err) |
| 124 | } |
| 125 | runInRepo(t, clone, "add", "extra.txt") |
| 126 | runInRepo(t, clone, "commit", "-m", "advance") |
| 127 | runInRepo(t, clone, "push", "origin", "main") |
| 128 | runInRepo(t, clone, "reset", "--hard", "HEAD~1") |
| 129 | |
| 130 | // Before merge: HEAD is behind origin/main by one commit |
| 131 | if err := FetchAll(ctx, clone); err != nil { |
| 132 | t.Fatalf("FetchAll: %v", err) |
| 133 | } |
| 134 | |
| 135 | if err := MergeFastForward(ctx, clone); err != nil { |
| 136 | t.Fatalf("MergeFastForward: %v", err) |
| 137 | } |
| 138 | |
| 139 | // After fast-forward, extra.txt should be present again |
| 140 | if _, err := os.Stat(filepath.Join(clone, "extra.txt")); err != nil { |
| 141 | t.Errorf("fast-forward should bring extra.txt back: %v", err) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func TestMergeFastForward_DivergedFails(t *testing.T) { |
| 146 | ctx := context.Background() |
| 147 | clone := setupRepoWithRemote(t) |
| 148 | |
| 149 | // Make and push one commit, then reset locally and make a different commit. |
| 150 | // After re-fetch, local and origin/main have diverged. |
| 151 | if err := os.WriteFile(filepath.Join(clone, "a.txt"), []byte("a"), 0o644); err != nil { |
| 152 | t.Fatal(err) |
| 153 | } |
| 154 | runInRepo(t, clone, "add", "a.txt") |
| 155 | runInRepo(t, clone, "commit", "-m", "a") |
| 156 | runInRepo(t, clone, "push", "origin", "main") |
| 157 | runInRepo(t, clone, "reset", "--hard", "HEAD~1") |
| 158 | if err := os.WriteFile(filepath.Join(clone, "b.txt"), []byte("b"), 0o644); err != nil { |
| 159 | t.Fatal(err) |
| 160 | } |
| 161 | runInRepo(t, clone, "add", "b.txt") |
| 162 | runInRepo(t, clone, "commit", "-m", "b") |
| 163 | |
| 164 | if err := FetchAll(ctx, clone); err != nil { |
| 165 | t.Fatalf("FetchAll: %v", err) |
| 166 | } |
| 167 | |
| 168 | if err := MergeFastForward(ctx, clone); err == nil { |
| 169 | t.Errorf("MergeFastForward should fail when diverged") |
| 170 | } |
| 171 | } |