HappyGoLucky

Web系サーバーサイド寄りの自動化大好きエンジニアの徒然なるブログ

vim上で git rebase -i が動かない

解決はしたが、原因は不明。

解決方法

sequence.editor を設定する。 core.editor ではだめ。

$ git config --global sequence.editor $(which vim)

環境

  • MacBook Pro
  • iTerm2
  • zsh 5.5.1 (x86_64-apple-darwin17.5.0)
  • MacVim
    • VIM - Vi IMproved 8.1 (2018 May 18, compiled Aug 31 2018 11:39:09)
    • macOS
    • 適用済パッチ: 1-332

調査

vim 上からコマンド打って rebase できなくなった。

$ vim

Created autostash: dcc6d6b
HEAD is now at fde30be fixup! fea
/usr/local/Cellar/git/2.18.0/libexec/git-core/git-rebase--interactive: line 277: /Users/a10lab-yasuhiroki/go/src/github.com/yasuhiroki/slackcat/.git/rebase-merge/git-rebase-todo: Permission denied
Applied autostash.
Could not execute editor

.git/rebase-merge/ ディレクトリは存在しない。

$ ls .git
COMMIT_EDITMSG  ORIG_HEAD       description     index           logs/           packed-refs     tags
HEAD            config          hooks/          info/           objects/        refs/

ディレクトリを作ってみると、zshVCS_INFO が怒る。

$ mkdir .git/rebase-merge
VCS_INFO_git_getbranch:25: no such file or directory: .git/rebase-merge/head-name

とりあえず rebase してみるも失敗。

$ vim

It seems that there is already a rebase-merge directory, and
I wonder if you are in the middle of another rebase.  If that is the
case, please try
        git rebase (--continue | --abort | --skip)
If that is not the case, please
        rm -fr "/Users/a10lab-yasuhiroki/go/src/github.com/yasuhiroki/slackcat/.git/rebase-merge"
and run me again.  I am stopping in case you still have something
valuable there.

/usr/local/Cellar/git/2.18.0/libexec/git-core/git-rebase--interactive の先頭で set -xv してデバッグログ出す。

f:id:yasuhiroki:20180831114918p:plain

...前略...
++ /Users/a10lab-yasuhiroki/go/src/github.com/yasuhiroki/slackcat/.git/rebase-merge/git-rebase-todo
/usr/local/Cellar/git/2.18.0/libexec/git-core/git-rebase--interactive: line 278: /Users/a10lab-yasuhiroki/go/src/github.com/yasuhiroki/slackcat/.git/rebase-merge/git-rebase-todo: Permission denied
gettext "Could not execute editor"
++ gettext 'Could not execute editor'
++ printf %s 'Could not execute editor'
+ die_abort 'Could not execute editor'
+ apply_autostash
+ test -f /Users/a10lab-yasuhiroki/go/src/github.com/yasuhiroki/slackcat/.git/rebase-merge/autostash
+ rm -rf /Users/a10lab-yasuhiroki/go/src/github.com/yasuhiroki/slackcat/.git/rebase-merge
+ die 'Could not execute editor'
+ die_with_status 1 'Could not execute editor'
+ status=1
+ shift
+ printf '%s\n' 'Could not execute editor'
Could not execute editor
+ exit 1

シェルが値を返しました 1

続けるにはENTERを押すかコマンドを入力してください
zsh: suspended  vim

ログをあちこちねじ込んで print デバッグしてくと git var GIT_EDITOR の戻り値がおかしかった。

手元でやると、

$ git var GIT_EDITOR
/usr/local/bin/vim

vim上だと、

:!git var GIT_EDITOR
→何もなし

なんでだろう。git config で core.editor は設定済みのはずだが...。
vim上で !git config -l してもちゃんと定義されてる。

f:id:yasuhiroki:20180831115008p:plain

!git var -l すると、 GIT_EDITOR が空になってる。

f:id:yasuhiroki:20180831115014p:plain

腑に落ちないけど、 sequence.editor を設定すればそっちから読み込んでくれそうなので設定してみる。

git_sequence_editor () {
    if test -z "$GIT_SEQUENCE_EDITOR"
    then
        GIT_SEQUENCE_EDITOR="$(git config sequence.editor)"
        if [ -z "$GIT_SEQUENCE_EDITOR" ]
        then
            GIT_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $?
        fi
    fi
    eval "$GIT_SEQUENCE_EDITOR" '"$@"'
}
$ git config --global sequence.editor $(which vim)
$ git config --global -l | grep editor
core.editor=/usr/local/bin/vim
sequence.editor=/usr/local/bin/vim

無事動くようになった。