Last active
June 16, 2016 13:40
-
-
Save omaraboumrad/1a141c12057128ca17762014eaddf686 to your computer and use it in GitHub Desktop.
GitHub-like commit differences
This file contains 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
#!/usr/bin/env python | |
""" | |
Checks the commit difference between current branch and target branch | |
a la GitHub. | |
Usage: | |
$ git cdiff upstream/master | |
This branch is 5 commits ahead, 10 commits behind upstream/master. | |
Installation: | |
- Make executable: chmod 755 git-cdiff | |
- Make accessible in path | |
""" | |
import subprocess | |
import sys | |
class GitException(Exception): | |
pass | |
def commit_diff(target): | |
cmd = ['git', '--no-pager', 'log', '--oneline', target] | |
process = subprocess.Popen(cmd, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
std_out, std_err = process.communicate() | |
if std_err: | |
raise GitException(std_err.strip()) | |
else: | |
return len(std_out.strip().splitlines()) | |
if __name__ == '__main__': | |
try: | |
branch = sys.argv[1] | |
print("This branch is {} commits ahead, {} commits behind {}".format( | |
commit_diff('{}..'.format(branch)), | |
commit_diff('..{}'.format(branch)), | |
branch)) | |
except IndexError: | |
print('Target branch name required') | |
exit(1) | |
except GitException as e: | |
print(e.message) | |
exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment