Patchwork [V2] subrepo: share instead of clone if the parent repo is shared (issue5675) (BC)

login
register
mail settings
Submitter Matt Harbison
Date Oct. 17, 2017, 4:27 a.m.
Message ID <7f5dd96256997c9ada54.1508214453@Envy>
Download mbox | patch
Permalink /patch/25077/
State Accepted
Headers show

Comments

Matt Harbison - Oct. 17, 2017, 4:27 a.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1508122082 14400
#      Sun Oct 15 22:48:02 2017 -0400
# Node ID 7f5dd96256997c9ada54c969919abe8c6f3f970f
# Parent  22d774b9dbab4689b186afc8acbca3165cd2a660
subrepo: share instead of clone if the parent repo is shared (issue5675) (BC)

Previously, only the top level repo was shared, and then any subrepos were
cloned on demand.  This is problematic because commits to the parent repo would
write an updated .hgsubstate to the share source, but the corresponding subrepo
commit would be stuck in the local subrepo.  That would prevent an update in the
source repo.  We already go to great lengths to avoid having inconsistent repos
(e.g., `hg push -r rev` will push _everything_ in a subrepo, even if it isn't
referenced in one of the parent's outgoing commits).  Therefore, this seems like
a bug fix, and there's no option to get the old behavior.  I can't imagine the
previous behavior was useful to anybody.

There shouldn't be an issue with svn, since it is centralized.  Maybe --git-dir
can be used for git subrepos, but I'll leave that to someone more familiar with
git.

An integer was previously being implicitly returned from commands.share(), which
caused dispatch() to start crashing when changing over to returning the shared
repo.  All error paths appear to raise, so this can be hardcoded to success.
The clone command checks for 'is None' in a similar pattern, but since
hg.clone() always returns a tuple, that seems wrong?

.. fix:: Issue 5675

   Creating a share of a repository with a Mercurial subrepository will now
   share the subrepository.

and

.. bc::

   Mercurial subrepositories are now shared instead of cloned when the parent
   repository is shared.  This prevents dangling subrepository references in the
   share source.  Previously shared repositories with cloned subrepositories
   will continue to function unchanged.
Yuya Nishihara - Oct. 17, 2017, 1:53 p.m.
On Tue, 17 Oct 2017 00:27:33 -0400, Matt Harbison wrote:
> # HG changeset patch
> # User Matt Harbison <matt_harbison@yahoo.com>
> # Date 1508122082 14400
> #      Sun Oct 15 22:48:02 2017 -0400
> # Node ID 7f5dd96256997c9ada54c969919abe8c6f3f970f
> # Parent  22d774b9dbab4689b186afc8acbca3165cd2a660
> subrepo: share instead of clone if the parent repo is shared (issue5675) (BC)

Queued, thanks for tackling this issue.

Patch

diff --git a/hgext/share.py b/hgext/share.py
--- a/hgext/share.py
+++ b/hgext/share.py
@@ -100,8 +100,9 @@ 
        the broken clone to reset it to a changeset that still exists.
     """
 
-    return hg.share(ui, source, dest=dest, update=not noupdate,
-                    bookmarks=bookmarks, relative=relative)
+    hg.share(ui, source, dest=dest, update=not noupdate,
+             bookmarks=bookmarks, relative=relative)
+    return 0
 
 @command('unshare', [], '')
 def unshare(ui, repo):
diff --git a/mercurial/hg.py b/mercurial/hg.py
--- a/mercurial/hg.py
+++ b/mercurial/hg.py
@@ -255,6 +255,7 @@ 
     r = repository(ui, destwvfs.base)
     postshare(srcrepo, r, bookmarks=bookmarks, defaultpath=defaultpath)
     _postshareupdate(r, update, checkout=checkout)
+    return r
 
 def postshare(sourcerepo, destrepo, bookmarks=True, defaultpath=None):
     """Called after a new shared repo is created.
diff --git a/mercurial/subrepo.py b/mercurial/subrepo.py
--- a/mercurial/subrepo.py
+++ b/mercurial/subrepo.py
@@ -857,21 +857,32 @@ 
 
     def _get(self, state):
         source, revision, kind = state
+        parentrepo = self._repo._subparent
+
         if revision in self._repo.unfiltered():
-            return True
+            # Allow shared subrepos tracked at null to setup the sharedpath
+            if len(self._repo) != 0 or not parentrepo.shared():
+                return True
         self._repo._subsource = source
         srcurl = _abssource(self._repo)
         other = hg.peer(self._repo, {}, srcurl)
         if len(self._repo) == 0:
-            self.ui.status(_('cloning subrepo %s from %s\n')
-                           % (subrelpath(self), srcurl))
-            parentrepo = self._repo._subparent
             # use self._repo.vfs instead of self.wvfs to remove .hg only
             self._repo.vfs.rmtree()
-            other, cloned = hg.clone(self._repo._subparent.baseui, {},
-                                     other, self._repo.root,
-                                     update=False)
-            self._repo = cloned.local()
+            if parentrepo.shared():
+                self.ui.status(_('sharing subrepo %s from %s\n')
+                               % (subrelpath(self), srcurl))
+                shared = hg.share(self._repo._subparent.baseui,
+                                  other, self._repo.root,
+                                  update=False, bookmarks=False)
+                self._repo = shared.local()
+            else:
+                self.ui.status(_('cloning subrepo %s from %s\n')
+                               % (subrelpath(self), srcurl))
+                other, cloned = hg.clone(self._repo._subparent.baseui, {},
+                                         other, self._repo.root,
+                                         update=False)
+                self._repo = cloned.local()
             self._initrepo(parentrepo, source, create=True)
             self._cachestorehash(srcurl)
         else:
diff --git a/tests/test-archive.t b/tests/test-archive.t
--- a/tests/test-archive.t
+++ b/tests/test-archive.t
@@ -18,6 +18,57 @@ 
   $ echo "subrepo = subrepo" > .hgsub
   $ hg add .hgsub
   $ hg ci -m "add subrepo"
+
+  $ cat >> $HGRCPATH <<EOF
+  > [extensions]
+  > share =
+  > EOF
+
+hg subrepos are shared when the parent repo is shared
+
+  $ cd ..
+  $ hg share test shared1
+  updating working directory
+  sharing subrepo subrepo from $TESTTMP/test/subrepo
+  5 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ cat shared1/subrepo/.hg/sharedpath
+  $TESTTMP/test/subrepo/.hg (no-eol) (glob)
+
+hg subrepos are shared into existence on demand if the parent was shared
+
+  $ hg clone -qr 1 test clone1
+  $ hg share clone1 share2
+  updating working directory
+  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ hg -R clone1 -q pull
+  $ hg -R share2 update tip
+  sharing subrepo subrepo from $TESTTMP/test/subrepo
+  3 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ cat share2/subrepo/.hg/sharedpath
+  $TESTTMP/test/subrepo/.hg (no-eol) (glob)
+  $ echo 'mod' > share2/subrepo/sub
+  $ hg -R share2 ci -Sqm 'subrepo mod'
+  $ hg -R clone1 update -C tip
+  cloning subrepo subrepo from $TESTTMP/test/subrepo
+  3 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ rm -rf clone1
+
+  $ hg clone -qr 1 test clone1
+  $ hg share clone1 shared3
+  updating working directory
+  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ hg -R clone1 -q pull
+  $ hg -R shared3 archive --config ui.archivemeta=False -r tip -S archive
+  sharing subrepo subrepo from $TESTTMP/test/subrepo
+  $ cat shared3/subrepo/.hg/sharedpath
+  $TESTTMP/test/subrepo/.hg (no-eol) (glob)
+  $ diff -r archive test
+  Only in test: .hg
+  Only in test/subrepo: .hg
+  [1]
+  $ rm -rf archive
+
+  $ cd test
   $ echo "[web]" >> .hg/hgrc
   $ echo "name = test-archive" >> .hg/hgrc
   $ echo "archivesubrepos = True" >> .hg/hgrc
diff --git a/tests/test-subrepo.t b/tests/test-subrepo.t
--- a/tests/test-subrepo.t
+++ b/tests/test-subrepo.t
@@ -1181,10 +1181,32 @@ 
 Check that share works with subrepo
   $ hg --config extensions.share= share . ../shared
   updating working directory
-  cloning subrepo subrepo-2 from $TESTTMP/subrepo-status/subrepo-2
+  sharing subrepo subrepo-1 from $TESTTMP/subrepo-status/subrepo-1
+  sharing subrepo subrepo-2 from $TESTTMP/subrepo-status/subrepo-2
   2 files updated, 0 files merged, 0 files removed, 0 files unresolved
-  $ test -f ../shared/subrepo-1/.hg/sharedpath
-  [1]
+  $ find ../shared/* | sort
+  ../shared/subrepo-1
+  ../shared/subrepo-1/.hg
+  ../shared/subrepo-1/.hg/cache
+  ../shared/subrepo-1/.hg/cache/storehash
+  ../shared/subrepo-1/.hg/cache/storehash/* (glob)
+  ../shared/subrepo-1/.hg/hgrc
+  ../shared/subrepo-1/.hg/requires
+  ../shared/subrepo-1/.hg/sharedpath
+  ../shared/subrepo-2
+  ../shared/subrepo-2/.hg
+  ../shared/subrepo-2/.hg/branch
+  ../shared/subrepo-2/.hg/cache
+  ../shared/subrepo-2/.hg/cache/checkisexec (execbit !)
+  ../shared/subrepo-2/.hg/cache/checklink (symlink !)
+  ../shared/subrepo-2/.hg/cache/checklink-target (symlink !)
+  ../shared/subrepo-2/.hg/cache/storehash
+  ../shared/subrepo-2/.hg/cache/storehash/* (glob)
+  ../shared/subrepo-2/.hg/dirstate
+  ../shared/subrepo-2/.hg/hgrc
+  ../shared/subrepo-2/.hg/requires
+  ../shared/subrepo-2/.hg/sharedpath
+  ../shared/subrepo-2/file
   $ hg -R ../shared in
   abort: repository default not found!
   [255]