Skip to content

Commit f1c6252

Browse files
authored
Merge branch 'main' into mac-test-fix
2 parents 76a1a5d + 02d430c commit f1c6252

4 files changed

Lines changed: 43 additions & 34 deletions

File tree

‎tools/wt-worktree/tests/test_git.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,19 @@ def test_list_worktrees(git_repo):
6868
def test_add_worktree(git_repo, temp_dir):
6969
"""Test adding a worktree."""
7070
wt_path = temp_dir / "test-worktree"
71-
git.add_worktree(wt_path, "test-branch", "HEAD", repo_path=git_repo)
71+
git.add_worktree(wt_path, "test-branch", True, "HEAD", repo_path=git_repo)
72+
73+
assert wt_path.exists()
74+
assert git.branch_exists("test-branch", git_repo)
75+
76+
worktrees = git.list_worktrees(git_repo)
77+
assert len(worktrees) == 2
78+
79+
def test_add_worktree_with_existing_branch(git_repo, temp_dir):
80+
"""Test adding a worktree with an existing branch."""
81+
git.create_branch("test-branch", "HEAD", git_repo)
82+
wt_path = temp_dir / "test-worktree"
83+
git.add_worktree(wt_path, "test-branch", False, "HEAD", repo_path=git_repo)
7284

7385
assert wt_path.exists()
7486
assert git.branch_exists("test-branch", git_repo)
@@ -80,7 +92,7 @@ def test_add_worktree(git_repo, temp_dir):
8092
def test_remove_worktree(git_repo, temp_dir):
8193
"""Test removing a worktree."""
8294
wt_path = temp_dir / "test-worktree"
83-
git.add_worktree(wt_path, "test-branch", "HEAD", repo_path=git_repo)
95+
git.add_worktree(wt_path, "test-branch", True, "HEAD", repo_path=git_repo)
8496

8597
git.remove_worktree(wt_path, repo_path=git_repo)
8698
assert not wt_path.exists()

‎tools/wt-worktree/wt/cli.py‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def init(ctx: Context, prefix: str, path_pattern: str):
7878
success(f"Configuration saved to {config_path}")
7979
info(f"\nBranch prefix: {prefix}")
8080
info(f"Path pattern: {path_pattern}")
81-
info(f"\nConfiguration will be used for all repositories.")
81+
info("\nConfiguration will be used for all repositories.")
8282
except ConfigError as e:
8383
error(str(e), EXIT_ERROR)
8484

@@ -148,20 +148,13 @@ def switch(ctx: Context, name: Optional[str], create: bool, base: Optional[str],
148148
if shell_helper:
149149
print(target_wt["path"])
150150
else:
151-
success(f"Switched to worktree '{name}' at {target_wt['path']}")
151+
success(f"To switch to worktree '{name}' run: cd {target_wt['path']}")
152152

153153
elif create:
154154
# Create new worktree
155155
try:
156156
# Check if branch exists
157157
full_branch = ctx.config.get_branch_name(name)
158-
if git.branch_exists(full_branch, ctx.repo_root):
159-
if not confirm(
160-
f"Branch '{full_branch}' already exists.\n"
161-
"Create worktree for existing branch?",
162-
default=True
163-
):
164-
sys.exit(EXIT_CANCELLED)
165158

166159
# Create worktree
167160
wt_path = ctx.manager.create_worktree(name, base, detached)
@@ -175,7 +168,7 @@ def switch(ctx: Context, name: Optional[str], create: bool, base: Optional[str],
175168
if shell_helper:
176169
print(wt_path)
177170
else:
178-
success(f"Created and switched to worktree '{name}' at {wt_path}")
171+
success(f"To switch to worktree '{name}' run: cd {wt_path}")
179172

180173
except git.GitError as e:
181174
error(str(e), EXIT_GIT_ERROR)

‎tools/wt-worktree/wt/git.py‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,19 @@ def worktree_exists(name: str, path: Optional[Path] = None) -> Tuple[bool, Optio
212212
return False, None
213213

214214

215-
def add_worktree(path: Path, branch: str, base: Optional[str] = None,
216-
detached: bool = False, repo_path: Optional[Path] = None):
215+
def add_worktree(path: Path,
216+
branch: str,
217+
create_branch: bool = False,
218+
base: Optional[str] = None,
219+
detached: bool = False,
220+
repo_path: Optional[Path] = None):
217221
"""
218222
Create a new worktree.
219223
220224
Args:
221225
path: Path where worktree will be created
222226
branch: Branch name for the worktree
227+
create_branch: create a new branch instead of using an existing one
223228
base: Base branch/commit (if None, uses current HEAD)
224229
detached: Create in detached HEAD state
225230
repo_path: Path to main repo (for running command)
@@ -228,13 +233,18 @@ def add_worktree(path: Path, branch: str, base: Optional[str] = None,
228233

229234
if detached:
230235
args.append("--detach")
231-
else:
236+
args.append(str(path))
237+
if base:
238+
args.append(base)
239+
elif create_branch:
232240
args.extend(["-b", branch])
233-
234-
args.append(str(path))
235-
236-
if base:
237-
args.append(base)
241+
args.append(str(path))
242+
if base:
243+
args.append(base)
244+
else:
245+
# Use existing branch - format: git worktree add <path> <existing-branch>
246+
args.append(str(path))
247+
args.append(branch)
238248

239249
run_git(args, cwd=repo_path)
240250

‎tools/wt-worktree/wt/worktree.py‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,14 @@ def create_worktree(self, name: str, base: Optional[str] = None,
141141
"""
142142
# Get full branch name
143143
branch = self.config.get_branch_name(name)
144+
create_branch = not git.branch_exists(branch, self.repo_root)
144145

145-
# Check if branch already exists
146-
if git.branch_exists(branch, self.repo_root):
147-
exists, path = git.worktree_exists(branch, self.repo_root)
148-
if exists:
149-
raise git.GitError(
150-
f"Worktree '{name}' already exists at {path}\n"
151-
f"Use 'wt switch {name}' to switch to it."
152-
)
153-
else:
154-
raise git.GitError(
155-
f"Branch '{branch}' already exists but has no worktree.\n"
156-
f"Use 'git worktree add' manually or delete the branch first."
157-
)
146+
exists, path = git.worktree_exists(branch, self.repo_root)
147+
if exists:
148+
raise git.GitError(
149+
f"Worktree '{name}' already exists at {path}\n"
150+
f"Use 'wt switch {name}' to switch to it."
151+
)
158152

159153
# Resolve worktree path
160154
wt_path = self.config.resolve_path_pattern(name, branch)
@@ -172,7 +166,7 @@ def create_worktree(self, name: str, base: Optional[str] = None,
172166

173167
# Create worktree
174168
try:
175-
git.add_worktree(wt_path, branch, base, detached, self.repo_root)
169+
git.add_worktree(wt_path, branch, create_branch, base, detached, self.repo_root)
176170
except git.GitError as e:
177171
raise git.GitError(f"Failed to create worktree: {e}")
178172

0 commit comments

Comments
 (0)