Tools for a jj megamerge workflow with one post-merge working copy per stack
README.md

jj-hydra #

hydra is a CLI for a jj megamerge workflow. It keeps several PR branches ("stacks") under one merge commit and gives each stack its own optional working copy, so several workspaces can work on the megamerge at the same time.

a jujutsu log output showing jj-hydra bookmarks A hydra megamerge with 4 stacks (HYS-*). Each stack sits between the megamerge base (HYB) and the megamerge head (HYH). Here the bench-criterion stack is edited from its working copy, HYWC-bench-criterion.

Concepts #

A megamerge is an octopus merge: one base commit, one head commit, and 1 to n stacks between them. You work on top of the head and move each change into the stack it belongs to. For background, read https://isaaccorbrey.com/notes/jujutsu-megamerges-for-fun-and-profit.

hydra marks every part of its topology with a bookmark:

Bookmark What it marks
HYB The megamerge base. Usually trunk(), but any commit works. Every stack is a child of it.
HYH The megamerge head: a merge commit whose parents are the stack heads.
HYCR Optional. The tip of a linear conflict-resolution chain on top of HYH. When it exists, working copies sit on it instead of on HYH.
HYS-<name> The head marker of stack <name>. Always empty, and always a parent of HYH. The stack's real commits are below it.
HYWC-<name> Optional. The working copy of stack <name>, on top of HYH (or HYCR). It can be empty. hydra moves its changes into the stack when you land them.

Stack names are unique. A stack does not need a working copy: hydra new <name> --working-copy=false creates one without it, and hydra start <name> adds it later.

Workflow #

hydra new cli      # create stack "cli" and its working copy HYWC-cli
                   # (also creates HYB and HYH if they are missing)
# ...edit files in HYWC-cli, or commit on top of it...
hydra land         # move the work below HYS-cli; HYWC-cli is empty again
hydra push         # push the stack's PR bookmark (creates $USER/cli if missing)
hydra start other  # switch to another stack's working copy

hydra land rebases the commits in HYWC-cli and everything on top of it so they become parents of HYS-cli, then leaves an empty HYWC-cli ready for more work.

Install #

Requires Go 1.24+.

GOBIN=~/.local/bin go install .

GOBIN sets the install directory. Omit it if go env GOBIN already points to a directory on your PATH.

hydra --version reports the revision that Go embeds from the colocated git repo. When you build by hand, set it with -ldflags:

go build -ldflags "-X main.version=$(jj log --no-graph -r @ -T 'change_id.short()')" -o ~/.local/bin/hydra .

Mark hydra's scaffold commits private so you cannot push them by accident. HYB is left out because you often push from stacks that sit on it.

[git]
private-commits = "bookmarks(glob:'HYH') | bookmarks(glob:'HYS-*') | bookmarks(glob:'HYWC-*')"

Aliases let you run hydra through jj:

[aliases]
hydra-init = ["util", "exec", "--", "hydra", "init"]
hy-init = ["hydra-init"] # i'm lazy

Agent skill #

skills/hydra/SKILL.md is a skill that teaches coding agents the hydra workflow.

Commands #

Arguments in [brackets] are optional. When a command needs a stack name that you omit, hydra either infers it or prompts for it.

Global flags #

  • --force: skip prompts, downgrade the base checks (see Safety checks) to warnings, and refresh a stale working copy without asking.
  • --no-sort: do not re-sort the stacks after the command (see Stack order).

Setup #

hydra init #

Create HYB (at revsets.init-base, default main) and HYH if they do not exist. hydra new does this for you.

hydra teardown #

