.githooks/commit-msg 913 B raw
1
#!/bin/sh
2
# Rejects a commit whose message breaks the format described in CONTRIBUTING.
3
# Adds a Change-Id when the footer has none.
4
# The rules live in scripts/check-commit-message.
5
6
top=$(git rev-parse --show-toplevel) || exit 1
7
8
if ! "$top/scripts/check-commit-message" "$1"; then
9
    echo "commit-msg: see WRITING COMMITS in CONTRIBUTING" >&2
10
    echo "commit-msg: the message is kept in $1; retry with" \
11
        "'git commit -e -F $1'" >&2
12
    exit 1
13
fi
14
15
trailers=$(git interpret-trailers --parse "$1") || exit 1
16
if printf '%s\n' "$trailers" | grep -qi '^Change-Id:'; then
17
    exit 0
18
fi
19
20
change_id=$(od -An -N20 -tx1 /dev/urandom) || exit 1
21
change_id=$(printf '%s' "$change_id" | tr -d '[:space:]') || exit 1
22
if [ "${#change_id}" -ne 40 ]; then
23
    echo "commit-msg: cannot read 20 random bytes from /dev/urandom" >&2
24
    exit 1
25
fi
26
27
git interpret-trailers --in-place --trailer "Change-Id: I$change_id" "$1"