Search code examples
mercurialbuildbotmercurial-hookrhodecode

Is it possible to get the files changed information from commit ids using mercurial python library?


I am implementing a custom hook in Rhodecode CI, which sends a build request to Buildbot on every push. The hook gives me the revisions commit-id, how can I extract information about the files that were changed as part of this commit.

@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

Can I use mercurial library or is there any other way to get this information programatically, using python?


Solution

  • With 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
    

    With 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