Commit cb74cd91 authored by David Wolever's avatar David Wolever
Browse files

archive: autodetect archive type by extension (issue2058)

parent e13797685ee6
......@@ -158,8 +158,10 @@ def archive(ui, repo, dest, **opts):
By default, the revision used is the parent of the working
directory; use -r/--rev to specify a different revision.
To specify the type of archive to create, use -t/--type. Valid
types are:
The archive type is automatically detected based on file
extension (or override using -t/--type).
Valid types are:
:``files``: a directory full of files (default)
:``tar``: tar archive, uncompressed
......@@ -184,16 +186,32 @@ def archive(ui, repo, dest, **opts):
dest = cmdutil.make_filename(repo, dest, node)
if os.path.realpath(dest) == repo.root:
raise util.Abort(_('repository root cannot be destination'))
matchfn = cmdutil.match(repo, [], opts)
kind = opts.get('type') or 'files'
def guess_type():
exttypes = {
'tar': ['.tar'],
'tbz2': ['.tbz2', '.tar.bz2'],
'tgz': ['.tgz', '.tar.gz'],
'zip': ['.zip'],
for type, extensions in exttypes.items():
if any(dest.endswith(ext) for ext in extensions):
return type
return None
kind = opts.get('type') or guess_type() or 'files'
prefix = opts.get('prefix')
if dest == '-':
if kind == 'files':
raise util.Abort(_('cannot archive plain files to stdout'))
dest = sys.stdout
if not prefix:
prefix = os.path.basename(repo.root) + '-%h'
prefix = cmdutil.make_filename(repo, prefix, node)
matchfn = cmdutil.match(repo, [], opts)
archival.archive(repo, dest, node, kind, not opts.get('no_decode'),
matchfn, prefix)
......@@ -74,6 +74,20 @@ bunzip2 -dc test.tar.bz2 | tar tf - 2>/dev/null
hg archive -t tgz -p %b-%h test-%h.tar.gz
gzip -dc test-$QTIP.tar.gz | tar tf - 2>/dev/null | sed "s/$QTIP/TIP/"
hg archive autodetected_test.tar
tar tf autodetected_test.tar
# The '-t' should override autodetection
hg archive -t tar
tar tf
for ext in tar tar.gz tgz tar.bz2 tbz2 zip; do
hg archive auto_test.$ext
if [ -d auto_test.$ext ]; then
echo "extension $ext was not autodetected."
cat > <<EOF
from hashlib import md5
......@@ -45,6 +45,14 @@ test-TIP/.hg_archival.txt
abort: archive prefix contains illegal components
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment