blob: 94927d445a80a9f59d4fc5d0415dd0d49d76f0c8 [file] [log] [blame]
Mehdi Amini7b484632016-11-07 20:00:47 +00001#!/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"""
13git-llvm integration
14====================
15
16This file provides integration for git.
17"""
18
19from __future__ import print_function
20import argparse
21import collections
22import contextlib
23import errno
24import os
25import re
26import subprocess
27import sys
28import tempfile
29import time
30assert sys.version_info >= (2, 7)
31
James Y Knight85362112018-11-16 23:59:23 +000032try:
33 dict.iteritems
34except AttributeError:
35 # Python 3
36 def iteritems(d):
37 return iter(d.items())
38else:
39 # Python 2
40 def iteritems(d):
41 return d.iteritems()
Mehdi Amini7b484632016-11-07 20:00:47 +000042
43# It's *almost* a straightforward mapping from the monorepo to svn...
44GIT_TO_SVN_DIR = {
45 d: (d + '/trunk')
46 for d in [
47 'clang-tools-extra',
48 'compiler-rt',
Peter Collingbournef27f51d2017-06-04 22:18:57 +000049 'debuginfo-tests',
Mehdi Amini7b484632016-11-07 20:00:47 +000050 'dragonegg',
51 'klee',
52 'libclc',
53 'libcxx',
54 'libcxxabi',
Peter Collingbournef27f51d2017-06-04 22:18:57 +000055 'libunwind',
Mehdi Amini7b484632016-11-07 20:00:47 +000056 'lld',
57 'lldb',
Peter Collingbournef27f51d2017-06-04 22:18:57 +000058 'llgo',
Mehdi Amini7b484632016-11-07 20:00:47 +000059 'llvm',
Peter Collingbournef27f51d2017-06-04 22:18:57 +000060 'openmp',
61 'parallel-libs',
Mehdi Amini7b484632016-11-07 20:00:47 +000062 'polly',
James Y Knight12167822018-11-16 22:36:17 +000063 'pstl',
Mehdi Amini7b484632016-11-07 20:00:47 +000064 ]
65}
66GIT_TO_SVN_DIR.update({'clang': 'cfe/trunk'})
James Y Knight12167822018-11-16 22:36:17 +000067GIT_TO_SVN_DIR.update({'': 'monorepo-root/trunk'})
Mehdi Amini7b484632016-11-07 20:00:47 +000068
69VERBOSE = False
70QUIET = False
Reid Kleckner45340972017-04-24 22:09:08 +000071dev_null_fd = None
Mehdi Amini7b484632016-11-07 20:00:47 +000072
73
74def eprint(*args, **kwargs):
75 print(*args, file=sys.stderr, **kwargs)
76
77
78def log(*args, **kwargs):
79 if QUIET:
80 return
81 print(*args, **kwargs)
82
83
84def log_verbose(*args, **kwargs):
85 if not VERBOSE:
86 return
87 print(*args, **kwargs)
88
89
90def die(msg):
91 eprint(msg)
92 sys.exit(1)
93
94
James Y Knight12167822018-11-16 22:36:17 +000095def 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 Amini7b484632016-11-07 20:00:47 +0000101
102
Reid Kleckner45340972017-04-24 22:09:08 +0000103def 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
111def shell(cmd, strip=True, cwd=None, stdin=None, die_on_failure=True,
Zachary Turnere5f47bb2018-10-09 23:42:28 +0000112 ignore_errors=False, force_binary_stdin=False):
James Y Knight12167822018-11-16 22:36:17 +0000113 log_verbose('Running in %s: %s' % (cwd, ' '.join(cmd)))
Mehdi Amini7b484632016-11-07 20:00:47 +0000114
Reid Kleckner45340972017-04-24 22:09:08 +0000115 err_pipe = subprocess.PIPE
116 if ignore_errors:
117 # Silence errors if requested.
118 err_pipe = get_dev_null()
119
Zachary Turnere5f47bb2018-10-09 23:42:28 +0000120 if force_binary_stdin and stdin:
121 stdin = stdin.encode('utf-8')
122
Mehdi Amini7b484632016-11-07 20:00:47 +0000123 start = time.time()
Zachary Turnere5f47bb2018-10-09 23:42:28 +0000124 text = not force_binary_stdin
Reid Kleckner45340972017-04-24 22:09:08 +0000125 p = subprocess.Popen(cmd, cwd=cwd, stdout=subprocess.PIPE, stderr=err_pipe,
Zachary Turnere5f47bb2018-10-09 23:42:28 +0000126 stdin=subprocess.PIPE, universal_newlines=text)
Mehdi Amini7b484632016-11-07 20:00:47 +0000127 stdout, stderr = p.communicate(input=stdin)
128 elapsed = time.time() - start
129
130 log_verbose('Command took %0.1fs' % elapsed)
131
Zachary Turnere5f47bb2018-10-09 23:42:28 +0000132 if not text:
133 stdout = stdout.decode('utf-8')
134 stderr = stderr.decode('utf-8')
135
Reid Kleckner45340972017-04-24 22:09:08 +0000136 if p.returncode == 0 or ignore_errors:
137 if stderr and not ignore_errors:
Mehdi Amini7b484632016-11-07 20:00:47 +0000138 eprint('`%s` printed to stderr:' % ' '.join(cmd))
139 eprint(stderr.rstrip())
140 if strip:
141 stdout = stdout.rstrip('\r\n')
James Y Knight12167822018-11-16 22:36:17 +0000142 if VERBOSE:
143 for l in stdout.splitlines():
144 log_verbose("STDOUT: %s" % l)
Mehdi Amini7b484632016-11-07 20:00:47 +0000145 return stdout
Mehdi Aminifbd26852016-11-12 01:17:59 +0000146 err_msg = '`%s` returned %s' % (' '.join(cmd), p.returncode)
147 eprint(err_msg)
Mehdi Amini7b484632016-11-07 20:00:47 +0000148 if stderr:
149 eprint(stderr.rstrip())
Mehdi Aminifbd26852016-11-12 01:17:59 +0000150 if die_on_failure:
151 sys.exit(2)
152 raise RuntimeError(err_msg)
Mehdi Amini7b484632016-11-07 20:00:47 +0000153
154
155def git(*cmd, **kwargs):
156 return shell(['git'] + list(cmd), kwargs.get('strip', True))
157
158
159def svn(cwd, *cmd, **kwargs):
160 # TODO: Better way to do default arg when we have *cmd?
Reid Kleckner45340972017-04-24 22:09:08 +0000161 return shell(['svn'] + list(cmd), cwd=cwd, stdin=kwargs.get('stdin', None),
162 ignore_errors=kwargs.get('ignore_errors', None))
Mehdi Amini7b484632016-11-07 20:00:47 +0000163
Rui Ueyama2f8db1d2017-05-23 21:50:40 +0000164def program_exists(cmd):
Zachary Turnerdc4cbc02017-05-24 00:28:46 +0000165 if sys.platform == 'win32' and not cmd.endswith('.exe'):
166 cmd += '.exe'
Rui Ueyama2f8db1d2017-05-23 21:50:40 +0000167 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 Amini7b484632016-11-07 20:00:47 +0000171
172def 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
186def 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 Knight12167822018-11-16 22:36:17 +0000199def clean_svn(svn_repo):
Mehdi Amini7b484632016-11-07 20:00:47 +0000200 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 Leeb074f142017-12-22 21:19:13 +0000204 for line in svn(svn_repo, 'status', '--no-ignore').split('\n'):
Mehdi Amini7b484632016-11-07 20:00:47 +0000205 if not line.startswith('?'):
206 continue
207 filename = line[1:].strip()
208 os.remove(os.path.join(svn_repo, filename))
209
Mehdi Amini7b484632016-11-07 20:00:47 +0000210
211def 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 Amini7b484632016-11-07 20:00:47 +0000215 svn(svn_root, 'checkout', '--depth=immediates',
216 'https://llvm.org/svn/llvm-project/', '.')
Mehdi Amini7b484632016-11-07 20:00:47 +0000217 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 Knight12167822018-11-16 22:36:17 +0000222def fix_eol_style_native(rev, svn_sr_path, files):
Reid Kleckner45340972017-04-24 22:09:08 +0000223 """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 Kleckner162c5cd2017-05-18 17:17:17 +0000232 # 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 Kleckner45340972017-04-24 22:09:08 +0000234 # 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 Kleckner0f442bc2017-05-12 00:10:19 +0000237 ignore_errors=True)
Reid Kleckner45340972017-04-24 22:09:08 +0000238 crlf_files = []
Reid Kleckner0f442bc2017-05-12 00:10:19 +0000239 if len(files) == 1:
240 # No need to split propget output on ' - ' when we have one file.
Zachary Turnere5f47bb2018-10-09 23:42:28 +0000241 if eol_props.strip() in ['native', 'CRLF']:
Reid Kleckner0f442bc2017-05-12 00:10:19 +0000242 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 Turnere5f47bb2018-10-09 23:42:28 +0000257 if crlf_files:
James Y Knight12167822018-11-16 22:36:17 +0000258 # 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 Turnere5f47bb2018-10-09 23:42:28 +0000261 shell(['dos2unix'] + crlf_files, ignore_errors=True, cwd=svn_sr_path)
Reid Kleckner45340972017-04-24 22:09:08 +0000262
James Y Knight12167822018-11-16 22:36:17 +0000263def 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
271def 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 Amini7b484632016-11-07 20:00:47 +0000282def 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 Knight12167822018-11-16 22:36:17 +0000285 if not files:
Mehdi Amini7b484632016-11-07 20:00:47 +0000286 raise RuntimeError('Empty diff for rev %s?' % rev)
287
James Y Knight12167822018-11-16 22:36:17 +0000288 # 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 Leeb074f142017-12-22 21:19:13 +0000294 status = svn(svn_repo, 'status', '--no-ignore')
Mehdi Amini7b484632016-11-07 20:00:47 +0000295 if status:
296 die("Can't push git rev %s because svn status is not empty:\n%s" %
297 (rev, status))
298
James Y Knight12167822018-11-16 22:36:17 +0000299 svn_dirs_to_update = set()
James Y Knight85362112018-11-16 23:59:23 +0000300 for sr, files in iteritems(subrepo_files):
James Y Knight12167822018-11-16 22:36:17 +0000301 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 Knight85362112018-11-16 23:59:23 +0000308 sorted_dirs_to_update = sorted(svn_dirs_to_update, key=len)
James Y Knight12167822018-11-16 22:36:17 +0000309
310 # SVN update only in the affected directories.
311 svn(svn_repo, 'update', '--depth=immediates', *sorted_dirs_to_update)
312
James Y Knight85362112018-11-16 23:59:23 +0000313 for sr, files in iteritems(subrepo_files):
Mehdi Amini7b484632016-11-07 20:00:47 +0000314 svn_sr_path = os.path.join(svn_repo, GIT_TO_SVN_DIR[sr])
Reid Kleckner45340972017-04-24 22:09:08 +0000315 if os.name == 'nt':
James Y Knight12167822018-11-16 22:36:17 +0000316 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 Amini7b484632016-11-07 20:00:47 +0000320 # git is the only thing that can handle its own patches...
321 log_verbose('Apply patch: %s' % diff)
James Y Knight12167822018-11-16 22:36:17 +0000322 if sr == '':
323 prefix_strip = '-p1'
324 else:
325 prefix_strip = '-p2'
Mehdi Aminifbd26852016-11-12 01:17:59 +0000326 try:
James Y Knight12167822018-11-16 22:36:17 +0000327 # 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 Aminifbd26852016-11-12 01:17:59 +0000331 except RuntimeError as e:
332 eprint("Patch doesn't apply: maybe you should try `git pull -r` "
333 "first?")
334 sys.exit(2)
Mehdi Amini7b484632016-11-07 20:00:47 +0000335
Walter Leeb074f142017-12-22 21:19:13 +0000336 status_lines = svn(svn_repo, 'status', '--no-ignore').split('\n')
Mehdi Amini7b484632016-11-07 20:00:47 +0000337
Walter Leeb074f142017-12-22 21:19:13 +0000338 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 Amini7b484632016-11-07 20:00:47 +0000341 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 Amini5c289b72016-11-30 19:12:53 +0000347 log(svn(svn_repo, 'commit', '-m', commit_msg, '--force-interactive'))
Mehdi Amini7b484632016-11-07 20:00:47 +0000348 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
353def 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 Aminif95a4592016-11-07 20:35:02 +0000369 dot_git_dir = git('rev-parse', '--git-common-dir')
370 svn_root = os.path.join(dot_git_dir, 'llvm-upstream-svn')
Mehdi Amini7b484632016-11-07 20:00:47 +0000371 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 Knight12167822018-11-16 22:36:17 +0000381 clean_svn(svn_root)
Mehdi Amini7b484632016-11-07 20:00:47 +0000382 svn_push_one_rev(svn_root, r, dry_run)
383
384
385if __name__ == '__main__':
Rui Ueyama2f8db1d2017-05-23 21:50:40 +0000386 if not program_exists('svn'):
387 die('error: git-llvm needs svn command, but svn is not installed.')
388
Mehdi Amini7b484632016-11-07 20:00:47 +0000389 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)