是否可以使用mercurial python库从commit id获取文件更改信息?

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

我在Rhodecode CI中实现了一个自定义钩子,它在每次推送时都会向Buildbot发送一个构建请求。钩子给我修改commit-id,我如何提取有关作为此提交的一部分而更改的文件的信息。

@has_kwargs({
'commit_ids': 'list of pushed commit_ids (sha1)',})
def _push_hook(*args, **kwargs):
     import mercurial # can I used this library to get this info?
     some_function(commit_id) # should return files changed

我可以使用mercurial库或有任何其他方式以编程方式获取此信息,使用python?

mercurial buildbot mercurial-hook rhodecode
1个回答
0
投票

使用rhodecode:

from rhodecode.model.db import Repository
from rhodecode.api.utils import build_commit_data, Optional


class Commit:

    def __init__(self, repo_name, commit_id):
        self._commit_id = commit_id
        repo = Repository.get_by_repo_name(repo_name)
        self.vcs_repo = repo.scm_instance(cache=False)

    def files_changed(self):
        changeset_details = self._changeset_details(self._commit_id)
        files_changed_ = [diff.get("filename") for diff in changeset_details["diff"]]
        return files_changed_

    def _changeset_details(self, commit_id):
        pre_load = ['author', 'branch', 'date', 'message', 'parents',
                    'status', '_commit', '_file_paths']
        changes_details = Optional.extract("full")
        changeset = self.vcs_repo.get_changeset(commit_id, pre_load=pre_load)
        changeset_data = changeset.__json__()
        changeset_data['diff'] = build_commit_data(changeset, changes_details)
        return changeset_data

有了HG:

from mercurial import ui, hg
hg_repo = hg.repository(ui.ui(), repo_path)
node = hg_repo.changelog.node(revision)    
log = hg_repo.changelog.read(node)
manifest, user, (time, timezone), files_changed, desc, extra = log
© www.soinside.com 2019 - 2024. All rights reserved.