Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # ======- git-llvm - LLVM Git Help Integration ---------*- python -*--========# |
| 4 | # |
| 5 | # The LLVM Compiler Infrastructure |
| 6 | # |
| 7 | # This file is distributed under the University of Illinois Open Source |
| 8 | # License. See LICENSE.TXT for details. |
| 9 | # |
| 10 | # ==------------------------------------------------------------------------==# |
| 11 | |
| 12 | """ |
| 13 | git-llvm integration |
| 14 | ==================== |
| 15 | |
| 16 | This file provides integration for git. |
| 17 | """ |
| 18 | |
| 19 | from __future__ import print_function |
| 20 | import argparse |
| 21 | import collections |
| 22 | import contextlib |
| 23 | import errno |
| 24 | import os |
| 25 | import re |
| 26 | import subprocess |
| 27 | import sys |
| 28 | import tempfile |
| 29 | import time |
| 30 | assert sys.version_info >= (2, 7) |
| 31 | |
James Y Knight | 8536211 | 2018-11-16 23:59:23 +0000 | [diff] [blame] | 32 | try: |
| 33 | dict.iteritems |
| 34 | except AttributeError: |
| 35 | # Python 3 |
| 36 | def iteritems(d): |
| 37 | return iter(d.items()) |
| 38 | else: |
| 39 | # Python 2 |
| 40 | def iteritems(d): |
| 41 | return d.iteritems() |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 42 | |
| 43 | # It's *almost* a straightforward mapping from the monorepo to svn... |
| 44 | GIT_TO_SVN_DIR = { |
| 45 | d: (d + '/trunk') |
| 46 | for d in [ |
| 47 | 'clang-tools-extra', |
| 48 | 'compiler-rt', |
Peter Collingbourne | f27f51d | 2017-06-04 22:18:57 +0000 | [diff] [blame] | 49 | 'debuginfo-tests', |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 50 | 'dragonegg', |
| 51 | 'klee', |
| 52 | 'libclc', |
| 53 | 'libcxx', |
| 54 | 'libcxxabi', |
Peter Collingbourne | f27f51d | 2017-06-04 22:18:57 +0000 | [diff] [blame] | 55 | 'libunwind', |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 56 | 'lld', |
| 57 | 'lldb', |
Peter Collingbourne | f27f51d | 2017-06-04 22:18:57 +0000 | [diff] [blame] | 58 | 'llgo', |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 59 | 'llvm', |
Peter Collingbourne | f27f51d | 2017-06-04 22:18:57 +0000 | [diff] [blame] | 60 | 'openmp', |
| 61 | 'parallel-libs', |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 62 | 'polly', |
James Y Knight | 1216782 | 2018-11-16 22:36:17 +0000 | [diff] [blame] | 63 | 'pstl', |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 64 | ] |
| 65 | } |
| 66 | GIT_TO_SVN_DIR.update({'clang': 'cfe/trunk'}) |
James Y Knight | 1216782 | 2018-11-16 22:36:17 +0000 | [diff] [blame] | 67 | GIT_TO_SVN_DIR.update({'': 'monorepo-root/trunk'}) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 68 | |
| 69 | VERBOSE = False |
| 70 | QUIET = False |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 71 | dev_null_fd = None |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 72 | |
| 73 | |
| 74 | def eprint(*args, **kwargs): |
| 75 | print(*args, file=sys.stderr, **kwargs) |
| 76 | |
| 77 | |
| 78 | def log(*args, **kwargs): |
| 79 | if QUIET: |
| 80 | return |
| 81 | print(*args, **kwargs) |
| 82 | |
| 83 | |
| 84 | def log_verbose(*args, **kwargs): |
| 85 | if not VERBOSE: |
| 86 | return |
| 87 | print(*args, **kwargs) |
| 88 | |
| 89 | |
| 90 | def die(msg): |
| 91 | eprint(msg) |
| 92 | sys.exit(1) |
| 93 | |
| 94 | |
James Y Knight | 1216782 | 2018-11-16 22:36:17 +0000 | [diff] [blame] | 95 | def split_first_path_component(d): |
| 96 | # Assuming we have a git path, it'll use slashes even on windows...I hope. |
| 97 | if '/' in d: |
| 98 | return d.split('/', 1) |
| 99 | else: |
| 100 | return (d, None) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 101 | |
| 102 | |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 103 | def get_dev_null(): |
| 104 | """Lazily create a /dev/null fd for use in shell()""" |
| 105 | global dev_null_fd |
| 106 | if dev_null_fd is None: |
| 107 | dev_null_fd = open(os.devnull, 'w') |
| 108 | return dev_null_fd |
| 109 | |
| 110 | |
| 111 | def shell(cmd, strip=True, cwd=None, stdin=None, die_on_failure=True, |
Zachary Turner | e5f47bb | 2018-10-09 23:42:28 +0000 | [diff] [blame] | 112 | ignore_errors=False, force_binary_stdin=False): |
James Y Knight | 1216782 | 2018-11-16 22:36:17 +0000 | [diff] [blame] | 113 | log_verbose('Running in %s: %s' % (cwd, ' '.join(cmd))) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 114 | |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 115 | err_pipe = subprocess.PIPE |
| 116 | if ignore_errors: |
| 117 | # Silence errors if requested. |
| 118 | err_pipe = get_dev_null() |
| 119 | |
Zachary Turner | e5f47bb | 2018-10-09 23:42:28 +0000 | [diff] [blame] | 120 | if force_binary_stdin and stdin: |
| 121 | stdin = stdin.encode('utf-8') |
| 122 | |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 123 | start = time.time() |
Zachary Turner | e5f47bb | 2018-10-09 23:42:28 +0000 | [diff] [blame] | 124 | text = not force_binary_stdin |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 125 | p = subprocess.Popen(cmd, cwd=cwd, stdout=subprocess.PIPE, stderr=err_pipe, |
Zachary Turner | e5f47bb | 2018-10-09 23:42:28 +0000 | [diff] [blame] | 126 | stdin=subprocess.PIPE, universal_newlines=text) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 127 | stdout, stderr = p.communicate(input=stdin) |
| 128 | elapsed = time.time() - start |
| 129 | |
| 130 | log_verbose('Command took %0.1fs' % elapsed) |
| 131 | |
Zachary Turner | e5f47bb | 2018-10-09 23:42:28 +0000 | [diff] [blame] | 132 | if not text: |
| 133 | stdout = stdout.decode('utf-8') |
| 134 | stderr = stderr.decode('utf-8') |
| 135 | |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 136 | if p.returncode == 0 or ignore_errors: |
| 137 | if stderr and not ignore_errors: |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 138 | eprint('`%s` printed to stderr:' % ' '.join(cmd)) |
| 139 | eprint(stderr.rstrip()) |
| 140 | if strip: |
| 141 | stdout = stdout.rstrip('\r\n') |
James Y Knight | 1216782 | 2018-11-16 22:36:17 +0000 | [diff] [blame] | 142 | if VERBOSE: |
| 143 | for l in stdout.splitlines(): |
| 144 | log_verbose("STDOUT: %s" % l) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 145 | return stdout |
Mehdi Amini | fbd2685 | 2016-11-12 01:17:59 +0000 | [diff] [blame] | 146 | err_msg = '`%s` returned %s' % (' '.join(cmd), p.returncode) |
| 147 | eprint(err_msg) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 148 | if stderr: |
| 149 | eprint(stderr.rstrip()) |
Mehdi Amini | fbd2685 | 2016-11-12 01:17:59 +0000 | [diff] [blame] | 150 | if die_on_failure: |
| 151 | sys.exit(2) |
| 152 | raise RuntimeError(err_msg) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 153 | |
| 154 | |
| 155 | def git(*cmd, **kwargs): |
| 156 | return shell(['git'] + list(cmd), kwargs.get('strip', True)) |
| 157 | |
| 158 | |
| 159 | def svn(cwd, *cmd, **kwargs): |
| 160 | # TODO: Better way to do default arg when we have *cmd? |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 161 | return shell(['svn'] + list(cmd), cwd=cwd, stdin=kwargs.get('stdin', None), |
| 162 | ignore_errors=kwargs.get('ignore_errors', None)) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 163 | |
Rui Ueyama | 2f8db1d | 2017-05-23 21:50:40 +0000 | [diff] [blame] | 164 | def program_exists(cmd): |
Zachary Turner | dc4cbc0 | 2017-05-24 00:28:46 +0000 | [diff] [blame] | 165 | if sys.platform == 'win32' and not cmd.endswith('.exe'): |
| 166 | cmd += '.exe' |
Rui Ueyama | 2f8db1d | 2017-05-23 21:50:40 +0000 | [diff] [blame] | 167 | for path in os.environ["PATH"].split(os.pathsep): |
| 168 | if os.access(os.path.join(path, cmd), os.X_OK): |
| 169 | return True |
| 170 | return False |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 171 | |
| 172 | def get_default_rev_range(): |
| 173 | # Get the branch tracked by the current branch, as set by |
| 174 | # git branch --set-upstream-to See http://serverfault.com/a/352236/38694. |
| 175 | cur_branch = git('rev-parse', '--symbolic-full-name', 'HEAD') |
| 176 | upstream_branch = git('for-each-ref', '--format=%(upstream:short)', |
| 177 | cur_branch) |
| 178 | if not upstream_branch: |
| 179 | upstream_branch = 'origin/master' |
| 180 | |
| 181 | # Get the newest common ancestor between HEAD and our upstream branch. |
| 182 | upstream_rev = git('merge-base', 'HEAD', upstream_branch) |
| 183 | return '%s..' % upstream_rev |
| 184 | |
| 185 | |
| 186 | def get_revs_to_push(rev_range): |
| 187 | if not rev_range: |
| 188 | rev_range = get_default_rev_range() |
| 189 | # Use git show rather than some plumbing command to figure out which revs |
| 190 | # are in rev_range because it handles single revs (HEAD^) and ranges |
| 191 | # (foo..bar) like we want. |
| 192 | revs = git('show', '--reverse', '--quiet', |
| 193 | '--pretty=%h', rev_range).splitlines() |
| 194 | if not revs: |
| 195 | die('Nothing to push: No revs in range %s.' % rev_range) |
| 196 | return revs |
| 197 | |
| 198 | |
James Y Knight | 1216782 | 2018-11-16 22:36:17 +0000 | [diff] [blame] | 199 | def clean_svn(svn_repo): |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 200 | svn(svn_repo, 'revert', '-R', '.') |
| 201 | |
| 202 | # Unfortunately it appears there's no svn equivalent for git clean, so we |
| 203 | # have to do it ourselves. |
Walter Lee | b074f14 | 2017-12-22 21:19:13 +0000 | [diff] [blame] | 204 | for line in svn(svn_repo, 'status', '--no-ignore').split('\n'): |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 205 | if not line.startswith('?'): |
| 206 | continue |
| 207 | filename = line[1:].strip() |
| 208 | os.remove(os.path.join(svn_repo, filename)) |
| 209 | |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 210 | |
| 211 | def svn_init(svn_root): |
| 212 | if not os.path.exists(svn_root): |
| 213 | log('Creating svn staging directory: (%s)' % (svn_root)) |
| 214 | os.makedirs(svn_root) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 215 | svn(svn_root, 'checkout', '--depth=immediates', |
| 216 | 'https://llvm.org/svn/llvm-project/', '.') |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 217 | log("svn staging area ready in '%s'" % svn_root) |
| 218 | if not os.path.isdir(svn_root): |
| 219 | die("Can't initialize svn staging dir (%s)" % svn_root) |
| 220 | |
| 221 | |
James Y Knight | 1216782 | 2018-11-16 22:36:17 +0000 | [diff] [blame] | 222 | def fix_eol_style_native(rev, svn_sr_path, files): |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 223 | """Fix line endings before applying patches with Unix endings |
| 224 | |
| 225 | SVN on Windows will check out files with CRLF for files with the |
| 226 | svn:eol-style property set to "native". This breaks `git apply`, which |
| 227 | typically works with Unix-line ending patches. Work around the problem here |
| 228 | by doing a dos2unix up front for files with svn:eol-style set to "native". |
| 229 | SVN will not commit a mass line ending re-doing because it detects the line |
| 230 | ending format for files with this property. |
| 231 | """ |
Reid Kleckner | 162c5cd | 2017-05-18 17:17:17 +0000 | [diff] [blame] | 232 | # Skip files that don't exist in SVN yet. |
| 233 | files = [f for f in files if os.path.exists(os.path.join(svn_sr_path, f))] |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 234 | # Use ignore_errors because 'svn propget' prints errors if the file doesn't |
| 235 | # have the named property. There doesn't seem to be a way to suppress that. |
| 236 | eol_props = svn(svn_sr_path, 'propget', 'svn:eol-style', *files, |
Reid Kleckner | 0f442bc | 2017-05-12 00:10:19 +0000 | [diff] [blame] | 237 | ignore_errors=True) |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 238 | crlf_files = [] |
Reid Kleckner | 0f442bc | 2017-05-12 00:10:19 +0000 | [diff] [blame] | 239 | if len(files) == 1: |
| 240 | # No need to split propget output on ' - ' when we have one file. |
Zachary Turner | e5f47bb | 2018-10-09 23:42:28 +0000 | [diff] [blame] | 241 | if eol_props.strip() in ['native', 'CRLF']: |
Reid Kleckner | 0f442bc | 2017-05-12 00:10:19 +0000 | [diff] [blame] | 242 | crlf_files = files |
| 243 | else: |
| 244 | for eol_prop in eol_props.split('\n'): |
| 245 | # Remove spare CR. |
| 246 | eol_prop = eol_prop.strip('\r') |
| 247 | if not eol_prop: |
| 248 | continue |
| 249 | prop_parts = eol_prop.rsplit(' - ', 1) |
| 250 | if len(prop_parts) != 2: |
| 251 | eprint("unable to parse svn propget line:") |
| 252 | eprint(eol_prop) |
| 253 | continue |
| 254 | (f, eol_style) = prop_parts |
| 255 | if eol_style == 'native': |
| 256 | crlf_files.append(f) |
Zachary Turner | e5f47bb | 2018-10-09 23:42:28 +0000 | [diff] [blame] | 257 | if crlf_files: |
James Y Knight | 1216782 | 2018-11-16 22:36:17 +0000 | [diff] [blame] | 258 | # Reformat all files with native SVN line endings to Unix format. SVN |
| 259 | # knows files with native line endings are text files. It will commit |
| 260 | # just the diff, and not a mass line ending change. |
Zachary Turner | e5f47bb | 2018-10-09 23:42:28 +0000 | [diff] [blame] | 261 | shell(['dos2unix'] + crlf_files, ignore_errors=True, cwd=svn_sr_path) |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 262 | |
James Y Knight | 1216782 | 2018-11-16 22:36:17 +0000 | [diff] [blame] | 263 | def get_all_parent_dirs(name): |
| 264 | parts = [] |
| 265 | head, tail = os.path.split(name) |
| 266 | while head: |
| 267 | parts.append(head) |
| 268 | head, tail = os.path.split(head) |
| 269 | return parts |
| 270 | |
| 271 | def split_subrepo(f): |
| 272 | # Given a path, splits it into (subproject, rest-of-path). If the path is |
| 273 | # not in a subproject, returns ('', full-path). |
| 274 | |
| 275 | subproject, remainder = split_first_path_component(f) |
| 276 | |
| 277 | if subproject in GIT_TO_SVN_DIR: |
| 278 | return subproject, remainder |
| 279 | else: |
| 280 | return '', f |
| 281 | |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 282 | def svn_push_one_rev(svn_repo, rev, dry_run): |
| 283 | files = git('diff-tree', '--no-commit-id', '--name-only', '-r', |
| 284 | rev).split('\n') |
James Y Knight | 1216782 | 2018-11-16 22:36:17 +0000 | [diff] [blame] | 285 | if not files: |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 286 | raise RuntimeError('Empty diff for rev %s?' % rev) |
| 287 | |
James Y Knight | 1216782 | 2018-11-16 22:36:17 +0000 | [diff] [blame] | 288 | # Split files by subrepo |
| 289 | subrepo_files = collections.defaultdict(list) |
| 290 | for f in files: |
| 291 | subrepo, remainder = split_subrepo(f) |
| 292 | subrepo_files[subrepo].append(remainder) |
| 293 | |
Walter Lee | b074f14 | 2017-12-22 21:19:13 +0000 | [diff] [blame] | 294 | status = svn(svn_repo, 'status', '--no-ignore') |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 295 | if status: |
| 296 | die("Can't push git rev %s because svn status is not empty:\n%s" % |
| 297 | (rev, status)) |
| 298 | |
James Y Knight | 1216782 | 2018-11-16 22:36:17 +0000 | [diff] [blame] | 299 | svn_dirs_to_update = set() |
James Y Knight | 8536211 | 2018-11-16 23:59:23 +0000 | [diff] [blame] | 300 | for sr, files in iteritems(subrepo_files): |
James Y Knight | 1216782 | 2018-11-16 22:36:17 +0000 | [diff] [blame] | 301 | svn_sr_path = GIT_TO_SVN_DIR[sr] |
| 302 | for f in files: |
| 303 | svn_dirs_to_update.update( |
| 304 | get_all_parent_dirs(os.path.join(svn_sr_path, f))) |
| 305 | |
| 306 | # Sort by length to ensure that the parent directories are passed to svn |
| 307 | # before child directories. |
James Y Knight | 8536211 | 2018-11-16 23:59:23 +0000 | [diff] [blame] | 308 | sorted_dirs_to_update = sorted(svn_dirs_to_update, key=len) |
James Y Knight | 1216782 | 2018-11-16 22:36:17 +0000 | [diff] [blame] | 309 | |
| 310 | # SVN update only in the affected directories. |
| 311 | svn(svn_repo, 'update', '--depth=immediates', *sorted_dirs_to_update) |
| 312 | |
James Y Knight | 8536211 | 2018-11-16 23:59:23 +0000 | [diff] [blame] | 313 | for sr, files in iteritems(subrepo_files): |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 314 | svn_sr_path = os.path.join(svn_repo, GIT_TO_SVN_DIR[sr]) |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 315 | if os.name == 'nt': |
James Y Knight | 1216782 | 2018-11-16 22:36:17 +0000 | [diff] [blame] | 316 | fix_eol_style_native(rev, svn_sr_path, files) |
| 317 | diff = git('show', '--binary', rev, '--', |
| 318 | *(os.path.join(sr, f) for f in files), |
| 319 | strip=False) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 320 | # git is the only thing that can handle its own patches... |
| 321 | log_verbose('Apply patch: %s' % diff) |
James Y Knight | 1216782 | 2018-11-16 22:36:17 +0000 | [diff] [blame] | 322 | if sr == '': |
| 323 | prefix_strip = '-p1' |
| 324 | else: |
| 325 | prefix_strip = '-p2' |
Mehdi Amini | fbd2685 | 2016-11-12 01:17:59 +0000 | [diff] [blame] | 326 | try: |
James Y Knight | 1216782 | 2018-11-16 22:36:17 +0000 | [diff] [blame] | 327 | # If we allow python to apply the diff in text mode, it will |
| 328 | # silently convert \n to \r\n which git doesn't like. |
| 329 | shell(['git', 'apply', prefix_strip, '-'], cwd=svn_sr_path, |
| 330 | stdin=diff, die_on_failure=False, force_binary_stdin=True) |
Mehdi Amini | fbd2685 | 2016-11-12 01:17:59 +0000 | [diff] [blame] | 331 | except RuntimeError as e: |
| 332 | eprint("Patch doesn't apply: maybe you should try `git pull -r` " |
| 333 | "first?") |
| 334 | sys.exit(2) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 335 | |
Walter Lee | b074f14 | 2017-12-22 21:19:13 +0000 | [diff] [blame] | 336 | status_lines = svn(svn_repo, 'status', '--no-ignore').split('\n') |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 337 | |
Walter Lee | b074f14 | 2017-12-22 21:19:13 +0000 | [diff] [blame] | 338 | for l in (l for l in status_lines if (l.startswith('?') or |
| 339 | l.startswith('I'))): |
| 340 | svn(svn_repo, 'add', '--no-ignore', l[1:].strip()) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 341 | for l in (l for l in status_lines if l.startswith('!')): |
| 342 | svn(svn_repo, 'remove', l[1:].strip()) |
| 343 | |
| 344 | # Now we're ready to commit. |
| 345 | commit_msg = git('show', '--pretty=%B', '--quiet', rev) |
| 346 | if not dry_run: |
Mehdi Amini | 5c289b7 | 2016-11-30 19:12:53 +0000 | [diff] [blame] | 347 | log(svn(svn_repo, 'commit', '-m', commit_msg, '--force-interactive')) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 348 | log('Committed %s to svn.' % rev) |
| 349 | else: |
| 350 | log("Would have committed %s to svn, if this weren't a dry run." % rev) |
| 351 | |
| 352 | |
| 353 | def cmd_push(args): |
| 354 | '''Push changes back to SVN: this is extracted from Justin Lebar's script |
| 355 | available here: https://github.com/jlebar/llvm-repo-tools/ |
| 356 | |
| 357 | Note: a current limitation is that git does not track file rename, so they |
| 358 | will show up in SVN as delete+add. |
| 359 | ''' |
| 360 | # Get the git root |
| 361 | git_root = git('rev-parse', '--show-toplevel') |
| 362 | if not os.path.isdir(git_root): |
| 363 | die("Can't find git root dir") |
| 364 | |
| 365 | # Push from the root of the git repo |
| 366 | os.chdir(git_root) |
| 367 | |
| 368 | # We need a staging area for SVN, let's hide it in the .git directory. |
Mehdi Amini | f95a459 | 2016-11-07 20:35:02 +0000 | [diff] [blame] | 369 | dot_git_dir = git('rev-parse', '--git-common-dir') |
| 370 | svn_root = os.path.join(dot_git_dir, 'llvm-upstream-svn') |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 371 | svn_init(svn_root) |
| 372 | |
| 373 | rev_range = args.rev_range |
| 374 | dry_run = args.dry_run |
| 375 | revs = get_revs_to_push(rev_range) |
| 376 | log('Pushing %d commit%s:\n%s' % |
| 377 | (len(revs), 's' if len(revs) != 1 |
| 378 | else '', '\n'.join(' ' + git('show', '--oneline', '--quiet', c) |
| 379 | for c in revs))) |
| 380 | for r in revs: |
James Y Knight | 1216782 | 2018-11-16 22:36:17 +0000 | [diff] [blame] | 381 | clean_svn(svn_root) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 382 | svn_push_one_rev(svn_root, r, dry_run) |
| 383 | |
| 384 | |
| 385 | if __name__ == '__main__': |
Rui Ueyama | 2f8db1d | 2017-05-23 21:50:40 +0000 | [diff] [blame] | 386 | if not program_exists('svn'): |
| 387 | die('error: git-llvm needs svn command, but svn is not installed.') |
| 388 | |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 389 | argv = sys.argv[1:] |
| 390 | p = argparse.ArgumentParser( |
| 391 | prog='git llvm', formatter_class=argparse.RawDescriptionHelpFormatter, |
| 392 | description=__doc__) |
| 393 | subcommands = p.add_subparsers(title='subcommands', |
| 394 | description='valid subcommands', |
| 395 | help='additional help') |
| 396 | verbosity_group = p.add_mutually_exclusive_group() |
| 397 | verbosity_group.add_argument('-q', '--quiet', action='store_true', |
| 398 | help='print less information') |
| 399 | verbosity_group.add_argument('-v', '--verbose', action='store_true', |
| 400 | help='print more information') |
| 401 | |
| 402 | parser_push = subcommands.add_parser( |
| 403 | 'push', description=cmd_push.__doc__, |
| 404 | help='push changes back to the LLVM SVN repository') |
| 405 | parser_push.add_argument( |
| 406 | '-n', |
| 407 | '--dry-run', |
| 408 | dest='dry_run', |
| 409 | action='store_true', |
| 410 | help='Do everything other than commit to svn. Leaves junk in the svn ' |
| 411 | 'repo, so probably will not work well if you try to commit more ' |
| 412 | 'than one rev.') |
| 413 | parser_push.add_argument( |
| 414 | 'rev_range', |
| 415 | metavar='GIT_REVS', |
| 416 | type=str, |
| 417 | nargs='?', |
| 418 | help="revs to push (default: everything not in the branch's " |
| 419 | 'upstream, or not in origin/master if the branch lacks ' |
| 420 | 'an explicit upstream)') |
| 421 | parser_push.set_defaults(func=cmd_push) |
| 422 | args = p.parse_args(argv) |
| 423 | VERBOSE = args.verbose |
| 424 | QUIET = args.quiet |
| 425 | |
| 426 | # Dispatch to the right subcommand |
| 427 | args.func(args) |