jj-hunk

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

jj-hunk

jj-hunk

Programmatic hunk selection for Jujutsu. Split, commit, or squash specific hunks without interactive prompts.
面向Jujutsu的程序化代码块选择工具。无需交互式提示即可拆分、提交或合并特定代码块。

Basic Workflow

基础工作流

List available hunks, create a spec file, then apply:
bash
undefined
列出可用代码块,创建规格文件,然后执行操作:
bash
undefined

See what hunks exist

查看现有代码块

jj-hunk list jj-hunk list --format text # Human-readable output jj-hunk list --files # Files with hunk counts only
jj-hunk list jj-hunk list --format text # 人类可读格式输出 jj-hunk list --files # 仅显示包含代码块的文件及数量

Split changes using a spec file

使用规格文件拆分变更

jj-hunk split --spec-file /tmp/spec.yaml "feat: add feature A"
jj-hunk split --spec-file /tmp/spec.yaml "feat: add feature A"

Commit specific files, leave rest uncommitted

提交特定文件,其余变更保留为未提交状态

jj-hunk commit --spec-file /tmp/commit-spec.yaml "fix: handle edge case"
jj-hunk commit --spec-file /tmp/commit-spec.yaml "fix: handle edge case"

Squash into parent

合并到父提交

jj-hunk squash --spec-file /tmp/squash-spec.yaml

Targets any revision with `-r <rev>` (default is `@` for working copy). Read specs from stdin with `-`.
jj-hunk squash --spec-file /tmp/squash-spec.yaml

可通过 `-r <rev>` 指定目标版本(默认是工作副本 `@`)。使用 `-` 从标准输入读取规格。

Spec Format

规格文件格式

YAML files control which hunks to keep or reset:
yaml
files:
  src/main.rs:
    hunks: [0, "hunk-7c3d..."] # Select by index or stable id
  path/to/skip:
    action: reset # Discard all changes
default: reset # Unlisted files get this action
  • hunks: [indices|ids]
    — select by 0-based index or stable id from
    jj-hunk list
  • ids: ["hunk-..."]
    — select by id string (sha256-based, stable across runs)
  • action: keep
    /
    action: reset
    — keep or discard all changes in a file
  • default
    — action for unlisted files
Use
jj-hunk list --spec-template
to generate an ID-based starting spec. IDs are more reliable than indices since they're stable across runs.
YAML文件用于控制保留或重置哪些代码块:
yaml
files:
  src/main.rs:
    hunks: [0, "hunk-7c3d..."] # 通过索引或稳定ID选择
  path/to/skip:
    action: reset # 丢弃所有变更
default: reset # 未列出的文件执行此操作
  • hunks: [indices|ids]
    — 通过0-based索引或
    jj-hunk list
    输出的稳定ID选择
  • ids: ["hunk-..."]
    — 通过ID字符串选择(基于sha256,跨运行稳定)
  • action: keep
    /
    action: reset
    — 保留或丢弃文件中的所有变更
  • default
    — 未列出文件的默认操作
使用
jj-hunk list --spec-template
生成基于ID的初始规格文件。相比索引,ID更可靠,因为它们在多次运行中保持稳定。

Example: Split by Hunk

示例:按代码块拆分

bash
undefined
bash
undefined

1. List hunks

1. 列出代码块

jj-hunk list --format text
jj-hunk list --format text

Output: M src/main.rs — hunk 0 insert hunk-abc123, hunk 1 insert hunk-def456

输出:M src/main.rs — hunk 0 insert hunk-abc123, hunk 1 insert hunk-def456

2. Create spec for first hunk only

2. 创建仅包含第一个代码块的规格文件

cat > /tmp/split-spec.yaml << 'EOF' files: src/main.rs: hunks: [0] default: reset EOF
cat > /tmp/split-spec.yaml << 'EOF' files: src/main.rs: hunks: [0] default: reset EOF

3. Split — first hunk becomes commit A, rest stays in working copy

3. 拆分操作 — 第一个代码块成为提交A,其余变更保留在工作副本中

jj-hunk split --spec-file /tmp/split-spec.yaml "feat: add feature A"
undefined
jj-hunk split --spec-file /tmp/split-spec.yaml "feat: add feature A"
undefined

Example: Commit Subset of Changes

示例:提交部分变更

bash
cat > /tmp/commit-spec.yaml << 'EOF'
files:
  src/fix.rs:
    action: keep
default: reset
EOF

jj-hunk commit --spec-file /tmp/commit-spec.yaml "fix: handle edge case"
bash
cat > /tmp/commit-spec.yaml << 'EOF'
files:
  src/fix.rs:
    action: keep
default: reset
EOF

jj-hunk commit --spec-file /tmp/commit-spec.yaml "fix: handle edge case"

Tips

小贴士

  • IDs are stable (sha256-based), indices are not — prefer ids for reproducibility
  • Use
    --files
    for a quick summary without full hunk details
  • Read spec from stdin:
    cat spec.yaml | jj-hunk commit - "message"
  • Use
    -r <rev>
    with split/squash to target any revision
  • When verifying after operations, always use the original change ID — see jj-core skill for template syntax (
    concat()
    ,
    description
    field)
  • ID是稳定的(基于sha256),索引则不是——为了可重复性优先使用ID
  • 使用
    --files
    快速获取摘要,无需查看完整代码块详情
  • 从标准输入读取规格:
    cat spec.yaml | jj-hunk commit - "message"
  • 使用
    -r <rev>
    配合split/squash操作指定目标版本
  • 操作后验证时,请始终使用原始变更ID——查看jj-core技能了解模板语法(
    concat()
    description
    字段)