I wanted to get only yaml diff as a string with git
Since it is called gitpython, I get git information Enter with pip
pip install gitpython
Click here for documentation https://pythonhosted.org/GitPython/0.3.1/reference.html
I could use it like this
diff.py 
# coding: utf-8
import git
from . import PATH
"""
git diff HEAD HEAD^
When.I want to take only the difference of the yaml file as a character string
"""
class Diff(object):
    """HEAD and HEAD^The diff of the difference is subtracted by yaml"""
    DELIMITER = "\n\n"
    def __init__(self, repo_path):
        self.repo = git.Repo(repo_path)
        self.head = self.repo.head.commit
        self.parent = self.head.parents[0]
    def yaml_diff_as_patch(self):
        yaml_diffs = [
            unicode(d.diff) for d
            in self.parent.diff(self.head, 
								create_patch=True)
			# .Only files named yaml
            if d.b_blob.name.endswith('.yaml') 
        ]
        return self.DELIMITER.join(yaml_diffs)
if __name__ == '__main__':
    obj = Diff(PATH)
    print obj.yaml_diff_as_patch()
Recommended Posts