| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Update third_party/WebKit using git. | 6 """Update third_party/WebKit using git. |
| 7 | 7 |
| 8 Under the assumption third_party/WebKit is a clone of git.webkit.org, | 8 Under the assumption third_party/WebKit is a clone of git.webkit.org, |
| 9 we can use git commands to make it match the version requested by DEPS. | 9 we can use git commands to make it match the version requested by DEPS. |
| 10 | 10 |
| 11 See http://code.google.com/p/chromium/wiki/UsingWebKitGit for details on | 11 See http://code.google.com/p/chromium/wiki/UsingWebKitGit for details on |
| 12 how to use this. | 12 how to use this. |
| 13 """ | 13 """ |
| 14 | 14 |
| 15 import logging | 15 import logging |
| 16 import optparse | 16 import optparse |
| 17 import os | 17 import os |
| 18 import re | 18 import re |
| 19 import subprocess | 19 import subprocess |
| 20 import sys | 20 import sys |
| 21 | 21 |
| 22 |
| 22 def RunGit(command): | 23 def RunGit(command): |
| 23 """Run a git subcommand, returning its output.""" | 24 """Run a git subcommand, returning its output.""" |
| 24 # On Windows, use shell=True to get PATH interpretation. | 25 # On Windows, use shell=True to get PATH interpretation. |
| 25 command = ['git'] + command | 26 command = ['git'] + command |
| 26 logging.info(' '.join(command)) | 27 logging.info(' '.join(command)) |
| 27 shell = (os.name == 'nt') | 28 shell = (os.name == 'nt') |
| 28 proc = subprocess.Popen(command, shell=shell, stdout=subprocess.PIPE) | 29 proc = subprocess.Popen(command, shell=shell, stdout=subprocess.PIPE) |
| 29 out = proc.communicate()[0].strip() | 30 out = proc.communicate()[0].strip() |
| 30 logging.info('Returned "%s"' % out) | 31 logging.info('Returned "%s"' % out) |
| 31 return out | 32 return out |
| 32 | 33 |
| 34 |
| 33 def GetOverrideShortBranchName(): | 35 def GetOverrideShortBranchName(): |
| 34 """Returns the user-configured override branch name, if any.""" | 36 """Returns the user-configured override branch name, if any.""" |
| 35 override_config_name = 'chromium.sync-branch' | 37 override_config_name = 'chromium.sync-branch' |
| 36 return RunGit(['config', '--get', override_config_name]) | 38 return RunGit(['config', '--get', override_config_name]) |
| 37 | 39 |
| 40 |
| 38 def GetGClientBranchName(): | 41 def GetGClientBranchName(): |
| 39 """Returns the name of the magic branch that lets us know that DEPS is | 42 """Returns the name of the magic branch that lets us know that DEPS is |
| 40 managing the update cycle.""" | 43 managing the update cycle.""" |
| 41 # Is there an override branch specified? | 44 # Is there an override branch specified? |
| 42 override_branch_name = GetOverrideShortBranchName() | 45 override_branch_name = GetOverrideShortBranchName() |
| 43 if not override_branch_name: | 46 if not override_branch_name: |
| 44 return 'refs/heads/gclient' # No override, so return the default branch. | 47 return 'refs/heads/gclient' # No override, so return the default branch. |
| 45 | 48 |
| 46 # Verify that the branch from config exists. | 49 # Verify that the branch from config exists. |
| 47 ref_branch = 'refs/heads/' + override_branch_name | 50 ref_branch = 'refs/heads/' + override_branch_name |
| 48 current_head = RunGit(['show-ref', '--hash', ref_branch]) | 51 current_head = RunGit(['show-ref', '--hash', ref_branch]) |
| 49 if current_head: | 52 if current_head: |
| 50 return ref_branch | 53 return ref_branch |
| 51 | 54 |
| 52 # Inform the user about the problem and how to fix it. | 55 # Inform the user about the problem and how to fix it. |
| 53 print ("The specified override branch ('%s') doesn't appear to exist." % | 56 print ("The specified override branch ('%s') doesn't appear to exist." % |
| 54 override_branch_name) | 57 override_branch_name) |
| 55 print "Please fix your git config value '%s'." % overide_config_name | 58 print "Please fix your git config value '%s'." % overide_config_name |
| 56 sys.exit(1) | 59 sys.exit(1) |
| 57 | 60 |
| 61 |
| 58 def GetWebKitRev(): | 62 def GetWebKitRev(): |
| 59 """Extract the 'webkit_revision' variable out of DEPS.""" | 63 """Extract the 'webkit_revision' variable out of DEPS.""" |
| 60 locals = {'Var': lambda _: locals["vars"][_], | 64 locals = {'Var': lambda _: locals["vars"][_], |
| 61 'From': lambda *args: None} | 65 'From': lambda *args: None} |
| 62 execfile('DEPS', {}, locals) | 66 execfile('DEPS', {}, locals) |
| 63 return locals['vars']['webkit_revision'] | 67 return locals['vars']['webkit_revision'] |
| 64 | 68 |
| 69 |
| 65 def FindSVNRev(target_rev): | 70 def FindSVNRev(target_rev): |
| 66 """Map an SVN revision to a git hash. | 71 """Map an SVN revision to a git hash. |
| 67 Like 'git svn find-rev' but without the git-svn bits.""" | 72 Like 'git svn find-rev' but without the git-svn bits.""" |
| 68 | 73 |
| 69 # We iterate through the commit log looking for "git-svn-id" lines, | 74 # We iterate through the commit log looking for "git-svn-id" lines, |
| 70 # which contain the SVN revision of that commit. We can stop once | 75 # which contain the SVN revision of that commit. We can stop once |
| 71 # we've found our target (or hit a revision number lower than what | 76 # we've found our target (or hit a revision number lower than what |
| 72 # we're looking for, indicating not found). | 77 # we're looking for, indicating not found). |
| 73 | 78 |
| 74 target_rev = int(target_rev) | 79 target_rev = int(target_rev) |
| (...skipping 24 matching lines...) Expand all Loading... |
| 99 print ("WARNING: r%d not found, so using next nearest earlier r%d" % | 104 print ("WARNING: r%d not found, so using next nearest earlier r%d" % |
| 100 (target_rev, rev)) | 105 (target_rev, rev)) |
| 101 return commit | 106 return commit |
| 102 else: | 107 else: |
| 103 saw_later = True | 108 saw_later = True |
| 104 | 109 |
| 105 print "Error: reached end of log without finding commit info." | 110 print "Error: reached end of log without finding commit info." |
| 106 print "Something has likely gone horribly wrong." | 111 print "Something has likely gone horribly wrong." |
| 107 return None | 112 return None |
| 108 | 113 |
| 114 |
| 109 def GetRemote(): | 115 def GetRemote(): |
| 110 branch = GetOverrideShortBranchName() | 116 branch = GetOverrideShortBranchName() |
| 111 if not branch: | 117 if not branch: |
| 112 branch = 'gclient' | 118 branch = 'gclient' |
| 113 | 119 |
| 114 remote = RunGit(['config', '--get', 'branch.' + branch + '.remote']) | 120 remote = RunGit(['config', '--get', 'branch.' + branch + '.remote']) |
| 115 if remote: | 121 if remote: |
| 116 return remote | 122 return remote |
| 117 return 'origin' | 123 return 'origin' |
| 118 | 124 |
| 125 |
| 119 def UpdateGClientBranch(webkit_rev, magic_gclient_branch): | 126 def UpdateGClientBranch(webkit_rev, magic_gclient_branch): |
| 120 """Update the magic gclient branch to point at |webkit_rev|. | 127 """Update the magic gclient branch to point at |webkit_rev|. |
| 121 | 128 |
| 122 Returns: true if the branch didn't need changes.""" | 129 Returns: true if the branch didn't need changes.""" |
| 123 target = FindSVNRev(webkit_rev) | 130 target = FindSVNRev(webkit_rev) |
| 124 if not target: | 131 if not target: |
| 125 print "r%s not available; fetching." % webkit_rev | 132 print "r%s not available; fetching." % webkit_rev |
| 126 subprocess.check_call(['git', 'fetch', GetRemote()], | 133 subprocess.check_call(['git', 'fetch', GetRemote()], |
| 127 shell=(os.name == 'nt')) | 134 shell=(os.name == 'nt')) |
| 128 target = FindSVNRev(webkit_rev) | 135 target = FindSVNRev(webkit_rev) |
| 129 if not target: | 136 if not target: |
| 130 print "ERROR: Couldn't map r%s to a git revision." % webkit_rev | 137 print "ERROR: Couldn't map r%s to a git revision." % webkit_rev |
| 131 sys.exit(1) | 138 sys.exit(1) |
| 132 | 139 |
| 133 current = RunGit(['show-ref', '--hash', magic_gclient_branch]) | 140 current = RunGit(['show-ref', '--hash', magic_gclient_branch]) |
| 134 if current == target: | 141 if current == target: |
| 135 return False # No change necessary. | 142 return False # No change necessary. |
| 136 | 143 |
| 137 subprocess.check_call(['git', 'update-ref', '-m', 'gclient sync', | 144 subprocess.check_call(['git', 'update-ref', '-m', 'gclient sync', |
| 138 magic_gclient_branch, target], | 145 magic_gclient_branch, target], |
| 139 shell=(os.name == 'nt')) | 146 shell=(os.name == 'nt')) |
| 140 return True | 147 return True |
| 141 | 148 |
| 149 |
| 142 def UpdateCurrentCheckoutIfAppropriate(magic_gclient_branch): | 150 def UpdateCurrentCheckoutIfAppropriate(magic_gclient_branch): |
| 143 """Reset the current gclient branch if that's what we have checked out.""" | 151 """Reset the current gclient branch if that's what we have checked out.""" |
| 144 branch = RunGit(['symbolic-ref', '-q', 'HEAD']) | 152 branch = RunGit(['symbolic-ref', '-q', 'HEAD']) |
| 145 if branch != magic_gclient_branch: | 153 if branch != magic_gclient_branch: |
| 146 print "We have now updated the 'gclient' branch, but third_party/WebKit" | 154 print "We have now updated the 'gclient' branch, but third_party/WebKit" |
| 147 print "has some other branch ('%s') checked out." % branch | 155 print "has some other branch ('%s') checked out." % branch |
| 148 print "Run 'git checkout gclient' under third_party/WebKit if you want" | 156 print "Run 'git checkout gclient' under third_party/WebKit if you want" |
| 149 print "to switch it to the version requested by DEPS." | 157 print "to switch it to the version requested by DEPS." |
| 150 return 1 | 158 return 1 |
| 151 | 159 |
| 152 if subprocess.call(['git', 'diff-index', '--exit-code', '--shortstat', | 160 if subprocess.call(['git', 'diff-index', '--exit-code', '--shortstat', |
| 153 'HEAD'], shell=(os.name == 'nt')): | 161 'HEAD'], shell=(os.name == 'nt')): |
| 154 print "Resetting tree state to new revision." | 162 print "Resetting tree state to new revision." |
| 155 subprocess.check_call(['git', 'reset', '--hard'], shell=(os.name == 'nt')) | 163 subprocess.check_call(['git', 'reset', '--hard'], shell=(os.name == 'nt')) |
| 156 | 164 |
| 165 |
| 157 def main(): | 166 def main(): |
| 158 parser = optparse.OptionParser() | 167 parser = optparse.OptionParser() |
| 159 parser.add_option('-v', '--verbose', action='store_true') | 168 parser.add_option('-v', '--verbose', action='store_true') |
| 160 options, args = parser.parse_args() | 169 options, args = parser.parse_args() |
| 161 if options.verbose: | 170 if options.verbose: |
| 162 logging.basicConfig(level=logging.INFO) | 171 logging.basicConfig(level=logging.INFO) |
| 163 if not os.path.exists('third_party/WebKit/.git'): | 172 if not os.path.exists('third_party/WebKit/.git'): |
| 164 if os.path.exists('third_party/WebKit'): | 173 if os.path.exists('third_party/WebKit'): |
| 165 print "ERROR: third_party/WebKit appears to not be under git control." | 174 print "ERROR: third_party/WebKit appears to not be under git control." |
| 166 else: | 175 else: |
| 167 print "ERROR: third_party/WebKit could not be found." | 176 print "ERROR: third_party/WebKit could not be found." |
| 168 print "Did you run this script from the right directory?" | 177 print "Did you run this script from the right directory?" |
| 169 | 178 |
| 170 print "See http://code.google.com/p/chromium/wiki/UsingWebKitGit for" | 179 print "See http://code.google.com/p/chromium/wiki/UsingWebKitGit for" |
| 171 print "setup instructions." | 180 print "setup instructions." |
| 172 return 1 | 181 return 1 |
| 173 | 182 |
| 174 webkit_rev = GetWebKitRev() | 183 webkit_rev = GetWebKitRev() |
| 175 print 'Desired revision: r%s.' % webkit_rev | 184 print 'Desired revision: r%s.' % webkit_rev |
| 176 os.chdir('third_party/WebKit') | 185 os.chdir('third_party/WebKit') |
| 177 magic_gclient_branch = GetGClientBranchName() | 186 magic_gclient_branch = GetGClientBranchName() |
| 178 changed = UpdateGClientBranch(webkit_rev, magic_gclient_branch) | 187 changed = UpdateGClientBranch(webkit_rev, magic_gclient_branch) |
| 179 if changed: | 188 if changed: |
| 180 return UpdateCurrentCheckoutIfAppropriate(magic_gclient_branch) | 189 return UpdateCurrentCheckoutIfAppropriate(magic_gclient_branch) |
| 181 else: | 190 else: |
| 182 print "Already on correct revision." | 191 print "Already on correct revision." |
| 183 return 0 | 192 return 0 |
| 184 | 193 |
| 194 |
| 185 if __name__ == '__main__': | 195 if __name__ == '__main__': |
| 186 sys.exit(main()) | 196 sys.exit(main()) |
| OLD | NEW |