Skip to content

Instantly share code, notes, and snippets.

@bryophyta
Last active December 5, 2023 10:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bryophyta/09e893397335e419572b587321d9bb9d to your computer and use it in GitHub Desktop.
Save bryophyta/09e893397335e419572b587321d9bb9d to your computer and use it in GitHub Desktop.
git commit coauthor search

Generate co-author messages for git repos

A zsh function which searches through the contributors to the repo in the current working directory, and generates 'co-authored by' messages for them, that can then be copied into your commit messages.

Searches through names and email addresses that have been used in commit messages in the current repo. The -S flag means that searches are 'smart case' by default: case insensitive, unless upper case letters are included in the search string, in which instance it will switch to case sensitive search. Other options can be found in the ripgrep user guide.

Setup

Dependencies

Installing this script

With these dependencies installed, all you need to do is make the shell function below available in your environment. If you paste the code below into your .zshrc file and refresh your terminal then it should be available. I've only tested this with zsh so far; the code might need to be adapted for other shells.

Usage

When you're inside a git repo, just type coauth [string] to generate co-author statements for contributors to the repo whose names or email addresses match the string. These can then be copied and pasted into your commit messages.

nb. The email addresses used will be taken from the addresses used in previous commit messages; these are subject to change over time, so it's worth checking with your co-author about which one to use!)

The code

# find co-authors in current repo
coauth() {
  # take first argument as param; defaults to searching for all
  search_regex=${1-'.*'};
  
  # list all names and emails from previous commits
  # search this list for the search term
  # remove numbers and spacing from the start, and add 'coauthor' message
  git shortlog --summary --numbered --email --all \
    | rg "$search_regex" \
    | rg -S "^\s+[0-9]+\s+" -r "Co-authored-by: "
}

This is just meant to be a quick utility script, but suggestions for improvements are always welcome!

Other projects

  • Hat tip to cwebster2, who created a coauthor extension for neovim. In my first version of this shell script I was using a simpler git log command to get the initial input, but the git shortlog command which I found in cwebster2's code saves a lot of awkward processing of the output!
  • Andrew Nowak built on the script above to create a git alias with live fuzzy search over coauthors in the command line! You can see the code (and lots of other cool things) in his config repo

Dependencies

This script uses ripgrep (the rg command) for quick search-and-replace functionality. It could be converted to use a combination of grep and sed, but I'm having trouble with sed on my computer at the moment so haven't been able to test it properly.

I'm sure it could be condensed to a single rg command, but for some reason I ran into trouble using ripgrep capture groups effectively here. (Any suggestions would be very welcome!) Given the speed of ripgrep, and the fact that the second pass is only over the shortlist of matching names, it doesn't seem likely to cause much of a bottleneck.

(I'm pretty sure that the only noticeable lag for larger repos is due to the git shortlog command; perhaps something could be done with caching here, if performance is an issue...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment