Skip to content

Commit 565d7bd

Browse files
Add find subcommand
Find authors and co-authors in git log
1 parent 66589cd commit 565d7bd

File tree

3 files changed

+78
-9
lines changed

3 files changed

+78
-9
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ You must use an email address associated with the co-author's GitHub account.
6767

6868
## Usage
6969

70+
This command expects you to commit using the commit template. If you run `git commit` with the message option `-m/--message` then the commit template is not used and any Co-authored-by trailers in the commit template won't be commited.
71+
7072
Pair with Ann:
7173

7274
```bash
@@ -96,7 +98,13 @@ aa 'Ann Author <[email protected]>'
9698
bb 'Bob Book <[email protected]>'
9799
```
98100

99-
This command expects you to commit using the commit template. If you run `git commit` with the message option `-m/--message` then the commit template is not used and any Co-authored-by trailers in the commit template won't be commited.
101+
Find authors in git log:
102+
103+
```bash
104+
$ git co-author find
105+
Ann Author <[email protected]>
106+
107+
```
100108

101109
## Test
102110

git-co-author

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ set -euo pipefail
55
readonly TRAILER_TOKEN="Co-authored-by"
66
readonly USAGE="usage: git co-author Show co-authors in commit template
77
or: git co-author <initials>... Update co-authors in commit template
8+
or: git co-author authors List authors in git config
89
or: git co-author clear Remove all co-authors from commit template
9-
or: git co-author authors List authors in git config"
10+
or: git co-author find Find authors in git log"
1011

1112
main() {
1213
if [[ $# -eq 0 ]]
@@ -23,11 +24,14 @@ main() {
2324
done
2425

2526
case "$1" in
27+
authors)
28+
list_authors
29+
;;
2630
clear)
2731
clear_trailers
2832
;;
29-
authors)
30-
list_authors
33+
find)
34+
find_authors
3135
;;
3236
esac
3337

@@ -56,6 +60,14 @@ list_authors() {
5660
exit 0
5761
}
5862

63+
find_authors() {
64+
{
65+
git log --pretty="%an <%ae>";
66+
git log --pretty="%(trailers:key=Co-authored-by,valueonly,only)" | awk NF;
67+
} | sort | uniq
68+
exit 0
69+
}
70+
5971
update_trailers() {
6072
ensure_template_file
6173
must_have_initials "$@"

test/git-co-author.bats

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ setup() {
99

1010
mkdir -p "$test_dir"
1111
cd "$test_dir" || exit 1
12+
rm -rf .git/
1213

13-
if ! git status &> /dev/null
14-
then
15-
git init
16-
fi
17-
14+
git init
1815
git config --local commit.template "$template_file"
16+
git config --local user.name 'Ann Author'
17+
git config --local user.email '[email protected]'
1918
git config --local co-authors.aa 'Ann Author <[email protected]>'
2019
git config --local co-authors.bb 'Bob Book <[email protected]>'
2120
git config --local --unset co-authors.ab || true
@@ -210,6 +209,56 @@ bb-2 'Bobby Book <[email protected]>'" ]
210209
[ "${lines[0]}" = "No authors in config." ]
211210
}
212211

212+
@test "find sumcommand prints errors when not in a git repository" {
213+
rm -rf .git/
214+
215+
run git co-author find
216+
[ $status -eq 128 ]
217+
[[ "${lines[0]}" =~ ^fatal:.* ]]
218+
}
219+
220+
@test "find subcommand prints error when there are no commits" {
221+
run git co-author find
222+
[ $status -eq 128 ]
223+
[[ "${lines[0]}" =~ ^fatal:.* ]]
224+
}
225+
226+
@test "find subcommand prints authors in git log" {
227+
git commit --allow-empty -m'Test'
228+
git commit --allow-empty -m'Test' --author="Bob Book <[email protected]>"
229+
230+
run git co-author find
231+
[ $status -eq 0 ]
232+
[ "$output" = "Ann Author <[email protected]>
233+
Bob Book <[email protected]>" ]
234+
}
235+
236+
@test "find subcommand prints co-authors in git log" {
237+
git commit --allow-empty -m'Test
238+
239+
Co-authored-by: Bob Book <[email protected]>'
240+
git commit --allow-empty -m'Test
241+
242+
Co-authored-by: Anna Book <[email protected]>'
243+
244+
run git co-author find
245+
[ $status -eq 0 ]
246+
[ "$output" = "Ann Author <[email protected]>
247+
Anna Book <[email protected]>
248+
Bob Book <[email protected]>" ]
249+
}
250+
251+
@test "find subcommand does not print other trailers in git log" {
252+
git commit --allow-empty -m"Test
253+
254+
Some-token: some value
255+
Another-token: another value"
256+
257+
run git co-author find
258+
[ $status -eq 0 ]
259+
[ "$output" = "Ann Author <[email protected]>" ]
260+
}
261+
213262
@test "adds a co-author" {
214263
run git-co-author aa
215264
[ $status -eq 0 ]

0 commit comments

Comments
 (0)