如何获取git信息库中包含的特定文件的特定行上的所有更改? [重复]

问题描述 投票:0回答:1
我们有一个已经使用多年的git仓库。说,这是一个Java-Maven项目。该项目的pom.xml文件包含重要信息,例如

<model_location>path/to/file/model_version</model_location>

我想从git存储库中获取所有模型版本名称。希望我可以使用GitPython库来做到这一点。我写了以下代码。

repo = Repo('/path/to/repository') remote_origin = repo.remotes.origin remote_origin.pull() path = "path/to/pom.xml" commits = repo.iter_commits('--all', max_count=1000, paths=path) for commit in commits: committer_name = commit.committer.name timestamp = time.strftime("%F %T", time.localtime(commit.committed_date)) hexsha = commit.hexsha print(f"{hexsha}\t{committer_name}\t{timestamp}")

代码显示以下结果。

afdb36153a53bc45eff52c319a9541498e1c3ce1 committer-1 2020-01-30 09:57:01 e85ca97121384fb34b0fdc1fa04cf78b6c1cd109 committer-1 2020-01-28 13:45:29 61bbf5d706b9dd2caa7b75e0374946ec1b96994c committer-2 2020-01-23 17:44:20 ... c10ead963802053147488b93626ca0b9aab643b1 committer-3 2015-01-06 09:04:33 cf74128281e9374cca8e88a0e23e52f55e37f109 committer-3 2014-12-05 15:01:24

我有一个提交ID列表,但我不知道如何读取特定提交ID的model_location中的pom.xml标签。 

目的是了解在特定时期使用了哪种模型。例如,在2014-12-05 15:01:24和2015-01-06 09:04:33之间使用了model version 3.14,并在模型中检查了committer-3等。

如果您能告诉我,我将非常感激。我写了另一个与svn相同的脚本,但是我对git不熟悉。

谢谢,

python python-3.x git gitpython
1个回答
0
投票
抱歉,我急急的问题。我找到了解决方法。

res = repo.git.show(f"{hexsha}:{path}/{os.path.basename(pom_xml_path)}") for line in res.split("\n"): match = re.match(regex, line) if match: model_location = match.group(1) model_name = os.path.basename(model_location) print(f"{hexsha}\t{committer_name}\t{model_name}\t{timestamp}")

© www.soinside.com 2019 - 2024. All rights reserved.