[email protected] | ec238db | 2014-06-02 20:39:42 | [diff] [blame] | 1 | #!/usr/bin/env bash |
[email protected] | 5a30792 | 2012-09-26 21:16:20 | [diff] [blame] | 2 | |
| 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] | 78d1134 | 2013-01-17 00:29:48 | [diff] [blame] | 7 | branch_name="" |
| 8 | checkout_branch=no |
| 9 | create_branch=no |
[email protected] | 1d23a42 | 2012-10-11 06:29:09 | [diff] [blame] | 10 | quiet=no |
[email protected] | cc9cbc8 | 2013-01-16 21:50:12 | [diff] [blame] | 11 | svn_lkgr= |
[email protected] | 1d23a42 | 2012-10-11 06:29:09 | [diff] [blame] | 12 | |
| 13 | while [ $# -gt 0 ]; do |
| 14 | case "$1" in |
[email protected] | 78d1134 | 2013-01-17 00:29:48 | [diff] [blame] | 15 | --checkout|--force-branch) |
| 16 | checkout_branch=yes |
| 17 | create_branch=yes |
| 18 | ;; |
[email protected] | f570936 | 2013-01-26 04:09:50 | [diff] [blame] | 19 | --closest) |
| 20 | use_closest=yes |
| 21 | ;; |
[email protected] | 78d1134 | 2013-01-17 00:29:48 | [diff] [blame] | 22 | --create) |
| 23 | create_branch=yes |
| 24 | ;; |
| 25 | -n|--name) |
| 26 | branch_name=$2 |
| 27 | create_branch=yes |
| 28 | shift |
[email protected] | 1d23a42 | 2012-10-11 06:29:09 | [diff] [blame] | 29 | ;; |
| 30 | -q|--quiet) |
| 31 | quiet=yes |
| 32 | ;; |
[email protected] | cc9cbc8 | 2013-01-16 21:50:12 | [diff] [blame] | 33 | -r|--revision) |
| 34 | svn_lkgr="$2" |
| 35 | shift |
| 36 | ;; |
[email protected] | 1d23a42 | 2012-10-11 06:29:09 | [diff] [blame] | 37 | *) |
| 38 | echo "Unknown option: $1" |
[email protected] | 78d1134 | 2013-01-17 00:29:48 | [diff] [blame] | 39 | echo "Usage:" |
| 40 | echo " --checkout Create a branch and check it out." |
| 41 | echo " --create Create a branch." |
[email protected] | f570936 | 2013-01-26 04:09:50 | [diff] [blame] | 42 | 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] | 78d1134 | 2013-01-17 00:29:48 | [diff] [blame] | 45 | 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] | 1d23a42 | 2012-10-11 06:29:09 | [diff] [blame] | 50 | ;; |
| 51 | esac |
| 52 | shift |
| 53 | done |
| 54 | |
[email protected] | cc9cbc8 | 2013-01-16 21:50:12 | [diff] [blame] | 55 | if [ -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 |
| 61 | fi |
| 62 | |
| 63 | if [ "${svn_lkgr:0:1}" = "r" ]; then |
| 64 | svn_lkgr="${svn_lkgr:1}" |
[email protected] | 5a30792 | 2012-09-26 21:16:20 | [diff] [blame] | 65 | fi |
| 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). |
| 69 | git svn info > /dev/null |
| 70 | if [ $? != 0 ]; then |
| 71 | cat <<EOF 1>&2 |
| 72 | Could not run a trivial git-svn command. You probably need to set up your |
| 73 | working directory for git-svn, by following these instructions: |
| 74 | |
| 75 | http://code.google.com/p/chromium/wiki/UsingNewGit#Initial_checkout |
| 76 | EOF |
| 77 | exit 1 |
| 78 | fi |
| 79 | |
| 80 | git_lkgr=`git svn find-rev r${svn_lkgr}` |
| 81 | if [ $? != 0 -o -z "$git_lkgr" ]; then |
| 82 | cat <<EOF 1>&2 |
| 83 | Could not map svn revision ${svn_lkgr} to a git commit. |
[email protected] | 78d1134 | 2013-01-17 00:29:48 | [diff] [blame] | 84 | You may need to 'git fetch' and try again. |
[email protected] | 5a30792 | 2012-09-26 21:16:20 | [diff] [blame] | 85 | EOF |
| 86 | exit 1 |
| 87 | fi |
| 88 | |
| 89 | set -o pipefail |
| 90 | closest_commit=`git rev-list --ancestry-path \ |
| 91 | --grep='SVN changes up to revision [0-9]*' \ |
| 92 | ${git_lkgr}..refs/remotes/origin/master | tail -1` |
| 93 | if [ $? != 0 -o -z "$closest_commit" ]; then |
[email protected] | cc9cbc8 | 2013-01-16 21:50:12 | [diff] [blame] | 94 | closest_commit= |
| 95 | closest_svn_commit= |
| 96 | else |
| 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] | 5a30792 | 2012-09-26 21:16:20 | [diff] [blame] | 101 | EOF |
[email protected] | cc9cbc8 | 2013-01-16 21:50:12 | [diff] [blame] | 102 | exit 1 |
| 103 | fi |
[email protected] | f570936 | 2013-01-26 04:09:50 | [diff] [blame] | 104 | closest_svn=`git svn find-rev ${closest_svn_commit}` |
| 105 | fi |
| 106 | |
| 107 | if [ "${use_closest}" = "yes" ]; then |
| 108 | svn_lkgr="${closest_svn}" |
| 109 | git_lkgr="${closest_svn_commit}" |
[email protected] | 5a30792 | 2012-09-26 21:16:20 | [diff] [blame] | 110 | fi |
| 111 | |
[email protected] | 78d1134 | 2013-01-17 00:29:48 | [diff] [blame] | 112 | # Determine lkgr_branch: |
| 113 | if [ "${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 |
| 124 | else |
| 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] | 1d23a42 | 2012-10-11 06:29:09 | [diff] [blame] | 129 | git rev-parse --verify -q "${lkgr_branch}" >/dev/null |
[email protected] | 78d1134 | 2013-01-17 00:29:48 | [diff] [blame] | 130 | 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 |
| 135 | fi |
[email protected] | 1d23a42 | 2012-10-11 06:29:09 | [diff] [blame] | 136 | |
[email protected] | 5a30792 | 2012-09-26 21:16:20 | [diff] [blame] | 137 | if [ "${closest_svn_commit}" = "${git_lkgr}" ]; then |
| 138 | echo "${closest_commit}" |
[email protected] | 78d1134 | 2013-01-17 00:29:48 | [diff] [blame] | 139 | 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] | 1d23a42 | 2012-10-11 06:29:09 | [diff] [blame] | 145 | fi |
[email protected] | 5a30792 | 2012-09-26 21:16:20 | [diff] [blame] | 146 | exit 0 |
[email protected] | 1d23a42 | 2012-10-11 06:29:09 | [diff] [blame] | 147 | elif [ "${quiet}" = "yes" ]; then |
| 148 | exit 1 |
[email protected] | 78d1134 | 2013-01-17 00:29:48 | [diff] [blame] | 149 | elif [ "${checkout_branch}" = "no" ]; then |
[email protected] | cc9cbc8 | 2013-01-16 21:50:12 | [diff] [blame] | 150 | echo "There is no master commit which corresponds exactly to svn revision ${svn_lkgr}." |
[email protected] | 78d1134 | 2013-01-17 00:29:48 | [diff] [blame] | 151 | echo "Call 'git lkgr --checkout' to create a branch with a commit to match ${svn_lkgr}." |
[email protected] | f570936 | 2013-01-26 04:09:50 | [diff] [blame] | 152 | 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] | 78d1134 | 2013-01-17 00:29:48 | [diff] [blame] | 156 | exit 0 |
[email protected] | 1d23a42 | 2012-10-11 06:29:09 | [diff] [blame] | 157 | fi |
| 158 | |
| 159 | current_head=`git branch | grep '^\*' | cut -c3-` |
| 160 | if [ "${current_head}" = "(no branch)" ]; then |
| 161 | current_head=`git rev-parse HEAD` |
| 162 | fi |
| 163 | |
| 164 | git checkout --detach "${git_lkgr}" && |
| 165 | python tools/deps2git/deps2git.py -d DEPS -o .DEPS.git -w .. && |
| 166 | git add .DEPS.git && |
| 167 | python tools/deps2git/deps2submodules.py .DEPS.git && |
| 168 | git commit -m "SVN changes up to revision $svn_lkgr" && |
[email protected] | 78d1134 | 2013-01-17 00:29:48 | [diff] [blame] | 169 | git branch ${force_branch} "${lkgr_branch}" HEAD |
[email protected] | 1d23a42 | 2012-10-11 06:29:09 | [diff] [blame] | 170 | |
| 171 | if [ $? != 0 ]; then |
| 172 | cat <<EOF |
| 173 | |
| 174 | -------------------------------------------------------------------------------- |
| 175 | Something went wrong! Restoring your previous state by checking out |
| 176 | $current_head |
| 177 | |
| 178 | Please file a bug report at http://new.crbug.com. |
| 179 | -------------------------------------------------------------------------------- |
| 180 | |
| 181 | EOF |
| 182 | git checkout --force $current_head |
[email protected] | 5a30792 | 2012-09-26 21:16:20 | [diff] [blame] | 183 | exit 1 |
| 184 | fi |
[email protected] | 1d23a42 | 2012-10-11 06:29:09 | [diff] [blame] | 185 | |
[email protected] | 78d1134 | 2013-01-17 00:29:48 | [diff] [blame] | 186 | git checkout "${lkgr_branch}" |
| 187 | |
[email protected] | 1d23a42 | 2012-10-11 06:29:09 | [diff] [blame] | 188 | cat <<EOF |
| 189 | |
| 190 | -------------------------------------------------------------------------------- |
| 191 | The new branch "$lkgr_branch" was branched from this commit: |
| 192 | |
| 193 | $git_lkgr |
| 194 | |
[email protected] | cc9cbc8 | 2013-01-16 21:50:12 | [diff] [blame] | 195 | ... which maps to the svn commit r${svn_lkgr}. The new branch |
[email protected] | 1d23a42 | 2012-10-11 06:29:09 | [diff] [blame] | 196 | has one additional commit, to bring .DEPS.git, .gitmodules, and the |
| 197 | invisible git submodule files up to date with DEPS. |
| 198 | |
| 199 | To 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 |
| 204 | your changes from my_new_branch, but *not* the .DEPS.git+.gitmodules+submodules |
| 205 | commit on $lkgr_branch. |
| 206 | -------------------------------------------------------------------------------- |
| 207 | |
| 208 | EOF |