Better generated branch names with jj - ddbeck.com
The jj version control system generates branch names that leave something to be desired.<br>Here’s my alternative.
I’ve been using jj (Jujutsu), a Git-compatible version control system recently.<br>I’m pretty happy with it.<br>Today I’m happy with its configurability.
For a number of reasons, jj expects and encourages the use of anonymous branches.<br>But if you push to a Git repository, then the anonymous branch needs a name (a “bookmark” in jj, a “branch” in Git).<br>The jj command-line interface provides a shorthand to push a given change, generating a name automatically.<br>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.<br>But suppose later on I’m looking at the list of branches on the repository on the GitHub website.<br>What actual work does push-xyz represent?<br>I cannot and will not remember.
I changed my configuration to fix this.<br>I wrote a new template alias, slugify(), and changed the git_push_bookmark template to use it:
[template-aliases]<br>"slugify(str)" = '''<br>truncate_end(<br>65,<br>str.first_line()<br>.replace(regex:'[^[[:alnum:]].]', '-')<br>.replace(regex:'-{2,}', '-')<br>.replace(regex:'\.{2,}', '.')<br>.replace(regex:'(^-+|-+$)', '')<br>.lower()<br>'''
[templates]<br>git_push_bookmark = 'slugify(description) ++ "/" ++ change_id.short()'<br>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).<br>In this case, it generates add-note-about-jj-bookmark-templates/ozkspkuyzpwu.<br>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]<br>git_push_bookmark = '"ddbeck/" ++ slugify(description) ++ "/" ++ change_id.short()'<br>One thing I didn’t do is make sure that the branch names are safe for Git.<br>Git has some awfully complicated rules to determine whether a branch name is valid.<br>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,<br>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<br>to spare.
Subscribe<br>Subscribe for periodic articles and offers. I won’t sell, rent, or give<br>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.