Convert the quick setup chapter to AsciiDoc and start converting its
[metze/wireshark/wip.git] / tools / backport-change
1 #!/bin/bash
2 #
3 # backport-change - Given a master git commit or trunk SVN revision, creates
4 # a backport request in Gerrit.
5 #
6 # $Id$
7 #
8 # Copyright 2013 Gerald Combs
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation; either version 2
13 # of the License, or (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 #
24
25 # A more complete and likely more robust workflow can be found at
26 # http://www.mediawiki.org/wiki/Gerrit/Advanced_usage#Submitting_a_change_to_a_branch_for_review_.28.22backporting.22.29
27
28 export GIT_PAGER=""
29 TOOLS_DIR=`dirname $0`
30 COMMIT_MSG_HOOK=`git rev-parse --git-dir`/hooks/commit-msg
31
32 function exit_err
33 {
34     if [ -n "$*" ] ; then
35         echo -e "Error: $*\n"
36     fi
37     echo "Usage:"
38     echo `basename $0` "<svn revision|git sha>"
39     echo "SVN revisions MUST be prefixed with \"r\""
40     exit 1
41 }
42
43 # Check if pbcopy (or similar) is available...
44 if [ `builtin type -p pbcopy` ] ; then
45     PBCOPY="pbcopy"
46 fi
47
48 if [ `builtin type -p xsel` ]  ; then
49     PBCOPY="xsel --clipboard --input"
50 fi
51
52 if [ `builtin type -p putclip` ]  ; then
53     PBCOPY="putclip"
54 fi
55
56 #
57 # Make sure we have a destination
58 #
59
60 git ls-remote --heads gerrit &> /dev/null
61 if [ $? -ne 0 ] ; then
62     exit_err "Can't find a remote named \"gerrit\".\nAdd one with one of\n" \
63     "\n    git remote add gerrit https://your.username@code.wireshark.org/review/wireshark" \
64     "\n    git remote add gerrit ssh://your.username@code.wireshark.org:29418/wireshark"
65 fi
66
67 if [ ! -f "$COMMIT_MSG_HOOK" ] ; then
68     exit_err "Can't find a commit-msg hook.\nDownload it with one with one of\n" \
69     "\n    curl -Lo .git/hooks/commit-msg https://code.wireshark.org/review/tools/hooks/commit-msg" \
70     "\n    scp -p -P 29418 your.username@code.wireshark.org:hooks/commit-msg .git/hooks/"
71 fi
72
73
74 #
75 # Make sure our working directory is clean
76 #
77
78 git diff --quiet HEAD || exit_err "You have changes in your working directory. Please clean them up."
79
80 #
81 # Make sure we're up to date
82 #
83 git fetch gerrit
84
85 #
86 # Find our change
87 #
88
89 CHANGE="$1"
90 LONG_HASH=""
91 TOPIC=""
92
93 if [ -z "$CHANGE" ] ; then exit_err ; fi
94
95 if [[ $CHANGE = r* ]] ; then
96     # Subversion
97     SVN_REV=${CHANGE:1}
98     LONG_HASH=`git log gerrit/master --grep "svn path=/trunk/; revision=${SVN_REV}\$" -1 --format=format:%H`
99     if [ -z "$LONG_HASH" ] ; then exit_err "Can't find SVN revision $CHANGE" ; fi
100     TOPIC="/backport-$CHANGE"
101 else
102     # Git
103     LONG_HASH=`git rev-parse $CHANGE`
104     SHORT_HASH=`git rev-parse --short $CHANGE`
105     if [ -z "$LONG_HASH" ] ; then exit_err "Can't find git commit $CHANGE" ; fi
106     TOPIC="/backport-g$SHORT_HASH"
107 fi
108
109 #
110 # Make sure our upstream is valid
111 #
112
113 UPSTREAM_NAME=`git rev-parse --abbrev-ref --symbolic-full-name @{upstream}`
114 UPSTREAM_NAME=`basename $UPSTREAM_NAME`
115
116 if [ $? -ne 0 -o -z "$UPSTREAM_NAME" ] ; then exit_err "Can't find upstream branch." ; fi
117
118 if [[ $UPSTREAM_NAME != master-[0-9].[0-9]* ]] ; then
119     exit_err "Can't backport to remote branch $UPSTREAM_NAME."
120 fi
121
122 PUSH_CMD="git push gerrit HEAD:refs/for/${UPSTREAM_NAME}${TOPIC}"
123
124 #
125 # On with the show
126 #
127
128 echo "Backporting $LONG_HASH"
129 git cherry-pick -x --strategy=recursive -Xtheirs $LONG_HASH
130
131 if [ $? -eq 0 ] ; then
132     echo "Running diff"
133     git diff
134     
135     # XXX We might want to install Gerrit's commit-msg hook instead, e.g.
136     # scp -p -P 29418 <user>@code.wireshark.org:hooks/commit-msg .git/hooks/
137
138     echo
139     echo "Attempting push to gerrit HEAD:refs/for/$UPSTREAM_NAME"
140     $PUSH_CMD
141
142     if [ $? -ne 0 ] ; then
143         # XXX - We might want to check for the commit-msg hook and if it's
144         # present run git revert + git commit as described at
145         # http://code.google.com/p/gerrit/issues/detail?id=843#c4
146         cat <<FIN
147
148 Push failed. If the server is complaining about a missing change id
149 copy it and run
150
151   git commit --amend
152   # Insert the change id in your editor
153   $PUSH_CMD
154 FIN
155     fi
156 fi
157
158 #
159 # Editor modelines  -  http://www.wireshark.org/tools/modelines.html
160 #
161 # Local variables:
162 # c-basic-offset: 4
163 # tab-width: 8
164 # indent-tabs-mode: nil
165 # End:
166 #
167 # vi: set shiftwidth=4 tabstop=8 expandtab:
168 # :indentSize=4:tabSize=8:noTabs=true:
169 #