[PATCH V2] hgweb: teach archive how to handle file patterns

Angel Ezquerra angel.ezquerra at gmail.com
Fri Feb 8 07:48:54 CST 2013


# HG changeset patch
# User Angel Ezquerra <angel.ezquerra at gmail.com>
# Date 1360141605 -3600
# Node ID 766ad3e48bdff8ee2b2a3a9276eff398dcaafa02
# Parent  1516d5624a2911fcb90ee051c6dc0679b49aef55
hgweb: teach archive how to handle file patterns

The archive web command now takes into account the "file" request entry, if one
is provided.

The provided "file" is processed as a "path" pattern by default, which makes it
easy to only archive a certain file or directory. However, it is possible to
specify a different type of pattern, such as relglob by specifying it
explicitly on the query URL.

With this change hgweb can to process requests such as:

1. http://mercurial.selenic.com/hg/tip.zip/mercurial/templates

    This will download all files on the mercurial/templates directory as a zip
    file

2. http://mercurial.selenic.com/hg/tip.tar.gz/relglob:*.py

    This will download all *.py files in the repository into a tar.gz file.

An so forth.

Note that this is a first step to add support for downloading directories from
the web interface. Currently the only way to use this feature is by manually
constructing the URL that you want to download. We will have to modify the
archiveentry map entry on the different templates so that it adds the current
folder path to the archive links.

This revision also adds a test for this feature to test-archive.t.

diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py
+++ b/mercurial/hgweb/webcommands.py
@@ -813,8 +813,13 @@
     req.respond(HTTP_OK, mimetype)
 
     ctx = webutil.changectx(web.repo, req)
+    pats = []
+    file = req.form.get('file', None)
+    if file:
+        pats = [req.form['file'][0]]
+    matchfn = scmutil.match(ctx, pats, default='path')
     archival.archive(web.repo, req, cnode, artype, prefix=name,
-                     matchfn=scmutil.match(ctx, []),
+                     matchfn=matchfn,
                      subrepos=web.configbool("web", "archivesubrepos"))
     return []
 
diff --git a/tests/test-archive.t b/tests/test-archive.t
--- a/tests/test-archive.t
+++ b/tests/test-archive.t
@@ -69,9 +69,14 @@
   >     msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
   > except ImportError:
   >     pass
-  > node, archive = sys.argv[1:]
-  > f = urllib2.urlopen('http://127.0.0.1:%s/?cmd=archive;node=%s;type=%s'
-  >                     % (os.environ['HGPORT'], node, archive))
+  > if len(sys.argv) <= 3:
+  >     node, archive = sys.argv[1:]
+  >     requeststr = 'cmd=archive;node=%s;type=%s' % (node, archive)
+  > else:
+  >     node, archive, file = sys.argv[1:]
+  >     requeststr = 'cmd=archive;node=%s;type=%s;file=%s' % (node, archive, file)
+  > f = urllib2.urlopen('http://127.0.0.1:%s/?%s'
+  >                     % (os.environ['HGPORT'], requeststr))
   > sys.stdout.write(f.read())
   > EOF
   $ python getarchive.py "$TIP" gz | gunzip | tar tf - 2>/dev/null
@@ -92,6 +97,8 @@
       testing: test-archive-2c0277f05ed4/baz/bletch   OK
       testing: test-archive-2c0277f05ed4/foo   OK
   No errors detected in compressed data of archive.zip.
+  $ python getarchive.py "$TIP" gz baz | gunzip | tar tf - 2>/dev/null
+  test-archive-2c0277f05ed4/baz/bletch
 
   $ "$TESTDIR/killdaemons.py" $DAEMON_PIDS
 


More information about the Mercurial-devel mailing list