Get the contents of git diff from python

Background

I wanted to get only yaml diff as a string with git

use git from python

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

Get the contents of git diff from python
[Python] Get the text of the law from the e-GOV Law API
Get the return code of the Python script from bat
Existence from the viewpoint of Python
Learning notes from the beginning of Python 1
Launch the Python interpreter from Git bash
[Python] Get the character code of the file
Learning notes from the beginning of Python 2
[Python] Get the main color from the screenshot
[Python] Get the update date of a news article from HTML
The contents of the Python tutorial (Chapter 5) are itemized.
The contents of the Python tutorial (Chapter 4) are itemized.
[Python] Get / edit the scale label of the figure
The contents of the Python tutorial (Chapter 2) are itemized.
[Python] Get the main topics of Yahoo News
Get the caller of a function in Python
The contents of the Python tutorial (Chapter 8) are itemized.
The contents of the Python tutorial (Chapter 1) are itemized.
the zen of Python
The contents of the Python tutorial (Chapter 10) are itemized.
[Python] Get the last updated date of the website
[Python3] Get date diff
The contents of the Python tutorial (Chapter 6) are itemized.
The contents of the Python tutorial (Chapter 3) are itemized.
[Python] Get the day of the week (English & Japanese)
Get the update date of the Python memo file.
The wall of changing the Django service from Python 2.7 to Python 3
Template of python script to read the contents of the file
How to get the number of digits in Python
Learn Nim with Python (from the beginning of the year).
[Python] Get the official file path of the shortcut file (.lnk)
[python] Get the list of classes defined in the module
Study from the beginning of Python Hour1: Hello World
Python points from the perspective of a C programmer
Get the size (number of elements) of UnionFind in Python
Not being aware of the contents of the data in python
Try to get the contents of Word with Golang
[Python] Get the list of ExifTags names of Pillow library
[Maya Python] Crush the contents of the script 2 ~ list Notes
Study from the beginning of Python Hour8: Using packages
Get the operation status of JR West with Python
[Python] Get the number of views of all posted articles
Get the URL of the HTTP redirect destination in Python
Towards the retirement of Python2
About the ease of Python
Get the number of digits
[Python] Get the previous month
About the features of Python
Simulation of the contents of the wallet
The Power of Pandas: Python
Get the value of a specific key in a list from the dictionary type in the list with Python
Try to get the function list of Python> os package
Get your heart rate from the fitbit API in Python!
Analyzing user dissatisfaction very easily from the contents of inquiries
[Maya Python] Crush the contents of the script 3 ~ List unknown Plugins
Different from the import type of python. from A import B meaning
[Maya Python] Crush the contents of the script 1 ~ Camera Speed Editor
Get the package version to register with PyPI from Git
Get the number of specific elements in a python list
Get the value while specifying the default value from dict in Python
What beginners learned from the basics of variables in python