简体   繁体   中英

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

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM