| name | jj-hunk |
| description | Programmatic hunk selection for jj (Jujutsu). Use when splitting commits, making partial commits, or selectively squashing changes without interactive UI. |
jj-hunk: Programmatic Hunk Selection
Use jj-hunk for non-interactive hunk selection in jj. Essential for AI agents that need to create clean, logical commits from mixed changes.
When to Use This Skill
- Splitting a commit into multiple logical commits
- Committing only specific hunks (partial commit)
- Squashing only certain changes into parent
- Any hunk selection that would normally require
jj split -i or jj squash -i
Setup
cargo install jj-hunk
Add to ~/.jjconfig.toml:
[merge-tools.jj-hunk]
program = "jj-hunk"
edit-args = ["select", "$left", "$right"]
Core Workflow
1. List Hunks
jj-hunk list
jj-hunk list --rev @
jj-hunk list --format yaml
Options:
--rev <revset> — diff the revision against its parent (revset must resolve to a single revision)
--format json|yaml|text — output format (default: json)
--include <glob> / --exclude <glob> — filter paths (repeatable)
--group none|directory|extension|status — group output
--binary skip|mark|include — binary handling (default: mark)
--max-bytes <n> / --max-lines <n> — truncate before diffing
--spec <json|yaml> / --spec-file <path> — preview using a spec filter
--files — list files with hunk counts only
--spec-template — emit a spec template (JSON/YAML only)
Output (JSON):
{
"files": [
{
"path": "src/foo.rs",
"status": "modified",
"hunks": [
{
"id": "hunk-7c3d...",
"index": 0,
"type": "replace",
"removed": "old\n",
"added": "new\n",
"before": {"start": 1, "lines": 1},
"after": {"start": 1, "lines": 1}
},
{
"id": "hunk-2f91...",
"index": 1,
"type": "insert",
"removed": "",
"added": "// added\n",
"before": {"start": 2, "lines": 0},
"after": {"start": 2, "lines": 1}
}
]
},
{
"path": "src/bar.rs",
"status": "modified",
"hunks": [
{
"id": "hunk-aa12...",
"index": 0,
"type": "delete",
"removed": "removed\n",
"added": "",
"before": {"start": 3, "lines": 1},
"after": {"start": 3, "lines": 0}
}
]
}
]
}
Each hunk includes a stable id (sha256) alongside the 0-based index.
2. Build a Spec
Select hunks by index or id (emitted as hunk-<sha256>), or use file-level actions. Specs can be JSON or YAML:
{
"files": {
"src/foo.rs": {"hunks": [0, "hunk-7c3d..."]},
"src/bar.rs": {"ids": ["hunk-aa12..."]},
"src/baz.rs": {"action": "keep"},
"src/qux.rs": {"action": "reset"}
},
"default": "reset"
}
| Spec | Effect |
|---|
{"hunks": [0, 2]} | Include only hunks 0 and 2 |
{"hunks": ["hunk-..."]} | Include hunks by id string |
{"ids": ["hunk-..."]} | Include hunks by stable id |
{"action": "keep"} | Include all changes |
{"action": "reset"} | Discard all changes |
"default": "reset" | Unlisted files are discarded |
"default": "keep" | Unlisted files are kept |
ids and hunks are merged if both are provided.
3. Execute
Specs can be provided inline, read from stdin with -, or loaded via --spec-file (omit <spec> when using --spec-file).
jj-hunk split '<spec>' "commit message"
jj-hunk split --spec-file spec.yaml "commit message"
jj-hunk commit '<spec>' "commit message"
cat spec.json | jj-hunk commit - "commit message"
jj-hunk squash '<spec>'
Examples
Split Mixed Changes into Logical Commits
You have refactoring and a new feature mixed together:
jj-hunk list
jj-hunk split '{"files": {"src/lib.rs": {"hunks": [0, 1]}}, "default": "reset"}' \
"refactor: extract helper function"
jj describe -m "feat: add new feature"
Commit Only Part of Your Changes
Keep experimental code in working copy while committing the fix:
jj-hunk commit '{"files": {"src/bug.rs": {"action": "keep"}}, "default": "reset"}' \
"fix: handle null case"
Squash Specific Files into Parent
jj-hunk squash '{"files": {"src/tests.rs": {"action": "keep"}}, "default": "reset"}'
Keep Everything Except One File
jj-hunk split '{"files": {"src/wip.rs": {"action": "reset"}}, "default": "keep"}' \
"feat: complete implementation"
Direct jj --tool Usage
The commands above are wrappers. For direct control:
echo '{"files": {"src/foo.rs": {"hunks": [0]}}, "default": "reset"}' > /tmp/spec.json
JJ_HUNK_SELECTION=/tmp/spec.json jj split -i --tool=jj-hunk -m "message"
Hunk Types
| Type | Meaning |
|---|
insert | New lines added |
delete | Lines removed |
replace | Lines changed (removed + added) |
Agent Workflow Examples
Understanding the Output
Always start by inspecting what hunks exist:
jj-hunk list
Example output:
{
"files": [
{
"path": "src/db/schema.ts",
"status": "modified",
"hunks": [
{"id": "hunk-98af...", "index": 0, "type": "insert", "removed": "", "added": "import { pgTable }...\n", "before": {"start": 1, "lines": 0}, "after": {"start": 1, "lines": 1}},
{"id": "hunk-21b3...", "index": 1, "type": "insert", "removed": "", "added": "export const users = pgTable...\n", "before": {"start": 2, "lines": 0}, "after": {"start": 2, "lines": 1}}
]
},
{
"path": "src/api/routes.ts",
"status": "modified",
"hunks": [
{"id": "hunk-cc19...", "index": 0, "type": "replace", "removed": "// TODO\n", "added": "app.get('/users', ...);\n", "before": {"start": 10, "lines": 1}, "after": {"start": 10, "lines": 1}},
{"id": "hunk-4b20...", "index": 1, "type": "insert", "removed": "", "added": "app.get('/posts', ...);\n", "before": {"start": 11, "lines": 0}, "after": {"start": 11, "lines": 1}}
]
},
{
"path": "src/lib/utils.ts",
"status": "modified",
"hunks": [
{"id": "hunk-11bf...", "index": 0, "type": "replace", "removed": "function old()...\n", "added": "function new()...\n", "before": {"start": 5, "lines": 1}, "after": {"start": 5, "lines": 1}},
{"id": "hunk-ee43...", "index": 1, "type": "insert", "removed": "", "added": "export function helper()...\n", "before": {"start": 6, "lines": 0}, "after": {"start": 6, "lines": 1}},
{"id": "hunk-09ad...", "index": 2, "type": "delete", "removed": "// dead code\n", "added": "", "before": {"start": 20, "lines": 1}, "after": {"start": 20, "lines": 0}}
]
}
]
}
File-Level Selection
When all hunks in a file belong to the same logical change:
jj-hunk split '{"files": {"src/db/schema.ts": {"action": "keep"}}, "default": "reset"}' "feat: add database schema"
Hunk-Level Selection
When a single file has mixed concerns (most powerful feature):
jj-hunk split '{"files": {"src/lib/utils.ts": {"hunks": [0, 2]}}, "default": "reset"}' "refactor: clean up utils"
jj describe -m "feat: add helper function"
Mixed Selection
Combine file-level and hunk-level in one spec:
jj-hunk split '{"files": {"src/db/schema.ts": {"action": "keep"}, "src/api/routes.ts": {"hunks": [0]}}, "default": "reset"}' "feat: add users table and endpoint"
jj-hunk split '{"files": {"src/api/routes.ts": {"action": "keep"}}, "default": "reset"}' "feat: add posts endpoint"
jj describe -m "refactor: utils cleanup"
Complete Workflow Example
Starting with a messy commit containing schema, API, and refactoring changes:
jj edit <revision>
jj-hunk list
jj-hunk split '{"files": {"src/db/schema.ts": {"action": "keep"}}, "default": "reset"}' "feat: add database schema"
jj-hunk split '{"files": {"src/lib/utils.ts": {"hunks": [0, 2]}}, "default": "reset"}' "refactor: clean up utils"
jj-hunk split '{"files": {"src/lib/utils.ts": {"action": "keep"}, "src/api/routes.ts": {"hunks": [0]}}, "default": "reset"}' "feat: add users endpoint"
jj describe -m "feat: add posts endpoint"
jj log -r 'trunk()..@'
Verifying Splits
After splitting, verify each commit has the right content:
jj diff -r <rev1> --stat
jj diff -r <rev2> --stat
jj log
Tips
- Always list first: Run
jj-hunk list to see hunk indices/ids before building specs
- Prefer ids for stability: Use
ids when hunks might shift between list and apply
- Use default wisely:
"default": "reset" is safer (explicit inclusion), "default": "keep" is convenient for excluding specific files
- Combine with jj: After splitting, use
jj describe to refine commit messages
- Exact paths required: File paths must match exactly (e.g.,
"src/lib.rs" not "src/")