|
| 1 | +local Path = require("plenary.path") |
| 2 | +local utils = require("neo-tree.utils") |
| 3 | + |
| 4 | +local os_sep = Path.path.sep |
| 5 | + |
| 6 | +local M = {} |
| 7 | + |
| 8 | +local function execute_command(cmd) |
| 9 | + local result = vim.fn.systemlist(cmd) |
| 10 | + |
| 11 | + -- An empty result is ok |
| 12 | + if vim.v.shell_error ~= 0 or (#result > 0 and vim.startswith(result[1], "fatal:")) then |
| 13 | + return false, {} |
| 14 | + else |
| 15 | + return true, result |
| 16 | + end |
| 17 | +end |
| 18 | + |
| 19 | +local function windowize_path(path) |
| 20 | + return path:gsub("/", "\\") |
| 21 | +end |
| 22 | + |
| 23 | +M.get_repository_root = function(path) |
| 24 | + local cmd = "git rev-parse --show-toplevel" |
| 25 | + if utils.truthy(path) then |
| 26 | + cmd = "git -C " .. path .. " rev-parse --show-toplevel" |
| 27 | + end |
| 28 | + local ok, git_root = execute_command(cmd) |
| 29 | + if not ok then |
| 30 | + return nil |
| 31 | + end |
| 32 | + git_root = git_root[1] |
| 33 | + |
| 34 | + if utils.is_windows then |
| 35 | + git_root = windowize_path(git_root) |
| 36 | + end |
| 37 | + |
| 38 | + return git_root |
| 39 | +end |
| 40 | + |
| 41 | +local function get_simple_git_status_code(status) |
| 42 | + -- Prioritze M then A over all others |
| 43 | + if status:match("U") or status == "AA" or status == "DD" then |
| 44 | + return "U" |
| 45 | + elseif status:match("M") then |
| 46 | + return "M" |
| 47 | + elseif status:match("[ACR]") then |
| 48 | + return "A" |
| 49 | + elseif status:match("!$") then |
| 50 | + return "!" |
| 51 | + elseif status:match("?$") then |
| 52 | + return "?" |
| 53 | + else |
| 54 | + local len = #status |
| 55 | + while len > 0 do |
| 56 | + local char = status:sub(len, len) |
| 57 | + if char ~= " " then |
| 58 | + return char |
| 59 | + end |
| 60 | + len = len - 1 |
| 61 | + end |
| 62 | + return status |
| 63 | + end |
| 64 | +end |
| 65 | + |
| 66 | +local function get_priority_git_status_code(status, other_status) |
| 67 | + if not status then |
| 68 | + return other_status |
| 69 | + elseif not other_status then |
| 70 | + return status |
| 71 | + elseif status == "U" or other_status == "U" then |
| 72 | + return "U" |
| 73 | + elseif status == "?" or other_status == "?" then |
| 74 | + return "?" |
| 75 | + elseif status == "M" or other_status == "M" then |
| 76 | + return "M" |
| 77 | + elseif status == "A" or other_status == "A" then |
| 78 | + return "A" |
| 79 | + else |
| 80 | + return status |
| 81 | + end |
| 82 | +end |
| 83 | + |
| 84 | +---Parse "git status" output for the current working directory. |
| 85 | +---@return table table Table with the path as key and the status as value. |
| 86 | +M.status = function(exclude_directories) |
| 87 | + local git_root = M.get_repository_root() |
| 88 | + if not git_root then |
| 89 | + return {} |
| 90 | + end |
| 91 | + local ok, result = execute_command("git status --porcelain=v1") |
| 92 | + if not ok then |
| 93 | + return {} |
| 94 | + end |
| 95 | + |
| 96 | + local git_status = {} |
| 97 | + for _, line in ipairs(result) do |
| 98 | + local status = line:sub(1, 2) |
| 99 | + local relative_path = line:sub(4) |
| 100 | + local arrow_pos = relative_path:find(" -> ") |
| 101 | + if arrow_pos ~= nil then |
| 102 | + relative_path = line:sub(arrow_pos + 5) |
| 103 | + end |
| 104 | + -- remove any " due to whitespace in the path |
| 105 | + relative_path = relative_path:gsub('^"', ""):gsub('$"', "") |
| 106 | + if utils.is_windows == true then |
| 107 | + relative_path = windowize_path(relative_path) |
| 108 | + end |
| 109 | + local absolute_path = string.format("%s%s%s", git_root, os_sep, relative_path) |
| 110 | + git_status[absolute_path] = status |
| 111 | + |
| 112 | + if not exclude_directories then |
| 113 | + -- Now bubble this status up to the parent directories |
| 114 | + local file_status = get_simple_git_status_code(status) |
| 115 | + local parents = Path:new(absolute_path):parents() |
| 116 | + for i = #parents, 1, -1 do |
| 117 | + local path = parents[i] |
| 118 | + local path_status = git_status[path] |
| 119 | + git_status[path] = get_priority_git_status_code(path_status, file_status) |
| 120 | + end |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + return git_status, git_root |
| 125 | +end |
| 126 | + |
| 127 | +M.load_ignored = function(path) |
| 128 | + local git_root = M.get_repository_root(path) |
| 129 | + if not git_root then |
| 130 | + return {} |
| 131 | + end |
| 132 | + local ok, result = execute_command( |
| 133 | + "git --no-optional-locks status --porcelain=v1 --ignored=matching --untracked-files=normal" |
| 134 | + ) |
| 135 | + if not ok then |
| 136 | + return {} |
| 137 | + end |
| 138 | + |
| 139 | + local ignored = {} |
| 140 | + for _, v in ipairs(result) do |
| 141 | + -- git ignore format: |
| 142 | + -- !! path/to/file |
| 143 | + -- !! path/to/path/ |
| 144 | + -- with paths relative to the repository root |
| 145 | + if v:sub(1, 2) == "!!" then |
| 146 | + local entry = v:sub(4) |
| 147 | + -- remove any " due to whitespace in the path |
| 148 | + entry = entry:gsub('^"', ""):gsub('$"', "") |
| 149 | + if utils.is_windows then |
| 150 | + entry = windowize_path(entry) |
| 151 | + end |
| 152 | + -- use the absolute path |
| 153 | + table.insert(ignored, string.format("%s%s%s", git_root, os_sep, entry)) |
| 154 | + end |
| 155 | + end |
| 156 | + |
| 157 | + return ignored |
| 158 | +end |
| 159 | + |
| 160 | +M.is_ignored = function(ignored, path, _type) |
| 161 | + path = _type == "directory" and (path .. os_sep) or path |
| 162 | + for _, v in ipairs(ignored) do |
| 163 | + if v:sub(-1) == os_sep then |
| 164 | + -- directory ignore |
| 165 | + if vim.startswith(path, v) then |
| 166 | + return true |
| 167 | + end |
| 168 | + else |
| 169 | + -- file ignore |
| 170 | + if path == v then |
| 171 | + return true |
| 172 | + end |
| 173 | + end |
| 174 | + end |
| 175 | +end |
| 176 | + |
| 177 | +return M |
0 commit comments