Chromium Code Reviews| Index: git_common.py |
| diff --git a/git_common.py b/git_common.py |
| index 4a430c609cbec773edeba785729e30591f857dcc..e58c9bcf90c5cb67b81a3c99e84a81157e961d1d 100644 |
| --- a/git_common.py |
| +++ b/git_common.py |
| @@ -91,6 +91,8 @@ GIT_TRANSIENT_ERRORS = ( |
| GIT_TRANSIENT_ERRORS_RE = re.compile('|'.join(GIT_TRANSIENT_ERRORS), |
| re.IGNORECASE) |
| +# Version which for-each-ref format string first supported upstream:track. |
|
Matt Giuca
2014/09/01 05:12:50
This is hard to read and I don't know what it's sa
calamity
2014/09/01 06:53:41
Done.
|
| +MIN_UPSTREAM_TRACK_GIT_VERSION = (1, 9) |
| class BadCommitRefException(Exception): |
| def __init__(self, refs): |
| @@ -436,8 +438,11 @@ def hash_multi(*reflike): |
| return run('rev-parse', *reflike).splitlines() |
| -def hash_one(reflike): |
| - return run('rev-parse', reflike) |
| +def hash_one(reflike, short=False): |
| + args = ['rev-parse', reflike] |
| + if short: |
| + args.insert(1, '--short') |
| + return run(*args) |
| def in_rebase(): |
| @@ -716,3 +721,36 @@ def upstream(branch): |
| branch+'@{upstream}') |
| except subprocess2.CalledProcessError: |
| return None |
| + |
| +def normalized_version(): |
|
iannucci
2014/08/29 18:41:47
add a docstring, it's not obvious that this is get
Matt Giuca
2014/09/01 05:12:50
Can you rename it to "get_git_version". Maybe then
calamity
2014/09/01 06:53:41
Done.
calamity
2014/09/01 06:53:42
Done. Left the docstring in anyway.
|
| + version_string = run('--version') |
| + version_match = re.search(r'(\d+.)+(\d+)', version_string) |
| + version = version_match.group() if version_match else '' |
| + |
| + return tuple(int(x) for x in version.split('.')) |
| + |
| + |
| +def get_all_tracking_info(): |
| + format_string = ( |
| + '--format=%(refname:short):%(objectname:short):%(upstream:short):') |
| + if normalized_version() >= MIN_UPSTREAM_TRACK_GIT_VERSION: |
| + format_string += '%(upstream:track)' |
| + |
| + info_map = {} |
| + data = run('for-each-ref', format_string, 'refs/heads') |
| + for line in data.splitlines(): |
| + (branch, branch_hash, upstream_branch, tracking_status) = line.split(':') |
|
iannucci
2014/08/29 18:41:47
won't this be wrong if your git version is < 1.9?
calamity
2014/09/01 06:53:42
It makes tracking_status an empty string which the
iannucci
2014/09/02 22:31:27
I think the .split(':') will explode on the assign
|
| + |
| + ahead_match = re.search(r'ahead \d+', tracking_status) |
| + ahead = ahead_match.group() if ahead_match else '' |
|
iannucci
2014/08/29 18:41:47
let's cast these to int() or None for this functio
calamity
2014/09/01 06:53:42
Done.
|
| + |
| + behind_match = re.search(r'behind \d+', tracking_status) |
| + behind = behind_match.group() if behind_match else '' |
| + |
| + info_map[branch] = { |
| + 'hash': branch_hash, |
| + 'upstream': upstream_branch, |
| + 'ahead': ahead, |
| + 'behind': behind |
| + } |
|
iannucci
2014/08/29 18:41:47
let's use a namedtuple for this, instead of a free
calamity
2014/09/01 06:53:42
Oh, cool!
|
| + return info_map |