Skip to content

Commit f98cae4

Browse files
authored
Merge pull request #179 from k1LoW/fix/error-on-start-point-with-existing-branch-or-worktree
fix: error when start-point is specified for existing branch or worktree
2 parents 5695521 + 1479d0c commit f98cae4

2 files changed

Lines changed: 57 additions & 16 deletions

File tree

cmd/root.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,10 @@ func handleWorktree(ctx context.Context, cmd *cobra.Command, wtName, branchName,
760760
}
761761

762762
if wt != nil {
763-
// Worktree exists, print path to stdout
764-
// start-point is ignored when switching to existing worktree
763+
if startPoint != "" {
764+
return fmt.Errorf("worktree for branch %q already exists at %s (start-point %q is not allowed when switching to an existing worktree)", wt.Branch, wt.Path, startPoint)
765+
}
766+
// Worktree exists, switch to it
765767
fmt.Println(resolveRelative(ctx, wt.Path, cfg.Relative))
766768
return nil
767769
}
@@ -779,8 +781,10 @@ func handleWorktree(ctx context.Context, cmd *cobra.Command, wtName, branchName,
779781
}
780782

781783
if exists {
784+
if startPoint != "" {
785+
return fmt.Errorf("branch %q already exists (start-point %q is not allowed for existing branches)", branchName, startPoint)
786+
}
782787
// Branch exists, create worktree with existing branch
783-
// start-point is ignored when using existing branch
784788
if err := git.AddWorktree(ctx, wtPath, branchName, copyOpts); err != nil {
785789
return fmt.Errorf("failed to create worktree: %w", err)
786790
}

e2e/basic_test.go

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -420,31 +420,68 @@ func TestE2E_CreateWorktree(t *testing.T) {
420420
}
421421
})
422422

423-
t.Run("start_point_ignored_for_existing_branch", func(t *testing.T) {
423+
t.Run("start_point_error_for_existing_branch", func(t *testing.T) {
424424
t.Parallel()
425425
repo := testutil.NewTestRepo(t)
426426
repo.CreateFile("README.md", "# Test")
427427
repo.Commit("initial commit")
428428

429429
repo.Git("branch", "existing-branch")
430430

431-
repo.CreateFile("new-file.txt", "new content")
432-
repo.Commit("second commit")
431+
_, _, err := runGitWtStdout(t, binPath, repo.Root, "existing-branch", "main")
432+
if err == nil {
433+
t.Fatal("expected error when start-point is specified for existing branch, but got none")
434+
}
435+
})
433436

434-
stdout, stderr, err := runGitWtStdout(t, binPath, repo.Root, "existing-branch", "main")
435-
if err != nil {
436-
t.Fatalf("git-wt failed: %v\nstderr: %s", err, stderr)
437+
t.Run("start_point_error_for_existing_worktree", func(t *testing.T) {
438+
t.Parallel()
439+
repo := testutil.NewTestRepo(t)
440+
repo.CreateFile("README.md", "# Test")
441+
repo.Commit("initial commit")
442+
443+
// Create worktree first
444+
if _, err := runGitWt(t, binPath, repo.Root, "feature-branch"); err != nil {
445+
t.Fatalf("failed to create worktree: %v", err)
437446
}
438447

439-
wtPath := strings.TrimSpace(stdout)
440-
if _, err := os.Stat(wtPath); os.IsNotExist(err) {
441-
t.Fatalf("worktree was not created at %s", wtPath)
448+
// Switching to existing worktree with start-point should error
449+
_, _, err := runGitWtStdout(t, binPath, repo.Root, "feature-branch", "main")
450+
if err == nil {
451+
t.Fatal("expected error when start-point is specified for existing worktree, but got none")
442452
}
453+
})
443454

444-
// Verify the worktree is based on existing-branch (should NOT have new-file.txt)
445-
newFilePath := filepath.Join(wtPath, "new-file.txt")
446-
if _, err := os.Stat(newFilePath); !os.IsNotExist(err) {
447-
t.Error("worktree should NOT have new-file.txt (start-point should be ignored for existing branch)")
455+
t.Run("start_point_error_for_existing_branch_with_b_flag", func(t *testing.T) {
456+
t.Parallel()
457+
repo := testutil.NewTestRepo(t)
458+
repo.CreateFile("README.md", "# Test")
459+
repo.Commit("initial commit")
460+
461+
repo.Git("branch", "existing-branch")
462+
463+
// Creating worktree with -b for existing branch and start-point should error
464+
_, _, err := runGitWtStdout(t, binPath, repo.Root, "mydir", "-b", "existing-branch", "main")
465+
if err == nil {
466+
t.Fatal("expected error when start-point is specified with -b for existing branch, but got none")
467+
}
468+
})
469+
470+
t.Run("start_point_error_for_existing_worktree_with_b_flag", func(t *testing.T) {
471+
t.Parallel()
472+
repo := testutil.NewTestRepo(t)
473+
repo.CreateFile("README.md", "# Test")
474+
repo.Commit("initial commit")
475+
476+
// Create worktree first
477+
if _, err := runGitWt(t, binPath, repo.Root, "feature-branch"); err != nil {
478+
t.Fatalf("failed to create worktree: %v", err)
479+
}
480+
481+
// Switching to existing worktree with -b and start-point should error
482+
_, _, err := runGitWtStdout(t, binPath, repo.Root, "feature-branch", "-b", "feature-branch", "main")
483+
if err == nil {
484+
t.Fatal("expected error when start-point is specified with -b for existing worktree, but got none")
448485
}
449486
})
450487

0 commit comments

Comments
 (0)