1793
1793
:force: Delete files and directories, even if they are changed and
1794
1794
even if the directories are not empty.
1796
## TODO: Normalize names
1798
1796
if isinstance(files, basestring):
1799
1797
files = [files]
1803
1801
new_files=set()
1804
unknown_files_in_directory=set()
1802
unknown_nested_files=set()
1806
1804
def recurse_directory_to_add_files(directory):
1807
# recurse directory and add all files
1805
# Recurse directory and add all files
1808
1806
# so we can check if they have changed.
1809
1807
for parent_info, file_infos in\
1810
1808
osutils.walkdirs(self.abspath(directory),
1812
1810
for relpath, basename, kind, lstat, abspath in file_infos:
1814
if self.path2id(relpath): #is it versioned?
1815
new_files.add(relpath)
1817
unknown_files_in_directory.add(
1818
(relpath, None, kind))
1811
# Is it versioned or ignored?
1812
if self.path2id(relpath) or self.is_ignored(relpath):
1813
# Add nested content for deletion.
1814
new_files.add(relpath)
1816
# Files which are not versioned and not ignored
1817
# should be treated as unknown.
1818
unknown_nested_files.add((relpath, None, kind))
1820
1820
for filename in files:
1821
1821
# Get file name into canonical form.
1825
1825
new_files.add(filename)
1826
1826
if osutils.isdir(abspath):
1827
1827
recurse_directory_to_add_files(filename)
1828
files = [f for f in new_files]
1829
files = list(new_files)
1830
1831
if len(files) == 0:
1831
1832
return # nothing to do
1833
1834
# Sort needed to first handle directory content before the directory
1834
1835
files.sort(reverse=True)
1837
# Bail out if we are going to delete files we shouldn't
1835
1838
if not keep_files and not force:
1836
has_changed_files = len(unknown_files_in_directory) > 0
1839
has_changed_files = len(unknown_nested_files) > 0
1837
1840
if not has_changed_files:
1838
1841
for (file_id, path, content_change, versioned, parent_id, name,
1839
1842
kind, executable) in self._iter_changes(self.basis_tree(),
1840
1843
include_unchanged=True, require_versioned=False,
1841
1844
want_unversioned=True, specific_files=files):
1842
# check if it's unknown OR changed but not deleted:
1843
if (versioned == (False, False)
1844
or (content_change and kind[1] != None)):
1845
# Check if it's an unknown (but not ignored) OR
1846
# changed (but not deleted) :
1847
if not self.is_ignored(path[1]) and (
1848
versioned == (False, False) or
1849
content_change and kind[1] != None):
1845
1850
has_changed_files = True
1848
1853
if has_changed_files:
1849
# make delta to show ALL applicable changes in error message.
1854
# Make delta show ALL applicable changes in error message.
1850
1855
tree_delta = self.changes_from(self.basis_tree(),
1856
require_versioned=False, want_unversioned=True,
1851
1857
specific_files=files)
1852
for unknown_file in unknown_files_in_directory:
1853
tree_delta.unversioned.extend((unknown_file,))
1858
for unknown_file in unknown_nested_files:
1859
if unknown_file not in tree_delta.unversioned:
1860
tree_delta.unversioned.extend((unknown_file,))
1854
1861
raise errors.BzrRemoveChangedFilesError(tree_delta)
1856
# do this before any modifications
1863
# Build inv_delta and delete files where applicaple,
1864
# do this before any modifications to inventory.
1857
1865
for f in files:
1858
1866
fid = self.path2id(f)
1861
message="%s is not versioned." % (f,)
1869
message = "%s is not versioned." % (f,)
1864
1872
# having removed it, it must be either ignored or unknown
1868
1876
new_status = '?'
1869
1877
textui.show_status(new_status, self.kind(fid), f,
1870
1878
to_file=to_file)
1872
1880
inv_delta.append((f, None, fid, None))
1873
message="removed %s" % (f,)
1881
message = "removed %s" % (f,)
1875
1883
if not keep_files:
1876
1884
abs_path = self.abspath(f)
1877
1885
if osutils.lexists(abs_path):
1878
1886
if (osutils.isdir(abs_path) and
1879
1887
len(os.listdir(abs_path)) > 0):
1880
message="%s is not empty directory "\
1881
"and won't be deleted." % (f,)
1889
osutils.rmtree(abs_path)
1891
message = "%s is not an empty directory "\
1892
"and won't be deleted." % (f,)
1883
1894
osutils.delete_any(abs_path)
1884
message="deleted %s" % (f,)
1895
message = "deleted %s" % (f,)
1885
1896
elif message is not None:
1886
# only care if we haven't done anything yet.
1887
message="%s does not exist." % (f,)
1897
# Only care if we haven't done anything yet.
1898
message = "%s does not exist." % (f,)
1889
# print only one message (if any) per file.
1900
# Print only one message (if any) per file.
1890
1901
if message is not None:
1892
1903
self.apply_inventory_delta(inv_delta)