[kteam-tools][PATCHv2] ktl: Git.current_branch() optimization
Kamal Mostafa
kamal at canonical.com
Thu May 30 19:57:23 UTC 2013
Query 'git symbolic-ref HEAD' instead of iterating the 'git branch' list.
Also changed behavior if current_branch() is called in a detached HEAD state:
before: returned the string "(no branch)"
now: throws an exception ktl.git.GetError GitError("no current branch")
No existing caller depends on the old "(no branch)" behavior.
Signed-off-by: Kamal Mostafa <kamal at canonical.com>
---
ktl/git.py | 16 +++++-----------
1 file changed, 5 insertions(+), 11 deletions(-)
diff --git a/ktl/git.py b/ktl/git.py
index 9483ad4..28412af 100644
--- a/ktl/git.py
+++ b/ktl/git.py
@@ -94,17 +94,11 @@ class Git:
#
@classmethod
def current_branch(cls):
- retval = ""
- status, result = run_command("git branch", cls.debug)
- if status == 0:
- for line in result:
- if line != '' and line[0] == '*':
- retval = line[1:].strip()
- break
- else:
- raise GitError(result)
-
- return retval
+ # Note: older versions of git symbolic-ref do not support --short
+ status, result = run_command("git symbolic-ref HEAD", cls.debug)
+ if status != 0:
+ raise GitError("no current branch")
+ return result[0].lstrip("refs/heads/")
# show
#
--
1.8.1.2
More information about the kernel-team
mailing list