Commit 22ba143d authored by Zhipeng Han's avatar Zhipeng Han Committed by Kai Chen
Browse files

[feat]: find root of Version Control Systems (#129)


Signed-off-by: default avatarZhipeng Han <hanzhipeng9@gmail.com>
parent 5b10dcd7
......@@ -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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment