GitPython do two files differ in different branches

Not quite as succinct as I would have liked, but this does work. This is a one liner in Bash, and I had hoped to do it with a single python call once I had the repo object established. In general, diffs are not simple to do on blob objects. I had trouble using the difflib and set approaches to diffing the two files, possibly due to the fact that I was originally not decoding the streams. Thus all the splitlines work probably can be simplified.

#!/bin/python3
 
import re
import io
import difflib
 
 
from git import Repo
from git import TagReference
 
 
 
path_to_dir = "./linux"
 
def check_topic_diff():
 
    repo = Repo.init(path_to_dir)
    last = None
    previous = repo.head
 
    for tagref in TagReference.list_items(repo):
        match  = re.search("^your_tag_pattern_here.*$", tagref.name)
        if (match is None):
            continue
        last = tagref
    if previous is None:
        print("no previous, exiting")
        return
 
    if last is None:
        ("No last, exiting")
        return
 
    prev_tree=previous.commit.tree
    last_tree=last.commit.tree
    prev_contents = prev_tree["PATCHES_BY_TOPIC"].data_stream.read().decode('utf-8')
    last_contents = last_tree["PATCHES_BY_TOPIC"].data_stream.read().decode('utf-8')
 
    prev_lines = prev_contents.splitlines()
    last_lines = last_contents.splitlines()
 
    are_diffs = False
    for prev_line in prev_lines:
        last_line = last_lines.pop(0)
        if (last_line == prev_line):
            continue
        are_diffs = True
 
    if (are_diffs):
        print("files differ")
    else:
        print("files are the same")
 
 
#    print(last_contents)
#    print(prev_contents)
#    print (set(prev_contents).difference(set(last_contents)))
 
 
 
HEAD:PATCHES_BY_TOPIC
 
check_topic_diff()

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.