blob: 80398baa9574fcc086fb5fc3b6b3c7a0d6a35280 [file] [log] [blame]
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +00001# Copyright 2014 The Chromium Authors. All rights reserved.
iannucci@chromium.orgaa74cf62013-11-19 20:00:49 +00002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5# Monkeypatch IMapIterator so that Ctrl-C can kill everything properly.
6# Derived from https://gist.github.com/aljungberg/626518
7import multiprocessing.pool
8from multiprocessing.pool import IMapIterator
9def wrapper(func):
10 def wrap(self, timeout=None):
11 return func(self, timeout=timeout or 1e100)
12 return wrap
13IMapIterator.next = wrapper(IMapIterator.next)
14IMapIterator.__next__ = IMapIterator.next
15# TODO(iannucci): Monkeypatch all other 'wait' methods too.
16
17
18import binascii
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000019import collections
iannucci@chromium.orgaa74cf62013-11-19 20:00:49 +000020import contextlib
21import functools
22import logging
iannucci@chromium.org97345eb2014-03-13 07:55:15 +000023import os
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000024import re
iannucci@chromium.orgaa74cf62013-11-19 20:00:49 +000025import signal
26import sys
27import tempfile
28import threading
29
30import subprocess2
31
32
33GIT_EXE = 'git.bat' if sys.platform.startswith('win') else 'git'
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000034TEST_MODE = False
35
36FREEZE = 'FREEZE'
37FREEZE_SECTIONS = {
38 'indexed': 'soft',
39 'unindexed': 'mixed'
40}
41FREEZE_MATCHER = re.compile(r'%s.(%s)' % (FREEZE, '|'.join(FREEZE_SECTIONS)))
iannucci@chromium.orgaa74cf62013-11-19 20:00:49 +000042
43
44class BadCommitRefException(Exception):
45 def __init__(self, refs):
46 msg = ('one of %s does not seem to be a valid commitref.' %
47 str(refs))
48 super(BadCommitRefException, self).__init__(msg)
49
50
51def memoize_one(**kwargs):
52 """Memoizes a single-argument pure function.
53
54 Values of None are not cached.
55
56 Kwargs:
57 threadsafe (bool) - REQUIRED. Specifies whether to use locking around
58 cache manipulation functions. This is a kwarg so that users of memoize_one
59 are forced to explicitly and verbosely pick True or False.
60
61 Adds three methods to the decorated function:
62 * get(key, default=None) - Gets the value for this key from the cache.
63 * set(key, value) - Sets the value for this key from the cache.
64 * clear() - Drops the entire contents of the cache. Useful for unittests.
65 * update(other) - Updates the contents of the cache from another dict.
66 """
67 assert 'threadsafe' in kwargs, 'Must specify threadsafe={True,False}'
68 threadsafe = kwargs['threadsafe']
69
70 if threadsafe:
71 def withlock(lock, f):
72 def inner(*args, **kwargs):
73 with lock:
74 return f(*args, **kwargs)
75 return inner
76 else:
77 def withlock(_lock, f):
78 return f
79
80 def decorator(f):
81 # Instantiate the lock in decorator, in case users of memoize_one do:
82 #
83 # memoizer = memoize_one(threadsafe=True)
84 #
85 # @memoizer
86 # def fn1(val): ...
87 #
88 # @memoizer
89 # def fn2(val): ...
90
91 lock = threading.Lock() if threadsafe else None
92 cache = {}
93 _get = withlock(lock, cache.get)
94 _set = withlock(lock, cache.__setitem__)
95
96 @functools.wraps(f)
97 def inner(arg):
98 ret = _get(arg)
99 if ret is None:
100 ret = f(arg)
101 if ret is not None:
102 _set(arg, ret)
103 return ret
104 inner.get = _get
105 inner.set = _set
106 inner.clear = withlock(lock, cache.clear)
107 inner.update = withlock(lock, cache.update)
108 return inner
109 return decorator
110
111
112def _ScopedPool_initer(orig, orig_args): # pragma: no cover
113 """Initializer method for ScopedPool's subprocesses.
114
115 This helps ScopedPool handle Ctrl-C's correctly.
116 """
117 signal.signal(signal.SIGINT, signal.SIG_IGN)
118 if orig:
119 orig(*orig_args)
120
121
122@contextlib.contextmanager
123def ScopedPool(*args, **kwargs):
124 """Context Manager which returns a multiprocessing.pool instance which
125 correctly deals with thrown exceptions.
126
127 *args - Arguments to multiprocessing.pool
128
129 Kwargs:
130 kind ('threads', 'procs') - The type of underlying coprocess to use.
131 **etc - Arguments to multiprocessing.pool
132 """
133 if kwargs.pop('kind', None) == 'threads':
134 pool = multiprocessing.pool.ThreadPool(*args, **kwargs)
135 else:
136 orig, orig_args = kwargs.get('initializer'), kwargs.get('initargs', ())
137 kwargs['initializer'] = _ScopedPool_initer
138 kwargs['initargs'] = orig, orig_args
139 pool = multiprocessing.pool.Pool(*args, **kwargs)
140
141 try:
142 yield pool
143 pool.close()
144 except:
145 pool.terminate()
146 raise
147 finally:
148 pool.join()
149
150
151class ProgressPrinter(object):
152 """Threaded single-stat status message printer."""
iannucci@chromium.org97345eb2014-03-13 07:55:15 +0000153 def __init__(self, fmt, enabled=None, fout=sys.stderr, period=0.5):
iannucci@chromium.orgaa74cf62013-11-19 20:00:49 +0000154 """Create a ProgressPrinter.
155
156 Use it as a context manager which produces a simple 'increment' method:
157
158 with ProgressPrinter('(%%(count)d/%d)' % 1000) as inc:
159 for i in xrange(1000):
160 # do stuff
161 if i % 10 == 0:
162 inc(10)
163
164 Args:
165 fmt - String format with a single '%(count)d' where the counter value
166 should go.
167 enabled (bool) - If this is None, will default to True if
168 logging.getLogger() is set to INFO or more verbose.
iannucci@chromium.org97345eb2014-03-13 07:55:15 +0000169 fout (file-like) - The stream to print status messages to.
iannucci@chromium.orgaa74cf62013-11-19 20:00:49 +0000170 period (float) - The time in seconds for the printer thread to wait
171 between printing.
172 """
173 self.fmt = fmt
174 if enabled is None: # pragma: no cover
175 self.enabled = logging.getLogger().isEnabledFor(logging.INFO)
176 else:
177 self.enabled = enabled
178
179 self._count = 0
180 self._dead = False
181 self._dead_cond = threading.Condition()
iannucci@chromium.org97345eb2014-03-13 07:55:15 +0000182 self._stream = fout
iannucci@chromium.orgaa74cf62013-11-19 20:00:49 +0000183 self._thread = threading.Thread(target=self._run)
184 self._period = period
185
186 def _emit(self, s):
187 if self.enabled:
188 self._stream.write('\r' + s)
189 self._stream.flush()
190
191 def _run(self):
192 with self._dead_cond:
193 while not self._dead:
194 self._emit(self.fmt % {'count': self._count})
195 self._dead_cond.wait(self._period)
196 self._emit((self.fmt + '\n') % {'count': self._count})
197
198 def inc(self, amount=1):
199 self._count += amount
200
201 def __enter__(self):
202 self._thread.start()
203 return self.inc
204
205 def __exit__(self, _exc_type, _exc_value, _traceback):
206 self._dead = True
207 with self._dead_cond:
208 self._dead_cond.notifyAll()
209 self._thread.join()
210 del self._thread
211
212
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000213def once(function):
214 """@Decorates |function| so that it only performs its action once, no matter
215 how many times the decorated |function| is called."""
216 def _inner_gen():
217 yield function()
218 while True:
219 yield
220 return _inner_gen().next
221
222
223## Git functions
224
225
226def branch_config(branch, option, default=None):
227 return config('branch.%s.%s' % (branch, option), default=default)
228
229
230def branch_config_map(option):
231 """Return {branch: <|option| value>} for all branches."""
232 try:
233 reg = re.compile(r'^branch\.(.*)\.%s$' % option)
234 lines = run('config', '--get-regexp', reg.pattern).splitlines()
235 return {reg.match(k).group(1): v for k, v in (l.split() for l in lines)}
236 except subprocess2.CalledProcessError:
237 return {}
238
239
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +0000240def branches(*args):
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000241 NO_BRANCH = ('* (no branch', '* (detached from ')
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +0000242 for line in run('branch', *args).splitlines():
243 if line.startswith(NO_BRANCH):
244 continue
245 yield line.split()[-1]
246
247
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000248def config(option, default=None):
249 try:
250 return run('config', '--get', option) or default
251 except subprocess2.CalledProcessError:
252 return default
253
254
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +0000255def config_list(option):
256 try:
257 return run('config', '--get-all', option).split()
258 except subprocess2.CalledProcessError:
259 return []
260
261
262def current_branch():
iannucci@chromium.orgaa74cf62013-11-19 20:00:49 +0000263 try:
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000264 return run('rev-parse', '--abbrev-ref', 'HEAD')
iannucci@chromium.orgaa74cf62013-11-19 20:00:49 +0000265 except subprocess2.CalledProcessError:
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000266 return None
iannucci@chromium.orgaa74cf62013-11-19 20:00:49 +0000267
268
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000269def del_branch_config(branch, option, scope='local'):
270 del_config('branch.%s.%s' % (branch, option), scope=scope)
iannucci@chromium.orgaa74cf62013-11-19 20:00:49 +0000271
iannucci@chromium.orgaa74cf62013-11-19 20:00:49 +0000272
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000273def del_config(option, scope='local'):
274 try:
275 run('config', '--' + scope, '--unset', option)
276 except subprocess2.CalledProcessError:
277 pass
278
279
280def freeze():
281 took_action = False
282
283 try:
284 run('commit', '-m', FREEZE + '.indexed')
285 took_action = True
286 except subprocess2.CalledProcessError:
287 pass
288
289 try:
290 run('add', '-A')
291 run('commit', '-m', FREEZE + '.unindexed')
292 took_action = True
293 except subprocess2.CalledProcessError:
294 pass
295
296 if not took_action:
297 return 'Nothing to freeze.'
298
299
300def get_branch_tree():
301 """Get the dictionary of {branch: parent}, compatible with topo_iter.
302
303 Returns a tuple of (skipped, <branch_tree dict>) where skipped is a set of
304 branches without upstream branches defined.
iannucci@chromium.orgaa74cf62013-11-19 20:00:49 +0000305 """
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000306 skipped = set()
307 branch_tree = {}
iannucci@chromium.org97345eb2014-03-13 07:55:15 +0000308
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000309 for branch in branches():
310 parent = upstream(branch)
311 if not parent:
312 skipped.add(branch)
313 continue
314 branch_tree[branch] = parent
iannucci@chromium.org97345eb2014-03-13 07:55:15 +0000315
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000316 return skipped, branch_tree
iannucci@chromium.orgaa74cf62013-11-19 20:00:49 +0000317
318
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000319def get_or_create_merge_base(branch, parent=None):
320 """Finds the configured merge base for branch.
iannucci@chromium.org97345eb2014-03-13 07:55:15 +0000321
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000322 If parent is supplied, it's used instead of calling upstream(branch).
iannucci@chromium.org97345eb2014-03-13 07:55:15 +0000323 """
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000324 base = branch_config(branch, 'base')
325 if base:
326 try:
327 run('merge-base', '--is-ancestor', base, branch)
328 logging.debug('Found pre-set merge-base for %s: %s', branch, base)
329 except subprocess2.CalledProcessError:
330 logging.debug('Found WRONG pre-set merge-base for %s: %s', branch, base)
331 base = None
332
333 if not base:
334 base = run('merge-base', parent or upstream(branch), branch)
335 manual_merge_base(branch, base)
336
337 return base
iannucci@chromium.org97345eb2014-03-13 07:55:15 +0000338
339
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000340def hash_multi(*reflike):
341 return run('rev-parse', *reflike).splitlines()
iannucci@chromium.org97345eb2014-03-13 07:55:15 +0000342
343
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +0000344def hash_one(reflike):
345 return run('rev-parse', reflike)
346
347
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000348def in_rebase():
349 git_dir = run('rev-parse', '--git-dir')
350 return (
351 os.path.exists(os.path.join(git_dir, 'rebase-merge')) or
352 os.path.exists(os.path.join(git_dir, 'rebase-apply')))
iannucci@chromium.orgaa74cf62013-11-19 20:00:49 +0000353
354
355def intern_f(f, kind='blob'):
356 """Interns a file object into the git object store.
357
358 Args:
359 f (file-like object) - The file-like object to intern
360 kind (git object type) - One of 'blob', 'commit', 'tree', 'tag'.
361
362 Returns the git hash of the interned object (hex encoded).
363 """
364 ret = run('hash-object', '-t', kind, '-w', '--stdin', stdin=f)
365 f.close()
366 return ret
367
368
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000369def is_dormant(branch):
370 # TODO(iannucci): Do an oldness check?
371 return branch_config(branch, 'dormant', 'false') != 'false'
372
373
374def manual_merge_base(branch, base):
375 set_branch_config(branch, 'base', base)
376
377
378def mktree(treedict):
379 """Makes a git tree object and returns its hash.
380
381 See |tree()| for the values of mode, type, and ref.
382
383 Args:
384 treedict - { name: (mode, type, ref) }
385 """
386 with tempfile.TemporaryFile() as f:
387 for name, (mode, typ, ref) in treedict.iteritems():
388 f.write('%s %s %s\t%s\0' % (mode, typ, ref, name))
389 f.seek(0)
390 return run('mktree', '-z', stdin=f)
391
392
393def parse_commitrefs(*commitrefs):
394 """Returns binary encoded commit hashes for one or more commitrefs.
395
396 A commitref is anything which can resolve to a commit. Popular examples:
397 * 'HEAD'
398 * 'origin/master'
399 * 'cool_branch~2'
400 """
401 try:
402 return map(binascii.unhexlify, hash_multi(*commitrefs))
403 except subprocess2.CalledProcessError:
404 raise BadCommitRefException(commitrefs)
405
406
407RebaseRet = collections.namedtuple('RebaseRet', 'success message')
408
409
410def rebase(parent, start, branch, abort=False):
411 """Rebases |start|..|branch| onto the branch |parent|.
412
413 Args:
414 parent - The new parent ref for the rebased commits.
415 start - The commit to start from
416 branch - The branch to rebase
417 abort - If True, will call git-rebase --abort in the event that the rebase
418 doesn't complete successfully.
419
420 Returns a namedtuple with fields:
421 success - a boolean indicating that the rebase command completed
422 successfully.
423 message - if the rebase failed, this contains the stdout of the failed
424 rebase.
425 """
426 try:
427 args = ['--onto', parent, start, branch]
428 if TEST_MODE:
429 args.insert(0, '--committer-date-is-author-date')
430 run('rebase', *args)
431 return RebaseRet(True, '')
432 except subprocess2.CalledProcessError as cpe:
433 if abort:
434 run('rebase', '--abort')
iannucci@chromium.org56a624a2014-03-26 21:23:09 +0000435 return RebaseRet(False, cpe.stdout)
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000436
437
438def remove_merge_base(branch):
439 del_branch_config(branch, 'base')
440
441
442def root():
443 return config('depot-tools.upstream', 'origin/master')
444
445
446def run(*cmd, **kwargs):
447 """The same as run_with_stderr, except it only returns stdout."""
448 return run_with_stderr(*cmd, **kwargs)[0]
449
450
451def run_stream(*cmd, **kwargs):
452 """Runs a git command. Returns stdout as a PIPE (file-like object).
453
454 stderr is dropped to avoid races if the process outputs to both stdout and
455 stderr.
456 """
457 kwargs.setdefault('stderr', subprocess2.VOID)
458 kwargs.setdefault('stdout', subprocess2.PIPE)
459 cmd = (GIT_EXE,) + cmd
460 proc = subprocess2.Popen(cmd, **kwargs)
461 return proc.stdout
462
463
464def run_with_stderr(*cmd, **kwargs):
465 """Runs a git command.
466
467 Returns (stdout, stderr) as a pair of strings.
468
469 kwargs
470 autostrip (bool) - Strip the output. Defaults to True.
471 indata (str) - Specifies stdin data for the process.
472 """
473 kwargs.setdefault('stdin', subprocess2.PIPE)
474 kwargs.setdefault('stdout', subprocess2.PIPE)
475 kwargs.setdefault('stderr', subprocess2.PIPE)
476 autostrip = kwargs.pop('autostrip', True)
477 indata = kwargs.pop('indata', None)
478
479 cmd = (GIT_EXE,) + cmd
480 proc = subprocess2.Popen(cmd, **kwargs)
481 ret, err = proc.communicate(indata)
482 retcode = proc.wait()
483 if retcode != 0:
484 raise subprocess2.CalledProcessError(retcode, cmd, os.getcwd(), ret, err)
485
486 if autostrip:
487 ret = (ret or '').strip()
488 err = (err or '').strip()
489
490 return ret, err
491
492
493def set_branch_config(branch, option, value, scope='local'):
494 set_config('branch.%s.%s' % (branch, option), value, scope=scope)
495
496
497def set_config(option, value, scope='local'):
498 run('config', '--' + scope, option, value)
499
500def squash_current_branch(header=None, merge_base=None):
501 header = header or 'git squash commit.'
502 merge_base = merge_base or get_or_create_merge_base(current_branch())
503 log_msg = header + '\n'
504 if log_msg:
505 log_msg += '\n'
506 log_msg += run('log', '--reverse', '--format=%H%n%B', '%s..HEAD' % merge_base)
507 run('reset', '--soft', merge_base)
508 run('commit', '-a', '-F', '-', indata=log_msg)
509
510
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +0000511def tags(*args):
512 return run('tag', *args).splitlines()
513
514
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +0000515def thaw():
516 took_action = False
517 for sha in (s.strip() for s in run_stream('rev-list', 'HEAD').xreadlines()):
518 msg = run('show', '--format=%f%b', '-s', 'HEAD')
519 match = FREEZE_MATCHER.match(msg)
520 if not match:
521 if not took_action:
522 return 'Nothing to thaw.'
523 break
524
525 run('reset', '--' + FREEZE_SECTIONS[match.group(1)], sha)
526 took_action = True
527
528
529def topo_iter(branch_tree, top_down=True):
530 """Generates (branch, parent) in topographical order for a branch tree.
531
532 Given a tree:
533
534 A1
535 B1 B2
536 C1 C2 C3
537 D1
538
539 branch_tree would look like: {
540 'D1': 'C3',
541 'C3': 'B2',
542 'B2': 'A1',
543 'C1': 'B1',
544 'C2': 'B1',
545 'B1': 'A1',
546 }
547
548 It is OK to have multiple 'root' nodes in your graph.
549
550 if top_down is True, items are yielded from A->D. Otherwise they're yielded
551 from D->A. Within a layer the branches will be yielded in sorted order.
552 """
553 branch_tree = branch_tree.copy()
554
555 # TODO(iannucci): There is probably a more efficient way to do these.
556 if top_down:
557 while branch_tree:
558 this_pass = [(b, p) for b, p in branch_tree.iteritems()
559 if p not in branch_tree]
560 assert this_pass, "Branch tree has cycles: %r" % branch_tree
561 for branch, parent in sorted(this_pass):
562 yield branch, parent
563 del branch_tree[branch]
564 else:
565 parent_to_branches = collections.defaultdict(set)
566 for branch, parent in branch_tree.iteritems():
567 parent_to_branches[parent].add(branch)
568
569 while branch_tree:
570 this_pass = [(b, p) for b, p in branch_tree.iteritems()
571 if not parent_to_branches[b]]
572 assert this_pass, "Branch tree has cycles: %r" % branch_tree
573 for branch, parent in sorted(this_pass):
574 yield branch, parent
575 parent_to_branches[parent].discard(branch)
576 del branch_tree[branch]
577
578
iannucci@chromium.orgaa74cf62013-11-19 20:00:49 +0000579def tree(treeref, recurse=False):
580 """Returns a dict representation of a git tree object.
581
582 Args:
583 treeref (str) - a git ref which resolves to a tree (commits count as trees).
584 recurse (bool) - include all of the tree's decendants too. File names will
585 take the form of 'some/path/to/file'.
586
587 Return format:
588 { 'file_name': (mode, type, ref) }
589
590 mode is an integer where:
591 * 0040000 - Directory
592 * 0100644 - Regular non-executable file
593 * 0100664 - Regular non-executable group-writeable file
594 * 0100755 - Regular executable file
595 * 0120000 - Symbolic link
596 * 0160000 - Gitlink
597
598 type is a string where it's one of 'blob', 'commit', 'tree', 'tag'.
599
600 ref is the hex encoded hash of the entry.
601 """
602 ret = {}
603 opts = ['ls-tree', '--full-tree']
604 if recurse:
605 opts.append('-r')
606 opts.append(treeref)
607 try:
608 for line in run(*opts).splitlines():
609 mode, typ, ref, name = line.split(None, 3)
610 ret[name] = (mode, typ, ref)
611 except subprocess2.CalledProcessError:
612 return None
613 return ret
614
615
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +0000616def upstream(branch):
617 try:
618 return run('rev-parse', '--abbrev-ref', '--symbolic-full-name',
619 branch+'@{upstream}')
620 except subprocess2.CalledProcessError:
621 return None