git: key off `git` in .hg/requires rather than separate file
authorAugie Fackler <raf@durin42.com>
Tue, 10 Mar 2020 13:10:53 -0400
changeset 44493 02c47b74366c
parent 44492 eb061d272af4
child 44494 283fd803afa5
git: key off `git` in .hg/requires rather than separate file Differential Revision: https://phab.mercurial-scm.org/D8265
hgext/git/__init__.py
tests/test-git-interop.t
--- a/hgext/git/__init__.py	Tue Mar 10 13:10:45 2020 -0400
+++ b/hgext/git/__init__.py	Tue Mar 10 13:10:53 2020 -0400
@@ -86,19 +86,24 @@
 
 
 def _makestore(orig, requirements, storebasepath, vfstype):
-    # Check for presence of pygit2 only here. The assumption is that we'll
-    # run this code iff we'll later need pygit2.
-    if gitutil.get_pygit2() is None:
-        raise error.Abort(
-            _(
-                b'the git extension requires the Python '
-                b'pygit2 library to be installed'
+    if b'git' in requirements:
+        if not os.path.exists(os.path.join(storebasepath, b'..', b'.git')):
+            raise error.Abort(
+                _(
+                    b'repository specified git format in '
+                    b'.hg/requires but has no .git directory'
+                )
             )
-        )
+        # Check for presence of pygit2 only here. The assumption is that we'll
+        # run this code iff we'll later need pygit2.
+        if gitutil.get_pygit2() is None:
+            raise error.Abort(
+                _(
+                    b'the git extension requires the Python '
+                    b'pygit2 library to be installed'
+                )
+            )
 
-    if os.path.exists(
-        os.path.join(storebasepath, b'this-is-git')
-    ) and os.path.exists(os.path.join(storebasepath, b'..', b'.git')):
         return gitstore(storebasepath, vfstype)
     return orig(requirements, storebasepath, vfstype)
 
@@ -128,9 +133,7 @@
             os.path.join(path, b'.git', b'info', b'exclude'), 'ab'
         ) as exclude:
             exclude.write(b'\n.hg\n')
-    with open(os.path.join(dothg, b'this-is-git'), 'wb') as f:
-        pass
-    with open(os.path.join(dothg, b'requirements'), 'wb') as f:
+    with open(os.path.join(dothg, b'requires'), 'wb') as f:
         f.write(b'git\n')
 
 
@@ -256,6 +259,11 @@
     return repo
 
 
+def _featuresetup(ui, supported):
+    # don't die on seeing a repo with the git requirement
+    supported |= {b'git'}
+
+
 def extsetup(ui):
     extensions.wrapfunction(localrepo, b'makestore', _makestore)
     extensions.wrapfunction(localrepo, b'makefilestorage', _makefilestorage)
@@ -264,3 +272,4 @@
     entry[1].extend(
         [(b'', b'git', None, b'setup up a git repository instead of hg')]
     )
+    localrepo.featuresetupfuncs.add(_featuresetup)
--- a/tests/test-git-interop.t	Tue Mar 10 13:10:45 2020 -0400
+++ b/tests/test-git-interop.t	Tue Mar 10 13:10:53 2020 -0400
@@ -1,13 +1,12 @@
 #require pygit2
 
 Setup:
-  > GIT_AUTHOR_NAME='test'; export GIT_AUTHOR_NAME
+  $ GIT_AUTHOR_NAME='test'; export GIT_AUTHOR_NAME
   > GIT_AUTHOR_EMAIL='test@example.org'; export GIT_AUTHOR_EMAIL
   > GIT_AUTHOR_DATE="2007-01-01 00:00:00 +0000"; export GIT_AUTHOR_DATE
   > GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; export GIT_COMMITTER_NAME
   > GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"; export GIT_COMMITTER_EMAIL
   > GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"; export GIT_COMMITTER_DATE
-
   > count=10
   > gitcommit() {
   >    GIT_AUTHOR_DATE="2007-01-01 00:00:$count +0000";
@@ -16,7 +15,28 @@
   >    count=`expr $count + 1`
   >  }
 
-  > echo "[extensions]" >> $HGRCPATH
+
+Test auto-loading extension works:
+  $ mkdir nogit
+  $ cd nogit
+  $ mkdir .hg
+  $ echo git >> .hg/requires
+  $ hg status
+  abort: repository specified git format in .hg/requires but has no .git directory
+  [255]
+  $ git init
+  Initialized empty Git repository in $TESTTMP/nogit/.git/
+This status invocation shows some hg gunk because we didn't use
+`hg init --git`, which fixes up .git/info/exclude for us.
+  $ hg status
+  ? .hg/cache/git-commits.sqlite
+  ? .hg/cache/git-commits.sqlite-shm
+  ? .hg/cache/git-commits.sqlite-wal
+  ? .hg/requires
+  $ cd ..
+
+Now globally enable extension for the rest of the test:
+  $ echo "[extensions]" >> $HGRCPATH
   > echo "git=" >> $HGRCPATH
 
 Make a new repo with git: