Created
June 10, 2013 17:37
-
-
Save tadeu/5750709 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def ExecuteCommandReturningStatusStdoutAndStderr(cmd, input=None, cwd=None, env=None): | |
| from subprocess import Popen, PIPE | |
| pipe = Popen(cmd, shell=False, cwd=cwd, env=env, stdout=PIPE, stderr=PIPE) | |
| (output, errout) = pipe.communicate(input=input) | |
| status = pipe.returncode | |
| return (status, output, errout) | |
| def Main(): | |
| import sys | |
| import re | |
| cyan = '\033[1;36m' | |
| red = '\033[1;31m' | |
| green = '\033[1;32m' | |
| normal = '\033[0m' | |
| down_arrow = '\031' | |
| up_arrow = '\030' | |
| status, output, errout = ExecuteCommandReturningStatusStdoutAndStderr('git branch -vv') | |
| if status != 0: | |
| sys.exit(status) | |
| # get line that starts with ' *' | |
| try: | |
| line = (line for line in output.splitlines() if line.startswith('*')).next() | |
| except StopIteration: | |
| sys.exit(1) | |
| branch_name = line.split(' ')[1] | |
| branch_status = '' | |
| status_search = re.search(r'\[([^\]]+)\]', line) | |
| if status_search: | |
| branch_full_status = re.search(r'\[([^\]]+)\]', line).group(1) | |
| try: | |
| branch_status = re.search(r': (.*)', branch_full_status).group(1) | |
| branch_status = branch_status.replace(', ', ' ') | |
| branch_status = branch_status.replace('behind ', red + down_arrow) | |
| branch_status = branch_status.replace('ahead ', green + up_arrow) | |
| branch_status = ' ' + branch_status | |
| except StandardError: | |
| branch_status = '' | |
| branch_description = ' ' + cyan + '[' + branch_name + branch_status + cyan + ']' + normal | |
| print branch_description | |
| # print status, '#', output, '#', errout | |
| if __name__ == '__main__': | |
| Main() |
Author
Nice! Had to change shell=True in line 4 to work on my system thought!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
May be used to show branch status in prompt, for example, in Take Command:
%@EXECSTR[X:\\scripts\\git-branch-status.py]