Patchwork [1,of,3] lfs: enable the extension locally after cloning a repo with 'lfs' requirement

login
register
mail settings
Submitter Matt Harbison
Date Dec. 1, 2017, 4:07 a.m.
Message ID <8eec160e4c4c3f88831b.1512101234@Envy>
Download mbox | patch
Permalink /patch/25842/
State Accepted
Headers show

Comments

Matt Harbison - Dec. 1, 2017, 4:07 a.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1510881800 18000
#      Thu Nov 16 20:23:20 2017 -0500
# Node ID 8eec160e4c4c3f88831bdc49daa30bd8cd105a23
# Parent  3180ff7f60253ee1d6ae1f4608acb96c60adeef3
lfs: enable the extension locally after cloning a repo with 'lfs' requirement

We do the same thing on clone for the largefiles extension, as a convenience.
Similar to largefiles, it's probably safer to only enable this extension on a
per repo basis because it is trivial to add an lfs file.  And that gives the
repository some centralized VCS characteristics.

Patch

diff --git a/hgext/lfs/__init__.py b/hgext/lfs/__init__.py
--- a/hgext/lfs/__init__.py
+++ b/hgext/lfs/__init__.py
@@ -37,6 +37,7 @@ 
     exchange,
     extensions,
     filelog,
+    hg,
     localrepo,
     registrar,
     revlog,
@@ -151,6 +152,8 @@ 
         ),
     )
 
+    wrapfunction(hg, 'clone', wrapper.hgclone)
+
     # Make bundle choose changegroup3 instead of changegroup2. This affects
     # "hg bundle" command. Note: it does not cover all bundle formats like
     # "packed1". Using "packed1" with lfs will likely cause trouble.
diff --git a/hgext/lfs/wrapper.py b/hgext/lfs/wrapper.py
--- a/hgext/lfs/wrapper.py
+++ b/hgext/lfs/wrapper.py
@@ -202,6 +202,25 @@ 
         if util.safehasattr(othervfs, name):
             setattr(self, name, getattr(othervfs, name))
 
+def hgclone(orig, ui, opts, *args, **kwargs):
+    result = orig(ui, opts, *args, **kwargs)
+
+    if result is not None:
+        sourcerepo, destrepo = result
+        repo = destrepo.local()
+
+        # When cloning to a remote repo (like through SSH), no repo is available
+        # from the peer.  Therefore the hgrc can't be updated.
+        if not repo:
+            return result
+
+        # If lfs is required for this repo, permanently enable it locally
+        if 'lfs' in repo.requirements:
+            with repo.vfs('hgrc', 'a', text=True) as fp:
+                fp.write('\n[extensions]\nlfs=\n')
+
+    return result
+
 def _canskipupload(repo):
     # if remotestore is a null store, upload is a no-op and can be skipped
     return isinstance(repo.svfs.lfsremoteblobstore, blobstore._nullremote)
diff --git a/tests/test-lfs.t b/tests/test-lfs.t
--- a/tests/test-lfs.t
+++ b/tests/test-lfs.t
@@ -199,6 +199,8 @@ 
   updating to branch default
   2 files updated, 0 files merged, 0 files removed, 0 files unresolved
   $ cd repo7
+  $ hg config extensions --debug | grep lfs
+  $TESTTMP/repo7/.hg/hgrc:*: extensions.lfs= (glob)
   $ cat large
   LARGE-BECAUSE-IT-IS-MORE-THAN-30-BYTES
   $ cat small