GitPythonでdiffをとる方法
Gitpythonを使ってdiffの情報を取得します。
diffの取り方
下のような感じにします!(雑!)
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 git | |
repo = git.Repo('<Repository Path>') | |
t = repo.head.commit.tree | |
print(repo.git.diff(t)) |
view raw gettingGitDiffs.py hosted with ❤ by GitHub
repo.head.commit.tree のdiffをとることで現在のコミットに含まれるすべてのファイルの差分を表示します。
いろんなdiff
他にもいろんなdiffを撮りたい方もいらっしゃると思います。
先ほど t と入れた部分には HEAD や HEAD^ 等、gitコマンドでよく見る奴らを入れることができます。
例えば、repo.git.diff(HEAD^) にすることで、最新のコミットとその一個前のコミットのdiffをとることができます。
この辺りは公式のドキュメントにも記載があります。↓
https://gitpython.readthedocs.io/en/stable/tutorial.html#obtaining-diff-information
以上♪