subrepo: cloning and updating of git subrepos
authorEric Eisner <ede@mit.edu>
Sun, 14 Nov 2010 18:20:13 -0500
changeset 12993 a91334380699
parent 12992 2b73a3279a9f
child 12994 845c602b8635
subrepo: cloning and updating of git subrepos gitsubrepo based on patch from David Soria Parra: http://bitbucket.org/segv/davids-poor-git-subrepo-attempt/
mercurial/subrepo.py
tests/test-subrepo-git.t
--- a/mercurial/subrepo.py	Sun Nov 14 18:15:26 2010 -0500
+++ b/mercurial/subrepo.py	Sun Nov 14 18:20:13 2010 -0500
@@ -581,6 +581,7 @@
         # 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 @@
         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 @@
                                     '--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:
--- a/tests/test-subrepo-git.t	Sun Nov 14 18:15:26 2010 -0500
+++ b/tests/test-subrepo-git.t	Sun Nov 14 18:20:13 2010 -0500
@@ -56,3 +56,28 @@
   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