diff options
author | mitchell <70453897+orbitalquark@users.noreply.github.com> | 2021-01-22 15:57:13 -0500 |
---|---|---|
committer | mitchell <70453897+orbitalquark@users.noreply.github.com> | 2021-01-22 15:57:13 -0500 |
commit | eec43aaccb3222c838091ac4eb8f8780d921d4cc (patch) | |
tree | 3cd59fdbf211b02dfa71ca03a3adb9cb7a3d4260 /core | |
parent | c5aa8f8dec9611828b6972bc1a72cc8571eab36a (diff) |
`io.get_project_root()` accepts an optional flag for returning a submodule root.
This is for systems like git that have '.git' files (not directories) for
submodules under a parent '.git' directory.
Diffstat (limited to 'core')
-rw-r--r-- | core/file_io.lua | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/core/file_io.lua b/core/file_io.lua index f02043b5..2ff36344 100644 --- a/core/file_io.lua +++ b/core/file_io.lua @@ -338,17 +338,22 @@ local vcs = {'.bzr', '.git', '.hg', '.svn', '_FOSSIL_'} -- VCSes are Bazaar, Fossil, Git, Mercurial, and SVN. -- @param path Optional filesystem path to a project or a file contained within -- a project. The default value is the buffer's filename or the current --- working directory. +-- working directory. This parameter may be omitted. +-- @param submodule Optional flag that indicates whether or not to return the +-- root of the current submodule (if applicable). The default value is +-- `false`. -- @return string root or nil -- @name get_project_root -function io.get_project_root(path) +function io.get_project_root(path, submodule) + if type(path) == 'boolean' then path, submodule = nil, path end if not assert_type(path, 'string/nil', 1) then path = buffer.filename or lfs.currentdir() end local dir = path:match('^(.+)[/\\]?') while dir do for i = 1, #vcs do - if lfs.attributes(dir .. '/' .. vcs[i], 'mode') then return dir end + local mode = lfs.attributes(dir .. '/' .. vcs[i], 'mode') + if mode and (submodule or mode == 'directory') then return dir end end dir = dir:match('^(.+)[/\\]') end |