feat(workspace): workspace creation ergonomics - #22
Merged
Conversation
Add the git-side primitives the new workspace-creation flags build on: - exec_bytes() to read raw command output without lossy UTF-8 conversion or trimming, so staged file contents round-trip exactly (binary and whitespace-significant files included). - staged_entries()/show_staged() to list and read the current worktree's staged index via 'git diff --cached --name-status -z' and 'git show :path'. - conflicting_branch() to detect git ref-namespace collisions (e.g. an existing 'foo' blocking 'foo/bar') before calling 'git worktree add'. - ref_resolvable() to check a base against local refs only, for the --no-fetch offline diagnostic. - switch_branch() and a --detach/--no-track aware add() for the existing-path branch switch and detached-HEAD creation flows.
Extend WorkspaceCommands::New with the creation ergonomics flags: --no-cd, --no-fetch, --from-staged [PATH...], --no-hooks, --detach, and --track. --detach conflicts with --branch/--track at the CLI layer. The flags are gathered into a NewWorkspaceOptions struct so the command handler signatures stay readable as creation options accumulate.
Wire the new creation flags into the workspace creation flow: - --no-cd creates the workspace without requesting shell navigation; stdout stays empty (reserved for the cd target) and the path is reported on stderr. - --no-fetch skips the origin fetch and resolves the base from local refs only; an unresolvable base yields a targeted diagnostic that names --no-fetch and suggests retrying without it. - --from-staged copies staged file contents from the current worktree into the new one, optionally filtered to specific paths. Added, modified, renamed, and copied entries are copied (nested paths preserved); deleted entries are skipped with a warning. The source worktree is left untouched, and a copy failure rolls back the freshly-created workspace. - --detach creates a detached HEAD; --track sets the base's remote branch as the new branch's upstream. - Ref-namespace conflicts are detected before 'git worktree add' and reported with a clear explanation and options. - When the target path is already a worktree on a clean, different branch, gx safely switches it; if the branch is checked out elsewhere it navigates there instead, and a dirty worktree refuses the switch. All human-readable output goes to stderr so stdout stays reserved for the shell-navigation target.
Add an integration test suite that drives the compiled gx binary against throwaway git repos in temp dirs (with an isolated config home so every created worktree stays inside the temp dir). Covers --no-cd stdout behavior, --no-fetch skipping the fetch and its offline hint, --from-staged for added/modified/nested/renamed/deleted/filtered paths, ref-conflict detection, the existing-path branch-switch flows, and --detach. Adds tempfile as a dev-dependency.
Document --no-cd, --no-fetch, --from-staged, --detach, and --track in the README workspace section, plus the ref-conflict and existing-path branch-switch behavior.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Extends
gx workspace newto cover common branch-extraction and review workflows without dropping down to raw git. Implements section 4 (Workspace Creation Ergonomics) of the workspace improvement plan.New flags:
--no-cd— create the workspace but do not request shell navigation. stdout (reserved for the cd target) stays empty; the path is reported on stderr. Useful for scripts and batch creation.--no-fetch— skip the origin fetch and resolve the base from local refs only. If the base can't be resolved locally, the error names--no-fetchand suggests retrying without it.--from-staged [PATH...]— copy staged file contents from the current workspace into the new one, optionally filtered to specific paths. Added/modified/renamed/copied entries are copied (nested paths preserved); staged deletions are skipped with a warning. The source workspace is left untouched, and a copy failure rolls back the freshly-created workspace.--detach— create a detached HEAD instead of a new branch (mirrorsgit worktree add --detach).--track— set the base's remote branch as the new branch's upstream.--no-hooks— threaded through for the hook runner that arrives with the shared-repo-config work (currently inert; no hook engine exists yet).Safety / diagnostics:
foo/barwhen branchfooexists) are detected viagit for-each-refbefore callinggit worktree add, with a clear explanation and options instead of a cryptic git failure.Why
Workspace creation should cover branch extraction and review workflows directly. Today users have to leave gx for offline base resolution, extracting staged work into a fresh branch, and detached/tracking checkouts, and they hit opaque git errors on ref conflicts and existing paths.
How it was tested
cargo buildandcargo testboth pass (137 unit tests + 16 new integration tests).tests/workspace_new.rsdrives the compiledgxbinary against throwaway git repos in temp dirs, with an isolated config home so every created worktree stays inside the temp dir. It covers:--no-cdstdout behavior,--no-fetchskipping the fetch and its offline hint,--from-stagedfor added/modified/nested/renamed/deleted/filtered paths, ref-conflict detection beforeworktree add, the existing-path clean-switch / dirty-refuse / navigate-elsewhere flows, and--detach.All human-readable output goes to stderr so stdout stays reserved for the shell-navigation target.