简体   繁体   English

如何使用gitpython查询特定git repo分支的日志?

[英]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. 目标:使用gitpython在python脚本中执行以下git命令。

cmdLine Version: cmdLine版本:

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. 当我将错误的cmdLine版本直接复制并粘贴到...时,命令行将按预期工作。

My hypothesis is that gitpython is looking at the master branch, when the actual working branch is not master. 我的假设是,当实际的工作分支不是master时,gitpython正在查看master分支。

Reasons for this hypothesis: 此假设的原因:

1) I have found many posts discussing the use of the command 1)我发现许多帖子讨论了命令的使用

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. 使用此命令后,当我从存储库中查询提交时,我仍然会从master获取提交。

2) Another reason to believe this is an issue with not successfully changing the branch within gitpython is with the following example. 2)相信这是未能成功更改gitpython中的分支的问题的另一个原因是以下示例。

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. 我愿意使用其他python git库。 Not tied to gitpython in anyway. 无论如何不与gitpython绑定。 I suppose another option would be to forego git libraries and interact with git through python command line calls. 我想另一种选择是放弃git库,并通过python命令行调用与git交互。

All help is appreciated, thanks! 感谢所有帮助,谢谢!

You need to split command line arguments into *args (list): 您需要将命令行参数拆分为*args (列表):

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) (逗号)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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