Skip to content

Commit 239c026

Browse files
authored
chore(git): keep Co-Authored-By trailers out of commits made with the beads hooks (#45)
Clones of this repo set `core.hooksPath` to `.beads/hooks` so the tracker's hooks run. That setting turns off the machine-wide git hooks, and one of those removed `Co-Authored-By` trailers from commit messages. With the tracker hooks on, nothing removed them any more. This adds one `commit-msg` hook beside the tracker's wrappers. It deletes lines that start with `Co-Authored-By:` and the blank lines left at the end. A mention inside a sentence stays. The hook cannot lose a message. A message without such a line is never touched. Otherwise the cleaned text is written to a fresh temporary file, checked against a second run, and renamed over the message only when every step worked. On any failure the message stays as it was and the commit stops. Tested before it went anywhere: 780 message, shell, awk and locale combinations, a really full disk, signals arriving mid-run, and 28 real git scenarios. Without the hook the trailer stays.
1 parent 424ade9 commit 239c026

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

‎.beads/hooks/commit-msg‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env sh
2+
# Every commit in this repository has one author. Clones that point
3+
# core.hooksPath at .beads/hooks skip the machine-wide git hooks, so this
4+
# hook does the one job those had here: it removes Co-Authored-By trailer
5+
# lines from the commit message. A mention inside a sentence is kept.
6+
#
7+
# A message without such a line is never touched. Otherwise the cleaned
8+
# text goes to a fresh file from mktemp, is compared with a second run,
9+
# and is renamed over the message only when every step worked; on any
10+
# failure the message stays as it was and the commit stops.
11+
msg="$1"
12+
[ -f "$msg" ] || exit 0
13+
case $msg in -*) msg="./$msg" ;; esac
14+
found=0
15+
LC_ALL=C grep -q -i '^[[:space:]]*co-authored-by:' "$msg" || found=$?
16+
case $found in
17+
0) ;;
18+
1) exit 0 ;;
19+
*) echo "commit-msg: cannot read $msg" >&2; exit 1 ;;
20+
esac
21+
clean() {
22+
LC_ALL=C awk 'tolower($0) !~ /^[ \t\r\f\v]*co-authored-by:/ { line[++n] = $0 }
23+
END { while (n > 0 && line[n] ~ /^[ \t\r\f\v]*$/) n--
24+
for (i = 1; i <= n; i++) print line[i] }' < "$msg"
25+
}
26+
tmp=$(mktemp "$msg.XXXXXX") || { echo "commit-msg: cannot create a temporary file beside $msg" >&2; exit 1; }
27+
trap 'rm -f "$tmp"' EXIT
28+
trap 'exit 1' HUP INT TERM
29+
clean > "$tmp" && clean | cmp -s - "$tmp" || { echo "commit-msg: cannot rewrite $msg" >&2; exit 1; }
30+
trap '' HUP INT TERM
31+
mv -f "$tmp" "$msg" || { echo "commit-msg: cannot replace $msg" >&2; exit 1; }
32+
exit 0

0 commit comments

Comments
 (0)