0

At the command line I can do

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

to enumerate all the changed files. How would I do this in gitpython?

jxramos
  • 7,356
  • 6
  • 57
  • 105

1 Answers1

0

Here's a technique I found snooping around the source to connect a git hash/sha to a particular gitpython Commit object.

# 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 ))

Comparing side by side with shell output

> 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
jxramos
  • 7,356
  • 6
  • 57
  • 105
  • UPDATE just found the [answer](https://stackoverflow.com/a/46008049/1330381) which apparently suggests a shorthand syntax which is also more robust capable of handling references such as `HEAD~1`. The shorthand goes as... `git.repo.fun.name_to_object( git_repo , source_hash ) === git_repo.commmit( source_hash )` – jxramos Sep 05 '19 at 00:53