Last active
June 8, 2018 12:39
-
-
Save deepakduggirala/86b754ac8cfe7e8f5d1ff1ac4c0135b7 to your computer and use it in GitHub Desktop.
Open git diff in github
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
import os | |
import subprocess | |
import re | |
import sys | |
def is_a_git_repo(): | |
return os.path.exists('.git') and os.path.isdir('.git') | |
def split_SSH_URL(s): | |
m = re.search('git@(.*?):(.*?)/(.*?).git', s) | |
urlObj = {} | |
urlObj['hostname'] = m.group(1) | |
urlObj['user'] = m.group(2) | |
urlObj['repo'] = m.group(3) | |
return urlObj | |
def split_https_URL(): | |
return '' | |
def getRemote(): | |
output = subprocess.check_output(['git', 'remote', '-v']) | |
s=output.decode('utf-8').split('\n')[0] | |
return split_SSH_URL(s) | |
def build_URL(urlObj, commit1, commit2): | |
return 'https://'+urlObj['hostname']+'/'+urlObj['user']+'/'+urlObj['repo']+'/compare/'+commit1+'...'+commit2 | |
def open_URL(url): | |
subprocess.check_output(['open', url]) | |
if __name__ == '__main__': | |
if len(sys.argv) == 3: | |
commit1 = sys.argv[1] | |
commit2 = sys.argv[2] | |
open_URL(build_URL(getRemote(), commit1, commit2)) | |
sys.exit(0) | |
else: | |
print('Invalid arguments.\nUsage: python ghdiff <commit1> <commit2>') | |
sys.exit(1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
alias ghdiff='function _ghdiff(){ python ~/ghdiff.py $@; };_ghdiff'