Automatic commit message template for Jujutsu

Commit messages in nixpkgs are often very predictable. Here is a way to automatically generate (parts of the) commit message for package changes.

First put this in your PATH as gen-commit-message:

#!/usr/bin/env bash
# requires: ripgrep, moreutils, jujutsu, vim (or other editor)
rm /tmp/commit_msg 2> /dev/null
for file in $(cat $1 | rg --only-matching -r '$1' "M (.+)"); do
  name=$(echo "$file" | rg --only-matching -r '$1' '([^/]+)/(package|default).nix')
  echo -n "$name: " >> /tmp/commit_msg
  jj diff --git -- "$file" | rg --only-matching -r '$1' 'version.*=.*"([^"]+)"' | xargs -L 2 sh -c 'if [[ ${#0} -ne 0 && ${#1} -ne 0 ]]; then echo $0 "->" $1; else echo ""; fi' >> /tmp/commit_msg
done
cat /tmp/commit_msg $1 | sponge $1
exec vim -c '1 | startinsert!' $1

Then you can execute JJ_EDITOR=gen-commit-message jj split <file> and the "package: " prefix of the commit message will be there already. If the version is updated, “old -> new” is added with the right values.

5 Likes