Как получить разницу между двумя хэшами в gitpython

В командной строке я могу сделать

git diff --name-only <hashA> <hashB>

для перечисления всех измененных файлов. Как бы я сделал это в gitpython?


person jxramos    schedule 04.09.2019    source источник


Ответы (1)


Вот метод, который я нашел, просматривая источник, чтобы подключить git hash/sha к определенному объекту Commit gitpython.

# test_git_diff.py
import git

#remote_repo="https://github.com/jxramos/DataApp--ParamCompare/commits/master"
git_repo = git.Repo( "." )

source_hash = '825faef1097207479f968c6a5353e41612127849'
target_hash = 'HEAD'

source_commit = git_repo.commit( source_hash )
target_commit = git_repo.commit( target_hash )

git_diff = source_commit.diff( target_commit )

changed_files = [ f.b_path for f in git_diff ]

print( "\n".join( changed_files ))

Сравнение бок о бок с выводом оболочки

> python test_git_diff.py
Model.py
README.md
data_app.py
param_plotting.py
param_stats.py
session_info.py
static/summary_page_styles.css
templates/data_explore_page.html
templates/embed.html
templates/lin_reg_result.html
templates/plot_page.html
templates/summary_page.html
test/test_data/1.x.csv
test/test_data/1.y.csv
>
>
> git diff --name-only 825faef1097207479f968c6a5353e41612127849  HEAD
Model.py
README.md
data_app.py
param_plotting.py
param_stats.py
session_info.py
static/summary_page_styles.css
templates/data_explore_page.html
templates/embed.html
templates/lin_reg_result.html
templates/plot_page.html
templates/summary_page.html
test/test_data/1.x.csv
test/test_data/1.y.csv
person jxramos    schedule 05.09.2019
comment
UPDATE только что нашел ответ, который, по-видимому, предлагает сокращенный синтаксис, который также более надежен и способен обрабатывать такие ссылки, как HEAD~1. Сокращение звучит как... git.repo.fun.name_to_object( git_repo , source_hash ) === git_repo.commmit( source_hash ) - person jxramos; 05.09.2019