forked from nvim-tree/nvim-tree.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.lua
78 lines (59 loc) · 1.87 KB
/
utils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
local M = {}
local log = require "nvim-tree.log"
local has_cygpath = vim.fn.executable "cygpath" == 1
function M.get_toplevel(cwd)
local cmd = "git -C " .. vim.fn.shellescape(cwd) .. " rev-parse --show-toplevel"
local ps = log.profile_start("git toplevel %s", cwd)
log.line("git", cmd)
local toplevel = vim.fn.system { "git", "-C", vim.fn.shellescape(cwd), "rev-parse", "--show-toplevel" }
log.raw("git", toplevel)
log.profile_end(ps, "git toplevel %s", cwd)
if vim.v.shell_error ~= 0 or not toplevel or #toplevel == 0 or toplevel:match "fatal" then
return nil
end
-- git always returns path with forward slashes
if vim.fn.has "win32" == 1 then
-- msys2 git support
if has_cygpath then
toplevel = vim.fn.system("cygpath -w " .. vim.fn.shellescape(toplevel))
if vim.v.shell_error ~= 0 then
return nil
end
end
toplevel = toplevel:gsub("/", "\\")
end
-- remove newline
return toplevel:sub(0, -2)
end
local untracked = {}
function M.should_show_untracked(cwd)
if untracked[cwd] ~= nil then
return untracked[cwd]
end
local cmd = "git -C " .. cwd .. " config --type=bool status.showUntrackedFiles"
local ps = log.profile_start("git untracked %s", cwd)
log.line("git", cmd)
local has_untracked = vim.fn.system(cmd)
log.raw("git", has_untracked)
log.profile_end(ps, "git untracked %s", cwd)
untracked[cwd] = vim.trim(has_untracked) ~= "false"
return untracked[cwd]
end
function M.file_status_to_dir_status(status, cwd)
local dirs = {}
for p, s in pairs(status) do
if s ~= "!!" then
local modified = vim.fn.fnamemodify(p, ":h")
dirs[modified] = s
end
end
for dirname, s in pairs(dirs) do
local modified = dirname
while modified ~= cwd and modified ~= "/" do
modified = vim.fn.fnamemodify(modified, ":h")
dirs[modified] = s
end
end
return dirs
end
return M