|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +// initRepo creates a fresh git repo with one commit on the default branch |
| 12 | +// (renamed to "main"), and returns the directory. |
| 13 | +func initRepo(t *testing.T) string { |
| 14 | + t.Helper() |
| 15 | + dir := t.TempDir() |
| 16 | + |
| 17 | + run := func(args ...string) { |
| 18 | + cmd := exec.CommandContext(context.Background(), "git", append([]string{"-C", dir}, args...)...) |
| 19 | + out, err := cmd.CombinedOutput() |
| 20 | + if err != nil { |
| 21 | + t.Fatalf("git %v failed: %v\n%s", args, err, out) |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + run("init", "-q", "-b", "main") |
| 26 | + run( "config", "user.email", "[email protected]") |
| 27 | + run("config", "user.name", "test") |
| 28 | + run("config", "commit.gpgsign", "false") |
| 29 | + |
| 30 | + if err := os.WriteFile(filepath.Join(dir, "README"), []byte("x\n"), 0o644); err != nil { |
| 31 | + t.Fatal(err) |
| 32 | + } |
| 33 | + run("add", "README") |
| 34 | + run("commit", "-q", "-m", "init") |
| 35 | + return dir |
| 36 | +} |
| 37 | + |
| 38 | +// addRemoteRef pins refs/remotes/<ref> to current HEAD without any network. |
| 39 | +func addRemoteRef(t *testing.T, dir, ref string) { |
| 40 | + t.Helper() |
| 41 | + cmd := exec.CommandContext(context.Background(), "git", "-C", dir, "update-ref", "refs/remotes/"+ref, "HEAD") |
| 42 | + if out, err := cmd.CombinedOutput(); err != nil { |
| 43 | + t.Fatalf("update-ref %s failed: %v\n%s", ref, err, out) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestResolveBranchAction_slashedNewName_isCreate(t *testing.T) { |
| 48 | + // Bug case from lj-0023: a name containing "/" that does not match any |
| 49 | + // remote branch must route to ActionCreate, not ActionCheckoutTracking. |
| 50 | + dir := initRepo(t) |
| 51 | + addRemoteRef(t, dir, "origin/main") |
| 52 | + |
| 53 | + got := ResolveBranchAction(dir, "feature/PROJ-1-foo") |
| 54 | + if got != ActionCreate { |
| 55 | + t.Errorf("ResolveBranchAction = %v, want ActionCreate", got) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestResolveBranchAction_existingLocal_isCheckout(t *testing.T) { |
| 60 | + dir := initRepo(t) |
| 61 | + |
| 62 | + got := ResolveBranchAction(dir, "main") |
| 63 | + if got != ActionCheckout { |
| 64 | + t.Errorf("ResolveBranchAction = %v, want ActionCheckout", got) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestResolveBranchAction_existingRemote_isTracking(t *testing.T) { |
| 69 | + dir := initRepo(t) |
| 70 | + // Use a name that does not exist locally. |
| 71 | + addRemoteRef(t, dir, "origin/feature-x") |
| 72 | + |
| 73 | + got := ResolveBranchAction(dir, "origin/feature-x") |
| 74 | + if got != ActionCheckoutTracking { |
| 75 | + t.Errorf("ResolveBranchAction = %v, want ActionCheckoutTracking", got) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func TestResolveBranchAction_plainName_isCreate(t *testing.T) { |
| 80 | + dir := initRepo(t) |
| 81 | + |
| 82 | + got := ResolveBranchAction(dir, "PROJ-1-foo") |
| 83 | + if got != ActionCreate { |
| 84 | + t.Errorf("ResolveBranchAction = %v, want ActionCreate", got) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestIsRemoteBranch_exactMatchRequired(t *testing.T) { |
| 89 | + dir := initRepo(t) |
| 90 | + addRemoteRef(t, dir, "origin/feature/x") |
| 91 | + |
| 92 | + if IsRemoteBranch(dir, "origin/feature/y") { |
| 93 | + t.Errorf("IsRemoteBranch matched non-existent sibling branch") |
| 94 | + } |
| 95 | + if !IsRemoteBranch(dir, "origin/feature/x") { |
| 96 | + t.Errorf("IsRemoteBranch did not match exact remote branch") |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestIsRemoteBranch_nonGitDir_returnsFalse(t *testing.T) { |
| 101 | + dir := t.TempDir() |
| 102 | + if IsRemoteBranch(dir, "origin/main") { |
| 103 | + t.Errorf("IsRemoteBranch returned true for non-git dir") |
| 104 | + } |
| 105 | +} |
0 commit comments