]> git.ozlabs.org Git - next-scripts/blob - check_commits
check_commits: check for unexpected files in commits
[next-scripts] / check_commits
1 #!/bin/bash
2
3 if [ "$#" -lt 1 ]; then
4         printf 'Usage: %s <commit range>\n' "$0" 1>&2
5         exit 1
6 fi
7
8 commits=$(git rev-list --no-merges "$@")
9 if [ -z "$commits" ]; then
10         printf 'No commits\n'
11         exit 0
12 fi
13
14 declare -a author_missing committer_missing
15
16 print_commits()
17 {
18         if [ "$#" -eq 1 ]; then
19                 return
20         fi
21
22         local t="$1"
23
24         shift
25
26         s=
27         is='is'
28         its='its'
29         if [ "$#" -gt 1 ]; then
30                 s='s'
31                 is='are'
32                 its='their'
33         fi
34         printf 'Commit%s\n\n' "$s"
35         git log --no-walk --pretty='format:  %h ("%s")' "$@"
36         printf '\n%s missing a Signed-off-by from %s %s%s.\n\n' \
37                 "$is" "$its" "$t" "$s"
38 }
39
40 check_unexpected_files()
41 {
42         local files
43
44         readarray files < <(git diff-tree -r --diff-filter=A --name-only --no-commit-id "$1" '*.rej' '*.orig')
45         if [ "${#files[@]}" -eq 0 ]; then
46                 return
47         fi
48
49         s=
50         this='this'
51         if [ "${#files[@]}" -gt 1 ]; then
52                 s='s'
53                 this='these'
54         fi
55
56         printf 'Commit\n\n'
57         git log --no-walk --pretty='format:  %h ("%s")' "$1"
58         printf '\nadded %s unexpected file%s:\n\n' "$this" "$s"
59         printf '  %s\n' "${files[@]}"
60 }
61
62 for c in $commits; do
63         ae=$(git log -1 --format='<%ae>%n<%aE>%n %an %n %aN ' "$c" | sort -u)
64         ce=$(git log -1 --format='<%ce>%n<%cE>%n %cn %n %cN ' "$c" | sort -u)
65         sob=$(git log -1 --format='%b' "$c" |
66                 sed -En 's/^\s*Signed-off-by:?\s*/ /ip')
67
68         if ! grep -i -F -q "$ae" <<<"$sob"; then
69                 author_missing+=("$c")
70         fi
71         if ! grep -i -F -q "$ce" <<<"$sob"; then
72                 committer_missing+=("$c")
73         fi
74
75         check_unexpected_files "$c"
76 done
77
78 print_commits 'author' "${author_missing[@]}"
79 print_commits 'committer' "${committer_missing[@]}"
80
81 exec gitk "$@"