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 | |
| 32 | |
| 33 | # It's *almost* a straightforward mapping from the monorepo to svn... |
| 34 | GIT_TO_SVN_DIR = { |
| 35 | d: (d + '/trunk') |
| 36 | for d in [ |
| 37 | 'clang-tools-extra', |
| 38 | 'compiler-rt', |
| 39 | 'dragonegg', |
| 40 | 'klee', |
| 41 | 'libclc', |
| 42 | 'libcxx', |
| 43 | 'libcxxabi', |
| 44 | 'lld', |
| 45 | 'lldb', |
| 46 | 'llvm', |
| 47 | 'polly', |
| 48 | ] |
| 49 | } |
| 50 | GIT_TO_SVN_DIR.update({'clang': 'cfe/trunk'}) |
| 51 | |
| 52 | VERBOSE = False |
| 53 | QUIET = False |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 54 | dev_null_fd = None |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 55 | |
| 56 | |
| 57 | def eprint(*args, **kwargs): |
| 58 | print(*args, file=sys.stderr, **kwargs) |
| 59 | |
| 60 | |
| 61 | def log(*args, **kwargs): |
| 62 | if QUIET: |
| 63 | return |
| 64 | print(*args, **kwargs) |
| 65 | |
| 66 | |
| 67 | def log_verbose(*args, **kwargs): |
| 68 | if not VERBOSE: |
| 69 | return |
| 70 | print(*args, **kwargs) |
| 71 | |
| 72 | |
| 73 | def die(msg): |
| 74 | eprint(msg) |
| 75 | sys.exit(1) |
| 76 | |
| 77 | |
| 78 | def first_dirname(d): |
| 79 | while True: |
| 80 | (head, tail) = os.path.split(d) |
| 81 | if not head or head == '/': |
| 82 | return tail |
| 83 | d = head |
| 84 | |
| 85 | |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 86 | def get_dev_null(): |
| 87 | """Lazily create a /dev/null fd for use in shell()""" |
| 88 | global dev_null_fd |
| 89 | if dev_null_fd is None: |
| 90 | dev_null_fd = open(os.devnull, 'w') |
| 91 | return dev_null_fd |
| 92 | |
| 93 | |
| 94 | def shell(cmd, strip=True, cwd=None, stdin=None, die_on_failure=True, |
| 95 | ignore_errors=False): |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 96 | log_verbose('Running: %s' % ' '.join(cmd)) |
| 97 | |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 98 | err_pipe = subprocess.PIPE |
| 99 | if ignore_errors: |
| 100 | # Silence errors if requested. |
| 101 | err_pipe = get_dev_null() |
| 102 | |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 103 | start = time.time() |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 104 | p = subprocess.Popen(cmd, cwd=cwd, stdout=subprocess.PIPE, stderr=err_pipe, |
| 105 | stdin=subprocess.PIPE) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 106 | stdout, stderr = p.communicate(input=stdin) |
| 107 | elapsed = time.time() - start |
| 108 | |
| 109 | log_verbose('Command took %0.1fs' % elapsed) |
| 110 | |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 111 | if p.returncode == 0 or ignore_errors: |
| 112 | if stderr and not ignore_errors: |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 113 | eprint('`%s` printed to stderr:' % ' '.join(cmd)) |
| 114 | eprint(stderr.rstrip()) |
| 115 | if strip: |
| 116 | stdout = stdout.rstrip('\r\n') |
| 117 | return stdout |
Mehdi Amini | fbd2685 | 2016-11-12 01:17:59 +0000 | [diff] [blame] | 118 | err_msg = '`%s` returned %s' % (' '.join(cmd), p.returncode) |
| 119 | eprint(err_msg) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 120 | if stderr: |
| 121 | eprint(stderr.rstrip()) |
Mehdi Amini | fbd2685 | 2016-11-12 01:17:59 +0000 | [diff] [blame] | 122 | if die_on_failure: |
| 123 | sys.exit(2) |
| 124 | raise RuntimeError(err_msg) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 125 | |
| 126 | |
| 127 | def git(*cmd, **kwargs): |
| 128 | return shell(['git'] + list(cmd), kwargs.get('strip', True)) |
| 129 | |
| 130 | |
| 131 | def svn(cwd, *cmd, **kwargs): |
| 132 | # TODO: Better way to do default arg when we have *cmd? |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 133 | return shell(['svn'] + list(cmd), cwd=cwd, stdin=kwargs.get('stdin', None), |
| 134 | ignore_errors=kwargs.get('ignore_errors', None)) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 135 | |
Rui Ueyama | 2f8db1d | 2017-05-23 21:50:40 +0000 | [diff] [blame] | 136 | def program_exists(cmd): |
| 137 | for path in os.environ["PATH"].split(os.pathsep): |
| 138 | if os.access(os.path.join(path, cmd), os.X_OK): |
| 139 | return True |
| 140 | return False |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 141 | |
| 142 | def get_default_rev_range(): |
| 143 | # Get the branch tracked by the current branch, as set by |
| 144 | # git branch --set-upstream-to See http://serverfault.com/a/352236/38694. |
| 145 | cur_branch = git('rev-parse', '--symbolic-full-name', 'HEAD') |
| 146 | upstream_branch = git('for-each-ref', '--format=%(upstream:short)', |
| 147 | cur_branch) |
| 148 | if not upstream_branch: |
| 149 | upstream_branch = 'origin/master' |
| 150 | |
| 151 | # Get the newest common ancestor between HEAD and our upstream branch. |
| 152 | upstream_rev = git('merge-base', 'HEAD', upstream_branch) |
| 153 | return '%s..' % upstream_rev |
| 154 | |
| 155 | |
| 156 | def get_revs_to_push(rev_range): |
| 157 | if not rev_range: |
| 158 | rev_range = get_default_rev_range() |
| 159 | # Use git show rather than some plumbing command to figure out which revs |
| 160 | # are in rev_range because it handles single revs (HEAD^) and ranges |
| 161 | # (foo..bar) like we want. |
| 162 | revs = git('show', '--reverse', '--quiet', |
| 163 | '--pretty=%h', rev_range).splitlines() |
| 164 | if not revs: |
| 165 | die('Nothing to push: No revs in range %s.' % rev_range) |
| 166 | return revs |
| 167 | |
| 168 | |
| 169 | def clean_and_update_svn(svn_repo): |
| 170 | svn(svn_repo, 'revert', '-R', '.') |
| 171 | |
| 172 | # Unfortunately it appears there's no svn equivalent for git clean, so we |
| 173 | # have to do it ourselves. |
| 174 | for line in svn(svn_repo, 'status').split('\n'): |
| 175 | if not line.startswith('?'): |
| 176 | continue |
| 177 | filename = line[1:].strip() |
| 178 | os.remove(os.path.join(svn_repo, filename)) |
| 179 | |
| 180 | svn(svn_repo, 'update', *list(GIT_TO_SVN_DIR.values())) |
| 181 | |
| 182 | |
| 183 | def svn_init(svn_root): |
| 184 | if not os.path.exists(svn_root): |
| 185 | log('Creating svn staging directory: (%s)' % (svn_root)) |
| 186 | os.makedirs(svn_root) |
Rui Ueyama | 62839f0 | 2016-12-20 05:49:56 +0000 | [diff] [blame] | 187 | log('This is a one-time initialization, please be patient for a few' |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 188 | ' minutes...') |
| 189 | svn(svn_root, 'checkout', '--depth=immediates', |
| 190 | 'https://llvm.org/svn/llvm-project/', '.') |
| 191 | svn(svn_root, 'update', *list(GIT_TO_SVN_DIR.values())) |
| 192 | log("svn staging area ready in '%s'" % svn_root) |
| 193 | if not os.path.isdir(svn_root): |
| 194 | die("Can't initialize svn staging dir (%s)" % svn_root) |
| 195 | |
| 196 | |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 197 | def fix_eol_style_native(rev, sr, svn_sr_path): |
| 198 | """Fix line endings before applying patches with Unix endings |
| 199 | |
| 200 | SVN on Windows will check out files with CRLF for files with the |
| 201 | svn:eol-style property set to "native". This breaks `git apply`, which |
| 202 | typically works with Unix-line ending patches. Work around the problem here |
| 203 | by doing a dos2unix up front for files with svn:eol-style set to "native". |
| 204 | SVN will not commit a mass line ending re-doing because it detects the line |
| 205 | ending format for files with this property. |
| 206 | """ |
| 207 | files = git('diff-tree', '--no-commit-id', '--name-only', '-r', rev, '--', |
| 208 | sr).split('\n') |
| 209 | files = [f.split('/', 1)[1] for f in files] |
Reid Kleckner | 162c5cd | 2017-05-18 17:17:17 +0000 | [diff] [blame] | 210 | # Skip files that don't exist in SVN yet. |
| 211 | 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] | 212 | # Use ignore_errors because 'svn propget' prints errors if the file doesn't |
| 213 | # have the named property. There doesn't seem to be a way to suppress that. |
| 214 | eol_props = svn(svn_sr_path, 'propget', 'svn:eol-style', *files, |
Reid Kleckner | 0f442bc | 2017-05-12 00:10:19 +0000 | [diff] [blame] | 215 | ignore_errors=True) |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 216 | crlf_files = [] |
Reid Kleckner | 0f442bc | 2017-05-12 00:10:19 +0000 | [diff] [blame] | 217 | if len(files) == 1: |
| 218 | # No need to split propget output on ' - ' when we have one file. |
| 219 | if eol_props.strip() == 'native': |
| 220 | crlf_files = files |
| 221 | else: |
| 222 | for eol_prop in eol_props.split('\n'): |
| 223 | # Remove spare CR. |
| 224 | eol_prop = eol_prop.strip('\r') |
| 225 | if not eol_prop: |
| 226 | continue |
| 227 | prop_parts = eol_prop.rsplit(' - ', 1) |
| 228 | if len(prop_parts) != 2: |
| 229 | eprint("unable to parse svn propget line:") |
| 230 | eprint(eol_prop) |
| 231 | continue |
| 232 | (f, eol_style) = prop_parts |
| 233 | if eol_style == 'native': |
| 234 | crlf_files.append(f) |
Reid Kleckner | 4534097 | 2017-04-24 22:09:08 +0000 | [diff] [blame] | 235 | # Reformat all files with native SVN line endings to Unix format. SVN knows |
| 236 | # files with native line endings are text files. It will commit just the |
| 237 | # diff, and not a mass line ending change. |
| 238 | shell(['dos2unix', '-q'] + crlf_files, cwd=svn_sr_path) |
| 239 | |
| 240 | |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 241 | def svn_push_one_rev(svn_repo, rev, dry_run): |
| 242 | files = git('diff-tree', '--no-commit-id', '--name-only', '-r', |
| 243 | rev).split('\n') |
| 244 | subrepos = {first_dirname(f) for f in files} |
| 245 | if not subrepos: |
| 246 | raise RuntimeError('Empty diff for rev %s?' % rev) |
| 247 | |
| 248 | status = svn(svn_repo, 'status') |
| 249 | if status: |
| 250 | die("Can't push git rev %s because svn status is not empty:\n%s" % |
| 251 | (rev, status)) |
| 252 | |
| 253 | for sr in subrepos: |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 254 | 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] | 255 | if os.name == 'nt': |
| 256 | fix_eol_style_native(rev, sr, svn_sr_path) |
| 257 | diff = git('show', '--binary', rev, '--', sr, strip=False) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 258 | # git is the only thing that can handle its own patches... |
| 259 | log_verbose('Apply patch: %s' % diff) |
Mehdi Amini | fbd2685 | 2016-11-12 01:17:59 +0000 | [diff] [blame] | 260 | try: |
| 261 | shell(['git', 'apply', '-p2', '-'], cwd=svn_sr_path, stdin=diff, |
| 262 | die_on_failure=False) |
| 263 | except RuntimeError as e: |
| 264 | eprint("Patch doesn't apply: maybe you should try `git pull -r` " |
| 265 | "first?") |
| 266 | sys.exit(2) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 267 | |
| 268 | status_lines = svn(svn_repo, 'status').split('\n') |
| 269 | |
| 270 | for l in (l for l in status_lines if l.startswith('?')): |
| 271 | svn(svn_repo, 'add', l[1:].strip()) |
| 272 | for l in (l for l in status_lines if l.startswith('!')): |
| 273 | svn(svn_repo, 'remove', l[1:].strip()) |
| 274 | |
| 275 | # Now we're ready to commit. |
| 276 | commit_msg = git('show', '--pretty=%B', '--quiet', rev) |
| 277 | if not dry_run: |
Mehdi Amini | 5c289b7 | 2016-11-30 19:12:53 +0000 | [diff] [blame] | 278 | log(svn(svn_repo, 'commit', '-m', commit_msg, '--force-interactive')) |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 279 | log('Committed %s to svn.' % rev) |
| 280 | else: |
| 281 | log("Would have committed %s to svn, if this weren't a dry run." % rev) |
| 282 | |
| 283 | |
| 284 | def cmd_push(args): |
| 285 | '''Push changes back to SVN: this is extracted from Justin Lebar's script |
| 286 | available here: https://github.com/jlebar/llvm-repo-tools/ |
| 287 | |
| 288 | Note: a current limitation is that git does not track file rename, so they |
| 289 | will show up in SVN as delete+add. |
| 290 | ''' |
| 291 | # Get the git root |
| 292 | git_root = git('rev-parse', '--show-toplevel') |
| 293 | if not os.path.isdir(git_root): |
| 294 | die("Can't find git root dir") |
| 295 | |
| 296 | # Push from the root of the git repo |
| 297 | os.chdir(git_root) |
| 298 | |
| 299 | # 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] | 300 | dot_git_dir = git('rev-parse', '--git-common-dir') |
| 301 | svn_root = os.path.join(dot_git_dir, 'llvm-upstream-svn') |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 302 | svn_init(svn_root) |
| 303 | |
| 304 | rev_range = args.rev_range |
| 305 | dry_run = args.dry_run |
| 306 | revs = get_revs_to_push(rev_range) |
| 307 | log('Pushing %d commit%s:\n%s' % |
| 308 | (len(revs), 's' if len(revs) != 1 |
| 309 | else '', '\n'.join(' ' + git('show', '--oneline', '--quiet', c) |
| 310 | for c in revs))) |
| 311 | for r in revs: |
| 312 | clean_and_update_svn(svn_root) |
| 313 | svn_push_one_rev(svn_root, r, dry_run) |
| 314 | |
| 315 | |
| 316 | if __name__ == '__main__': |
Rui Ueyama | 2f8db1d | 2017-05-23 21:50:40 +0000 | [diff] [blame] | 317 | if not program_exists('svn'): |
| 318 | die('error: git-llvm needs svn command, but svn is not installed.') |
| 319 | |
Mehdi Amini | 7b48463 | 2016-11-07 20:00:47 +0000 | [diff] [blame] | 320 | argv = sys.argv[1:] |
| 321 | p = argparse.ArgumentParser( |
| 322 | prog='git llvm', formatter_class=argparse.RawDescriptionHelpFormatter, |
| 323 | description=__doc__) |
| 324 | subcommands = p.add_subparsers(title='subcommands', |
| 325 | description='valid subcommands', |
| 326 | help='additional help') |
| 327 | verbosity_group = p.add_mutually_exclusive_group() |
| 328 | verbosity_group.add_argument('-q', '--quiet', action='store_true', |
| 329 | help='print less information') |
| 330 | verbosity_group.add_argument('-v', '--verbose', action='store_true', |
| 331 | help='print more information') |
| 332 | |
| 333 | parser_push = subcommands.add_parser( |
| 334 | 'push', description=cmd_push.__doc__, |
| 335 | help='push changes back to the LLVM SVN repository') |
| 336 | parser_push.add_argument( |
| 337 | '-n', |
| 338 | '--dry-run', |
| 339 | dest='dry_run', |
| 340 | action='store_true', |
| 341 | help='Do everything other than commit to svn. Leaves junk in the svn ' |
| 342 | 'repo, so probably will not work well if you try to commit more ' |
| 343 | 'than one rev.') |
| 344 | parser_push.add_argument( |
| 345 | 'rev_range', |
| 346 | metavar='GIT_REVS', |
| 347 | type=str, |
| 348 | nargs='?', |
| 349 | help="revs to push (default: everything not in the branch's " |
| 350 | 'upstream, or not in origin/master if the branch lacks ' |
| 351 | 'an explicit upstream)') |
| 352 | parser_push.set_defaults(func=cmd_push) |
| 353 | args = p.parse_args(argv) |
| 354 | VERBOSE = args.verbose |
| 355 | QUIET = args.quiet |
| 356 | |
| 357 | # Dispatch to the right subcommand |
| 358 | args.func(args) |