How to query the log of a specific git repo branch using gitpython?
Asked Answered
F

1

0

Goal: Perform the following git command within a python script using gitpython.

cmdLine Version:

git log -L :dataLoad:/home/ubuntu/projects/alpha-draw/py/enigma.py

Script Version:

repo.git.log(f'-L :dataLoad:/home/ubuntu/projects/alpha-draw/py/enigma.py')

This results in the following error,

git.exc.GitCommandError: Cmd('git') failed due to: exit code(128)
cmdline: git log -L :dataLoad:/home/ubuntu/projects/alpha-draw/py/enigma.py
stderr: 'fatal: -L argument not 'start,end:file' or ':funcname:file':  :dataLoad:/home/ubuntu/projects/alpha-draw/py/enigma.py'

When I copy and paste the cmdLine version from the error straight into... well the command line it works as expected.

My hypothesis is that gitpython is looking at the master branch, when the actual working branch is not master.

Reasons for this hypothesis:

1) I have found many posts discussing the use of the command

repo.git.checkout("<branch>")

This has the effect of letting me know that my branch is up to date. When I query the commits from the repo after using this command I still get commits from master.

2) Another reason to believe this is an issue with not successfully changing the branch within gitpython is with the following example.

commits = list(repo.iter_commits('<branch>'))[:5]

When I change the branch I get the expected result of retrieving the commits associated with the specified .

I am open to using other python git libraries. Not tied to gitpython in anyway. I suppose another option would be to forego git libraries and interact with git through python command line calls.

All help is appreciated, thanks!

Fleshings answered 10/4, 2019 at 23:54 Comment(0)
H
3

You need to split command line arguments into *args (list):

from:

repo.git.log('-L :dataLoad:/home/ubuntu/projects/alpha-draw/py/enigma.py')

to:

repo.git.log('-L', ':dataLoad:/home/ubuntu/projects/alpha-draw/py/enigma.py')

(comma)

Hylton answered 11/4, 2019 at 3:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.