如何使用gitpython查询特定git repo分支的日志? [英] How to query the log of a specific git repo branch using gitpython?

查看:170
本文介绍了如何使用gitpython查询特定git repo分支的日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:使用gitpython在python脚本中执行以下git命令.

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

cmdLine版本:

cmdLine Version:

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

脚本版本:

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

这将导致以下错误,

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'

当我将错误中的cmdLine版本直接复制并粘贴到...命令行中时,它可以按预期工作.

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

我的假设是,当实际的工作分支不是master时,gitpython正在查看master分支.

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

该假设的原因:

1)我发现很多帖子都在讨论该命令的使用

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)认为这是未成功更改gitpython中的分支的问题的另一个原因是以下示例.

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 .

我愿意使用其他python git库.无论如何不与gitpython捆绑在一起.我想另一种选择是放弃git库,并通过python命令行调用与git交互.

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.

感谢所有帮助,谢谢!

推荐答案

您需要将命令行参数拆分为*args(列表):

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

来自:

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

收件人:

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

(逗号)

这篇关于如何使用gitpython查询特定git repo分支的日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