Added ADT script
[akaros.git] / scripts / git / adt
1 #!/bin/bash
2 # Gan Shun Lim (ganshun@gmail.com)
3 # Copyright 2016 Google Inc
4
5 # Use local branch name as the default topic unless overridden
6 GERRIT_TOPIC=`git status | grep "# On branch .*" | cut -f 4 -d " "`
7
8 GERRIT_REMOTE=gerrit
9 GERRIT_PUSH_COMMAND="git push $GERRIT_REMOTE HEAD:refs/for/master"
10 GERRIT_REVIEWERS_LINE="%"
11
12 REVIEWERS=""
13 CC=""
14
15
16 if [ $# -ne 1 ] && [ $# -ne 3 ] && [ $# -ne 5 ] && [ $# -ne 7 ]
17 then
18         echo "Usage: adt mail [-m reviewer1,reviewer2...] [-cc cc1,cc2...] [-t topic]"
19         echo "Other functionalities are not yet supported"
20         exit -1
21 fi
22
23 if [ $1 != "mail" ]
24 then
25         echo "Only mail functionality is supported right now. Please do adt mail."
26         exit -1
27 fi
28
29 OLDIFS=$IFS
30 I=2
31 while [ $I -lt $# ]
32 do
33         if [ ${!I} = "-t" ]
34         then
35                 # Doesn't support multiple topics. not sure what gerrit will do.
36                 (( I++ ))
37                 GERRIT_TOPIC=${!I}
38                 (( I++ ))
39         elif [ ${!I} = "-m" ]
40         then
41                 (( I++ ))
42                 IFS=$IFS","
43                 for R in ${!I}
44                 do
45                         IFS=$OLDIFS
46                         if [ $GERRIT_REVIEWERS_LINE = "%" ]
47                         then
48                                 GERRIT_REVIEWERS_LINE="${GERRIT_REVIEWERS_LINE}r=$R"
49                         else
50                                 GERRIT_REVIEWERS_LINE="${GERRIT_REVIEWERS_LINE},r=$R"
51                         fi
52                 done
53                 (( I++ ))
54         elif [ ${!I} = "-cc" ]
55         then
56                 (( I++ ))
57                 IFS=$IFS","
58                 for R in ${!I}
59                 do
60                         IFS=$OLDIFS
61                         if [ $GERRIT_REVIEWERS_LINE = "%" ]
62                         then
63                                 GERRIT_REVIEWERS_LINE="${GERRIT_REVIEWERS_LINE}cc=$R"
64                         else
65                                 GERRIT_REVIEWERS_LINE="${GERRIT_REVIEWERS_LINE},cc=$R"
66                         fi
67                 done
68                 (( I++ ))
69         else
70                 echo "Unsupported flag ${!I}."
71                 echo "Usage: adt mail [-m reviewer1,reviewer2...] [-cc cc1,cc2...] [-t topic]"
72                 exit -1
73         fi
74 done
75
76 if [ $GERRIT_TOPIC != "" ]
77 then
78         # Append topic
79         GERRIT_PUSH_COMMAND="$GERRIT_PUSH_COMMAND/$GERRIT_TOPIC"
80 fi
81
82 if [ $GERRIT_REVIEWERS_LINE != "%" ]
83 then
84         eval "$GERRIT_PUSH_COMMAND$GERRIT_REVIEWERS_LINE"
85 else
86         eval $GERRIT_PUSH_COMMAND
87 fi
88
89 if [ "$?" -ne 0 ]
90 then
91         echo "Error occurred during git push. Please make sure that your remote for gerrit exists, and that GERRIT_REMOTE in this script is pointing to the correct remote."
92         exit -1
93 fi