# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1537489621 25200
#      Thu Sep 20 17:27:01 2018 -0700
# Node ID 6637b079ae451452d258f3feccca8627d7d62b85
# Parent  2c0aa02ecd5a05ae76b6345962ee3a0ef773bd8a
lfs: autoload the extension when cloning from repo with lfs enabled

This is based on a patch by Gregory Szorc.  I made small adjustments to
clean up the messaging when the server has the extension enabled, but the
client has it disabled (to prevent autoloading).  Additionally, I added
a second server capability to distinguish between the server having the
extension enabled, and the server having LFS commits.  This helps prevent
unnecessary requirement propagation- the client shouldn't add a requirement
that the server doesn't have, just because the server had the extension
loaded.  The TODO I had about advertising a capability when the server can
natively serve up blobs isn't relevant anymore (we've had 2 releases that
support this), so I dropped it.

Currently, we lazily add the "lfs" requirement to a repo when we first
encounter LFS data. Due to a pretxnchangegroup hook that looks for LFS
data, this can happen at the end of clone.

Now that we have more control over how repositories are created, we can
do better.

This commit adds a repo creation option to add the "lfs" requirement.
hg.clone() sets this creation option if the remote peer is advertising
lfs usage (as opposed to just support needed to push).

So, what this change effectively does is have cloned repos
automatically inherit the "lfs" requirement.

Differential Revision: https://phab.mercurial-scm.org/D5130

diff --git a/hgext/lfs/wrapper.py b/hgext/lfs/wrapper.py
--- a/hgext/lfs/wrapper.py
+++ b/hgext/lfs/wrapper.py
@@ -46,7 +46,13 @@
     '''Wrap server command to announce lfs server capability'''
     caps = orig(repo, proto)
     if util.safehasattr(repo.svfs, 'lfslocalblobstore'):
-        # XXX: change to 'lfs=serve' when separate git server isn't required?
+        # Advertise a slightly different capability when lfs is *required*, so
+        # that the client knows it MUST load the extension.  If lfs is not
+        # required on the server, there's no reason to autoload the extension
+        # on the client.
+        if b'lfs' in repo.requirements:
+            caps.append('lfs-serve')
+
         caps.append('lfs')
     return caps
 
diff --git a/mercurial/hg.py b/mercurial/hg.py
--- a/mercurial/hg.py
+++ b/mercurial/hg.py
@@ -578,6 +578,23 @@
 
         createopts['narrowfiles'] = True
 
+    if srcpeer.capable(b'lfs-serve'):
+        # Repository creation honors the config if it disabled the extension, so
+        # we can't just announce that lfs will be enabled.  This check avoids
+        # saying that lfs will be enabled, and then saying it's an unknown
+        # feature.  The lfs creation option is set in either case so that a
+        # requirement is added.  If the extension is explicitly disabled but the
+        # requirement is set, the clone aborts early, before transferring any
+        # data.
+        createopts['lfs'] = True
+
+        if extensions.disabledext('lfs'):
+            ui.status(_('(remote is using large file support (lfs), but it is '
+                        'explicitly disabled in the local configuration)\n'))
+        else:
+            ui.status(_('(remote is using large file support (lfs); lfs will '
+                        'be enabled for this repository)\n'))
+
     shareopts = shareopts or {}
     sharepool = shareopts.get('pool')
     sharenamemode = shareopts.get('mode')
diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -2895,6 +2895,9 @@
     if createopts.get('narrowfiles'):
         requirements.add(repository.NARROW_REQUIREMENT)
 
+    if createopts.get('lfs'):
+        requirements.add('lfs')
+
     return requirements
 
 def filterknowncreateopts(ui, createopts):
@@ -2913,6 +2916,7 @@
     """
     known = {
         'backend',
+        'lfs',
         'narrowfiles',
         'sharedrepo',
         'sharedrelative',
@@ -2931,6 +2935,9 @@
 
     backend
        The storage backend to use.
+    lfs
+       Repository will be created with ``lfs`` requirement. The lfs extension
+       will automatically be loaded when the repository is accessed.
     narrowfiles
        Set up repository to support narrow file storage.
     sharedrepo
diff --git a/tests/test-lfs-serve-access.t b/tests/test-lfs-serve-access.t
--- a/tests/test-lfs-serve-access.t
+++ b/tests/test-lfs-serve-access.t
@@ -42,6 +42,7 @@
 Downloads fail...
 
   $ hg clone http://localhost:$HGPORT httpclone
+  (remote is using large file support (lfs); lfs will be enabled for this repository)
   requesting all changes
   adding changesets
   adding manifests
@@ -76,6 +77,7 @@
   $ hg clone --debug http://localhost:$HGPORT/subdir/mount/point cloned2
   using http://localhost:$HGPORT/subdir/mount/point
   sending capabilities command
+  (remote is using large file support (lfs); lfs will be enabled for this repository)
   query 1; heads
   sending batch command
   requesting all changes
@@ -88,7 +90,6 @@
   adding file changes
   adding lfs.bin revisions
   added 1 changesets with 1 changes to 1 files
-  calling hook pretxnchangegroup.lfs: hgext.lfs.checkrequireslfs
   bundle2-input-part: total payload size 648
   bundle2-input-part: "listkeys" (params: 1 mandatory) supported
   bundle2-input-part: "phase-heads" supported
@@ -239,6 +240,7 @@
 Test an I/O error in localstore.verify() (Batch API) with GET
 
   $ hg clone http://localhost:$HGPORT1 httpclone2
+  (remote is using large file support (lfs); lfs will be enabled for this repository)
   requesting all changes
   adding changesets
   adding manifests
diff --git a/tests/test-lfs-serve.t b/tests/test-lfs-serve.t
--- a/tests/test-lfs-serve.t
+++ b/tests/test-lfs-serve.t
@@ -304,10 +304,10 @@
   $ grep 'lfs' .hg/requires $SERVER_REQUIRES
   $TESTTMP/server/.hg/requires:lfs
 
-TODO: fail more gracefully.
-
-  $ hg clone -q http://localhost:$HGPORT $TESTTMP/client4_clone
-  abort: HTTP Error 500: Internal Server Error
+  $ hg clone http://localhost:$HGPORT $TESTTMP/client4_clone
+  (remote is using large file support (lfs), but it is explicitly disabled in the local configuration)
+  abort: repository requires features unknown to this Mercurial: lfs!
+  (see https://mercurial-scm.org/wiki/MissingRequirement for more information)
   [255]
   $ grep 'lfs' $TESTTMP/client4_clone/.hg/requires $SERVER_REQUIRES
   grep: $TESTTMP/client4_clone/.hg/requires: $ENOENT$
@@ -661,8 +661,6 @@
   $ cat $TESTTMP/errors.log | grep '^[A-Z]'
   Traceback (most recent call last):
   ValueError: no common changegroup version
-  Traceback (most recent call last):
-  ValueError: no common changegroup version
 #else
   $ cat $TESTTMP/errors.log
 #endif