blob: c6a222ddbafbcdd6e3037cfb6f95dbe299398654 [file] [log] [blame]
[email protected]ec238db2014-06-02 20:39:421#!/usr/bin/env bash
[email protected]5a307922012-09-26 21:16:202
3# Copyright (c) 2012 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
[email protected]78d11342013-01-17 00:29:487branch_name=""
8checkout_branch=no
9create_branch=no
[email protected]1d23a422012-10-11 06:29:0910quiet=no
[email protected]cc9cbc82013-01-16 21:50:1211svn_lkgr=
[email protected]1d23a422012-10-11 06:29:0912
13while [ $# -gt 0 ]; do
14 case "$1" in
[email protected]78d11342013-01-17 00:29:4815 --checkout|--force-branch)
16 checkout_branch=yes
17 create_branch=yes
18 ;;
[email protected]f5709362013-01-26 04:09:5019 --closest)
20 use_closest=yes
21 ;;
[email protected]78d11342013-01-17 00:29:4822 --create)
23 create_branch=yes
24 ;;
25 -n|--name)
26 branch_name=$2
27 create_branch=yes
28 shift
[email protected]1d23a422012-10-11 06:29:0929 ;;
30 -q|--quiet)
31 quiet=yes
32 ;;
[email protected]cc9cbc82013-01-16 21:50:1233 -r|--revision)
34 svn_lkgr="$2"
35 shift
36 ;;
[email protected]1d23a422012-10-11 06:29:0937 *)
38 echo "Unknown option: $1"
[email protected]78d11342013-01-17 00:29:4839 echo "Usage:"
40 echo " --checkout Create a branch and check it out."
41 echo " --create Create a branch."
[email protected]f5709362013-01-26 04:09:5042 echo " --closest Use closest git commit to the target svn revision."
43 echo " Otherwise --checkout may be required to create"
44 echo " a git commit for a specific svn revision."
[email protected]78d11342013-01-17 00:29:4845 echo " -n, --name <name> Specify the name of branch to create or reset."
46 echo " This will force the branch using 'git branch -f '."
47 echo " -q, --quiet Quiet."
48 echo " -r, --revision <r> Svn revision number use instead of server provided lkgr."
49 exit 1
[email protected]1d23a422012-10-11 06:29:0950 ;;
51 esac
52 shift
53done
54
[email protected]cc9cbc82013-01-16 21:50:1255if [ -z "$svn_lkgr" ]; then
56 svn_lkgr=`curl -s http://chromium-status.appspot.com/lkgr`
57 if [ $? != 0 -o -z "$svn_lkgr" ]; then
58 echo 'Could not get svn lkgr from chromium-status.appspot.com/lkgr'
59 exit 1
60 fi
61fi
62
63if [ "${svn_lkgr:0:1}" = "r" ]; then
64 svn_lkgr="${svn_lkgr:1}"
[email protected]5a307922012-09-26 21:16:2065fi
66
67# Run a trivial git-svn command to force it to update the revision cache
68# (which causes spew that might otherwise confuse the next command).
69git svn info > /dev/null
70if [ $? != 0 ]; then
71 cat <<EOF 1>&2
72Could not run a trivial git-svn command. You probably need to set up your
73working directory for git-svn, by following these instructions:
74
75http://code.google.com/p/chromium/wiki/UsingNewGit#Initial_checkout
76EOF
77 exit 1
78fi
79
80git_lkgr=`git svn find-rev r${svn_lkgr}`
81if [ $? != 0 -o -z "$git_lkgr" ]; then
82 cat <<EOF 1>&2
83Could not map svn revision ${svn_lkgr} to a git commit.
[email protected]78d11342013-01-17 00:29:4884You may need to 'git fetch' and try again.
[email protected]5a307922012-09-26 21:16:2085EOF
86 exit 1
87fi
88
89set -o pipefail
90closest_commit=`git rev-list --ancestry-path \
91 --grep='SVN changes up to revision [0-9]*' \
92 ${git_lkgr}..refs/remotes/origin/master | tail -1`
93if [ $? != 0 -o -z "$closest_commit" ]; then
[email protected]cc9cbc82013-01-16 21:50:1294 closest_commit=
95 closest_svn_commit=
96else
97 closest_svn_commit=`git rev-list -n 1 ${closest_commit}^1`
98 if [ $? != 0 -o -z "$closest_svn_commit" ]; then
99 cat <<EOF 1>&2
100 I am thoroughly confused. Please file a bug report at http://new.crbug.com.
[email protected]5a307922012-09-26 21:16:20101EOF
[email protected]cc9cbc82013-01-16 21:50:12102 exit 1
103 fi
[email protected]f5709362013-01-26 04:09:50104 closest_svn=`git svn find-rev ${closest_svn_commit}`
105fi
106
107if [ "${use_closest}" = "yes" ]; then
108 svn_lkgr="${closest_svn}"
109 git_lkgr="${closest_svn_commit}"
[email protected]5a307922012-09-26 21:16:20110fi
111
[email protected]78d11342013-01-17 00:29:48112# Determine lkgr_branch:
113if [ "${branch_name}" != "" ]; then
114 # Use the provided name for the branch.
115 lkgr_branch="${branch_name}"
116
117 # If the branch already exists, force the update to it.
118 git rev-parse --verify -q "${branch_name}" >/dev/null
119 if [ $? -eq 0 ]; then
120 old_branch_value=`git rev-parse "${branch_name}"`
121 echo "Will update branch ${lkgr_branch}, it previously was at ${old_branch_value}."
122 force_branch="--force"
123 fi
124else
125 # Pick a name for the new branch. Use `git rev-parse` to make sure the branch
126 # doesn't already exist; if it does, iterate an integer suffix to uniquify it.
127 lkgr_branch="lkgr_r${svn_lkgr}"
128 digit=1
[email protected]1d23a422012-10-11 06:29:09129 git rev-parse --verify -q "${lkgr_branch}" >/dev/null
[email protected]78d11342013-01-17 00:29:48130 while [ $? -eq 0 ]; do
131 lkgr_branch="lkgr_r${svn_lkgr}_${digit}"
132 digit=`expr $digit + 1`
133 git rev-parse --verify -q "${lkgr_branch}" >/dev/null
134 done
135fi
[email protected]1d23a422012-10-11 06:29:09136
[email protected]5a307922012-09-26 21:16:20137if [ "${closest_svn_commit}" = "${git_lkgr}" ]; then
138 echo "${closest_commit}"
[email protected]78d11342013-01-17 00:29:48139 if [ "$create_branch" = "yes" ]; then
140 echo "Creating branch ${lkgr_branch}"
141 git branch ${force_branch} "${lkgr_branch}" "${closest_commit}" || exit 1
142 fi
143 if [ "$checkout_branch" = "yes" ]; then
144 git checkout "${lkgr_branch}"
[email protected]1d23a422012-10-11 06:29:09145 fi
[email protected]5a307922012-09-26 21:16:20146 exit 0
[email protected]1d23a422012-10-11 06:29:09147elif [ "${quiet}" = "yes" ]; then
148 exit 1
[email protected]78d11342013-01-17 00:29:48149elif [ "${checkout_branch}" = "no" ]; then
[email protected]cc9cbc82013-01-16 21:50:12150 echo "There is no master commit which corresponds exactly to svn revision ${svn_lkgr}."
[email protected]78d11342013-01-17 00:29:48151 echo "Call 'git lkgr --checkout' to create a branch with a commit to match ${svn_lkgr}."
[email protected]f5709362013-01-26 04:09:50152 if [ -n "$closest_commit" ]; then
153 echo "The closest commit is r${closest_svn}, ${closest_commit}."
154 echo "Use the --closest option to use the closest instead of the target revision."
155 fi
[email protected]78d11342013-01-17 00:29:48156 exit 0
[email protected]1d23a422012-10-11 06:29:09157fi
158
159current_head=`git branch | grep '^\*' | cut -c3-`
160if [ "${current_head}" = "(no branch)" ]; then
161 current_head=`git rev-parse HEAD`
162fi
163
164git checkout --detach "${git_lkgr}" &&
165python tools/deps2git/deps2git.py -d DEPS -o .DEPS.git -w .. &&
166git add .DEPS.git &&
167python tools/deps2git/deps2submodules.py .DEPS.git &&
168git commit -m "SVN changes up to revision $svn_lkgr" &&
[email protected]78d11342013-01-17 00:29:48169git branch ${force_branch} "${lkgr_branch}" HEAD
[email protected]1d23a422012-10-11 06:29:09170
171if [ $? != 0 ]; then
172 cat <<EOF
173
174--------------------------------------------------------------------------------
175Something went wrong! Restoring your previous state by checking out
176$current_head
177
178Please file a bug report at http://new.crbug.com.
179--------------------------------------------------------------------------------
180
181EOF
182 git checkout --force $current_head
[email protected]5a307922012-09-26 21:16:20183 exit 1
184fi
[email protected]1d23a422012-10-11 06:29:09185
[email protected]78d11342013-01-17 00:29:48186git checkout "${lkgr_branch}"
187
[email protected]1d23a422012-10-11 06:29:09188cat <<EOF
189
190--------------------------------------------------------------------------------
191The new branch "$lkgr_branch" was branched from this commit:
192
193$git_lkgr
194
[email protected]cc9cbc82013-01-16 21:50:12195... which maps to the svn commit r${svn_lkgr}. The new branch
[email protected]1d23a422012-10-11 06:29:09196has one additional commit, to bring .DEPS.git, .gitmodules, and the
197invisible git submodule files up to date with DEPS.
198
199To create a working branch, do this:
200
201\$ git branch --track my_new_branch $lkgr_branch
202
203'git-cl upload' will do the right thing, i.e., it will cherry-pick all
204your changes from my_new_branch, but *not* the .DEPS.git+.gitmodules+submodules
205commit on $lkgr_branch.
206--------------------------------------------------------------------------------
207
208EOF