git-for-windows / git

A fork of Git containing Windows-specific patches.
http://gitforwindows.org/
Other
8.37k stars 2.54k forks source link

git vim cursor is not always at the first line #1207

Closed zjturner closed 7 years ago

zjturner commented 7 years ago

Setup

$ git --version --build-options

git version 2.13.0.windows.1
built from commit: eba7af3dbb4c846c6303c5f64102acee696c9ab0
sizeof-long: 4
machine: x86_64
$ cmd.exe /c ver

Microsoft Windows [Version 10.0.15063]
# One of the following:
> type "C:\Program Files\Git\etc\install-options.txt"
> type "C:\Program Files (x86)\Git\etc\install-options.txt"
> type "%USERPROFILE%\AppData\Local\Programs\Git\etc\install-options.txt"
$ cat /etc/install-options.txt

Path Option: Cmd
SSH Option: OpenSSH
CURL Option: OpenSSL
CRLF Option: CRLFCommitAsIs
Bash Terminal Option: ConHost
Performance Tweaks FSCache: Enabled
Use Credential Manager: Enabled
Enable Symlinks: Disabled

Nothing

Details

cmd

git commit

Expected a vim window to be open with a pending commit message and the cursor in the upper left corner.

The problem is non-deterministic. I can do the same thing 5 times and the problem might only happen once. But the cursor will be on the first column of a random line. Pressing i to start typing a commit message then starts editing in the middle of the auto-generated commit-message comments.

I don't think it's repository specific, but regardless I am using this repository https://github.com/llvm-project/llvm-project-20170507.git.

Here is my entire git config.

core.symlinks=false
core.autocrlf=false
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
diff.astextplain.textconv=astextplain
rebase.autosquash=true
http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.required=true
filter.lfs.process=git-lfs filter-process
credential.helper=manager
user.name=foo
user.email=foo@foo.com
core.whitespace=trailing-space,space-before-tab
core.longpaths=true
format.signature=
color.ui=true
diff.tool=araxis
difftool.araxis.path=C:\Program Files\Araxis\Araxis Merge\compare.exe
merge.tool=araxis
mergetool.araxis.path=C:\Program Files\Araxis\Araxis Merge\compare.exe
alias.dos2unix=!git diff --name-only --cached | xargs dos2unix
alias.tidy=!git diff --name-only --cached | xargs sed --in-place 's/[[:space:]]\+$//' | xargs dos2unix
color.status.changed=cyan
color.status.untracked=cyan
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
filter.lfs.clean=git-lfs clean -- %f
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
core.hidedotfiles=dotGitOnly
remote.origin.url=https://github.com/llvm-project/llvm-project-20170507.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.rawstyle.remote=origin
branch.rawstyle.merge=refs/heads/master
branch.support.remote=origin
branch.support.merge=refs/heads/master
branch.commit.remote=origin
branch.commit.merge=refs/heads/master
branch.fixbuild.remote=origin
branch.fixbuild.merge=refs/heads/master
dscho commented 7 years ago

I think this is related to this part of /etc/vimrc:

    "Remember the positions in files with some git-specific exceptions"
    autocmd BufReadPost *
      \ if line("'\"") > 0 && line("'\"") <= line("$")
      \           && expand("%") !~ "COMMIT_EDITMSG"
      \           && expand("%") !~ "ADD_EDIT.patch"
      \           && expand("%") !~ "addp-hunk-edit.diff"
      \           && expand("%") !~ "git-rebase-todo" |
      \   exe "normal g`\"" |
      \ endif

I could imagine that there are other basenames we need to add. Could you see whether you can find out what is the precise file name of the file to be edited, next time this happens?

zjturner commented 7 years ago

Attached is a screenshot of my window immediately upon typing git commit. You can see the filename at the bottom, and you can see the cursor about halfway down in the first column. It seems plausible that the above code is related, but the filename seems to match the pattern, so maybe this code isn't being executed for some reason?

untitled

zjturner commented 7 years ago

Note that this seems to be a regression. I recently upgraded from 2.10 and did not have this problem.

zjturner commented 7 years ago

It definitely does seem to be related to remembering the file position. If I do git commit, type an arbitrary commit message, then make a trivial change and use git commit --amend the cursor is usually on the last character of the previously typed commit message, which implies the remembering logic is not working.

That said, I don't know anything about editing .vimrc files by hand, so I can't tell if it's a bug in the .vimrc file or a bug in vim itself.

dscho commented 7 years ago

I just had a quick look and it would appear that expand("%") expands to the full path.

Could you try the following: in C:\Program Files\Git\etc\vimrc, change the expand("%") calls by expand("%:t") in the part to which I referred earlier.

Note: you will have to edit the file as administrator. The easiest way may be to right-click on Git CMD in the Start Menu, select Run as Administrator and then call git config -f "C:\Program Files\Git\etc\vimrc" -e.

Sidenote: I got that information from http://vimdoc.sourceforge.net/htmldoc/eval.html#expand(), and I tried it out using :echom expand("%:t") in vim.

If that change fixes the symptoms on your side, I will take it from there.

toby-allsopp commented 7 years ago

I have the same problem. I tried replacing all the "%" with "%:t" in /etc/vimrc and it didn't make any difference. I added an echom inside the if to see if it's actually running the normal g command, and after your suggested change, it is not running it, but the cursor position is still updated.

There is suspicious looking code in /usr/share/vim/vim80/defaults.vim that does a similar thing. This file is sourced if there is no ~/.vimrc, which is new behaviour is Vim 8.0.

So, to fix this, simply create ~/.vimrc (it can be empty)/

toby-allsopp commented 7 years ago

Might be a good idea to add "MERGE_MSG" to the list as well.

dscho commented 7 years ago

@toby-allsopp good point about that new defaults.vim feature. It's not the first time this has caused surprise.

I tried to disable the vimStartup autogroup, as recommended, but as I did it in /etc/vimrc (and that file is parsed before defaults.vim) it did not work.

Now, I could skip the defaults.vim entirely by defining skip_defaults_vim, but that seems quite a bit heavy-handed.

Probably the best I can do is to edit defaults.vim via git-extra's post-install script. Ugly, but probably would work...

toby-allsopp commented 7 years ago

It seems that this problem is known upstream also: https://github.com/vim/vim/commit/9a48961d8bd7ffea14330b9b0181a6cdbe9288f7

dscho commented 7 years ago

It seems that this problem is known upstream also: vim/vim@9a48961

Thanks. This is only half a solution, though, as it does not take into account other file types that need adjusting... So I am relatively comfortable with the workaround I implemented.