Skip to content

#1217 show git status for link targets, when no status on the link itself #1263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lua/nvim-tree/explorer/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,39 @@ local uv = vim.loop

local M = {}

local function get_dir_git_status(parent_ignored, status, absolute_path)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These could be public.

if parent_ignored then
return "!!"
end
local dir_status = status.dirs and status.dirs[absolute_path]
local file_status = status.files and status.files[absolute_path]
return dir_status or file_status
end

local function get_git_status(parent_ignored, status, absolute_path)
return parent_ignored and "!!" or status.files and status.files[absolute_path]
end

function M.has_one_child_folder(node)
return #node.nodes == 1 and node.nodes[1].nodes and uv.fs_access(node.nodes[1].absolute_path, "R")
end

function M.update_git_status(node, parent_ignored, status)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be moved to new module lua/nvim-tree/explorer/git.lua

-- status of the node's absolute path
if node.nodes then
node.git_status = get_dir_git_status(parent_ignored, status, node.absolute_path)
else
node.git_status = get_git_status(parent_ignored, status, node.absolute_path)
end

-- status of the link target, if the link itself is not dirty
if node.link_to and not node.git_status then
if node.nodes then
node.git_status = get_dir_git_status(parent_ignored, status, node.link_to)
else
node.git_status = get_git_status(parent_ignored, status, node.link_to)
end
end
end

return M
11 changes: 8 additions & 3 deletions lua/nvim-tree/explorer/explore.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,21 @@ local function populate_children(handle, cwd, node, status)
and not filters.should_ignore_git(abs, status.files)
and not nodes_by_path[abs]
then
local child = nil
if t == "directory" and uv.fs_access(abs, "R") then
table.insert(node.nodes, builders.folder(node, abs, name, status, node_ignored))
child = builders.folder(node, abs, name, status, node_ignored)
elseif t == "file" then
table.insert(node.nodes, builders.file(node, abs, name, status, node_ignored))
child = builders.file(node, abs, name, status, node_ignored)
elseif t == "link" then
local link = builders.link(node, abs, name, status, node_ignored)
if link.link_to ~= nil then
table.insert(node.nodes, link)
child = link
end
end
if child then
table.insert(node.nodes, child)
common.update_git_status(child, node_ignored, status)
end
end
end
end
Expand Down
22 changes: 3 additions & 19 deletions lua/nvim-tree/explorer/node-builders.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,13 @@ local M = {
is_windows = vim.fn.has "win32" == 1,
}

function M.get_dir_git_status(parent_ignored, status, absolute_path)
if parent_ignored then
return "!!"
end
local dir_status = status.dirs and status.dirs[absolute_path]
local file_status = status.files and status.files[absolute_path]
return dir_status or file_status
end

function M.get_git_status(parent_ignored, status, absolute_path)
return parent_ignored and "!!" or status.files and status.files[absolute_path]
end

function M.folder(parent, absolute_path, name, status, parent_ignored)
function M.folder(parent, absolute_path, name)
local handle = uv.fs_scandir(absolute_path)
local has_children = handle and uv.fs_scandir_next(handle) ~= nil

return {
absolute_path = absolute_path,
fs_stat = uv.fs_stat(absolute_path),
git_status = M.get_dir_git_status(parent_ignored, status, absolute_path),
group_next = nil, -- If node is grouped, this points to the next child dir/link node
has_children = has_children,
name = name,
Expand All @@ -42,15 +28,14 @@ local function is_executable(absolute_path, ext)
return uv.fs_access(absolute_path, "X")
end

function M.file(parent, absolute_path, name, status, parent_ignored)
function M.file(parent, absolute_path, name)
local ext = string.match(name, ".?[^.]+%.(.*)") or ""

return {
absolute_path = absolute_path,
executable = is_executable(absolute_path, ext),
extension = ext,
fs_stat = uv.fs_stat(absolute_path),
git_status = M.get_git_status(parent_ignored, status, absolute_path),
name = name,
parent = parent,
}
Expand All @@ -61,7 +46,7 @@ end
-- links (for instance libr2.so in /usr/lib) and thus even with a C program realpath fails
-- when it has no real reason to. Maybe there is a reason, but errno is definitely wrong.
-- So we need to check for link_to ~= nil when adding new links to the main tree
function M.link(parent, absolute_path, name, status, parent_ignored)
function M.link(parent, absolute_path, name)
--- I dont know if this is needed, because in my understanding, there isnt hard links in windows, but just to be sure i changed it.
local link_to = uv.fs_realpath(absolute_path)
local open, nodes, has_children
Expand All @@ -75,7 +60,6 @@ function M.link(parent, absolute_path, name, status, parent_ignored)
return {
absolute_path = absolute_path,
fs_stat = uv.fs_stat(absolute_path),
git_status = M.get_git_status(parent_ignored, status, absolute_path),
group_next = nil, -- If node is grouped, this points to the next child dir/link node
has_children = has_children,
link_to = link_to,
Expand Down
6 changes: 1 addition & 5 deletions lua/nvim-tree/explorer/reload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ local M = {}
local function update_status(nodes_by_path, node_ignored, status)
return function(node)
if nodes_by_path[node.absolute_path] then
if node.nodes then
node.git_status = builders.get_dir_git_status(node_ignored, status, node.absolute_path)
else
node.git_status = builders.get_git_status(node_ignored, status, node.absolute_path)
end
common.update_git_status(node, node_ignored, status)
end
return node
end
Expand Down