atomaka.com/_posts/2023-04-15-give-credit-where-credit-s-due.md

76 lines
2.6 KiB
Markdown
Raw Normal View History

2023-04-16 11:48:01 -04:00
---
layout: post
title: Give credit where credit's due
date: 2023-04-15 09:17 -0400
---
I love pair programming. It is a great way to improve solutions, share context,
and continuously review code. But the passenger in a pairing session often ends
up being the unsung hero, not showing up in the commit history for their
invaluable contributions because they were not typing on the keyboard.
Commit logs are obviously not the authority on an individual's contributions to
a codebase, but I still like to give credit where credit is due. Fortunately,
using git's [trailers][1], Github allows you to [commit together][2]. Including
2023-04-16 12:25:14 -04:00
a `Co-authored-by:` citation with your pairing partner's name and email will
2023-04-16 11:48:01 -04:00
properly attribute them in Github's commit log.
I have used this to share credit for several years, but it is a bit painful.
Making sure to type your partner's name and email is a hassle. Any typo will
not credit appropriately and become permanantly engraved in the history of your
repository when you merge it to master!
So avoid the typos!
First, I created a simple git alias that will allow me to easily modify a commit
to add a trailer. In your global `.gitconfig` file, add
```
[alias]
add-trailer = commit --no-edit --amend --trailer
```
This can be used as part of a two step process:
1. Make your commit as you normally would
2023-04-16 12:25:14 -04:00
1. Use the alias to add a trailer
- `git add-trailer "Co-authored-by: Partner <a@example.com>"`
2023-04-16 11:48:01 -04:00
We can still have typos though. But if our pairing partner has worked on this
repository in the past, we can find their information in the git log. This is
still painful to do manually. [Fzf][3] can help us here though. We can pass our
log information into fzf and then pass our selection on to our newly created git
alias:
```
git log --pretty="%an <%ae>" \
| sort -u \
| fzf \
| xargs -I "{}" git add-trailer "Co-authored-by: {}"
```
Breaking this down:
2023-04-16 12:25:14 -04:00
- `git log --pretty="%an <%ae>"`
- List the author of every commit in our repositry and format it appropriately
2023-04-16 11:48:01 -04:00
for attribution
2023-04-16 12:25:14 -04:00
- `sort -u`
- Make the list sorted and unique
- `fzf`
- Prompt us to make a selection from the list
- `xargs -I "{}" git add-trailer "Co-authored-by: {}"`
- Take the result from our prompt and pass it into our new git alias
2023-04-16 11:48:01 -04:00
And when everything is in place, attribution is a breeze!
![Example git acknowledge](/assets/gack-example.gif)
And in the spirit of attribution, thanks to last week's pairing partner:
2023-04-16 12:25:14 -04:00
Encouraged-by: nichol alexander \<nichol.alexander@gmail.com>
2023-04-16 11:48:01 -04:00
[1]: https://git-scm.com/docs/git-interpret-trailers
[2]: https://github.blog/2018-01-29-commit-together-with-co-authors/
[3]: https://github.com/junegunn/fzf