Skip to content

feat(workspace): workspace creation ergonomics - #22

Merged
reckerp merged 5 commits into
mainfrom
feat/workspace-creation-ergonomics
Jun 29, 2026
Merged

feat(workspace): workspace creation ergonomics#22
reckerp merged 5 commits into
mainfrom
feat/workspace-creation-ergonomics

Conversation

@reckerp

@reckerp reckerp commented Jun 29, 2026

Copy link
Copy Markdown
Owner

What

Extends gx workspace new to 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-fetch and 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 (mirrors git 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:

  • Ref-namespace conflicts (e.g. creating foo/bar when branch foo exists) are detected via git for-each-ref before calling git worktree add, with a clear explanation and options instead of a cryptic git failure.
  • When the target path is already a worktree on a clean, different branch, gx safely switches it. If the branch is already checked out elsewhere it navigates there instead, and a dirty worktree refuses the switch.

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 build and cargo test both pass (137 unit tests + 16 new integration tests).
  • New tests/workspace_new.rs 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. It 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 before worktree add, the existing-path clean-switch / dirty-refuse / navigate-elsewhere flows, and --detach.
  • Unit tests cover the ref-conflict and name-status parsing logic and the raw-bytes exec helper.

All human-readable output goes to stderr so stdout stays reserved for the shell-navigation target.

reckerp added 5 commits June 29, 2026 21:52
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.
@reckerp
reckerp merged commit 1b109e1 into main Jun 29, 2026
6 checks passed
@reckerp
reckerp deleted the feat/workspace-creation-ergonomics branch June 29, 2026 19:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant