如何在 GitPython 中使用 git blame ?

Alg*_*rys 4 python gitpython

我正在尝试在我的脚本中使用 GitPython 模块......但我不能。这不是很有记录:GitPython Blame

我想我还没有那么远,因为我想要重现的通常 git 责备如下: git blame -L127,+1 ../../core/src/filepath.cpp -e

这是我的脚本:

from git import *
    repo = Repo("C:\\Path\\to\\my\\repos\\")
    assert not repo.bare
    # log_line = open("lineDeb.txt")
    # for line in log_line:
    repo.git.blame(L='127,+1' '../../core/src/filepath.cpp', e=True)
Run Code Online (Sandbox Code Playgroud)

注释的两行是为了在我的“lineDeb.txt”文件中的每个数字行上进行 git blame 的最终目标。

我有以下输出:

...
git.exc.GitCommandError: 'git blame -L127,+1../../core/src/filepath.cpp -e' returned with exit code 129
stderr: 'usage: git blame [options] [rev-opts] [rev] [--] file
...
Run Code Online (Sandbox Code Playgroud)

我知道我可以用 os 模块制作它,但我想保留在 python 中。

如果这个模块或 python 的专家可以帮助我?

也许我没有正确使用 GitPython?

目标是获取行提交者的电子邮件......

提前致谢。

Byr*_*ron 5

for commit, lines in repo.blame('HEAD', filepath):
    print("%s changed these lines: %s" % (commit, lines))
Run Code Online (Sandbox Code Playgroud)

Thecommitlines按照文件中出现的顺序更改给定, 的那个。因此,如果您将所有内容都lines写入一个文件,则该文件filepath的修订版将是HEAD

如果您只查找特定行,并且由于当前没有可以传递给blame子命令的选项,则您必须自己计算该行。

ln = 127 # lines start at 0 here
tlc = 0

for commit, lines in repo.blame('HEAD', filepath):
    if tlc <= ln < (tlc + len(lines)):
         print(commit)
    tlc += len(lines)
Run Code Online (Sandbox Code Playgroud)

这不如将相应-L选项传递给git blame,但应该可以完成这项工作。

如果原来是太慢了,你可以考虑做公关,增加了**kwargsRepo.blame传授给git blame