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.
(path, markers=('.git', ))
| 79 | |
| 80 | |
| 81 | def find_vcs_root(path, markers=('.git', )): |
| 82 | """Finds the root directory (including itself) of specified markers. |
| 83 | Args: |
| 84 | path (str): Path of directory or file. |
| 85 | markers (list[str], optional): List of file or directory names. |
| 86 | Returns: |
| 87 | The directory contained one of the markers or None if not found. |
| 88 | """ |
| 89 | if osp.isfile(path): |
| 90 | path = osp.dirname(path) |
| 91 | |
| 92 | prev, cur = None, osp.abspath(osp.expanduser(path)) |
| 93 | while cur != prev: |
| 94 | if any(osp.exists(osp.join(cur, marker)) for marker in markers): |
| 95 | return cur |
| 96 | prev, cur = cur, osp.split(cur)[0] |
| 97 | return None |