comparison mercurial/util.py @ 38493:da2a7d8354b2

unlinkpath: make empty directory removal optional (issue5901) (issue5826) There are known cases where performing operations such as rebase from a directory that is newly created can fail or at least lead to being in a directory handle that no longer exists. This is even reproducible by just doing something as simple as: cd foo; hg rm * The behavior is different if you use `hg addremove`, the directory is not removed until we attempt to go back to the node after committing it: cd foo; rm *; hg addremove; hg ci -m'bye foo'; hg co .^; hg co tip Differential Revision: https://phab.mercurial-scm.org/D3859
author Kyle Lippincott <spectral@google.com>
date Thu, 28 Jun 2018 18:07:22 -0700
parents 63e6f5ae84bc
children 152f4822d210
comparison
equal deleted inserted replaced
38492:2394cd58b81f 38493:da2a7d8354b2
2137 if exctype is not None: 2137 if exctype is not None:
2138 self.discard() 2138 self.discard()
2139 else: 2139 else:
2140 self.close() 2140 self.close()
2141 2141
2142 def unlinkpath(f, ignoremissing=False): 2142 def unlinkpath(f, ignoremissing=False, rmdir=True):
2143 """unlink and remove the directory if it is empty""" 2143 """unlink and remove the directory if it is empty"""
2144 if ignoremissing: 2144 if ignoremissing:
2145 tryunlink(f) 2145 tryunlink(f)
2146 else: 2146 else:
2147 unlink(f) 2147 unlink(f)
2148 # try removing directories that might now be empty 2148 if rmdir:
2149 try: 2149 # try removing directories that might now be empty
2150 removedirs(os.path.dirname(f)) 2150 try:
2151 except OSError: 2151 removedirs(os.path.dirname(f))
2152 pass 2152 except OSError:
2153 pass
2153 2154
2154 def tryunlink(f): 2155 def tryunlink(f):
2155 """Attempt to remove a file, ignoring ENOENT errors.""" 2156 """Attempt to remove a file, ignoring ENOENT errors."""
2156 try: 2157 try:
2157 unlink(f) 2158 unlink(f)