-
Notifications
You must be signed in to change notification settings - Fork 0
Add checkout_branch command to switch to existing branches #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -87,17 +87,68 @@ def init(ctx: Context, prefix: str, path_pattern: str): | |||||||||||||||||||
| @click.argument("name", required=False) | ||||||||||||||||||||
| @click.option("-c", "--create", is_flag=True, help="Create worktree if it doesn't exist") | ||||||||||||||||||||
| @click.option("-b", "--base", help="Base branch for new worktree") | ||||||||||||||||||||
| @click.option("-B", "--branch", "checkout_branch", default=None, | ||||||||||||||||||||
| help="Checkout an existing branch into a new worktree (use with -c)") | ||||||||||||||||||||
| @click.option("-f", "--fetch", is_flag=True, help="Fetch branch from remote before checkout") | ||||||||||||||||||||
| @click.option("-d", "--detached", is_flag=True, help="Create in detached HEAD state") | ||||||||||||||||||||
| @click.option("--shell-helper", is_flag=True, hidden=True, | ||||||||||||||||||||
| help="Internal flag for shell integration") | ||||||||||||||||||||
| @pass_context | ||||||||||||||||||||
| def switch(ctx: Context, name: Optional[str], create: bool, base: Optional[str], | ||||||||||||||||||||
| detached: bool, shell_helper: bool): | ||||||||||||||||||||
| """Switch to a worktree, optionally creating it.""" | ||||||||||||||||||||
| checkout_branch: Optional[str], fetch: bool, detached: bool, shell_helper: bool): | ||||||||||||||||||||
| """Switch to a worktree, optionally creating it. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Use -B/--branch to checkout an existing branch into a new worktree: | ||||||||||||||||||||
|
|
||||||||||||||||||||
| \b | ||||||||||||||||||||
| wt switch -c -B fix/login-bug | ||||||||||||||||||||
| wt switch -c review -B fix/login-bug | ||||||||||||||||||||
| wt switch -c -B fix/login-bug --fetch | ||||||||||||||||||||
| """ | ||||||||||||||||||||
| if not ctx.repo_root or not ctx.manager: | ||||||||||||||||||||
| error("Not in a git repository.", EXIT_GIT_ERROR) | ||||||||||||||||||||
| return | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # Validate flag combinations | ||||||||||||||||||||
| if checkout_branch and not create: | ||||||||||||||||||||
| error("--branch/-B requires --create/-c", EXIT_INVALID_ARGS) | ||||||||||||||||||||
| return | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if checkout_branch and detached: | ||||||||||||||||||||
| error("--branch/-B cannot be used with --detached/-d", EXIT_INVALID_ARGS) | ||||||||||||||||||||
| return | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if checkout_branch and base: | ||||||||||||||||||||
| error("--branch/-B cannot be used with --base/-b", EXIT_INVALID_ARGS) | ||||||||||||||||||||
| return | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if fetch and not checkout_branch: | ||||||||||||||||||||
| error("--fetch/-f requires --branch/-B", EXIT_INVALID_ARGS) | ||||||||||||||||||||
| return | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # Handle checkout of existing branch | ||||||||||||||||||||
| if checkout_branch: | ||||||||||||||||||||
| from .worktree import WorktreeManager | ||||||||||||||||||||
|
|
||||||||||||||||||||
| try: | ||||||||||||||||||||
| wt_path = ctx.manager.checkout_branch(checkout_branch, name, fetch) | ||||||||||||||||||||
| display_name = name if name else WorktreeManager._derive_name_from_branch(checkout_branch) | ||||||||||||||||||||
|
Comment on lines
+131
to
+135
|
||||||||||||||||||||
| from .worktree import WorktreeManager | |
| try: | |
| wt_path = ctx.manager.checkout_branch(checkout_branch, name, fetch) | |
| display_name = name if name else WorktreeManager._derive_name_from_branch(checkout_branch) | |
| try: | |
| wt_path = ctx.manager.checkout_branch(checkout_branch, name, fetch) | |
| display_name = name if name else type(ctx.manager)._derive_name_from_branch(checkout_branch) |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -493,6 +493,21 @@ def fetch_remote(remote: str = "origin", path: Optional[Path] = None): | |||||||||||
| run_git(["fetch", remote], cwd=path) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def fetch_branch(branch: str, remote: str = "origin", path: Optional[Path] = None): | ||||||||||||
| """ | ||||||||||||
| Fetch a specific branch from remote. | ||||||||||||
|
|
||||||||||||
| Args: | ||||||||||||
| branch: Branch name to fetch | ||||||||||||
| remote: Remote name | ||||||||||||
| path: Repository path | ||||||||||||
|
|
||||||||||||
| Raises: | ||||||||||||
| GitError: If fetch fails | ||||||||||||
| """ | ||||||||||||
| run_git(["fetch", remote, branch], cwd=path) | ||||||||||||
|
||||||||||||
| run_git(["fetch", remote, branch], cwd=path) | |
| # Use an explicit refspec so remote-only branches are fetched into the | |
| # expected remote-tracking reference (refs/remotes/<remote>/<branch>). | |
| refspec = f"{branch}:refs/remotes/{remote}/{branch}" | |
| run_git(["fetch", remote, refspec], cwd=path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test coverage for
checkout_branchdoesn't include tests with actual remote branches. While there is agit_repo_with_remotefixture available in conftest.py, the tests only verify local branch checkout behavior. Consider adding tests that verify the remote branch checkout scenario, especially testing the fetch functionality and the case where a branch exists only on the remote.