airblade/vim-gitgutter

g:gitgutter_diff_base () is invalid

BaktashGorgani opened this issue · 10 comments

What is the latest commit SHA in your installed vim-gitgutter?
67ef116
What vim/nvim version are you on?
NVIM v0.9.2

I see that #874 exists however that should have been fixed in commit b9e9ad2.

I am on the latest code and still getting this issue.

Let me also add this is only happening when I am on Windows. On Ubuntu and Fedora it seems to run fine.

The plugin still works but this just shows up whenever I open a file.

Please could you turn on logging (let g:gitgutter_log=1), reproduce the error, then look in the log file for the command it ran? If you run that command by hand on the terminal, does it work?

@airblade thanks so much for responding!

So this helped me figure out what the issue is, just not too sure how you can go about solving it.

From the gitgutter.log I can see the commands that are run and the one that fails looks like this

cd "C:\Users\bgorgani\Dev\Arris_LC\modules" && (git --no-pager show --textconv :modules/arris_cmts.py > C:\Users\bgorgani\AppData\Local\Temp\nvim.0\lRIDJR\0.4.1.py || exit 0) && (git --no-pager diff --no-ext-diff --no-color -U0 -- C:\Users\bgorgani\AppData\Local\Temp\nvim.0\lRIDJR\0.4.1.py C:\Users\bgorgani\AppData\Local\Temp\nvim.0\lRIDJR\1.4.1.py | grep "^@@ " || exit 0)

With the error message that I get back looking like this

error: Could not access 'C:UsersbgorganiAppDataLocalTempnvim.0lRIDJR0.4.1.py'

When I saw the slashes were missing from the path I knew it was because I was using Git Bash (MinGW64) and how it doesn't always play nicely with windows paths.

When I tried running Neovim again from powershell, the error did not come back.

As I said, the plugin still seems to function properly but you just get the error message when you open any file.

Here is a comparison of the log when neovim is run from Git Bash and Powershell

Git Bash

$cat gitgutter.log

========== start log session 11.01.2024 18:32:53 ==========

  0.002237 BufEnter Autocommands for "*"..function <SNR>5_on_bufenter[24]..gitgutter#process_buffer[14]..<SNR>55_setup_path[3]..gitgutter#utility#set_repo_path[15]..gitgutter#async#execute[1]:
  0.002237 [async] cd "C:\Users\bgorgani\Dev\Arris_LC\modules" && git ls-files -v --error-unmatch --full-name -z -- arris_cmts.py

  0.207275 function <SNR>8_on_exit_nvim[2]..1[10]..gitgutter#process_buffer[23]..gitgutter#diff#run_diff[55]..gitgutter#utility#base_path[31]..<SNR>6_obtain_file_renames[3]..gitgutter#utility#git_supports_command_line_config_override[2]..gitgutter#utility#system[1]:
  0.207275 git -c foo.bar=baz --version

  0.441456 function <SNR>8_on_exit_nvim[2]..1[10]..gitgutter#process_buffer[23]..gitgutter#diff#run_diff[55]..gitgutter#utility#base_path[31]..<SNR>6_obtain_file_renames[7]..gitgutter#utility#system[1]:
  0.441456 cd "C:\Users\bgorgani\Dev\Arris_LC\modules" && git diff --diff-filter=R --name-status

  0.660061 function <SNR>8_on_exit_nvim[2]..1[10]..gitgutter#process_buffer[23]..gitgutter#diff#run_diff[87]..gitgutter#async#execute[1]:
  0.660061 [async] cd "C:\Users\bgorgani\Dev\Arris_LC\modules" && (git --no-pager show --textconv :modules/arris_cmts.py > C:\Users\bgorgani\AppData\Local\Temp\nvim.0\lRIDJR\0.4.1.py || exit 0) && (git --no-pager diff --no-ext-diff --no-color -U0  -- C:\Users\bgorgani\AppData\Local\Temp\nvim.0\lRIDJR\0.4.1.py C:\Users\bgorgani\AppData\Local\Temp\nvim.0\lRIDJR\1.4.1.py | grep "^@@ " || exit 0)

  0.947186 function <SNR>8_on_exit_nvim[2]..gitgutter#diff#handler[1]:
  0.947186

Powershell

$cat gitgutter.log

