Commits

Markus Zapke-Gründemann committed d7abcce

Added new option --retain-empty-dirs to import empty directories.

Comments (0)

Files changed (3)

 - Symlinks are no longer dereferenced on Linux.
 - Renamed --exclude option to --exclude-pattern.
 - Added new option --exclude-path to exclude a path relative to SOURCE.
+- Added new option --retain-empty-dirs to import empty directories.
 
 1.0.1
 =====
         raise shutil.Error(errors)
 
 
+def create_empty_file(name, dirname, names):
+    """Creates an empty file named "name" in dirname if dirname is empty.
+
+    This solves the problem of adding empty directories to a repository.
+    """
+    if len(names) == 0:
+        empty_file = os.path.join(dirname, name)
+        with open(empty_file, 'w'):
+            pass
+
+
 def importfs(ui, repo, source, *pats, **opts):
     """Import a set of files from a file-system into a repository.
 
     The --exclude-path option takes an exact path as value. The result is that
     the files in the path are not imported. The option can be used several
     times for different paths. The path is specified relative to SOURCE.
+
+    Using the --retain-empty-dirs option will create a new file, .empty, during
+    the import when a empty directory is encountered so that the directory is
+    added to the repository. This is executed after the contents of the SOURCE
+    are determined (e.g. after the exclude options are applied).
     """
     sources = []
     for node in (source,) + pats:
             for warning in warnings:
                 ui.write('Warning: Failed to copy %s to %s (%s).\n' %
                     warning.args[0][0])
+    if opts.get('retain_empty_dirs') is True:
+        os.path.walk(repo.root, create_empty_file, '.empty')
     commands.addremove(ui, repo, similarity=opts.get('similarity'))
     message = opts.get('message') or 'importfs commit.'
     commands.commit(ui, repo, message=message)
     ('', 'exclude-path', list(),
         _('Exclude the exact path relative to SOURCE.'), _('PATH')),
     ('', 'ignore-copy-errors', None, _('Turn all errors during the file copy '
-        'operation into warnings.'))],
+        'operation into warnings.')),
+    ('', 'retain-empty-dirs', None,
+        _('Add empty directories to the repository.'))],
     '[OPTION]... REPO SOURCE...')
 }
 commands.norepo += ' importfs'

test-importfs-empty-dirs.t

+Load the extension:
+
+  $ echo "[extensions]" >> $HGRCPATH
+  $ echo "importfs = $TESTDIR/importfs.py" >> $HGRCPATH
+
+Create a simple file system structure to import:
+
+  $ mkdir -p d1/d2
+  $ echo c1 > d1/f1
+  $ echo c2 > d1/d2/f2
+  $ mkdir d1/d3
+
+Perform the import. The directory d3 should not be imported because it's empty:
+
+  $ hg importfs r1 d1
+  created repository $TESTTMP/r1
+  0 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  adding d2/f2
+  adding f1
+
+Perform the import again, now using the --retain-empty-dirs option:
+
+  $ hg importfs --retain-empty-dirs r2 d1
+  created repository $TESTTMP/r2
+  0 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  adding d2/f2
+  adding d3/.empty
+  adding f1