comparison mercurial/pathutil.py @ 43633:0b7733719d21

utils: move finddirs() to pathutil This is a follow-up to c21aca51b392 (utils: move the `dirs` definition in pathutil (API), 2019-11-06). finddirs() is closely related to dirs and used by it. Differential Revision: https://phab.mercurial-scm.org/D7388
author Martin von Zweigbergk <martinvonz@google.com>
date Thu, 14 Nov 2019 08:03:26 -0800
parents c21aca51b392
children d84420232492
comparison
equal deleted inserted replaced
43632:2e017696181f 43633:0b7733719d21
273 return path + pycompat.ossep 273 return path + pycompat.ossep
274 else: 274 else:
275 return path 275 return path
276 276
277 277
278 def finddirs(path):
279 pos = path.rfind(b'/')
280 while pos != -1:
281 yield path[:pos]
282 pos = path.rfind(b'/', 0, pos)
283 yield b''
284
285
278 class dirs(object): 286 class dirs(object):
279 '''a multiset of directory names from a set of file paths''' 287 '''a multiset of directory names from a set of file paths'''
280 288
281 def __init__(self, map, skip=None): 289 def __init__(self, map, skip=None):
282 self._dirs = {} 290 self._dirs = {}
293 for f in map: 301 for f in map:
294 addpath(f) 302 addpath(f)
295 303
296 def addpath(self, path): 304 def addpath(self, path):
297 dirs = self._dirs 305 dirs = self._dirs
298 for base in util.finddirs(path): 306 for base in finddirs(path):
299 if base.endswith(b'/'): 307 if base.endswith(b'/'):
300 raise ValueError( 308 raise ValueError(
301 "found invalid consecutive slashes in path: %r" % base 309 "found invalid consecutive slashes in path: %r" % base
302 ) 310 )
303 if base in dirs: 311 if base in dirs:
305 return 313 return
306 dirs[base] = 1 314 dirs[base] = 1
307 315
308 def delpath(self, path): 316 def delpath(self, path):
309 dirs = self._dirs 317 dirs = self._dirs
310 for base in util.finddirs(path): 318 for base in finddirs(path):
311 if dirs[base] > 1: 319 if dirs[base] > 1:
312 dirs[base] -= 1 320 dirs[base] -= 1
313 return 321 return
314 del dirs[base] 322 del dirs[base]
315 323