Kallithea issues archive

Issue #150: Retrieving branch from changset in hook.

Reported by: Jimmy Karlsson
State: closed
Created on: 2015-07-29 05:50
Updated on: 2015-07-31 09:23


I'm working against a mercurial repository and i've added a hook that gets called when someone pushes a changeset.

I'd like to retrieve information regarding what branch the changeset belongs to.

The following code works 'sometimes';

def push_action(ui, repo, **kwargs):
    ex = _extract_extras()

    changeset_id = kwargs['node']
    repo_name = ex.repository

    print("    Repository: " + repo_name)

    r = RepoModel().get_by_repo_name(repo_name)

    changeset = r.get_changeset(changeset_id)
    branch_name = changeset.branch

But more often then not it throws the following exception;

remote: Traceback (most recent call last):
remote:   File "/opt/kallithea/venv/local/lib/python2.7/site-packages/kallithea/lib/bb.py", line 29, in push_action
remote:     branch_name = changeset.branch
remote:   File "/opt/kallithea/venv/local/lib/python2.7/site-packages/kallithea/lib/vcs/utils/lazy.py", line 40, in __get__
remote:     value = self._func(obj)
remote:   File "/opt/kallithea/venv/local/lib/python2.7/site-packages/kallithea/lib/vcs/backends/base.py", line 1004, in branch
remote:     return get_backend(self.alias).DEFAULT_BRANCH_NAME
remote:   File "/opt/kallithea/venv/local/lib/python2.7/site-packages/kallithea/lib/vcs/backends/__init__.py", line 53, in get_backend
remote:     "%s" % (alias, pformat(settings.BACKENDS.keys())))
remote: VCSError: Given alias 'None' is not recognized! Allowed aliases:
remote: ['git', 'hg']

Is this a bug or do I need to do something else to make this work?



Comment by Mads Kiilerich, on 2015-07-29 12:54

It seems to be caused by unfortunate design. We have this EmptyChangeset which for some weird reason have ini parameters that can make it everything but empty, but it doesn't get "alias" (weird name for vcs kind) set and can thus not know what the default branch is.

I guess the right solution is to refactor the code and remove all the parameters from EmptyChangeset except alias which should be mandatory.

You can work around it in your hook, but a patch fixing as described would be better ;-)

Also: Mercurial Python hooks are not really recommended by Mercurial. But when you make such hooks, you could consider only doing Mercurial stuff, without calling back into Kallithea internals.

Comment by Jimmy Karlsson, on 2015-07-30 04:08

Well, I'm not too familiar with the design of Kallithea but shouldn't the changeset object be of type MercurialChangeset instead?

Anyway, I updated my code to the following;

def push_action(ui, repo, **kwargs):
    ex = _extract_extras()

    changeset_id = kwargs['node']
    repo_name = ex.repository

    r = Repository.get_by_repo_name(repo_name).scm_instance

    changeset = r.get_changeset(changeset_id)
    branch_name = changeset.branch

This also works sometimes, but now I get the following exception;

remote: Traceback (most recent call last):
remote:   File "/opt/kallithea/venv/local/lib/python2.7/site-packages/kallithea/lib/bb.py", line 30, in push_action
remote:   File "/opt/kallithea/venv/local/lib/python2.7/site-packages/kallithea/lib/vcs/backends/hg/repository.py", line 499, in get_changeset
remote:     revision = self._get_revision(revision)
remote:   File "/opt/kallithea/venv/local/lib/python2.7/site-packages/kallithea/lib/vcs/backends/hg/repository.py", line 428, in _get_revision
remote:     raise ChangesetDoesNotExistError(msg)
remote: ChangesetDoesNotExistError: Revision 6deabc2288d1589522d5f770cb8be8fb06ebcfba does not exist for <MercurialRepository at /home/scm/repositories/hg/ritab_rav_server>

If the revision doesn't exist, why do I have a revision number?

I'm afraid I won't have the time to familiarise myself with the code enough to be able to produce a patch for this. (Although I've made a non-related minor change to the REST-api that I'll likely send in.).

All the other hooks in hooks.py were written in python so I used them as a sort of template, so I wouldn't have to deal with configuration issues and what not in the future, instead I could rely on Kallithea for that.

Comment by Mads Kiilerich, on 2015-07-30 20:30

I agree that it could seem to make more sense if it you always got MercurialChangeset from Mercurial ... but for some reason there is one shared class for "no changeset" shared between hg and git.

I guess the problem could be because .smc_instance is using a cached old repo that doesn't know about the new revision. The push hasn't really succeeded yet so it kind of makes sense that the cached repo hasn't been invalidated yet. Try using .scm_instance_no_cache instead.

Comment by Jimmy Karlsson, on 2015-07-31 09:22

Ah, I see. Then perhaps it isn't a bug, but more of an undocumented (unintuitive?) behaviour.

Anyway, I moved away from using Kallithea internals to querying the repository directly with the hg-command. Works like a charm now.

Comment by Jimmy Karlsson, on 2015-07-31 09:23

Not an issue, just confusing behaviour from the part of Kallithea.