Forget every local-only hydra bookmark (HYB, HYH, HYCR, HYS-*, HYWC-*, $USER/*) and abandon the empty orphan commits left behind. Bookmarks with remote tracking are skipped.

Stacks #

hydra new [--working-copy=<bool>] <name> #

Create stack <name>: an empty HYS-<name> marker on HYB, added to the parents of HYH. --working-copy (alias --wc, default true) also creates HYWC-<name> and moves @ to it.

hydra adopt [--revset <revset> | [--from <rev>] [--to <rev>]] [<name>] #

Turn an existing linear chain into a new stack. The chain defaults to trunk()+::@. -r/--revset names the chain directly; -f/--from and -t/--to set its bottom and top instead. <name> is required; when you omit it, hydra prompts, and <tab> fills in the chain head's short change id.

hydra land [<name>] #

Move HYWC-<name> and its descendants into the stack, below HYS-<name>. <name> is inferred from the HYWC-* bookmark in HYH::@. Refuses when another part of the topology (HYCR, or a sibling HYWC-*) sits on that chain and would be carried into the stack.

hydra push [<name>] #

Advance the stack's PR bookmark to the stack tip and push it. If the stack has no PR bookmark, creates $USER/<name> (see push.branch-prefix). <name> is inferred from @.

hydra rename [--rename-remote-bookmark] [<old> [<new>]] #

Rename HYS-<old> and HYWC-<old>, and rewrite their descriptions if they still match hydra's stock messages. Prompts for missing names. --rename-remote-bookmark allows renaming a bookmark that was pushed.

hydra remove [--dest <dest>] [<name>] #

Remove stack <name>. Its non-empty content (the stack chain and any work in HYWC-<name>) moves onto -d/--dest (default HYB). hydra forgets HYS-<name> and HYWC-<name> and abandons empty orphans. A revision that another part of the topology sits on stays in place, so the rest of the hydra is not dragged along; for example, HYCR sharing a revision with HYWC-<name>.

hydra sort [<name>] #

Reorder the stacks so the most recently worked-on one is at the top of jj log. <name> pins that stack to the top. See Stack order.

Working copies #

hydra start [<name>] (aliases: go, wc, working-copy) #

Switch @ to HYWC-<name>, creating it on HYCR/HYH if the stack has none. Prompts for <name> when you omit it. If no stack has that name, hydra offers the stack whose name it looks like a typo of, then offers to create <name> as a new stack.

hydra stop [<name>] #

Forget HYWC-<name>, and abandon its revision if it is an empty orphan. The stack itself is untouched. Prompts first if the working copy has unlanded work (which stays on HYCR/HYH) or another workspace sits on it.

hydra stop-all #

Pick HYWC-* working copies to remove from a list. The list toggles between all, other (all but the stack in use here), and inactive (those no workspace sits on). Prompts once for the same reasons as stop. A workspace left on a removed working copy moves to a new commit on trunk(). With --force, or with no terminal, removes all of them.

Inspection and repair #

hydra status [--toml] #

Print four sections:

  1. The HYB/HYH/HYCR revisions: anchor, change id, commit title.
  2. The stacks in log order: name, commits in the stack, working-copy revision, and when the stack was last worked on.
  3. The configuration in effect, with the source of each value (default, file, or env var).
  4. Whatever hydra doctor finds.

--toml prints the same report, unstyled, as one TOML document on stdout. Missing pieces are omitted rather than shown as placeholders.

hydra doctor [--fix] #

Validate the topology:

  • the anchors exist;
  • every HYS-* marker is empty and a parent of HYH;
  • every HYWC-* is a child of HYCR/HYH and belongs to an existing stack;
  • HYH..HYCR is linear.

Exits 0 only when nothing is broken. Conflicts and a stale base are warnings. When a HYCR chain exists, HYH and the inner chain commits may be conflicted (that is what the chain is for), so only an unresolved HYCR tip is warned about.

hydra prune #

Forget HYS-* bookmarks that are no longer parents of HYH and HYWC-* bookmarks that are no longer children of the working-copy parent, then abandon their empty orphan commits.

hydra commit-descriptions <add|remove> #

Add or remove hydra's stock commit messages ("hydra head", "hydra conflict resolution", "hydra stack <name>", "hydra working copy <name>") on the HYH, HYCR, HYS-*, and HYWC-* commits. add fills blank descriptions. remove replaces stock messages with a blank (single-space) description, so your own descriptions on working copies or the HYCR chain are untouched. This is a one-time rewrite; it does not change the bookmarks.commit-descriptions setting.

hydra help #

Show help.

Safety checks #

Every command runs these checks before doing anything.

Repo health. hydra's queries treat an unresolvable revset as "nothing", because a missing bookmark is a normal answer. So hydra first confirms that jj can read the repo at all; otherwise a broken repo would look like "no hydra here". (For example, hydra status once printed an empty topology in a workspace whose working copy a sibling's hydra land had made stale.)

  • Stale working copy: hydra offers jj workspace update-stale. --force accepts without asking; with no terminal, hydra aborts.
  • Any other repo failure (no repo, unreadable working copy, ...) aborts with jj's own message.
  • The help commands report the failure as a warning and still print help.
  • Other query failures are reported as warnings when they happen.

Stale base (mutating commands only). If HYB is a strict ancestor of trunk(), the hydra hangs off stale history, and the command offers to move HYB to trunk(). Declining, or running without a terminal, aborts with a non-zero exit. Only the bookmark moves; nothing is rebased. To move commits already on the old base onto trunk, rebase them onto HYB yourself.

  • hydra help makes the same offer after printing help, but never fails.
  • hydra status and hydra doctor never prompt about the base; they report it as a finding.

Immutable descendants. If HYB has immutable descendants, the hydra sits below the repo's immutable boundary, usually because protected commits were built on it. Rebases would fail or rewrite protected history, so the command stops with an error. Only descendants reachable from local bookmarks (::bookmarks()) count: an untracked remote bookmark fetched on top of the base is immutable, but it is not part of the hydra and nothing rebases it.

--force skips the trunk prompt and downgrades both base checks to warnings.

Stack order #

jj log prints a merge's parents in reverse order: the last parent of HYH is the top row, and the first parent takes the leftmost column at the bottom. Timestamps only order siblings, so a busy stack never floats up on its own. hydra therefore keeps the parents of HYH sorted stalest first, which puts the stack with the freshest work at the top of jj log -r 'HYB::'.

Recency of a stack is the newest timestamp among its own commits and any work in its HYWC-<name> revision, so uncommitted work in a working copy counts as activity even if nothing has landed for weeks. Empty revisions (the HYS-* marker, an untouched working copy) do not count: hydra rewrites them every time it reparents HYH, so counting them would make every stack look equally busy. Ties are broken by author timestamp (which rewrites preserve, unlike committer timestamp), then by stack name, alphabetically from the top.

When it runs:

  • hydra new, hydra adopt, and hydra start pin their stack to the top: a stack you just created or switched to is active by intent.
  • hydra land, hydra push, and hydra remove re-sort by recency alone.
  • hydra sort runs it on demand; --no-sort disables the automatic pass.

If the order is already correct, the pass costs one revset query per stack and rewrites nothing. When it reorders, it rewrites HYH and the working copies on it. Change ids survive, so bookmarks stay put, but sibling workspaces will need jj workspace update-stale.

Output colors #

Values that belong to jj (change ids, commit titles, the (empty) marker) are rendered by jj log --color=always, so unique id prefixes look exactly as they do in a log. Bookmark names that hydra prints itself (for example a HYWC-* that does not exist yet) use the same escape sequences as jj's bookmarks label, learned once per run from a sample render, so your jj color settings carry over. Data values (revset arguments, names to compare) are never colored. Color is off entirely for pipes, dumb terminals, and when NO_COLOR is set.

Configuration #

hydra reads $XDG_CONFIG_HOME/hydra/hydra.toml (usually ~/.config/hydra/hydra.toml), or the file HYDRA_CONFIG points to. The file is optional; every key has a default. See hydra.example.toml.

[bookmarks]
prefix = "HY"                  # overall hydra prefix
base = "B"                     # base anchor            -> HYB
head = "H"                     # megamerge head         -> HYH
conflict-resolution = "CR"     # optional CR stack head -> HYCR
stack-head = "S"               # stack marker           -> HYS-<name>
stack-working-copy = "WC"      # stack working copy     -> HYWC-<name>
commit-descriptions = false    # stock commit messages on hydra commits

[push]
branch-prefix = "$USER/"       # default PR push bookmark from `hydra push`

[revsets]
tidyable = "tidyable"          # junk revs dropped before landing
trunk = "trunk()"              # upstream the base is checked against
init-base = "main"             # where `hydra init` creates the base
  • bookmarks.commit-descriptions: when on, hydra writes stock messages ("hydra head", "hydra stack <name>", "hydra working copy <name>") on the commits its bookmarks point at. When off (the default), those commits get a blank (single-space) description, so jj does not mark them "(no description set)".
  • revsets.tidyable: the default depends on a tidyable revset alias in your jj configuration. To avoid that dependency, inline the revset here.

Environment overrides #

Each key has an environment variable that takes precedence over the file:

Key Variable
bookmarks.prefix JJ_HYDRA_BOOKMARK_PREFIX
bookmarks.base JJ_HYDRA_BOOKMARK_BASE
bookmarks.head JJ_HYDRA_BOOKMARK_HEAD
bookmarks.conflict-resolution JJ_HYDRA_BOOKMARK_CONFLICT_RESOLUTION
bookmarks.stack-head JJ_HYDRA_BOOKMARK_STACK_HEAD
bookmarks.stack-working-copy JJ_HYDRA_BOOKMARK_STACK_WORKING_COPY
bookmarks.commit-descriptions JJ_HYDRA_BOOKMARK_COMMIT_DESCRIPTIONS
push.branch-prefix JJ_HYDRA_PUSH_BRANCH_PREFIX
revsets.tidyable JJ_HYDRA_REVSET_TIDYABLE
revsets.trunk JJ_HYDRA_REVSET_TRUNK
revsets.init-base JJ_HYDRA_REVSET_INIT_BASE

hydra status shows each value in effect and its source (default, file, or the env var name): the fastest way to check what an override did.

About this repo #

Commercial LLMs were heavily used in the creation of this code.

MIT licensed. I don't care, take this code or the concepts and do whatever you see fit. I'm not the dad of you.