OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 # Monkeypatch IMapIterator so that Ctrl-C can kill everything properly. | 5 # Monkeypatch IMapIterator so that Ctrl-C can kill everything properly. |
6 # Derived from https://gist.github.com/aljungberg/626518 | 6 # Derived from https://gist.github.com/aljungberg/626518 |
7 import multiprocessing.pool | 7 import multiprocessing.pool |
8 from multiprocessing.pool import IMapIterator | 8 from multiprocessing.pool import IMapIterator |
9 def wrapper(func): | 9 def wrapper(func): |
10 def wrap(self, timeout=None): | 10 def wrap(self, timeout=None): |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 git config %s <new_limit> | 312 git config %s <new_limit> |
313 """ % (num, limit, key)) | 313 """ % (num, limit, key)) |
314 sys.exit(1) | 314 sys.exit(1) |
315 | 315 |
316 for line in raw_branches: | 316 for line in raw_branches: |
317 if line.startswith(NO_BRANCH): | 317 if line.startswith(NO_BRANCH): |
318 continue | 318 continue |
319 yield line.split()[-1] | 319 yield line.split()[-1] |
320 | 320 |
321 | 321 |
322 def run_with_retcode(*cmd, **kwargs): | |
323 """Run a command but only return the status code.""" | |
324 try: | |
325 run(*cmd, **kwargs) | |
326 return 0 | |
327 except subprocess2.CalledProcessError as cpe: | |
328 return cpe.returncode | |
329 | |
330 | |
331 def config(option, default=None): | 322 def config(option, default=None): |
332 try: | 323 try: |
333 return run('config', '--get', option) or default | 324 return run('config', '--get', option) or default |
334 except subprocess2.CalledProcessError: | 325 except subprocess2.CalledProcessError: |
335 return default | 326 return default |
336 | 327 |
337 | 328 |
338 def config_list(option): | 329 def config_list(option): |
339 try: | 330 try: |
340 return run('config', '--get-all', option).split() | 331 return run('config', '--get-all', option).split() |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 | 535 |
545 def root(): | 536 def root(): |
546 return config('depot-tools.upstream', 'origin/master') | 537 return config('depot-tools.upstream', 'origin/master') |
547 | 538 |
548 | 539 |
549 def run(*cmd, **kwargs): | 540 def run(*cmd, **kwargs): |
550 """The same as run_with_stderr, except it only returns stdout.""" | 541 """The same as run_with_stderr, except it only returns stdout.""" |
551 return run_with_stderr(*cmd, **kwargs)[0] | 542 return run_with_stderr(*cmd, **kwargs)[0] |
552 | 543 |
553 | 544 |
| 545 def run_with_retcode(*cmd, **kwargs): |
| 546 """Run a command but only return the status code.""" |
| 547 try: |
| 548 run(*cmd, **kwargs) |
| 549 return 0 |
| 550 except subprocess2.CalledProcessError as cpe: |
| 551 return cpe.returncode |
| 552 |
| 553 |
554 def run_stream(*cmd, **kwargs): | 554 def run_stream(*cmd, **kwargs): |
555 """Runs a git command. Returns stdout as a PIPE (file-like object). | 555 """Runs a git command. Returns stdout as a PIPE (file-like object). |
556 | 556 |
557 stderr is dropped to avoid races if the process outputs to both stdout and | 557 stderr is dropped to avoid races if the process outputs to both stdout and |
558 stderr. | 558 stderr. |
559 """ | 559 """ |
560 kwargs.setdefault('stderr', subprocess2.VOID) | 560 kwargs.setdefault('stderr', subprocess2.VOID) |
561 kwargs.setdefault('stdout', subprocess2.PIPE) | 561 kwargs.setdefault('stdout', subprocess2.PIPE) |
562 cmd = (GIT_EXE, '-c', 'color.ui=never') + cmd | 562 cmd = (GIT_EXE, '-c', 'color.ui=never') + cmd |
563 proc = subprocess2.Popen(cmd, **kwargs) | 563 proc = subprocess2.Popen(cmd, **kwargs) |
(...skipping 29 matching lines...) Expand all Loading... |
593 return ret, err | 593 return ret, err |
594 | 594 |
595 | 595 |
596 def set_branch_config(branch, option, value, scope='local'): | 596 def set_branch_config(branch, option, value, scope='local'): |
597 set_config('branch.%s.%s' % (branch, option), value, scope=scope) | 597 set_config('branch.%s.%s' % (branch, option), value, scope=scope) |
598 | 598 |
599 | 599 |
600 def set_config(option, value, scope='local'): | 600 def set_config(option, value, scope='local'): |
601 run('config', '--' + scope, option, value) | 601 run('config', '--' + scope, option, value) |
602 | 602 |
| 603 |
603 def squash_current_branch(header=None, merge_base=None): | 604 def squash_current_branch(header=None, merge_base=None): |
604 header = header or 'git squash commit.' | 605 header = header or 'git squash commit.' |
605 merge_base = merge_base or get_or_create_merge_base(current_branch()) | 606 merge_base = merge_base or get_or_create_merge_base(current_branch()) |
606 log_msg = header + '\n' | 607 log_msg = header + '\n' |
607 if log_msg: | 608 if log_msg: |
608 log_msg += '\n' | 609 log_msg += '\n' |
609 log_msg += run('log', '--reverse', '--format=%H%n%B', '%s..HEAD' % merge_base) | 610 log_msg += run('log', '--reverse', '--format=%H%n%B', '%s..HEAD' % merge_base) |
610 run('reset', '--soft', merge_base) | 611 run('reset', '--soft', merge_base) |
611 run('commit', '-a', '-F', '-', indata=log_msg) | 612 run('commit', '-a', '-F', '-', indata=log_msg) |
612 | 613 |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
716 return ret | 717 return ret |
717 | 718 |
718 | 719 |
719 def upstream(branch): | 720 def upstream(branch): |
720 try: | 721 try: |
721 return run('rev-parse', '--abbrev-ref', '--symbolic-full-name', | 722 return run('rev-parse', '--abbrev-ref', '--symbolic-full-name', |
722 branch+'@{upstream}') | 723 branch+'@{upstream}') |
723 except subprocess2.CalledProcessError: | 724 except subprocess2.CalledProcessError: |
724 return None | 725 return None |
725 | 726 |
| 727 |
726 def get_git_version(): | 728 def get_git_version(): |
727 """Returns a tuple that contains the numeric components of the current git | 729 """Returns a tuple that contains the numeric components of the current git |
728 version.""" | 730 version.""" |
729 version_string = run('--version') | 731 version_string = run('--version') |
730 version_match = re.search(r'(\d+.)+(\d+)', version_string) | 732 version_match = re.search(r'(\d+.)+(\d+)', version_string) |
731 version = version_match.group() if version_match else '' | 733 version = version_match.group() if version_match else '' |
732 | 734 |
733 return tuple(int(x) for x in version.split('.')) | 735 return tuple(int(x) for x in version.split('.')) |
734 | 736 |
735 | 737 |
(...skipping 23 matching lines...) Expand all Loading... |
759 hash=branch_hash, upstream=upstream_branch, ahead=ahead, behind=behind) | 761 hash=branch_hash, upstream=upstream_branch, ahead=ahead, behind=behind) |
760 | 762 |
761 # Set None for upstreams which are not branches (e.g empty upstream, remotes | 763 # Set None for upstreams which are not branches (e.g empty upstream, remotes |
762 # and deleted upstream branches). | 764 # and deleted upstream branches). |
763 missing_upstreams = {} | 765 missing_upstreams = {} |
764 for info in info_map.values(): | 766 for info in info_map.values(): |
765 if info.upstream not in info_map and info.upstream not in missing_upstreams: | 767 if info.upstream not in info_map and info.upstream not in missing_upstreams: |
766 missing_upstreams[info.upstream] = None | 768 missing_upstreams[info.upstream] = None |
767 | 769 |
768 return dict(info_map.items() + missing_upstreams.items()) | 770 return dict(info_map.items() + missing_upstreams.items()) |
OLD | NEW |