From c7630fc00b715b77da18ed5f041860e4241b3291 Mon Sep 17 00:00:00 2001 From: Kai Hoewelmeyer Date: Sun, 10 Sep 2023 19:00:32 +0200 Subject: [PATCH] fix(nvim): fzf git log on fugitive special commit Fixes: #651 --- nvim/config--nvim--init.vim.symlink | 7 ++++++ nvim/tests/fzf-git.test.vim | 35 ++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/nvim/config--nvim--init.vim.symlink b/nvim/config--nvim--init.vim.symlink index 8df4c87104..1987c45c15 100644 --- a/nvim/config--nvim--init.vim.symlink +++ b/nvim/config--nvim--init.vim.symlink @@ -270,6 +270,13 @@ function commit_and_path() if parsed[2] ~= '' then -- we parsed a fugitive:// url local commit, path = unpack(vim.fn.split(parsed[1], ':')) + if string.match(commit, "^[0-3]$") then -- special fugitive commit + -- Reset to HEAD to match fugitive's gclog function's behavior. + -- There is no obvious, public function to restore the actual commit hash from. + -- For "0", there would not be a matching commit hash (other than HEAD anyhow). + commit = "HEAD" + end + local worktree = vim.fn.FugitiveWorkTree() local abs_path = path and worktree .. '/' .. path or worktree return {commit or "HEAD", abs_path} diff --git a/nvim/tests/fzf-git.test.vim b/nvim/tests/fzf-git.test.vim index 030eef2979..3b86d95fc2 100644 --- a/nvim/tests/fzf-git.test.vim +++ b/nvim/tests/fzf-git.test.vim @@ -457,7 +457,7 @@ function Test_CopiedFileFollow() call assert_match('firstfile$', bufname(), 'Wrong file name (copied-from)') endfunction -function Check_DifferentPwd(id) +function Check_Preview(id) call WaitForFzfResults(1) call WaitForScreenContent('Author:\|fatal:') @@ -486,15 +486,44 @@ function Test_DifferentPwd() " English language necessary to check for "fatal:" and "Author:" strings call setenv('LC_ALL', 'en_US.UTF8') - call timer_start(50, funcref('Check_DifferentPwd')) + call timer_start(50, funcref('Check_Preview')) call feedkeys(',gl', 'tx!') " Explicitly use the HEAD version of the 'firstfile' Gedit HEAD:% - call timer_start(50, funcref('Check_DifferentPwd')) + call timer_start(50, funcref('Check_Preview')) call feedkeys(',gl', 'tx!') + " Explicitly use the staging version of the 'firstfile' + Gedit :0:% + + call timer_start(50, funcref('Check_Preview')) + call feedkeys(',gl', 'tx!') +endfunction + +" showing commit history of a buffer should still working on the staging +" / etc. buffers that are extensions in fugitive with ":0", ":1", ":2", ":3" +" Since those are not properly integrated in fugitive as well (e.g., copying +" the commit hash with "y ctrl-g" leaves the special hashes unresolved. +" Moreover, "0" references the staging area, which by design has no hash +" associated. +function Test_SpecialCommit() + call CdTestDir() + + call RunSystemCommand(['git', 'init']) + call assert_equal(0, writefile(['something', 'something2', 'something3'], 'firstfile')) + call RunSystemCommand(['git', 'add', 'firstfile']) + call RunSystemCommand(['git', 'commit', '-m', 'first commit', '--no-verify', '--no-gpg-sign']) + + edit firstfile + Gedit :0:% + + " English language necessary to check for "fatal:" and "Author:" strings + call setenv('LC_ALL', 'en_US.UTF8') + + call timer_start(50, funcref('Check_Preview')) + call feedkeys(',gl', 'tx!') endfunction function Test()