Skip to content

Git Actions #2474

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

Closed
hinell opened this issue Oct 16, 2023 · 14 comments
Closed

Git Actions #2474

hinell opened this issue Oct 16, 2023 · 14 comments

Comments

@hinell
Copy link
Contributor

hinell commented Oct 16, 2023

function M.rename(node, to)
local notify_from = notify.render_path(node.absolute_path)
local notify_to = notify.render_path(to)
if utils.file_exists(to) then
notify.warn(err_fmt(notify_from, notify_to, "file already exists"))
return
end
events._dispatch_will_rename_node(node.absolute_path, to)
local success, err = vim.loop.fs_rename(node.absolute_path, to)
if not success then
return notify.warn(err_fmt(notify_from, notify_to, err))
end
notify.info(notify_from .. "" .. notify_to)
utils.rename_loaded_buffers(node.absolute_path, to)
events._dispatch_node_renamed(node.absolute_path, to)
end

Describe the solution you'd like

The e and r don't respect git repo: by renaming something we loose track of file path history. I would like to be able to rename filepath by using git mv (instead of system command) in order to keep track of file renames:

git mv <src_path> <dst_path>

Currently, nvim-tree doesn't provide this feature forcing me to rename file by using git manually.

Describe alternatives you've considered

Running !git mv % <path_no_new_name> is rather clunky...
Running !git -A <path_no_new_name> is rather clunky and not something I want...

Can this functionality be implemented utilising API?

NO.

Additional context

I'ts difficult to easily contribute API feature, cause:

  1. The runner is not a generic lua wrapper for git:
  • it's hard-wired to run only single git subcommand: status
  • quite intricate interally - it's hard to extend
  • no docs/comments
  1. This repo has no dedicated git wrapper for running & handling git output (there is a dedicated fast libgit2 lua bindings btw)
  2. Intent of nvim-tree.git is unclear
@alex-courtis
Copy link
Member

This comprehensive solution might help you out: Git Stage Unstage Files And Directories From The Tree

I'm using a (dumber) approach for git operations, mapping this one to gu:

local function git_unstage()
  local path, dir = node_path_dir()
  if path and dir then
    vim.fn.system({ "git", "-C", dir, "restore", "--staged", path })
  end
end

Changes to .git/index are be seen and the tree status is updated.

@alex-courtis
Copy link
Member

I'll investigate a git wrapper however I can't promise anything.

@hinell
Copy link
Contributor Author

hinell commented Oct 17, 2023

@alex-courtis For a low-cost solution, you may peek wrappers at (it would be best if all maintainers unite to create unified api):

I'm using a (dumber) approach

It's nice to have htis for paths; I usually stick to gitsigns to stage hunk-by-hunk

@alex-courtis
Copy link
Member

Unfortunately we can't practically move to libgit2 lua bindings as it requires native compilation. Almost no plugins use that due to installation dramas.

It could be an optional implementation for power users.

@alex-courtis
Copy link
Member

For a low-cost solution, you may peek wrappers at (it would be best if all maintainers unite to create unified api):

It would be sensible for many git plugins to use a single abstraction layer. That would be a large task...

@alex-courtis alex-courtis changed the title Rename file by using git Optional libgit2 Oct 21, 2023
@alex-courtis alex-courtis added performance performance enhancement and removed feature request labels Oct 21, 2023
@hinell
Copy link
Contributor Author

hinell commented Oct 21, 2023

Unfortunately we can't practically move

I'm not asking for that. I'm requesting a feture that would take renaming into account in git, so git kees track of it. It's totally fine if we go with something like vim.cmd([[!git mv ... ]]).

@hinell hinell changed the title Optional libgit2 Rename file by using git Oct 21, 2023
@hinell
Copy link
Contributor Author

hinell commented Oct 21, 2023

I've changed title back. It's all about using git for renaming. Whether to use dedicated libraries or terminal git command or not - is up to implementer.

@alex-courtis
Copy link
Member

We do have a mechanism for renaming - the recipe. A git executable call is the only "API" that git provides.

Is there an issue with the renaming? The git watcher should pick up changes immediately.

@hinell hinell changed the title Rename file by using git [feature]: Rename file by using git Oct 21, 2023
@hinell
Copy link
Contributor Author

hinell commented Oct 21, 2023

It's a feature request.

Currently if you rename a file by using nvim-tree in a git repo the git will count it as deleted and the renamed file will be considered as untracked forcing me to make a full circle back to rename it back and then git mv.

It would be nice if nvim-tree avoids this situation by using git mv command for for tracked files so their renames is handled by git and history isn't lost.

@alex-courtis
Copy link
Member

alex-courtis commented Oct 21, 2023

I see, no. mv file file.newname results in deleted / untracked as expected. When you stage, git will recognise the rename.

The tools are there, you just need to implement it yourself: as a mapping, or an event handler.

This would be a great opportunity for an extension plugin: https://github.com/nvim-tree/nvim-tree.lua/wiki/Extension-Plugins

@hinell
Copy link
Contributor Author

hinell commented Oct 21, 2023

When you stage, git will recognise the rename.

It won't unless -A flag is given to git add. Another consideration is that I use gisings for staging from within neovim, not from terminal i.e. to stage hunks, not entire files. Imagine it's already renamed and changes added.

@alex-courtis alex-courtis added feature request and removed performance performance enhancement labels Oct 22, 2023
@alex-courtis alex-courtis changed the title [feature]: Rename file by using git Git Actions Oct 22, 2023
@alex-courtis
Copy link
Member

alex-courtis commented Oct 22, 2023

Feature

API additions to allow git mutation. These may be mapped by default.

gu, ga, gr etc. would be intuitive mappings.

Prior Art

Alternative

Extension Plugin

Scope

Fugitive is stable and mature and interacts seamlessly with nvim-tree. Its mutation feature set would be safe and sufficient.

@hinell
Copy link
Contributor Author

hinell commented Oct 22, 2023

Well, even if we make an extension, we can't hook into default rename action. Users will have to redefine it.

@alex-courtis
Copy link
Member

See #2551 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants