# HG changeset patch # User Benoit Boissinot # Date 1129705852 25200 # Node ID c6e6ca96a033b6cb451a71164face55f4e756d72 # Parent 32fde51910c0180e1ae266d62dad57c1efb88a44 refactor some unlink/remove code and make sure we prune empty dir diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -1441,12 +1441,7 @@ def remove(ui, repo, pat, *pats, **opts) if okaytoremove(abs, rel, exact): if ui.verbose or not exact: ui.status(_('removing %s\n') % rel) names.append(abs) - for name in names: - try: - os.unlink(name) - except OSError, inst: - if inst.errno != errno.ENOENT: raise - repo.remove(names) + repo.remove(names, unlink=True) def rename(ui, repo, *pats, **opts): """rename files; equivalent of copy + remove""" @@ -1454,12 +1449,8 @@ def rename(ui, repo, *pats, **opts): names = [] for abs, rel, exact in copied: if ui.verbose or not exact: ui.status(_('removing %s\n') % rel) - try: - os.unlink(rel) - except OSError, inst: - if inst.errno != errno.ENOENT: raise names.append(abs) - repo.remove(names) + repo.remove(names, unlink=True) return errs def revert(ui, repo, *names, **opts): diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py --- a/mercurial/localrepo.py +++ b/mercurial/localrepo.py @@ -536,7 +536,13 @@ class localrepository: else: self.dirstate.forget([f]) - def remove(self, list): + def remove(self, list, unlink=False): + if unlink: + for f in list: + try: + util.unlink(self.wjoin(f)) + except OSError, inst: + if inst.errno != errno.ENOENT: raise for f in list: p = self.wjoin(f) if os.path.exists(p): @@ -1264,14 +1270,11 @@ class localrepository: for f in remove: self.ui.note(_("removing %s\n") % f) try: - os.unlink(self.wjoin(f)) + util.unlink(self.wjoin(f)) except OSError, inst: if inst.errno != errno.ENOENT: self.ui.warn(_("update failed to remove %s: %s!\n") % (f, inst.strerror)) - # try removing directories that might now be empty - try: os.removedirs(os.path.dirname(self.wjoin(f))) - except: pass if moddirstate: if branch_merge: self.dirstate.update(remove, 'r') diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -310,6 +310,13 @@ def rename(src, dst): os.unlink(dst) os.rename(src, dst) +def unlink(f): + """unlink and remove the directory if it is empty""" + os.unlink(f) + # try removing directories that might now be empty + try: os.removedirs(os.path.dirname(f)) + except: pass + def copyfiles(src, dst, hardlink=None): """Copy a directory tree using hardlinks if possible"""