# HG changeset patch # User Martin Geisler # Date 2010-07-15 16:10:05 # Node ID a8614c5a5e9a312c7f4d956e9b19af5314f48ce6 # Parent 91c4e6d2c9e55d5a7ad0e665a964bfb2a630b17b subrepos: support remapping of .hgsub source paths Given a .hgsub file containing lib/libfoo = http://server/libfoo the 'lib/libfoo' subrepo will be cloned from 'http://server/libfoo'. This changeset introduces a remapping mechanism whereby the source paths (the right-hand sides) in the .hgsub file can be remapped. This subpaths section [subpaths] http://server = /local will result in the 'lib/libfoo' subrepo being cloned from '/local/libfoo' instead of from 'http://server/libfoo'. The patterns (left-hand sides) are really regular expressions and the replacement strings (right-hand sides) can refer to matched groups using normal backreferences. This can be used for more complicated replacements such as [subpaths] http://server/(.*)-hg/ = http://hg.server/\1/ which replaces 'http://server/foo-hg/' with 'http://hg.server/foo/'. All patterns are applied in the order by which they are listed in the config files. diff --git a/mercurial/context.py b/mercurial/context.py --- a/mercurial/context.py +++ b/mercurial/context.py @@ -75,7 +75,7 @@ class changectx(object): @propertycache def substate(self): - return subrepo.state(self) + return subrepo.state(self, self._repo.ui) def __contains__(self, key): return key in self._manifest diff --git a/mercurial/subrepo.py b/mercurial/subrepo.py --- a/mercurial/subrepo.py +++ b/mercurial/subrepo.py @@ -12,7 +12,7 @@ hg = None nullstate = ('', '', 'empty') -def state(ctx): +def state(ctx, ui): """return a state dict, mapping subrepo paths configured in .hgsub to tuple: (source from .hgsub, revision from .hgsubstate, kind (key in types dict)) @@ -27,6 +27,9 @@ def state(ctx): if '.hgsub' in ctx: read('.hgsub') + for path, src in ui.configitems('subpaths'): + p.set('subpaths', path, src, ui.configsource('subpaths', path)) + rev = {} if '.hgsubstate' in ctx: try: @@ -45,6 +48,14 @@ def state(ctx): raise util.Abort(_('missing ] in subrepo source')) kind, src = src.split(']', 1) kind = kind[1:] + + for pattern, repl in p.items('subpaths'): + try: + src = re.sub(pattern, repl, src, 1) + except re.error, e: + raise util.Abort(_("bad subrepository pattern in %s: %s") + % (p.source('subpaths', pattern), e)) + state[path] = (src.strip(), rev.get(path, ''), kind) return state diff --git a/tests/test-subrepo-paths b/tests/test-subrepo-paths new file mode 100755 --- /dev/null +++ b/tests/test-subrepo-paths @@ -0,0 +1,27 @@ +#!/bin/sh + +hg init outer +cd outer + +echo 'sub = http://example.net/libfoo' > .hgsub +hg add .hgsub + +echo '% hg debugsub with no remapping' +hg debugsub + +cat > .hg/hgrc < .hg/hgrc <&1 | "$TESTDIR/filtertmp.py" + +exit 0 diff --git a/tests/test-subrepo-paths.out b/tests/test-subrepo-paths.out new file mode 100644 --- /dev/null +++ b/tests/test-subrepo-paths.out @@ -0,0 +1,10 @@ +% hg debugsub with no remapping +path sub + source http://example.net/libfoo + revision +% hg debugsub with remapping +path sub + source ssh://localhost/libfoo + revision +% test bad subpaths pattern +abort: bad subrepository pattern in $HGTMP/test-subrepo-paths/outer/.hg/hgrc:2: invalid group reference