Skip to content

Commit

Permalink
[feat]: find root of Version Control Systems (open-mmlab#129)
Browse files Browse the repository at this point in the history
Signed-off-by: Zhipeng Han <[email protected]>
  • Loading branch information
OneDirection9 authored and hellock committed Sep 7, 2019
1 parent 5b10dcd commit 22ba143
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions mmcv/utils/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,24 @@ def scandir(dir_path, suffix=None):
return _scandir_py35(dir_path, suffix)
else:
return _scandir_py(dir_path, suffix)


def find_vcs_root(path, markers=('.git', )):
"""Finds the root directory (including itself) of specified markers.
Args:
path (str): Path of directory or file.
markers (list[str], optional): List of file or directory names.
Returns:
The directory contained one of the markers or None if not found.
"""
if osp.isfile(path):
path = osp.dirname(path)

prev, cur = None, osp.abspath(osp.expanduser(path))
while cur != prev:
if any(osp.exists(osp.join(cur, marker)) for marker in markers):
return cur
prev, cur = cur, osp.split(cur)[0]
return None

0 comments on commit 22ba143

Please sign in to comment.