lewis6991 / gitsigns.nvim

Git integration for buffers
MIT License
4.91k stars 187 forks source link

Manual `:Gitsigns attach` does not work if `attach_to_untracked = false` #1026

Open przepompownia opened 3 months ago

przepompownia commented 3 months ago

Description

For reporting previous issues I tested Gitsigns without attach_to_untracked (false by default since some moment) and found that I cannot manually attach new untracked file.

Neovim version

v0.11.0-dev-124+g90a4b1a59

Operating system and version

Debian Sid

Expected behavior

:Gitsigns attach works independently of attach_to_untracked.

Actual behavior

:Gitsigns attach seems to not work.

Minimal config

for name, url in pairs{
  gitsigns = 'https://github.com/lewis6991/gitsigns.nvim',
  -- ADD OTHER PLUGINS _NECESSARY_ TO REPRODUCE THE ISSUE
} do
  local install_path = vim.fn.fnamemodify('gitsigns_issue/'..name, ':p')
  if vim.fn.isdirectory(install_path) == 0 then
    vim.fn.system { 'git', 'clone', '--depth=1', url, install_path }
  end
  vim.opt.runtimepath:append(install_path)
end

require('gitsigns').setup{
  debug_mode = true, -- You must add this to enable debug messages
  -- ADD GITSIGNS CONFIG THAT IS _NECESSARY_ FOR REPRODUCING THE ISSUE
}

-- ADD INIT.LUA SETTINGS THAT IS _NECESSARY_ FOR REPRODUCING THE ISSUE

Steps to reproduce

Inside some Git repo

  1. nvim --clean -u minimal.lua new3
  2. Gitsigns attach
  3. :Gistigns stage_buffer
  4. quit or suspend nvim
  5. GIT_PAGER= git diff --staged -- new3

Gitsigns debug messages

attach(1): Attaching (trigger=BufNewFile)
run_job: git --no-pager --no-optional-locks --literal-pathspecs -c gc.auto=0 config user.name
run_job: git --version
run_job: git --no-pager --no-optional-locks --literal-pathspecs -c gc.auto=0 rev-parse --show-toplevel --absolute-git-dir --abbrev-ref HEAD
run_job: git --no-pager --no-optional-locks --literal-pathspecs -c gc.auto=0 --git-dir .../.git -c core.quotepath=off ls-files --stage --others --exclude-standard --eol .../new3
attach(1): Not a file
run_job: git --no-pager --no-optional-locks --literal-pathspecs -c gc.auto=0 rev-parse --show-toplevel --absolute-git-dir --abbrev-ref HEAD
cli.run: Running action 'attach' with arguments { "%" }
attach(%): Attaching
cli.run: Running action 'attach' with arguments { 1 }
attach(1): Attaching
run_job: git --no-pager --no-optional-locks --literal-pathspecs -c gc.auto=0 config user.name
run_job: git --no-pager --no-optional-locks --literal-pathspecs -c gc.auto=0 rev-parse --show-toplevel --absolute-git-dir --abbrev-ref HEAD
run_job: git --no-pager --no-optional-locks --literal-pathspecs -c gc.auto=0 --git-dir .../.git -c core.quotepath=off ls-files --stage --others --exclude-standard --eol .../new3
lewis6991 commented 3 months ago

This is because the logic that attach_to_untracked drives attach():

  if not config.attach_to_untracked and git_obj.object_name == nil then
    dprint('File is untracked')
    return
  end

attach() does a lot of checks to make sure the buffer is attachable. I'm not how else to implement this so attach() ignores only some of these checks.

przepompownia commented 3 months ago

I'm not encouraging you to make any bigger effort here, especially that this issue is only a side effect of working on a minimal reproduction environment. I have set attach_to_untracked to true in my real environment.

lewis6991 commented 3 months ago

FWIW I think it would be nice if it worked. If a user has attach_to_untracked = false, :Gitsigns attach provides a convenient way to stage an untracked buffer.