Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Side by Side Diff: git_auto_svn.py

Issue 611253003: Introduct git-auto-svn (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Better docs Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """Performs all git-svn setup steps necessary for 'git svn dcommit' to work.
7
8 Assumes that trunk of the svn remote maps to master of the git remote.
9
10 Example:
11 git clone https://chromium.googlesource.com/chromium/tools/depot_tools
12 cd depot_tools
13 git auto-svn
14 """
15
16 import argparse
17 import os
18 import sys
19 import urlparse
20
21 import subprocess2
22
23 from git_common import run as run_git
24 from git_common import run_stream as run_git_stream
25 from git_common import set_config, root, ROOT
26 from git_footers import parse_footers, get_unique, GIT_SVN_ID_PATTERN
27
28
29 SVN_EXE = ROOT+'\\svn.bat' if sys.platform.startswith('win') else 'svn'
30
31
32 def run_svn(*cmd, **kwargs):
33 """Runs an svn command.
34
35 Returns (stdout, stderr) as a pair of strings.
36
37 Raises subprocess2.CalledProcessError on nonzero return code.
38 """
39 kwargs.setdefault('stdin', subprocess2.PIPE)
40 kwargs.setdefault('stdout', subprocess2.PIPE)
41 kwargs.setdefault('stderr', subprocess2.PIPE)
42
43 cmd = (SVN_EXE,) + cmd
44 proc = subprocess2.Popen(cmd, **kwargs)
45 ret, err = proc.communicate()
46 retcode = proc.wait()
47 if retcode != 0:
48 raise subprocess2.CalledProcessError(retcode, cmd, os.getcwd(), ret, err)
49
50 return ret, err
51
52
53 def main(argv):
54 # No command line flags. Just use the parser to prevent people from trying
55 # to pass flags that don't do anything, and to provide 'usage'.
56 parser = argparse.ArgumentParser(
57 description='Automatically set up git-svn for a repo mirrored from svn.')
58 parser.parse_args(argv[1:])
59
60 upstream = root()
61 message = run_git('log', '-1', '--format=%B', upstream)
62 footers = parse_footers(message)
63 git_svn_id = get_unique(footers, 'git-svn-id')
64 match = GIT_SVN_ID_PATTERN.match(git_svn_id)
65 assert match, 'No valid git-svn-id footer found on %s.' % upstream
66 print 'Found git-svn-id footer %s on %s' % (match.group(1), upstream)
67
68 parsed_svn = urlparse.urlparse(match.group(1))
69 path_components = parsed_svn.path.split('/')
70 svn_repo = None
71 svn_path = None
72 for i in xrange(len(path_components)):
73 try:
74 maybe_repo = '%s://%s%s' % (
75 parsed_svn.scheme, parsed_svn.netloc, '/'.join(path_components[:i+1]))
76 print 'Checking ', maybe_repo
77 run_svn('info', maybe_repo)
78 svn_repo = maybe_repo
79 svn_path = '/'.join(path_components[i+1:])
80 break
81 except subprocess2.CalledProcessError:
82 continue
83 assert svn_repo is not None, 'Unable to find svn repo for %s' % match.group(1)
84 print 'Found upstream svn repo %s and path %s' % (svn_repo, svn_path)
85
86 prefix = upstream.rsplit('/')[0]
87 run_git('svn', 'init', '--prefix=%s' % prefix, '-T', svn_path, svn_repo)
88 set_config('svn-remote.svn.fetch',
89 '%s:refs/remotes/%s' % (svn_path, upstream))
90 print 'Configured metadata, running "git svn fetch". This may take some time.'
91 for line in run_git_stream('svn', 'fetch').xreadlines():
92 print line.strip()
93
94
95 if __name__ == '__main__':
96 sys.exit(main(sys.argv))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698