OLD | NEW |
| (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_with_retcode as run_git_stream_with_retcode | |
25 from git_common import set_config, root, ROOT, current_branch | |
26 from git_common import upstream as get_upstream | |
27 from git_footers import get_footer_svn_id | |
28 | |
29 | |
30 SVN_EXE = ROOT+'\\svn.bat' if sys.platform.startswith('win') else 'svn' | |
31 | |
32 | |
33 def run_svn(*cmd, **kwargs): | |
34 """Runs an svn command. | |
35 | |
36 Returns (stdout, stderr) as a pair of strings. | |
37 | |
38 Raises subprocess2.CalledProcessError on nonzero return code. | |
39 """ | |
40 kwargs.setdefault('stdin', subprocess2.PIPE) | |
41 kwargs.setdefault('stdout', subprocess2.PIPE) | |
42 kwargs.setdefault('stderr', subprocess2.PIPE) | |
43 | |
44 cmd = (SVN_EXE,) + cmd | |
45 proc = subprocess2.Popen(cmd, **kwargs) | |
46 ret, err = proc.communicate() | |
47 retcode = proc.wait() | |
48 if retcode != 0: | |
49 raise subprocess2.CalledProcessError(retcode, cmd, os.getcwd(), ret, err) | |
50 | |
51 return ret, err | |
52 | |
53 | |
54 def main(argv): | |
55 # No command line flags. Just use the parser to prevent people from trying | |
56 # to pass flags that don't do anything, and to provide 'usage'. | |
57 parser = argparse.ArgumentParser( | |
58 description='Automatically set up git-svn for a repo mirrored from svn.') | |
59 parser.parse_args(argv) | |
60 | |
61 upstreams = [] | |
62 # Always configure the upstream trunk. | |
63 upstreams.append(root()) | |
64 # Optionally configure whatever upstream branch might be currently checked | |
65 # out. This is needed for work on svn-based branches, otherwise git-svn gets | |
66 # very confused and tries to relate branch commits back to trunk, making a big | |
67 # mess of the codereview patches, and generating all kinds of spurious errors | |
68 # about the repo being in some sort of bad state. | |
69 curr_upstream = get_upstream(current_branch()) | |
70 # There will be no upstream if the checkout is in detached HEAD. | |
71 if curr_upstream: | |
72 upstreams.append(curr_upstream) | |
73 for upstream in upstreams: | |
74 config_svn(upstream) | |
75 return 0 | |
76 | |
77 | |
78 def config_svn(upstream): | |
79 svn_id = get_footer_svn_id(upstream) | |
80 assert svn_id, 'No valid git-svn-id footer found on %s.' % upstream | |
81 print 'Found git-svn-id footer %s on %s' % (svn_id, upstream) | |
82 | |
83 parsed_svn = urlparse.urlparse(svn_id) | |
84 path_components = parsed_svn.path.split('/') | |
85 svn_repo = None | |
86 svn_path = None | |
87 for i in xrange(len(path_components)): | |
88 try: | |
89 maybe_repo = '%s://%s%s' % ( | |
90 parsed_svn.scheme, parsed_svn.netloc, '/'.join(path_components[:i+1])) | |
91 print 'Checking ', maybe_repo | |
92 run_svn('info', maybe_repo) | |
93 svn_repo = maybe_repo | |
94 svn_path = '/'.join(path_components[i+1:]) | |
95 break | |
96 except subprocess2.CalledProcessError, e: | |
97 if 'E170001' in str(e): | |
98 print 'Authentication failed:' | |
99 print e | |
100 print ('Try running "svn ls %s" with the password' | |
101 ' from https://chromium-access.appspot.com' % maybe_repo) | |
102 print | |
103 continue | |
104 assert svn_repo is not None, 'Unable to find svn repo for %s' % svn_id | |
105 print 'Found upstream svn repo %s and path %s' % (svn_repo, svn_path) | |
106 | |
107 run_git('config', '--local', '--replace-all', 'svn-remote.svn.url', svn_repo) | |
108 run_git('config', '--local', '--replace-all', 'svn-remote.svn.fetch', | |
109 '%s:refs/remotes/%s' % (svn_path, upstream), | |
110 'refs/remotes/%s$' % upstream) | |
111 print 'Configured metadata, running "git svn fetch". This may take some time.' | |
112 with run_git_stream_with_retcode('svn', 'fetch') as stdout: | |
113 for line in stdout.xreadlines(): | |
114 print line.strip() | |
115 | |
116 | |
117 if __name__ == '__main__': | |
118 try: | |
119 sys.exit(main(sys.argv[1:])) | |
120 except KeyboardInterrupt: | |
121 sys.stderr.write('interrupted\n') | |
122 sys.exit(1) | |
OLD | NEW |