diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -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) diff --git a/tests/test-archive b/tests/test-archive --- a/tests/test-archive +++ b/tests/test-archive @@ -74,6 +74,20 @@ bunzip2 -dc test.tar.bz2 | tar tf - 2>/d 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 autodetect_override_test.zip +tar tf autodetect_override_test.zip + +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." + fi +done + cat > md5comp.py <