========== start log session 11.01.2024 18:44:39 ==========

  0.002287 BufEnter Autocommands for "*"..function <SNR>5_on_bufenter[24]..gitgutter#process_buffer[14]..<SNR>55_setup_path[3]..gitgutter#utility#set_repo_path[15]..gitgutter#async#execute[1]:
  0.002287 [async] cd /d "C:\Users\bgorgani\Dev\Arris_LC\modules" && git ls-files -v --error-unmatch --full-name -z -- arris_cmts.py

  0.317865 function <SNR>8_on_exit_nvim[2]..1[10]..gitgutter#process_buffer[23]..gitgutter#diff#run_diff[55]..gitgutter#utility#base_path[31]..<SNR>6_obtain_file_renames[3]..gitgutter#utility#git_supports_command_line_config_override[2]..gitgutter#utility#system[1]:
  0.317865 git -c foo.bar=baz --version

  0.583942 function <SNR>8_on_exit_nvim[2]..1[10]..gitgutter#process_buffer[23]..gitgutter#diff#run_diff[55]..gitgutter#utility#base_path[31]..<SNR>6_obtain_file_renames[7]..gitgutter#utility#system[1]:
  0.583942 cd /d "C:\Users\bgorgani\Dev\Arris_LC\modules" && git -c "core.safecrlf=false" diff --diff-filter=R --name-status

  0.858046 function <SNR>8_on_exit_nvim[2]..1[10]..gitgutter#process_buffer[23]..gitgutter#diff#run_diff[87]..gitgutter#async#execute[1]:
  0.858046 [async] cd /d "C:\Users\bgorgani\Dev\Arris_LC\modules" && (git --no-pager show --textconv :modules/arris_cmts.py > C:\Users\bgorgani\AppData\Local\Temp\nvim.0\92xRDg\1.4.1.py || exit 0) && (git --no-pager -c "diff.autorefreshindex=0" -c "diff.noprefix=false" -c "core.safecrlf=false" diff --no-ext-diff --no-color -U0  -- C:\Users\bgorgani\AppData\Local\Temp\nvim.0\92xRDg\1.4.1.py C:\Users\bgorgani\AppData\Local\Temp\nvim.0\92xRDg\2.4.1.py || exit 0)

  1.183877 function <SNR>8_on_exit_nvim[2]..gitgutter#diff#handler[1]:
  1.183877

wrapping all paths in quotes seems to allow the command to run

$cd "C:\Users\bgorgani\Dev\Arris_LC\modules" && (git --no-pager show --textconv :modules/arris_cmts.py > "C:\Users\bgorgani\AppData\Local\Temp\nvim.0\GJRL21\0.4.1.py" || exit 0) && (git --no-pager diff --no-ext-diff --no-color -U0  -- "C:\Users\bgorgani\AppData\Local\Temp\nvim.0\GJRL21\0.4.1.py" "C:\Users\bgorgani\AppData\Local\Temp\nvim.0\GJRL21\1.4.1.py" | grep "^@@ " || exit 0)
warning: in the working copy of 'C:/Users/bgorgani/AppData/Local/Temp/nvim.0/GJRL21/0.4.1.py', LF will be replaced by CRLF the next time Git touches it

Thanks for digging into that. Adding quotation marks seems like a good idea, though with Windows you never know. I'll try to patch it over the next few days.

Please could you try this on Git Bash (MinGW64), as well as Powershell, Ubuntu, and Fedora?

diff --git i/autoload/gitgutter/diff.vim w/autoload/gitgutter/diff.vim
index 6325ca3..35de9ba 100644
--- i/autoload/gitgutter/diff.vim
+++ w/autoload/gitgutter/diff.vim
@@ -97,6 +97,8 @@ function! gitgutter#diff#run_diff(bufnr, from, preserve_full_diff) abort
     let buff_file .= '.'.extension
   endif
 
+  let buff_file = gitgutter#utility#shellescape(buff_file)
+
   " Write buffer to temporary file.
   " Note: this is synchronous.
   call s:write_buffer(a:bufnr, buff_file)
@@ -114,6 +116,8 @@ function! gitgutter#diff#run_diff(bufnr, from, preserve_full_diff) abort
       let from_file .= '.'.extension
     endif
 
+    let from_file = gitgutter#utility#shellescape(from_file)
+
     " Write file from index to temporary file.
     let index_name = gitgutter#utility#get_diff_base(a:bufnr).':'.gitgutter#utility#base_path(a:bufnr)
     let cmd .= gitgutter#git().' --no-pager show --textconv '.index_name.' > '.from_file.' || exit 0) && ('

I made those 2 changes for nvim on Git Bash and, well, I am getting a different error this time and suspecting gitgutter isn't the cause of it.

This is the error I am receiving now

Error detected while processing function <SNR>28_on_exit_nvim[2]..1[10]..gitgutter#process_buffer[31]..function <SNR>28_on_exit_nvim[2]..1[10]..gitgutter#process_buffer[23]..gitgutter#diff#run_diff[41]..<SNR>88_write_buffer:
line   40:
E739: Cannot create directory "C:\Users\bgorgani\AppData\Local\Temp\nvim.0\QBixCn: invalid argument

The trace points to the mkdir() call in this code:

" The file we are writing to is a temporary file. Sometimes the parent
" directory is deleted outside Vim but, because Vim caches the directory
" name at startup and does not check for its existence subsequently, Vim
" does not realise. This causes E482 errors.
try
call writefile(bufcontents, a:file, 'b')
catch /E482/
call mkdir(fnamemodify(a:file, ':h'), '', '0700')
call writefile(bufcontents, a:file, 'b')
endtry

I don't know if the problem is escaping backslashes on Git Bash or some difficulty with the temp directory area.

What happens if you run it by hand, i.e. :call mkdir("C:\Users\bgorgani\AppData\Local\Temp\nvim.0\QBixCn", '', '0700') (assuming nvim.0 exists)?

Does it work with vim (instead of nvim) on Git Bash?

Have you had a chance to test this?