Show diff between commits

前端 未结 11 2037
闹比i
闹比i 2020-12-02 04:00

I am using Git on Ubuntu 10.04 (Lucid Lynx).

I have made some commits to my master.

However, I want to get the difference between these commits. All of

11条回答
  •  被撕碎了的回忆
    2020-12-02 04:38

    I wrote a script which displays diff between two commits, works well on Ubuntu.

    https://gist.github.com/jacobabrahamb4/a60624d6274ece7a0bd2d141b53407bc

    #!/usr/bin/env python
    import sys, subprocess, os
    
    TOOLS = ['bcompare', 'meld']
    
    def execute(command):
        return subprocess.check_output(command)
    
    def getTool():
        for tool in TOOLS:
            try:
                out = execute(['which', tool]).strip()
                if tool in out:
                    return tool
            except subprocess.CalledProcessError:
                pass
        return None
    
    def printUsageAndExit():
        print 'Usage: python bdiff.py   '
        print 'Example: python bdiff.py  0 1'
        print 'Example: python bdiff.py  fhejk7fe d78ewg9we'
        print 'Example: python bdiff.py  0 d78ewg9we'
        sys.exit(0)
    
    def getCommitIds(name, first, second):
        commit1 = None
        commit2 = None
        try:
            first_index = int(first) - 1
            second_index = int(second) - 1
            if int(first) < 0 or int(second) < 0:
                print "Cannot handle negative values: "
                sys.exit(0)
            logs = execute(['git', '-C', name, 'log', '--oneline', '--reverse']).splitlines()
            if first_index >= 0:
                commit1 = logs[first_index].split(' ')[0]
            if second_index >= 0:
                commit2 = logs[second_index].split(' ')[0]
        except ValueError:
            if first is not '0':
                commit1 = first
            if second is not '0':
                commit2 = second
        return commit1, commit2
    
    def validateCommitIds(name, commit1, commit2):
        if not commit1 and not commit2:
            print "Nothing to do, exit!"
            return False
        try:
            if commit1:
                execute(['git', '-C', name, 'cat-file', '-t', commit1])
            if commit2:
                execute(['git', '-C', name, 'cat-file', '-t', commit2])
        except subprocess.CalledProcessError:
            return False
        return True
    
    def cleanup(commit1, commit2):
            execute(['rm', '-rf', '/tmp/'+(commit1 if commit1 else '0'), '/tmp/'+(commit2 if commit2 else '0')])
    
    def checkoutCommit(name, commit):
        if commit:
            execute(['git', 'clone', name, '/tmp/'+commit])
            execute(['git', '-C', '/tmp/'+commit, 'checkout', commit])
        else:
            execute(['mkdir', '/tmp/0'])
    
    def compare(tool, commit1, commit2):
            execute([tool, '/tmp/'+(commit1 if commit1 else '0'), '/tmp/'+(commit2 if commit2 else '0')])
    
    if __name__=='__main__':
        tool = getTool()
        if not tool:
            print "No GUI diff tools, install bcompare or meld"
            sys.exit(0)
        if len(sys.argv) is not 4:
            printUsageAndExit()
    
        name, first, second = None, 0, 0
        try:
            name, first, second = sys.argv[1], sys.argv[2], sys.argv[3]
        except IndexError:
            printUsageAndExit()
    
        commit1, commit2 = getCommitIds(name, first, second)
    
        if validateCommitIds(name, commit1, commit2) is False:
            sys.exit(0)
    
        cleanup(commit1, commit2)
    
        try:
            checkoutCommit(name, commit1)
            checkoutCommit(name, commit2)
            compare(tool, commit1, commit2)
        except KeyboardInterrupt:
            pass
        finally:
            cleanup(commit1, commit2)
        sys.exit(0)
    

提交回复
热议问题