OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 """Performs all git-svn setup steps necessary for 'git svn dcommit' to work. | 6 """Performs all git-svn setup steps necessary for 'git svn dcommit' to work. |
7 | 7 |
8 Assumes that trunk of the svn remote maps to master of the git remote. | 8 Assumes that trunk of the svn remote maps to master of the git remote. |
9 | 9 |
10 Example: | 10 Example: |
11 git clone https://chromium.googlesource.com/chromium/tools/depot_tools | 11 git clone https://chromium.googlesource.com/chromium/tools/depot_tools |
12 cd depot_tools | 12 cd depot_tools |
13 git auto-svn | 13 git auto-svn |
14 """ | 14 """ |
15 | 15 |
16 import argparse | 16 import argparse |
17 import os | 17 import os |
18 import sys | 18 import sys |
19 import urlparse | 19 import urlparse |
20 | 20 |
21 import subprocess2 | 21 import subprocess2 |
22 | 22 |
23 from git_common import run as run_git | 23 from git_common import run as run_git |
24 from git_common import run_stream_with_retcode as run_git_stream_with_retcode | 24 from git_common import run_stream_with_retcode as run_git_stream_with_retcode |
25 from git_common import set_config, root, ROOT | 25 from git_common import set_config, root, ROOT, current_branch |
| 26 from git_common import upstream as get_upstream |
26 from git_footers import get_footer_svn_id | 27 from git_footers import get_footer_svn_id |
27 | 28 |
28 | 29 |
29 SVN_EXE = ROOT+'\\svn.bat' if sys.platform.startswith('win') else 'svn' | 30 SVN_EXE = ROOT+'\\svn.bat' if sys.platform.startswith('win') else 'svn' |
30 | 31 |
31 | 32 |
32 def run_svn(*cmd, **kwargs): | 33 def run_svn(*cmd, **kwargs): |
33 """Runs an svn command. | 34 """Runs an svn command. |
34 | 35 |
35 Returns (stdout, stderr) as a pair of strings. | 36 Returns (stdout, stderr) as a pair of strings. |
(...skipping 14 matching lines...) Expand all Loading... |
50 return ret, err | 51 return ret, err |
51 | 52 |
52 | 53 |
53 def main(argv): | 54 def main(argv): |
54 # No command line flags. Just use the parser to prevent people from trying | 55 # 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 # to pass flags that don't do anything, and to provide 'usage'. |
56 parser = argparse.ArgumentParser( | 57 parser = argparse.ArgumentParser( |
57 description='Automatically set up git-svn for a repo mirrored from svn.') | 58 description='Automatically set up git-svn for a repo mirrored from svn.') |
58 parser.parse_args(argv) | 59 parser.parse_args(argv) |
59 | 60 |
60 upstream = root() | 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): |
61 svn_id = get_footer_svn_id(upstream) | 79 svn_id = get_footer_svn_id(upstream) |
62 assert svn_id, 'No valid git-svn-id footer found on %s.' % upstream | 80 assert svn_id, 'No valid git-svn-id footer found on %s.' % upstream |
63 print 'Found git-svn-id footer %s on %s' % (svn_id, upstream) | 81 print 'Found git-svn-id footer %s on %s' % (svn_id, upstream) |
64 | 82 |
65 parsed_svn = urlparse.urlparse(svn_id) | 83 parsed_svn = urlparse.urlparse(svn_id) |
66 path_components = parsed_svn.path.split('/') | 84 path_components = parsed_svn.path.split('/') |
67 svn_repo = None | 85 svn_repo = None |
68 svn_path = None | 86 svn_path = None |
69 for i in xrange(len(path_components)): | 87 for i in xrange(len(path_components)): |
70 try: | 88 try: |
71 maybe_repo = '%s://%s%s' % ( | 89 maybe_repo = '%s://%s%s' % ( |
72 parsed_svn.scheme, parsed_svn.netloc, '/'.join(path_components[:i+1])) | 90 parsed_svn.scheme, parsed_svn.netloc, '/'.join(path_components[:i+1])) |
73 print 'Checking ', maybe_repo | 91 print 'Checking ', maybe_repo |
74 run_svn('info', maybe_repo) | 92 run_svn('info', maybe_repo) |
75 svn_repo = maybe_repo | 93 svn_repo = maybe_repo |
76 svn_path = '/'.join(path_components[i+1:]) | 94 svn_path = '/'.join(path_components[i+1:]) |
77 break | 95 break |
78 except subprocess2.CalledProcessError, e: | 96 except subprocess2.CalledProcessError, e: |
79 if 'E170001' in str(e): | 97 if 'E170001' in str(e): |
80 print 'Authentication failed:' | 98 print 'Authentication failed:' |
81 print e | 99 print e |
82 print ('Try running "svn ls %s" with the password' | 100 print ('Try running "svn ls %s" with the password' |
83 ' from https://chromium-access.appspot.com' % maybe_repo) | 101 ' from https://chromium-access.appspot.com' % maybe_repo) |
84 print | 102 print |
85 continue | 103 continue |
86 assert svn_repo is not None, 'Unable to find svn repo for %s' % svn_id | 104 assert svn_repo is not None, 'Unable to find svn repo for %s' % svn_id |
87 print 'Found upstream svn repo %s and path %s' % (svn_repo, svn_path) | 105 print 'Found upstream svn repo %s and path %s' % (svn_repo, svn_path) |
88 | 106 |
89 set_config('svn-remote.svn.url', svn_repo) | 107 run_git('config', '--local', '--replace-all', 'svn-remote.svn.url', svn_repo) |
90 set_config('svn-remote.svn.fetch', | 108 run_git('config', '--local', '--replace-all', 'svn-remote.svn.fetch', |
91 '%s:refs/remotes/%s' % (svn_path, upstream)) | 109 '%s:refs/remotes/%s' % (svn_path, upstream), |
| 110 'refs/remotes/%s$' % upstream) |
92 print 'Configured metadata, running "git svn fetch". This may take some time.' | 111 print 'Configured metadata, running "git svn fetch". This may take some time.' |
93 with run_git_stream_with_retcode('svn', 'fetch') as stdout: | 112 with run_git_stream_with_retcode('svn', 'fetch') as stdout: |
94 for line in stdout.xreadlines(): | 113 for line in stdout.xreadlines(): |
95 print line.strip() | 114 print line.strip() |
96 return 0 | |
97 | 115 |
98 | 116 |
99 if __name__ == '__main__': | 117 if __name__ == '__main__': |
100 try: | 118 try: |
101 sys.exit(main(sys.argv[1:])) | 119 sys.exit(main(sys.argv[1:])) |
102 except KeyboardInterrupt: | 120 except KeyboardInterrupt: |
103 sys.stderr.write('interrupted\n') | 121 sys.stderr.write('interrupted\n') |
104 sys.exit(1) | 122 sys.exit(1) |
OLD | NEW |