gerrit: add a dry-run mode to rfc script
[obnox/glusterfs.git] / rfc.sh
1 #!/bin/bash -e
2
3
4 branch="master";
5
6
7 function set_hooks_commit_msg()
8 {
9     f=".git/hooks/commit-msg";
10     u="http://review.gluster.com/tools/hooks/commit-msg";
11
12     if [ -x "$f" ]; then
13         return;
14     fi
15
16     curl -o $f $u || wget -O $f $u;
17
18     chmod +x .git/hooks/commit-msg;
19 }
20
21
22 function is_num()
23 {
24     local num;
25
26     num="$1";
27
28     [ -z "$(echo $num | sed -e 's/[0-9]//g')" ]
29 }
30
31
32 function rebase_changes()
33 {
34     git fetch;
35
36     GIT_EDITOR=$0 git rebase -i origin/$branch;
37 }
38
39
40 function editor_mode()
41 {
42     if [ $(basename "$1") = "git-rebase-todo" ]; then
43         sed -i 's/^pick /reword /g' "$1";
44         return;
45     fi
46
47     if [ $(basename "$1") = "COMMIT_EDITMSG" ]; then
48         if grep -qi '^BUG: ' $1; then
49             return;
50         fi
51         while true; do
52             echo Commit: "\"$(head -n 1 $1)\""
53             echo -n "Enter Bug ID: "
54             read bug
55             if [ -z "$bug" ]; then
56                 return;
57             fi
58             if ! is_num "$bug"; then
59                 echo "Invalid Bug ID ($bug)!!!";
60                 continue;
61             fi
62
63             sed -i "s/^\(Change-Id: .*\)$/\1\nBUG: $bug/g" $1;
64             return;
65         done
66     fi
67
68     cat <<EOF
69 $0 - editor_mode called on unrecognized file $1 with content:
70 $(cat $1)
71 EOF
72     return 1;
73 }
74
75
76 function assert_diverge()
77 {
78     git diff origin/$branch..HEAD | grep -q .;
79 }
80
81
82 function main()
83 {
84     if [ -e "$1" ]; then
85         editor_mode "$@";
86         return;
87     fi
88
89     set_hooks_commit_msg;
90
91     rebase_changes;
92
93     assert_diverge;
94
95     bug=$(git show --format='%b' | grep -i '^BUG: ' | awk '{print $2}');
96
97     if [ "$DRY_RUN" = 1 ]; then
98         drier='echo -e Please use the following command to send your commits to review:\n\n'
99     else
100         drier=
101     fi
102
103     if [ -z "$bug" ]; then
104         $drier git push origin HEAD:refs/for/$branch/rfc;
105     else
106         $drier git push origin HEAD:refs/for/$branch/bug-$bug;
107     fi
108 }
109
110 main "$@"