4

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

我想我还没有到目前为止,因为我想重现的通常 git blame 如下: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)

注释的两行是为了最终目标是在我的“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
...

我知道我可以使用 os 模块来实现,但我想保留在 python 中。

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

也许我没有正确使用 GitPython?

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

提前致谢。

4

2 回答 2

5
for commit, lines in repo.blame('HEAD', filepath):
    print("%s changed these lines: %s" % (commit, lines))

是按照文件中出现的顺序commit更改给定的。lines因此,如果您将所有lines内容写入文件,您的文件将filepath位于 revision 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)

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

如果结果太慢,您可以考虑制作一个 PR 以添加**kwargsRepo.blame传递给git blame.

于 2015-03-09T13:20:19.640 回答
0

如果你责备大量的行,你可能会发现这种更高的性能:

blame = []
cmd = 'cd {path};git blame {fname}'.format(
            path=repo_path,
            fname=rootpath + fname)
with os.popen(cmd) as process:
   blame = process.readlines()

print blame[line_number]
于 2015-09-17T11:45:16.833 回答