3 # backport-change - Given a master git commit or trunk SVN revision, creates
4 # a backport request in Gerrit.
8 # Copyright 2013 Gerald Combs
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.
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.
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.
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
29 TOOLS_DIR=`dirname $0`
30 COMMIT_MSG_HOOK=`git rev-parse --git-dir`/hooks/commit-msg
38 echo `basename $0` "<svn revision|git sha>"
39 echo "SVN revisions MUST be prefixed with \"r\""
43 # Check if pbcopy (or similar) is available...
44 if [ `builtin type -p pbcopy` ] ; then
48 if [ `builtin type -p xsel` ] ; then
49 PBCOPY="xsel --clipboard --input"
52 if [ `builtin type -p putclip` ] ; then
57 # Make sure we have a destination
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"
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/"
75 # Make sure our working directory is clean
78 git diff --quiet HEAD || exit_err "You have changes in your working directory. Please clean them up."
81 # Make sure we're up to date
93 if [ -z "$CHANGE" ] ; then exit_err ; fi
95 if [[ $CHANGE = r* ]] ; then
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"
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"
110 # Make sure our upstream is valid
113 UPSTREAM_NAME=`git rev-parse --abbrev-ref --symbolic-full-name @{upstream}`
114 UPSTREAM_NAME=`basename $UPSTREAM_NAME`
116 if [ $? -ne 0 -o -z "$UPSTREAM_NAME" ] ; then exit_err "Can't find upstream branch." ; fi
118 if [[ $UPSTREAM_NAME != master-[0-9].[0-9]* ]] ; then
119 exit_err "Can't backport to remote branch $UPSTREAM_NAME."
122 PUSH_CMD="git push gerrit HEAD:refs/for/${UPSTREAM_NAME}${TOPIC}"
128 echo "Backporting $LONG_HASH"
129 git cherry-pick -x --strategy=recursive -Xtheirs $LONG_HASH
131 if [ $? -eq 0 ] ; then
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/
139 echo "Attempting push to gerrit HEAD:refs/for/$UPSTREAM_NAME"
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
148 Push failed. If the server is complaining about a missing change id
152 # Insert the change id in your editor
159 # Editor modelines - http://www.wireshark.org/tools/modelines.html
164 # indent-tabs-mode: nil
167 # vi: set shiftwidth=4 tabstop=8 expandtab:
168 # :indentSize=4:tabSize=8:noTabs=true: