Source

metashelf / hg.py

Full commit
#  metashelf: A vcs-friendly store for filesystem metadata
#
#  Copyright (C) 2008       Solomon Hykes <solomon.hykes@gmail.com>
#  Copyright (C) 2009-2010  dotCloud SARL <solomon.hykes@dotcloud.com>

import re
import os
import sys
import mercurial.ignore

from metashelf import Protector

def ignore(ignorefunc, pattern=None):
    if pattern:
        rx = re.compile(pattern)
        def match(file):
            return rx.match(file) or ignorefunc(file)
        return match
    return ignorefunc

def init_protector(ui, repo, ignore=None):
    target = os.path.join(repo.path, "..")
    ignorefunc = mercurial.ignore.ignore(repo.path, [os.path.join(target, ".hgignore")], sys.stdout.write)
    return Protector(path=os.path.join(target, '.hgmeta'), targetDir=target, ignorefunc=ignore(ignorefunc, "^(\.hg/|\.hg$|\.$|\.hgmeta$)") if ignore else None)

def hook_remember(ui, repo, hooktype=None, args=None):
    """ Mercurial hook to store a repository's metadata to an index file (ie. before committing) """
    protector = init_protector(ui, repo, ignore=ignore)
    protector.remember()

def hook_restore(ui, repo, hooktype=None, args=None, result=None):
    """ Mercurial hook to restore a repository's metadata form an index file (ie. after updating) """
    protector = init_protector(ui, repo)
    protector.restore()