Error using the :GDiff command of fugitive.vim using gvim for windows and msys git 1.7.0.2
Asked Answered
J

4

11

I've been using git along with fugitive.vim to manage code when I'm on windows. However, I've run into a problem. According to the documentation, the :GDiff command should a diff window and allow me to stage only parts of a file. However, when I issue the command in a file with changes, I get the following error message: alt text

Is this a problem with windows? vim? fugitive? msysgit? file permissions? Anybody know?

Josiahjosias answered 28/5, 2010 at 20:43 Comment(1)
I'm having the _exact_ same problem - please do let us know if/how you get this resolved!Galatia
G
14

I had the same problem - here's how I fixed it.

By default, Vim will try to store swap files beside the original. Fugitive creates a buffer that does not correspond to any real file path, so Vim barfs when it tries to create the swap file. The solution is to make sure that Vim has a path it can write to.

Here's how I fixed this:

if has("win32") || has("win64")
   set directory=$TMP
else
   set directory=~/tmp
end

Here's the original issue on GitHub: https://github.com/tpope/vim-fugitive/issues/9

EDIT:

As fow points out below, it's probably better to use something like the following:

set directory+=,~/tmp,$TMP

Vim should use the first path it can.

Galatia answered 1/5, 2011 at 5:51 Comment(4)
It may be worth noting that the 'directory' option is a list of directories, and Vim will use the first one it can. It's best to simply add a value to the list rather than obliterate the whole thing, as in set directory+=$TMP. It's safe to do this without an OS check since ~/tmp is added by default in Unix, and even if $TMP is empty on Unix, adding it will do nothingBenedick
@fow: +1 -- I do need to include , when I do set directory+=, right (since it's a comment delimited list)?Galatia
Also, try set directory^=$TMP (see :help :set^=).Friction
@Chris Johnsen: Awesome; I was curious if I could do something like that.Galatia
F
2

I do not have a Windows machine, but I was able to reproduce and isolate this problem on a Mac OS X 10.6 system.

Either create C:\TMP or C:\TEMP (:help 'directory' says both are in the default value of the directory option on Windows builds of Vim), or add an existing directory to the value of the directory option.

I use a Vim-specific temporary directory under my home directory (again, no Windows here) and add two trailing slashes (~/tmp/.vim-swaps//). This kind of setting centralizes the swap files but gives them unique names based on the directories of the files being edited. See “Advantages” and “Disadvantages” in :help :swapname as well as the “if a directory ends in two path separators” bit in :help 'directory'.

Use ^= to prepend1 your directory (so that it is always used, if it exists):

set directory^=C:\\some\\existing\\directory//

Or, use += to append your directory (so that it is only used if it exists and no other applicable directory entries exist):

set directory+=C:\\some\\existing\\directory//

Charles’ answer suggests that $TMP (a reference to an environment variable) might be a good value for Windows:

set directory+=$TMP//

The root cause is related to the default value of the directory option combined with the pathname of :Gdiff’s index “pseudo” file.

The buffer for the index side of a :Gdiff view uses a special pathname that looks like fugitive:///path/to/repository/.git//0/path/to/file/under/repository (fugitive captures reads and writes to so named buffers and redirects them to the index by invoking Git “plumbing” commands).

The first entry in directory is usually .. This means that Vim will try to put the swapfile in the same directory as the edited file (i.e. foo/bar.txt will try to use foo/.bar.swp). If this preferred swapfile can not be created, then subsequent entries from directory will be tried.

With . as the first directory, the preferred swapfile for fugitive:///path/to/repository/.git//0/path/to/file/under/repository will be fugitive:///path/to/repository/.git//0/path/to/file/under/.repository.swp. The leading components of this pathname (fugitive:, path, to, etc.) probably do not exist, so Vim will go on to the next entry from directory. If none of the other directory entries are usable for creating swap files (e.g. they do not exist), then you get error E302.

I was able to reproduce your problem on a Unix-y system by using set directory=.,~/no-such-dir,/var/no-such-dir,/no-such-dir (i.e. taking the default Unix value and changing occurrences of tmp to no-such-dir). None of the “no-such-dir” directories actually existed. I got the same error when I use :Gdiff.

1 :help :set^= only says “add”, but the code shows that set listopt^=… prepends similar to how set listopt+=… appends (as the latter is documented to do). Both should automatically insert commas as needed (though there may have been bugs in this area).

Friction answered 7/5, 2011 at 8:8 Comment(2)
i wonder why fugitive allows both read and write to those fugitive:///path/to/repository/.git//0/path/to/file/under/repository buffers? Why are they not read-only? In my case I can modify those buffers, and if I save them and then execute git diff, the output is affected.Caducity
@HảiPhong: Also try git diff --cached. With those pseudo-pathnames (the ones like fugitive://…/.git//0/…), editing and saving them is equivalent to directly changing the content that is staged for the next commit (i.e. Git’s “index”: stage 0). If you are confident in using the index to craft your next commit, it can be a powerful tool (like git add or git reset with --patch, but much more direct). If you do not like to use the index, you can “clear” its contents (make it match HEAD) with git reset <pathname> (or :Gread - in the buffer in question).Friction
S
1

That may be related to the issue 428 of msysgit mentioned in my SO answer.

Try patching the cmd/git.cmd as I describe in my previous answer and see if this help.

Baring that, 2 options remains:

  • either a locking problem (the file is locked by a process): an utility like process monitor or process explorer might help to see if it is the case (and what handle exactly there is on that swap file)
  • or a Git issue. One good trick is then to install the net-installer of msysgit, which will build the latest version of Git on your Windows. You can then try and see if the problem persists with that updated version.
    Note: third option, trying to downgrade Git, again to rule out any link with that tool.
Shan answered 28/5, 2010 at 20:51 Comment(3)
I already patched git.cmd, which did fix the earlier issue (thanks for that, by the way.Josiahjosias
@Paul: I didn't realize that you were the one asking that previous question! ;)Shan
I checked the swap files and it looks like vim is the only app with a handle for those files. I'm working on checking different versions of msysgit right now and I've also filed an issue on github.Josiahjosias
D
1

BTW same error I got, when I tried to :gdiff unsaved file - when i saved it (:w) it works!

Debacle answered 9/1, 2011 at 13:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.