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.
ripgrep
, a fast, git-aware search tool. You can install it using homebrew and many other package managers, e.g.:brew install ripgrep
git
, available from the command line. (If you don't have it, see instructions installing git.)
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.
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!)
# 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!
- 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 thegit 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
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...)