[PATCH 4 of 5] subrepo: fix subrelpath for git subrepos

Eric Eisner ede at MIT.EDU
Mon Dec 20 13:11:10 CST 2010


# HG changeset patch
# User Eric Eisner <ede at mit.edu>
# Date 1292871573 18000
# Node ID 413bef846806cc5fbd71dbcf14e444ce6ecf05cd
# Parent  a79e0688a5ee2a17d500b178d6ebd8e73ef6f1af
subrepo: fix subrelpath for git subrepos

This patch conforms gitsubrepo code to the variable naming scheme of the other
subrepo implementations. All user-facing path references should be relative
to the root repository.

diff --git a/mercurial/subrepo.py b/mercurial/subrepo.py
--- a/mercurial/subrepo.py
+++ b/mercurial/subrepo.py
@@ -172,6 +172,8 @@ def reporelpath(repo):
 
 def subrelpath(sub):
     """return path to this subrepo as seen from outermost repo"""
+    if hasattr(sub, '_relpath'):
+        return sub._relpath
     if not hasattr(sub, '_repo'):
         return sub._path
     return reporelpath(sub._repo)
@@ -617,15 +619,17 @@ class gitsubrepo(abstractsubrepo):
         # TODO add git version check.
         self._state = state
         self._ctx = ctx
-        self._relpath = path
-        self._path = ctx._repo.wjoin(path)
+        self._path = path
+        self._relpath = os.path.join(reporelpath(ctx._repo), path)
+        self._abspath = ctx._repo.wjoin(path)
         self._ui = ctx._repo.ui
 
     def _gitcommand(self, commands, env=None, stream=False):
         return self._gitdir(commands, env=env, stream=stream)[0]
 
     def _gitdir(self, commands, env=None, stream=False):
-        return self._gitnodir(commands, env=env, stream=stream, cwd=self._path)
+        return self._gitnodir(commands, env=env, stream=stream,
+                              cwd=self._abspath)
 
     def _gitnodir(self, commands, env=None, stream=False, cwd=None):
         """Calls the git command
@@ -711,9 +715,9 @@ class gitsubrepo(abstractsubrepo):
         return tracking
 
     def _fetch(self, source, revision):
-        if not os.path.exists('%s/.git' % self._path):
+        if not os.path.exists(os.path.join(self._abspath, '.git')):
             self._ui.status(_('cloning subrepo %s\n') % self._relpath)
-            self._gitnodir(['clone', source, self._path])
+            self._gitnodir(['clone', source, self._abspath])
         if self._githavelocally(revision):
             return
         self._ui.status(_('pulling subrepo %s\n') % self._relpath)
@@ -725,7 +729,7 @@ class gitsubrepo(abstractsubrepo):
         self._gitcommand(['fetch', source])
         if not self._githavelocally(revision):
             raise util.Abort(_("revision %s does not exist in subrepo %s\n") %
-                               (revision, self._path))
+                               (revision, self._relpath))
 
     def dirty(self, ignoreupdate=False):
         # version checked out changed?
@@ -859,16 +863,16 @@ class gitsubrepo(abstractsubrepo):
     def remove(self):
         if self.dirty():
             self._ui.warn(_('not removing repo %s because '
-                            'it has changes.\n') % self._path)
+                            'it has changes.\n') % self._relpath)
             return
         # we can't fully delete the repository as it may contain
         # local-only history
-        self._ui.note(_('removing subrepo %s\n') % self._path)
+        self._ui.note(_('removing subrepo %s\n') % self._relpath)
         self._gitcommand(['config', 'core.bare', 'true'])
-        for f in os.listdir(self._path):
+        for f in os.listdir(self._abspath):
             if f == '.git':
                 continue
-            path = os.path.join(self._path, f)
+            path = os.path.join(self._abspath, f)
             if os.path.isdir(path) and not os.path.islink(path):
                 shutil.rmtree(path)
             else:
@@ -892,7 +896,7 @@ class gitsubrepo(abstractsubrepo):
                 data = info.linkname
             else:
                 data = tar.extractfile(info).read()
-            archiver.addfile(os.path.join(prefix, self._relpath, info.name),
+            archiver.addfile(os.path.join(prefix, self._path, info.name),
                              info.mode, info.issym(), data)
             ui.progress(_('archiving (%s)') % relpath, i + 1,
                         unit=_('files'))
diff --git a/tests/test-subrepo-git.t b/tests/test-subrepo-git.t
--- a/tests/test-subrepo-git.t
+++ b/tests/test-subrepo-git.t
@@ -34,7 +34,7 @@ add subrepo clone
   $ git clone -q ../gitroot s
   $ hg add .hgsub
   $ hg commit -m 'new git subrepo'
-  committing subrepository $TESTTMP/t/s
+  committing subrepository s
   $ hg debugsub
   path s
    source   ../gitroot
@@ -53,7 +53,7 @@ record a new commit from upstream from a
 
   $ cd ..
   $ hg commit -m 'update git subrepo'
-  committing subrepository $TESTTMP/t/s
+  committing subrepository s
   $ hg debugsub
   path s
    source   ../gitroot
@@ -100,7 +100,7 @@ clone root, make local change
   $ cd ../ta
   $ echo ggg >> s/g
   $ hg commit -m ggg
-  committing subrepository $TESTTMP/ta/s
+  committing subrepository s
   $ hg debugsub
   path s
    source   ../gitroot
@@ -120,7 +120,7 @@ clone root separately, make different lo
   $ cd ..
 
   $ hg commit -m f
-  committing subrepository $TESTTMP/tb/s
+  committing subrepository s
   $ hg debugsub
   path s
    source   ../gitroot
@@ -159,7 +159,7 @@ user a pulls, merges, commits
   gg
   ggg
   $ hg commit -m 'merge'
-  committing subrepository $TESTTMP/ta/s
+  committing subrepository s
   $ hg debugsub
   path s
    source   ../gitroot
@@ -212,7 +212,7 @@ sync to upstream git, distribute changes
   $ git pull -q >/dev/null 2>/dev/null
   $ cd ..
   $ hg commit -m 'git upstream sync'
-  committing subrepository $TESTTMP/ta/s
+  committing subrepository s
   $ hg debugsub
   path s
    source   ../gitroot
@@ -260,3 +260,35 @@ archive subrepos
   g
   gg
   ggg
+
+create nested repo
+
+  $ cd ..
+  $ hg init outer
+  $ cd outer
+  $ echo b>b
+  $ hg add b
+  $ hg commit -m b
+
+  $ hg clone ../t inner
+  updating to branch default
+  cloning subrepo s
+  3 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ echo inner = inner > .hgsub
+  $ hg add .hgsub
+  $ hg commit -m 'nested sub'
+  committing subrepository inner
+
+nested commit
+
+  $ echo ffff >> inner/s/f
+  $ hg commit -m nested
+  committing subrepository inner
+  committing subrepository inner/s
+
+nested archive
+
+  $ hg archive --subrepos ../narchive
+  $ ls ../narchive/inner/s
+  f
+  g


More information about the Mercurial-devel mailing list