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