diff --git a/plugins/gw/.claude-plugin/plugin.json b/plugins/gw/.claude-plugin/plugin.json index b0a3991..df361c3 100644 --- a/plugins/gw/.claude-plugin/plugin.json +++ b/plugins/gw/.claude-plugin/plugin.json @@ -1,5 +1,5 @@ { - "name": "gw", + "name": "git-workflow", "description": "Stacked branch management for Claude Code. Teaches Claude to use gw for branch creation, pushing, rebasing, and syncing instead of raw git commands. Includes MCP tools for direct stack manipulation.", "author": { "name": "Jon Deibel" diff --git a/plugins/gw/skills/gw/SKILL.md b/plugins/gw/skills/gw/SKILL.md index b97ca8b..8db22b2 100644 --- a/plugins/gw/skills/gw/SKILL.md +++ b/plugins/gw/skills/gw/SKILL.md @@ -9,7 +9,7 @@ description: > work into pieces, rebase, sync with main, or any git workflow operation. DO NOT TRIGGER when: reading files, running tests, editing code with no git intent. -allowed-tools: Bash, Read, Edit, Write, Glob, Grep, mcp__gw__gw_stack_create, mcp__gw__gw_stack_list, mcp__gw__gw_stack_delete, mcp__gw__gw_branch_create, mcp__gw__gw_branch_remove, mcp__gw__gw_log, mcp__gw__gw_log_pr, mcp__gw__gw_push, mcp__gw__gw_rebase, mcp__gw__gw_rebase_continue, mcp__gw__gw_rebase_abort, mcp__gw__gw_sync, mcp__gw__gw_sync_rebase, mcp__gw__gw_sync_merged, mcp__gw__gw_switch, mcp__github__create_pull_request +allowed-tools: Bash, Read, Edit, Write, Glob, Grep, mcp__gw__gw_stack_create, mcp__gw__gw_stack_list, mcp__gw__gw_stack_delete, mcp__gw__gw_branch_create, mcp__gw__gw_branch_remove, mcp__gw__gw_log, mcp__gw__gw_log_pr, mcp__gw__gw_push, mcp__gw__gw_force_push, mcp__gw__gw_rebase, mcp__gw__gw_rebase_continue, mcp__gw__gw_rebase_abort, mcp__gw__gw_sync, mcp__gw__gw_sync_rebase, mcp__gw__gw_sync_merged, mcp__gw__gw_switch, mcp__gw__gw_split, mcp__gw__gw_split_continue, mcp__gw__gw_split_abort, mcp__github__create_pull_request --- # Git Workflow with gw @@ -37,6 +37,7 @@ You are working in a repo that uses **gw**, a stacked branch manager. Use gw too - **Rebasing** - `gw_rebase` instead of `git rebase` (propagates to descendants) - **Syncing with base** - `gw_sync` instead of `git pull --rebase` - **Viewing branch state** - `gw_log` instead of `git branch` +- **Splitting branches** - `gw_split` to decompose a fat branch into a stack ## Workflow Patterns @@ -58,14 +59,45 @@ When the user has a large change or asks to split work into reviewable pieces: 4. Repeat until done 5. Each branch becomes its own PR, stacked on the previous one -**If the work is already on a single branch:** -1. Use `gw split` to decompose the existing branch into a clean stack -2. The interactive TUI lets you assign each commit to a named bucket/branch -3. Or use `gw split --plan ` with a plan file for scripting -4. Cherry-pick conflicts can be resolved with `gw split --continue` -5. Use `gw split --abort` to roll back if needed - -Good stack boundaries: model/schema changes, API/service layer, UI/frontend, tests, migrations. Each branch should be independently reviewable and tell a coherent story. +**If the work is already on a single branch (use gw_split):** + +This is the most common case when refactoring after the fact. Follow these steps precisely: + +1. **Get the commits** on the current branch using `git log --format='%H %s' ..HEAD` where `` is the branch you branched from (usually `main` or `dev`). You need the full 40-character SHAs. + +2. **Design the split** by grouping commits into logical units. Good boundaries: + - Model/schema changes + - API/service layer + - UI/frontend + - Tests + - Migrations + Each group becomes a branch. Every commit must be assigned to exactly one branch. + +3. **Build the plan** as a string with one line per commit: + ``` + pick + pick + pick + ``` + Commits assigned to the same branch-name are grouped together. The first unique branch name becomes the root of the stack, the second becomes its child, etc. + +4. **Call `gw_split`** with the plan string and optionally a stack name. + +5. **If conflicts occur**, resolve them: + - Check `git status` to see conflicted files + - Read and resolve the conflicts + - Stage resolved files with `git add` + - Call `gw_split_continue` to resume + - If things go wrong, `gw_split_abort` rolls back everything + +**Example split plan for a branch with 4 commits:** +``` +pick a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0 feature-models +pick b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1 feature-models +pick c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2 feature-api +pick d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3 feature-tests +``` +This creates a 3-branch stack: `feature-models` (2 commits) → `feature-api` (1 commit) → `feature-tests` (1 commit). ### Pushing work @@ -112,3 +144,5 @@ Use `gw_log_pr` to see PR status for all branches in the stack. 4. **Check `gw_log` first** before creating new branches to understand the current stack state. 5. **One concern per branch** - each branch in a stack should have a clear, reviewable purpose. 6. **Commit with git, manage with gw** - this is the fundamental split. +7. **Full SHAs in split plans** - `gw_split` requires 40-character commit SHAs, not short hashes. +8. **Every commit must be assigned** - a split plan must cover all commits on the branch and use at least 2 branch names. diff --git a/src/commands/mcp_server.rs b/src/commands/mcp_server.rs index efb01b0..927e214 100644 --- a/src/commands/mcp_server.rs +++ b/src/commands/mcp_server.rs @@ -140,6 +140,46 @@ impl GwServer { fn gw_force_push(&self) -> String { gw(&["push", "--yes"]).unwrap_or_else(|e| format!("{e}")) } + + #[tool(description = "Split the current branch into a stack of focused branches using a plan file. The plan is a series of 'pick ' lines. Commits are grouped by branch name, and each unique branch name becomes a branch in the new stack. Requires at least 2 branches. Before calling this, use git log to get the full SHAs of the commits on the current branch.")] + fn gw_split( + &self, + #[tool(param)] + #[schemars(description = "Plan file content: lines of 'pick '. Example:\npick abc123...def456 feature-models\npick 789abc...012def feature-api")] + plan: String, + #[tool(param)] + #[schemars(description = "Optional stack name (defaults to current branch name)")] + name: Option, + ) -> String { + // Write plan to a temp file + let tmp = std::env::temp_dir().join(format!("gw-split-{}.txt", std::process::id())); + if let Err(e) = std::fs::write(&tmp, &plan) { + return format!("Failed to write plan file: {e}"); + } + + let tmp_str = tmp.to_string_lossy().to_string(); + let mut args = vec!["split", "--plan", &tmp_str, "--yes"]; + let name_val; + if let Some(ref n) = name { + name_val = n.clone(); + args.push("--name"); + args.push(&name_val); + } + + let result = gw(&args).unwrap_or_else(|e| format!("{e}")); + let _ = std::fs::remove_file(&tmp); + result + } + + #[tool(description = "Continue a split after resolving cherry-pick conflicts. Resolve conflicts, git add the files, then call this.")] + fn gw_split_continue(&self) -> String { + gw(&["split", "--continue"]).unwrap_or_else(|e| format!("{e}")) + } + + #[tool(description = "Abort a split in progress and roll back all created branches")] + fn gw_split_abort(&self) -> String { + gw(&["split", "--abort"]).unwrap_or_else(|e| format!("{e}")) + } } #[tool(tool_box)]