Source

hghooks / checksubrepo.py

"""Check subrepo validity and common mistakes

Usage::

    [hooks]
    # validate nodeid before accepting changesets
    pretxnchangegroup.checksubrepo = python:hghooks.checksubrepo.hook
    # warn if tip of subrepo is not referenced by tip of parent
    changegroup.checksubrepo = python:hghooks.checksubrepo.hook
    outgoing.checksubrepo = python:hghooks.checksubrepo.hook
"""
import os
from mercurial import hg, error, subrepo

def checksubstate(ui, repo, state):
    # TODO: check only if .hgsubstate has been modified
    for path, (src, rev, kind) in state.iteritems():
        # only supports relative hg subrepos
        if path != src or kind != 'hg':
            continue

        srepo = hg.repository(ui, os.path.join(repo.root, src))
        if rev not in srepo:
            raise error.Abort('invalid .hgsubstate. revision %s'
                              ' not exists in sub repository <%s>'
                              % (rev[:12], path))

def checktiprev(ui, repo):
    for path, (src, rev, kind) in substate(repo['tip'], ui).iteritems():
        # only supports relative hg subrepos
        if path != src or kind != 'hg':
            continue

        srepo = hg.repository(ui, os.path.join(repo.root, src))
        if srepo['tip'].hex() != rev:
            ui.warn('tip of sub repository <%s> is not referenced from tip '
                    'of parent\n' % path)

def substate(ctx, ui):
    try:
        return subrepo.state(ctx, ui)
    except TypeError:  # Mercurial<1.7
        return subrepo.state(ctx)

def hook(ui, repo, hooktype, **kwargs):
    if hooktype == 'pretxnchangegroup':
        firstnode = kwargs['node']
        for rev in xrange(repo[firstnode].rev(), len(repo)):
            checksubstate(ui, repo, substate(repo[rev], ui))
    elif hooktype in ('changegroup', 'outgoing'):
        checktiprev(ui, repo)
    else:
        ui.warn('unexpected hooktype: %s\n' % hooktype)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.