Skip to content

Commit 66f9dfb

Browse files
committed
Update commands to confirm repository cloning
1 parent 17ae0bc commit 66f9dfb

3 files changed

Lines changed: 104 additions & 32 deletions

File tree

cli/gitx/cli.py

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ def resolve_worktree(
5151
if not create_branch:
5252
raise RuntimeError("Branch does not exist")
5353

54+
try:
55+
git.create_branch(repo_cfg, branch)
56+
except git.GitCommandFailed as exc:
57+
raise RuntimeError(f"Failed to create branch '{branch}': {exc}") from exc
58+
5459
create_worktree = typer.confirm(
5560
f"Worktree for branch '{branch}' does not exist. Create it?",
5661
default=True,
@@ -70,18 +75,26 @@ def resolve_worktree(
7075
# go
7176
# ===========================================================================
7277

73-
@app.command()
74-
def debug(repo: str) -> None:
75-
repo_cfg: RepoConfig | None = _config.resolve_workspace(repo)
76-
pprint(git.iter_worktrees(repo_cfg), expand_all=True, console=console)
77-
7878
@app.command()
7979
def go(repo: str, branch: Optional[str] = None) -> None:
8080
repo_cfg: RepoConfig | None = _config.resolve_workspace(repo)
8181

8282
if repo_cfg is None:
83-
console.print(f"[yellow]Repository '{repo}' does not exist.[/]")
84-
raise typer.Exit(code=1)
83+
clone_repo = typer.confirm(
84+
f"Repository '{repo}' does not exist. Clone it?",
85+
default=True,
86+
)
87+
if not clone_repo:
88+
raise typer.Exit(code=1)
89+
90+
try:
91+
repo_cfg = git.clone_and_add_worktree(repo)
92+
except Exception as exc: # GitCommandFailed, GitxError, etc.
93+
console.print(f"[red]{exc}[/]")
94+
raise typer.Exit(code=1)
95+
96+
_config.workspaces.update({repo: repo_cfg})
97+
_config.save()
8598

8699
try:
87100
path = resolve_worktree(repo_cfg, branch)
@@ -94,16 +107,29 @@ def go(repo: str, branch: Optional[str] = None) -> None:
94107

95108

96109
# ===========================================================================
97-
# jump
110+
# code
98111
# ===========================================================================
99112

100113
@app.command()
101-
def jump(repo: str, branch: Optional[str] = None) -> None:
114+
def code(repo: str, branch: Optional[str] = None) -> None:
102115
repo_cfg: RepoConfig | None = _config.resolve_workspace(repo)
103116

104117
if repo_cfg is None:
105-
console.print(f"[yellow]Repository '{repo}' does not exist.[/]")
106-
raise typer.Exit(code=1)
118+
clone_repo = typer.confirm(
119+
f"Repository '{repo}' does not exist. Clone it?",
120+
default=True,
121+
)
122+
if not clone_repo:
123+
raise typer.Exit(code=1)
124+
125+
try:
126+
repo_cfg = git.clone_and_add_worktree(repo)
127+
except Exception as exc: # GitCommandFailed, GitxError, etc.
128+
console.print(f"[red]{exc}[/]")
129+
raise typer.Exit(code=1)
130+
131+
_config.workspaces.update({repo: repo_cfg})
132+
_config.save()
107133

108134
try:
109135
path = resolve_worktree(repo_cfg, branch)
@@ -138,10 +164,11 @@ def clone(repo: str) -> None:
138164
)
139165
raise typer.Exit(code=1)
140166

141-
repo_cfg = git.clone_and_add_worktree(repo)
142-
143-
if isinstance(repo_cfg, int):
144-
raise typer.Exit(code=repo_cfg)
167+
try:
168+
repo_cfg = git.clone_and_add_worktree(repo)
169+
except Exception as exc: # GitCommandFailed, GitxError, etc.
170+
console.print(f"[red]{exc}[/]")
171+
raise typer.Exit(code=1)
145172

146173
_config.workspaces.update({repo: repo_cfg})
147174
_config.save()
@@ -198,10 +225,26 @@ def branch_add(repo: str, branch: str) -> None:
198225
repo_cfg: RepoConfig | None = _config.resolve_workspace(repo)
199226

200227
if repo_cfg is None:
201-
console.print(f"[yellow]Repository '{repo}' does not exist.[/]")
202-
raise typer.Exit(code=1)
228+
clone_repo = typer.confirm(
229+
f"Repository '{repo}' does not exist. Clone it?",
230+
default=True,
231+
)
232+
if not clone_repo:
233+
raise typer.Exit(code=1)
234+
235+
if git.branch_exists(repo_cfg.repo_root_path(), branch):
236+
console.print(f"[red]Branch '{branch}' already exists locally.[/]")
237+
raise typer.Exit(code=0)
238+
239+
try:
240+
repo_cfg = git.clone_and_add_worktree(repo)
241+
except Exception as exc: # GitCommandFailed, GitxError, etc.
242+
console.print(f"[red]{exc}[/]")
243+
raise typer.Exit(code=1)
244+
245+
_config.workspaces.update({repo: repo_cfg})
246+
_config.save()
203247

204-
git.add_worktree(repo_cfg, branch)
205248
raise typer.Exit(code=0)
206249

207250

@@ -217,8 +260,21 @@ def branch_delete(repo: str, branch: str) -> None:
217260
console.print(f"[yellow]Repository '{repo}' does not exist.[/]")
218261
raise typer.Exit(code=1)
219262

220-
code = git.delete_branch(repo_cfg, branch)
221-
raise typer.Exit(code=code)
263+
delete_remote = typer.confirm(
264+
f"Also delete remote branch 'origin/{branch}'?",
265+
default=False,
266+
)
267+
268+
try:
269+
git.delete_branch(repo_cfg, branch, delete_remote=delete_remote)
270+
except git.BranchDoesNotExist as exc:
271+
console.print(f"[red]{exc}[/]")
272+
raise typer.Exit(code=1)
273+
except git.GitCommandFailed as exc:
274+
console.print(f"[red]{exc}[/]")
275+
raise typer.Exit(code=1)
276+
277+
raise typer.Exit(code=0)
222278

223279

224280
# ===========================================================================

cli/gitx/helpers/cli.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
console = Console()
88

99

10-
def cmd(path: Path, *args: str):
11-
console.print("[bold green]Running:[/]", " ".join(args), "in", str(path))
10+
def cmd(path: Path, *args: str) -> subprocess.CompletedProcess[str]:
1211
return subprocess.run(
1312
[*args],
1413
cwd=str(path),
@@ -19,7 +18,6 @@ def cmd(path: Path, *args: str):
1918

2019

2120
def cmd_capture(path: Path, *args: str) -> subprocess.CompletedProcess[str]:
22-
console.print("[bold green]Running:[/]", " ".join(args), "in", str(path))
2321
return subprocess.run(
2422
[*args],
2523
cwd=str(path),

cli/gitx/helpers/git.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def __init__(self, args: list[str], result: subprocess.CompletedProcess):
3333
self.code = result.returncode
3434

3535

36+
class GitxError(RuntimeError):
37+
pass
38+
39+
3640
# ===========================================================================
3741
# models
3842
# ===========================================================================
@@ -129,22 +133,36 @@ def add_worktree(repo: RepoConfig, branch: str) -> None:
129133
)
130134

131135

132-
def delete_branch(repo: RepoConfig, branch: str) -> None:
136+
def delete_branch(repo: RepoConfig, branch: str, *, delete_remote: bool = False) -> None:
133137
repo_root = repo.repo_root_path()
134138

135139
local = cmd(repo_root, "git", "show-ref", "--verify", f"refs/heads/{branch}")
136-
if local.returncode != 0:
140+
remote = cmd(repo_root, "git", "show-ref", "--verify", f"refs/remotes/origin/{branch}")
141+
142+
# No local branch and remote deletion not requested: behave as before
143+
if local.returncode != 0 and not delete_remote:
137144
raise BranchDoesNotExist(branch)
138145

139-
wt_path = repo.worktree_path_for_branch(branch)
140-
if wt_path.exists():
141-
res = cmd(repo_root, "git", "worktree", "remove", str(wt_path))
146+
# If a local branch exists, clean up worktree and local ref
147+
if local.returncode == 0:
148+
wt_path = repo.worktree_path_for_branch(branch)
149+
if wt_path.exists():
150+
res = cmd(repo_root, "git", "worktree", "remove", str(wt_path))
151+
if res.returncode != 0:
152+
raise GitCommandFailed(["git", "worktree", "remove", str(wt_path)], res)
153+
154+
res = cmd(repo_root, "git", "branch", "-d", branch)
142155
if res.returncode != 0:
143-
raise GitCommandFailed(["git", "worktree", "remove", str(wt_path)], res)
156+
raise GitCommandFailed(["git", "branch", "-d", branch], res)
144157

145-
res = cmd(repo_root, "git", "branch", "-d", branch)
146-
if res.returncode != 0:
147-
raise GitCommandFailed(["git", "branch", "-d", branch], res)
158+
# Optionally delete the remote branch, even if there is no local one
159+
if delete_remote:
160+
if remote.returncode != 0:
161+
raise BranchDoesNotExist(branch)
162+
163+
res = cmd(repo_root, "git", "push", "origin", "--delete", branch)
164+
if res.returncode != 0:
165+
raise GitCommandFailed(["git", "push", "origin", "--delete", branch], res)
148166

149167

150168
# ===========================================================================

0 commit comments

Comments
 (0)