# HG changeset patch # User David Wolever # Date 1268319137 -3600 # Node ID 9ea7238ad9356ec9ac8104e5cf4aa9380d7ec191 # Parent e13797685ee6e2f26498a4161b708cae640b5847 archive: autodetect archive type by extension (issue2058) diff -r e13797685ee6 -r 9ea7238ad935 mercurial/commands.py --- a/mercurial/commands.py Thu Mar 11 15:38:35 2010 +0100 +++ b/mercurial/commands.py Thu Mar 11 15:52:17 2010 +0100 @@ -158,8 +158,10 @@ 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 @@ 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 -r e13797685ee6 -r 9ea7238ad935 tests/test-archive --- a/tests/test-archive Thu Mar 11 15:38:35 2010 +0100 +++ b/tests/test-archive Thu Mar 11 15:52:17 2010 +0100 @@ -74,6 +74,20 @@ 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 <