diff options
author | Alexis Christoforides <alexis@thenull.net> | 2017-02-09 23:17:29 +0300 |
---|---|---|
committer | Alexis Christoforides <alexis@thenull.net> | 2017-02-09 23:17:29 +0300 |
commit | 803d55f454a7e210d09984cc6f81676fd6f7bf74 (patch) | |
tree | 8d2d2f6bbf843b2de58dbb1403bcb63e8d4c0516 | |
parent | 4d33a73ffb78397c987f1d04abd4b9e0b1569384 (diff) |
Small Git operation improvements
-rw-r--r-- | bockbuild/util/util.py | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/bockbuild/util/util.py b/bockbuild/util/util.py index 43f2efa..794277b 100644 --- a/bockbuild/util/util.py +++ b/bockbuild/util/util.py @@ -332,12 +332,26 @@ def find_git(self, echo=False): error('git not found in PATH') @retry - def git_operation(self, args, cwd, hazard = False, output_on_fail = None, singleline_output = False): + def git_operation(self, args, cwd, hazard = False, allow_fail = False, singleline_output = False): if hazard: root = git_rootdir (self, cwd) assert_modifiable_repo (root) - (exit, out, err) = run(git_bin, args.split(' '), cwd) - return out.split('\n') + try: + (exit, out, err) = run(git_bin, args.split(' '), cwd) + except CommandException: + if allow_fail: + return None + else: + raise + + lines = out.split('\n') + if singleline_output: + if len(lines) > 1: + error ('Single line output expected from git. Received the following:\n%s' % out) + else: + return lines[0] + + return lines self.git = git_operation.__get__(self, self.__class__) self.git_bin = git_bin @@ -360,14 +374,7 @@ def git_get_revision(self, cwd): def git_get_branch(self, cwd): - revision = git_get_revision(self, cwd) - try: - output = self.git('symbolic-ref -q --short HEAD', cwd) - except: - return None # detached HEAD - else: - return output[0] - + return self.git('symbolic-ref -q --short HEAD', cwd, allow_fail = True, singleline_output = True) def git_is_dirty(self, cwd): return 'dirty' in git_shortid (self, cwd) @@ -383,7 +390,7 @@ def git_shortid(self, cwd): if branch is None: return short_rev else: - return '%s-%s' % (branch, short_rev) + return '%s@%s' % (branch, short_rev) def git_isrootdir(self, cwd): try: @@ -639,7 +646,7 @@ def run(cmd, args, cwd, env=None): raise CommandException('"%s" failed, error code %s\nstderr:\n%s' % ( cmd + str(args), exit_code, stderr), cwd=cwd) - return (exit_code, stdout, stderr) + return (exit_code, stdout[:-1], stderr) def run_shell(cmd, print_cmd=False, cwd=None): |