如何计算使用 Azure DevOps(使用 GitPython)的项目中拉取请求的确切总更改 LOC

问题描述 投票:0回答:0

我有一个使用 Azure DevOps 作为版本控制系统的项目。我想获得项目中每个拉取请求的总更改代码行数。为此,我发送了一个请求以获取项目的所有拉取请求,其中包括 merge_commit id 和 last_merge_target_commit id。然后我计算这两个提交的差异,以使用 GitPython 计算拉取请求中更改的 loc 总量。这是相关的代码片段;

...

last_commit_of_pull_request = pull_request["commits"][0]["commit_id"]
first_commit_of_pull_request = pull_request["commits"][commits_count-1]["commit_id"]
last_merge_commit = pull_request["last_merge_commit"]["commit_id"]
last_merge_target_commit = pull_request["last_merge_target_commit"]["commit_id"]

#? This is a workaround for prs that include rebase action. If the pr is completed, the last_merge_commit in the pr json file is included in real git history. Else, it is an artificial commit which is not included in the git history.
if(pull_request["status"] != "active"):
    diff_str = repo.git.diff('{}..{}'.format(last_merge_target_commit, last_merge_commit),"--ignore-all-space", "--numstat")

else:
    # Active prs which includes only one commit
    if last_commit_of_pull_request == first_commit_of_pull_request:
        diff_str = repo.git.show(first_commit_of_pull_request, "--ignore-all-space", "--numstat")
    else:
        diff_str = repo.git.diff('{}..{}'.format(last_commit_of_pull_request, first_commit_of_pull_request), "--ignore-all-space", "--numstat")

...

问题是生成的差异不包括更改块中的最后一个空行(如果有的话)。这会导致计算出的变化量和实际变化量不一致。生成的 diff 在更改块的末尾少了一个 +。例如;

Diff generated by GitPython

Diff shown in Azure DevOps UI

在这种情况下,如何计算拉取请求的确切更改 loc 量?或者,有没有办法直接从 Azure DevOps Rest API 获取此信息?

azure-devops-rest-api git-diff gitpython
© www.soinside.com 2019 - 2024. All rights reserved.