summaryrefslogtreecommitdiffstats
path: root/tools/sync-webkit-git.py
blob: 4b993575e28fcbfcb9f6834217ef38ebdcc362f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python
# Copyright (c) 2009 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Update third_party/WebKit using git.

Under the assumption third_party/WebKit is a clone of git.webkit.org,
we can use git commands to make it match the version requested by DEPS.

See http://code.google.com/p/chromium/wiki/UsingWebKitGit for details on
how to use this.
"""

import os
import re
import subprocess
import sys

# The name of the magic branch that lets us know that DEPS is managing
# the update cycle.
MAGIC_GCLIENT_BRANCH = 'refs/heads/gclient'

def RunGit(command):
  """Run a git subcommand, returning its output."""
  proc = subprocess.Popen(['git'] + command, stdout=subprocess.PIPE)
  return proc.communicate()[0].strip()

def GetWebKitRev():
  """Extract the 'webkit_revision' variable out of DEPS."""
  locals = {'Var': lambda _: ''}
  execfile('DEPS', {}, locals)
  return locals['vars']['webkit_revision']

def FindSVNRev(target_rev):
  """Map an SVN revision to a git hash.
  Like 'git svn find-rev' but without the git-svn bits."""

  # We iterate through the commit log looking for "git-svn-id" lines,
  # which contain the SVN revision of that commit.  We can stop once
  # we've found our target (or hit a revision number lower than what
  # we're looking for, indicating not found).

  target_rev = int(target_rev)

  # regexp matching the "commit" line from the log.
  commit_re = re.compile(r'^commit ([a-f\d]{40})$')
  # regexp matching the git-svn line from the log.
  git_svn_re = re.compile(r'^\s+git-svn-id: [^@]+@(\d+) ')

  log = subprocess.Popen(['git', 'log', '--no-color', '--first-parent',
                          '--pretty=medium', 'origin'],
                         stdout=subprocess.PIPE)
  # Track whether we saw a revision *later* than the one we're seeking.
  saw_later = False
  for line in log.stdout:
    match = commit_re.match(line)
    if match:
      commit = match.group(1)
      continue
    match = git_svn_re.match(line)
    if match:
      rev = int(match.group(1))
      if rev <= target_rev:
        log.stdout.close()  # Break pipe.
        if rev < target_rev:
          if not saw_later:
            return None  # Can't be sure whether this rev is ok.
          print ("WARNING: r%d not found, so using next nearest earlier r%d" %
                 (target_rev, rev))
        return commit
      else:
        saw_later = True

  print "Error: reached end of log without finding commit info."
  print "Something has likely gone horribly wrong."
  return None

def UpdateGClientBranch(webkit_rev):
  """Update the magic gclient branch to point at |webkit_rev|.

  Returns: true if the branch didn't need changes."""
  target = FindSVNRev(webkit_rev)
  if not target:
    print "r%s not available; fetching." % webkit_rev
    subprocess.check_call(['git', 'fetch'])
    target = FindSVNRev(webkit_rev)
  if not target:
    print "ERROR: Couldn't map r%s to a git revision." % webkit_rev
    sys.exit(1)

  current = RunGit(['show-ref', '--hash', MAGIC_GCLIENT_BRANCH])
  if current == target:
    return False  # No change necessary.

  subprocess.check_call(['git', 'update-ref', '-m', 'gclient sync',
                         MAGIC_GCLIENT_BRANCH, target])
  return True

def UpdateCurrentCheckoutIfAppropriate():
  """Reset the current gclient branch if that's what we have checked out."""
  branch = RunGit(['symbolic-ref', '-q', 'HEAD'])
  if branch != MAGIC_GCLIENT_BRANCH:
    print "We have now updated the 'gclient' branch, but third_party/WebKit"
    print "has some other branch ('%s') checked out." % branch
    print "Run 'git checkout gclient' under third_party/WebKit if you want"
    print "to switch it to the version requested by DEPS."
    return

  if subprocess.call(['git', 'diff-index', '--exit-code', '--shortstat',
                      'HEAD']):
    print "Resetting tree state to new revision."
    subprocess.check_call(['git', 'reset', '--hard'])

def main():
  if not os.path.exists('third_party/WebKit/.git'):
    if os.path.exists('third_party/WebKit'):
      print "ERROR: third_party/WebKit appears to not be under git control."
    else:
      print "ERROR: third_party/WebKit could not be found."
      print "Did you run this script from the right directory?"

    print "See http://code.google.com/p/chromium/wiki/UsingWebKitGit for"
    print "setup instructions."
    return

  webkit_rev = GetWebKitRev()
  print 'Desired revision: r%s.' % webkit_rev
  os.chdir('third_party/WebKit')
  changed = UpdateGClientBranch(webkit_rev)
  if changed:
    UpdateCurrentCheckoutIfAppropriate()
  else:
    print "Already on correct revision."

if __name__ == '__main__':
  main()