# HG changeset patch # User Henrik Stuart # Date 2009-05-12 13:50:44 # Node ID fa901423ac2379729e1180a74e6b4bec3675146d # Parent c8e81f557da7784e07514fd4c29f588ea0d8edb7 windows: avoid deleting non-empty reparse points If a hg repository including working directory is a reparse point (directory symlinked or a junction point), then using os.removedirs will remove the reparse point erroneously. This is fixed by only removing directories if they are empty. diff --git a/mercurial/windows.py b/mercurial/windows.py --- a/mercurial/windows.py +++ b/mercurial/windows.py @@ -240,6 +240,33 @@ def groupname(gid=None): If gid is None, return the name of the current group.""" return None +def _removedirs(name): + """special version of os.removedirs that does not remove symlinked + directories or junction points if they actually contain files""" + if osutil.listdir(name): + return + os.rmdir(name) + head, tail = os.path.split(name) + if not tail: + head, tail = os.path.split(head) + while head and tail: + try: + if osutil.listdir(name): + return + os.rmdir(head) + except: + break + head, tail = os.path.split(head) + +def unlink(f): + """unlink and remove the directory if it is empty""" + os.unlink(f) + # try removing directories that might now be empty + try: + _removedirs(os.path.dirname(f)) + except OSError: + pass + try: # override functions with win32 versions if possible from win32 import *