largefiles: fix cat for largefiles (issue3352)
authorNa'Tosha Bard <natosha@unity3d.com>
Mon, 16 Apr 2012 17:03:39 +0200
changeset 16439 290850e7aa43
parent 16438 28a90cdf0ca0
child 16440 692bf06bb1af
largefiles: fix cat for largefiles (issue3352) This is a fix to largefiles so that 'hg cat' will work correctly when a largefile is specified. As per discussion on Issue 3352: 1) The file will be printed regardless if it is binary or large. 2) The file is downloaded if it is not readily available (not found in the system cache), so that it can be printed. If the download fails, then we abort.
hgext/largefiles/lfcommands.py
hgext/largefiles/overrides.py
hgext/largefiles/uisetup.py
tests/test-largefiles.t
--- a/hgext/largefiles/lfcommands.py	Mon Apr 16 08:50:40 2012 -0700
+++ b/hgext/largefiles/lfcommands.py	Mon Apr 16 17:03:39 2012 +0200
@@ -11,7 +11,7 @@
 import os
 import shutil
 
-from mercurial import util, match as match_, hg, node, context, error
+from mercurial import util, match as match_, hg, node, context, error, cmdutil
 from mercurial.i18n import _
 
 import lfutil
@@ -485,6 +485,23 @@
         lfdirstate.drop(lfile)
     return ret
 
+def catlfile(repo, lfile, rev, filename):
+    hash = lfutil.readstandin(repo, lfile, rev)
+    if not lfutil.inusercache(repo.ui, hash):
+        store = basestore._openstore(repo)
+        success, missing = store.get([(lfile, hash)])
+        if len(success) != 1:
+            raise util.Abort(
+                _('largefile %s is not in cache and could not be downloaded')
+                    % lfile)
+    path = lfutil.usercachepath(repo.ui, hash)
+    fpout = cmdutil.makefileobj(repo, filename)
+    fpin = open(path, "rb")
+    fpout.write(fpin.read())
+    fpout.close()
+    fpin.close()
+    return 0
+
 # -- hg commands declarations ------------------------------------------------
 
 cmdtable = {
--- a/hgext/largefiles/overrides.py	Mon Apr 16 08:50:40 2012 -0700
+++ b/hgext/largefiles/overrides.py	Mon Apr 16 17:03:39 2012 +0200
@@ -966,3 +966,10 @@
     finally:
         repo._istransplanting = False
     return result
+
+def overridecat(orig, ui, repo, file1, *pats, **opts):
+    rev = opts.get('rev')
+    if not lfutil.standin(file1) in repo[rev]:
+        result = orig(ui, repo, file1, *pats, **opts)
+        return result
+    return lfcommands.catlfile(repo, file1, opts.get('rev'), opts.get('output'))
--- a/hgext/largefiles/uisetup.py	Mon Apr 16 08:50:40 2012 -0700
+++ b/hgext/largefiles/uisetup.py	Mon Apr 16 17:03:39 2012 +0200
@@ -64,6 +64,8 @@
                                    overrides.overrideupdate)
     entry = extensions.wrapcommand(commands.table, 'pull',
                                    overrides.overridepull)
+    entry = extensions.wrapcommand(commands.table, 'cat',
+                                   overrides.overridecat)
     entry = extensions.wrapfunction(merge, '_checkunknownfile',
                                     overrides.overridecheckunknownfile)
     entry = extensions.wrapfunction(merge, 'manifestmerge',
--- a/tests/test-largefiles.t	Mon Apr 16 08:50:40 2012 -0700
+++ b/tests/test-largefiles.t	Mon Apr 16 17:03:39 2012 +0200
@@ -761,6 +761,19 @@
   $ cat sub2/large7
   large7
 
+Cat a largefile
+  $ hg cat normal3
+  normal3-modified
+  $ hg cat sub/large4
+  large4-modified
+  $ rm ${USERCACHE}/*
+  $ hg cat -r a381d2c8c80e -o cat.out sub/large4
+  $ cat cat.out
+  large4-modified
+  $ rm cat.out
+  $ hg cat -r a381d2c8c80e normal3
+  normal3-modified
+
 Test that renaming a largefile results in correct output for status
 
   $ hg rename sub/large4 large4-renamed