Chromium Code Reviews| Index: git_common.py |
| diff --git a/git_common.py b/git_common.py |
| index 1215d9cd41d44385f98583c884188b006b8561f1..fa6d3fc75556b1e4a9a61724315eee4ab4fd9f7c 100644 |
| --- a/git_common.py |
| +++ b/git_common.py |
| @@ -27,8 +27,22 @@ import threading |
| import subprocess2 |
| +class DEFAULT(object): |
|
agable
2014/02/28 19:54:58
Why? This is so completely unnecessary. Maybe kind
iannucci
2014/03/06 00:18:39
*sigh* going back to useless-mode. lcd for the win
|
| + def __init__(self, constructor): |
| + self.constructor = constructor |
| + |
| + @staticmethod |
| + def get_from(inst_or_obj): |
| + if isinstance(inst_or_obj, DEFAULT): |
| + return inst_or_obj.constructor() |
| + else: |
| + return inst_or_obj |
| + |
| + |
| GIT_EXE = 'git.bat' if sys.platform.startswith('win') else 'git' |
| +NO_BRANCH = ('* (no branch)', '* (detached from ') |
| + |
| class BadCommitRefException(Exception): |
| def __init__(self, refs): |
| @@ -199,6 +213,28 @@ class ProgressPrinter(object): |
| del self._thread |
| +def abbrev(ref): |
| + return run('rev-parse', '--abbrev-ref', ref) |
| + |
| + |
| +def branches(*args): |
| + for line in run('branch', *args).splitlines(): |
| + if line.startswith(NO_BRANCH): |
| + continue |
| + yield line.split()[-1] |
| + |
| + |
| +def config_list(option, default=DEFAULT(list)): |
|
agable
2014/02/28 19:54:58
default=None
if default is None: default=[]
Let's
iannucci
2014/03/06 00:18:39
sometimes I hate python. The =None idiom is so uni
agable
2014/03/06 18:09:07
I know, and I totally agree. But I don't think thi
|
| + try: |
| + return run('config', '--get-all', option).split() |
| + except subprocess2.CalledProcessError: |
| + return DEFAULT.get_from(default) |
| + |
| + |
| +def current_branch(): |
| + return abbrev('HEAD') |
| + |
| + |
| def parse_commitrefs(*commitrefs): |
| """Returns binary encoded commit hashes for one or more commitrefs. |
| @@ -208,7 +244,7 @@ def parse_commitrefs(*commitrefs): |
| * 'cool_branch~2' |
| """ |
| try: |
| - return map(binascii.unhexlify, hashes(*commitrefs)) |
| + return map(binascii.unhexlify, hash_multi(*commitrefs)) |
| except subprocess2.CalledProcessError: |
| raise BadCommitRefException(commitrefs) |
| @@ -231,7 +267,11 @@ def run(*cmd, **kwargs): |
| return ret |
| -def hashes(*reflike): |
| +def hash_one(reflike): |
| + return run('rev-parse', reflike) |
| + |
| + |
| +def hash_multi(*reflike): |
|
agable
2014/02/28 19:54:58
This should be "*reflikes" (cf. "*args").
iannucci
2014/03/06 00:18:39
Done.
|
| return run('rev-parse', *reflike).splitlines() |
| @@ -249,6 +289,13 @@ def intern_f(f, kind='blob'): |
| return ret |
| +def tags(*args): |
| + for line in run('tag', *args).splitlines(): |
| + if line.startswith(NO_BRANCH): |
| + continue |
| + yield line.split()[-1] |
| + |
| + |
| def tree(treeref, recurse=False): |
| """Returns a dict representation of a git tree object. |
| @@ -286,6 +333,14 @@ def tree(treeref, recurse=False): |
| return ret |
| +def upstream(branch): |
| + try: |
| + return run('rev-parse', '--abbrev-ref', '--symbolic-full-name', |
| + branch+'@{upstream}') |
| + except subprocess2.CalledProcessError: |
| + return None |
| + |
| + |
| def mktree(treedict): |
| """Makes a git tree object and returns its hash. |