[PATCH] export with top directory for tar format

William Dodé wilk-ml at flibuste.net
Wed Jul 6 14:45:17 BST 2005


Hi,

When we export in tar format we cannot give a top directory like
bzr-0.0.5/... It's very annoying when we extract it.

If there is no better idea, i did a patch to can give the root
directory as an option like :
bzr export --format=tgz --root=bzr-0.0.5 bzr-0.0.5.tgz
(maybe --top instead of root to don't make confusion with bzr root ?)


*** modified file 'bzrlib/commands.py'
--- bzrlib/commands.py 
+++ bzrlib/commands.py 
@@ -1042,18 +1042,20 @@
     If no revision is specified this exports the last committed revision.
 
     Format may be an "exporter" name, such as tar, tgz, tbz2.  If none is
-    given, exports to a directory (equivalent to --format=dir)."""
+    given, exports to a directory (equivalent to --format=dir).
+
+    Root may be the top directory for tar, tgz and tbz2 formats."""
     # TODO: list known exporters
     takes_args = ['dest']
-    takes_options = ['revision', 'format']
-    def run(self, dest, revision=None, format='dir'):
+    takes_options = ['revision', 'format', 'root']
+    def run(self, dest, revision=None, format='dir', root=None):
         b = find_branch('.')
         if revision == None:
             rh = b.revision_history()[-1]
         else:
             rh = b.lookup_revision(int(revision))
         t = b.revision_tree(rh)
-        t.export(dest, format)
+        t.export(dest, format, root)
 
 
 class cmd_cat(Command):
@@ -1354,6 +1356,7 @@
     'email':                  None,
     'update':                 None,
     'long':                   None,
+    'root':                   str,
     }
 
 SHORT_OPTIONS = {

*** modified file 'bzrlib/tree.py'
--- bzrlib/tree.py 
+++ bzrlib/tree.py 
@@ -18,6 +18,7 @@
 """
 
 from osutils import pumpfile, appendpath, fingerprint_file
+import os
 
 from bzrlib.trace import mutter, note
 from bzrlib.errors import BzrError
@@ -93,13 +94,13 @@
         pumpfile(self.get_file(fileid), sys.stdout)
         
         
-    def export(self, dest, format='dir'):
+    def export(self, dest, format='dir', root=None):
         """Export this tree."""
         try:
             exporter = exporters[format]
         except KeyError:
             raise BzrCommandError("export format %r not supported" % format)
-        exporter(self, dest)
+        exporter(self, dest, root)
 
 
 
@@ -223,7 +224,7 @@
 ######################################################################
 # export
 
-def dir_exporter(tree, dest):
+def dir_exporter(tree, dest, root):
     """Export this tree to a new directory.
 
     `dest` should not exist, and will be created holding the
@@ -256,7 +257,7 @@
 except ImportError:
     pass
 else:
-    def tar_exporter(tree, dest, compression=None):
+    def tar_exporter(tree, dest, root, compression=None):
         """Export this tree to a new tar file.
 
         `dest` will be created holding the contents of this tree; if it
@@ -273,7 +274,10 @@
         inv = tree.inventory
         for dp, ie in inv.iter_entries():
             mutter("  export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
-            item = tarfile.TarInfo(dp)
+            if root:
+                item = tarfile.TarInfo(os.path.join(root, dp))
+            else:
+                item = tarfile.TarInfo(dp)
             # TODO: would be cool to actually set it to the timestamp of the
             # revision it was last changed
             item.mtime = now
@@ -296,12 +300,12 @@
         ball.close()
     exporters['tar'] = tar_exporter
 
-    def tgz_exporter(tree, dest):
-        tar_exporter(tree, dest, compression='gz')
+    def tgz_exporter(tree, dest, root):
+        tar_exporter(tree, dest, root, compression='gz')
     exporters['tgz'] = tgz_exporter
 
-    def tbz_exporter(tree, dest):
-        tar_exporter(tree, dest, compression='bz2')
+    def tbz_exporter(tree, dest, root):
+        tar_exporter(tree, dest, root, compression='bz2')
     exporters['tbz2'] = tbz_exporter
 
 


-- 
William Dodé - http://flibuste.net





More information about the bazaar mailing list