]>
Commit | Line | Data |
---|---|---|
60718ca3 EY |
1 | #!/bin/sh |
2 | # | |
3 | # A hook script that blocks committer and author names that are | |
4 | # root (or a configured blacklisted name). | |
5 | # XXX: should allow only applying this to certain branches. | |
6 | # | |
7 | # Called by "git receive-pack" with arguments: refname sha1-old sha1-new | |
8 | # | |
9 | # Config | |
10 | # ------ | |
11 | # hooks.noroot.branches | |
12 | # List of branches for which commits by root should be disallowed. | |
13 | # hooks.noroot.match | |
14 | # XXX: figure out format | |
15 | # | |
16 | ||
17 | set -e | |
18 | ||
19 | # --- Command line | |
20 | refname="$1" | |
21 | oldrev="$2" | |
22 | newrev="$3" | |
23 | ||
24 | # --- Safety check | |
25 | if [ -z "$GIT_DIR" ]; then | |
26 | echo "Don't run this script from the command line." >&2 | |
27 | echo " (if you want, you could supply GIT_DIR then run" >&2 | |
28 | echo " $0 <ref> <oldrev> <newrev>)" >&2 | |
29 | exit 1 | |
30 | fi | |
31 | ||
32 | if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then | |
33 | echo "Usage: $0 <ref> <oldrev> <newrev>" >&2 | |
34 | exit 1 | |
35 | fi | |
36 | ||
37 | # --- Config | |
38 | branches=$(git config --get hooks.noroot.branches || echo "") | |
39 | # XXX: hooks.noroot.match | |
40 | ||
41 | # --- Check types | |
42 | # if $newrev is 0000...0000, it's a commit to delete a ref. | |
43 | zero="0000000000000000000000000000000000000000" | |
44 | if [ "$newrev" = "$zero" ]; then | |
45 | newrev_type=delete | |
46 | else | |
47 | newrev_type=$(git cat-file -t $newrev) | |
48 | fi | |
49 | ||
50 | case "$refname","$newrev_type" in | |
51 | refs/heads/*,commit) | |
52 | git log --pretty="format:%h \"%an\" \"%cn\"%n" "$oldrev".."$newrev" | \ | |
53 | while read hash an cn; do | |
54 | if [ "$an" = "\"root\"" -o "$cn" = "\"root\"" ]; then | |
55 | echo "*** Committing as root not allowed in this repository," >&2 | |
56 | echo "*** Please fix your GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL," | |
57 | echo "*** GIT_COMMITTER_NAME and GIT_COMMITTER_EMAIL." | |
58 | echo "*** Offending commit was $hash." | |
59 | exit 1 | |
60 | fi | |
61 | done | |
62 | ;; | |
63 | refs/remotes/*,commit) | |
64 | # tracking branch | |
65 | ;; | |
66 | refs/tags/*,*) | |
67 | # we could track tags, but we've decided they're not | |
68 | # interesting | |
69 | ;; | |
70 | *,delete) | |
71 | # not interesting | |
72 | ;; | |
73 | *) | |
74 | # Anything else (is there anything else?) | |
75 | echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2 | |
76 | exit 1 | |
77 | ;; | |
78 | esac | |
79 | ||
80 | # --- Finished | |
81 | exit 0 |