[PATCH 2 of 5 VERSION2] subrepo: cloning and updating of git subrepos

Eric Eisner ede at MIT.EDU
Sun Nov 14 17:45:00 CST 2010


# HG changeset patch
# User Eric Eisner <ede at mit.edu>
# Date 1289776813 18000
# Node ID 4d02ef5717dafd00f31592b61346e78e98877c50
# Parent  2ed3e78dbf7321aaca3fb467a41e603de6df0f34
subrepo: cloning and updating of git subrepos

gitsubrepo based on patch from David Soria Parra:
http://bitbucket.org/segv/davids-poor-git-subrepo-attempt/

diff --git a/mercurial/subrepo.py b/mercurial/subrepo.py
--- a/mercurial/subrepo.py
+++ b/mercurial/subrepo.py
@@ -581,6 +581,7 @@ class gitsubrepo(object):
         # TODO add git version check.
         self._state = state
         self._ctx = ctx
+        self._relpath = path
         self._path = ctx._repo.wjoin(path)
         self._ui = ctx._repo.ui
 
@@ -633,6 +634,18 @@ class gitsubrepo(object):
         out, code = self._gitdir(['cat-file', '-e', revision])
         return code == 0
 
+    def _fetch(self, source, revision):
+        if not os.path.exists('%s/.git' % self._path):
+            self._ui.status(_('cloning subrepo %s\n') % self._relpath)
+            self._gitnodir(['clone', source, self._path])
+        if self._githavelocally(revision):
+            return
+        self._ui.status(_('pulling subrepo %s\n') % self._relpath)
+        self._gitcommand(['fetch', '--all', '-q'])
+        if not self._githavelocally(revision):
+            raise util.Abort(_("revision %s does not exist in subrepo %s\n") %
+                               (revision, self._path))
+
     def dirty(self):
         if self._state[1] != self._gitstate(): # version checked out changed?
             return True
@@ -642,6 +655,16 @@ class gitsubrepo(object):
                                     '--untracked-files=no'])
         return bool(changed.strip())
 
+    def get(self, state):
+        source, revision, kind = state
+        self._fetch(source, revision)
+        if self._gitstate() != revision:
+            self._ui.warn(_('checking out detached HEAD in subrepo %s\n') %
+                          self._relpath)
+            self._ui.warn(_('check out a git branch if you intend '
+                            'to make changes\n'))
+            self._gitcommand(['checkout', '-q', revision])
+
     def commit(self, text, user, date):
         cmd = ['commit', '-a', '-m', text]
         if user:
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
@@ -56,3 +56,28 @@ record a new commit from upstream
   path s
    source   ../gitroot
    revision 126f2a14290cd5ce061fdedc430170e8d39e1c5a
+
+clone root
+
+  $ hg clone . ../tc
+  updating to branch default
+  cloning subrepo s
+  3 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ cd ../tc
+  $ hg debugsub
+  path s
+   source   ../gitroot
+   revision 126f2a14290cd5ce061fdedc430170e8d39e1c5a
+
+update to previous substate
+
+  $ hg update 1
+  checking out detached HEAD in subrepo s
+  check out a git branch if you intend to make changes
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ cat s/g
+  g
+  $ hg debugsub
+  path s
+   source   ../gitroot
+   revision da5f5b1d8ffcf62fb8327bcd3c89a4367a6018e7


More information about the Mercurial-devel mailing list