Daniel D. Beck

Better generated branch names with jj

The jj version control system generates branch names that leave something to be desired. Here’s my alternative.

I’ve been using jj (Jujutsu), a Git-compatible version control system recently. I’m pretty happy with it. Today I’m happy with its configurability.

For a number of reasons, jj expects and encourages the use of anonymous branches. But if you push to a Git repository, then the anonymous branch needs a name (a “bookmark” in jj, a “branch” in Git). The jj command-line interface provides a shorthand to push a given change, generating a name automatically. For example, jj git push --change xyz pushes the revision with the ID xyz to a Git branch named push-xyz.

As a default, it makes sense to emphasize the change ID, since the jj CLI shows and uses those IDs often. But suppose later on I’m looking at the list of branches on the repository on the GitHub website. What actual work does push-xyz represent? I cannot and will not remember.

I changed my configuration to fix this. I wrote a new template alias, slugify(), and changed the git_push_bookmark template to use it:

[template-aliases]
"slugify(str)" = '''
  truncate_end(
    65,
    str.first_line()
      .replace(regex:'[^[[:alnum:]].]', '-')
      .replace(regex:'-{2,}', '-')
      .replace(regex:'\.{2,}', '.')
      .replace(regex:'(^-+|-+$)', '')
      .lower()
  )
'''

[templates]
git_push_bookmark = 'slugify(description) ++ "/" ++ change_id.short()'

Now, if I run jj git push --change ozkspkuyzpwu, jj generates a short slug-like name from the change’s description (the commit message, if you’re coming from Git). In this case, it generates add-note-about-jj-bookmark-templates/ozkspkuyzpwu. This is more readable while retaining the link back to the revision IDs that show up in the jj CLI.

If I were to push to a repo shared with others, then I would change it put my branches into a namespace for myself:

[templates]
git_push_bookmark = '"ddbeck/" ++ slugify(description) ++ "/" ++ change_id.short()'

One thing I didn’t do is make sure that the branch names are safe for Git. Git has some awfully complicated rules to determine whether a branch name is valid. I assume that I’ll get an error if my template ever generates an invalid branch name, at which point I’ll have to create a bookmark manually.

If you liked this, then subscribe to get updates on making documentation, tech writing, and docs as code. When you sign up, you’ll also get The 30-Minute Information Rescue, a short guide to interviewing subject matter experts when there’s no time to spare.

    Subscribe for periodic articles and offers. I won’t sell, rent, or give away your personal information. Unsubscribe at any time. Read my privacy policy for details.

    Don’t want more email? Subscribe to my RSS feed instead.