Repeatedly call Blame to go further back in history
dblume

dblume commited on 2023-04-25 21:52:21
Showing 1 changed files, with 18 additions and 4 deletions.

... ...
@@ -143,24 +143,37 @@ function! OpenCurrentAsNewTab()
143 143
 endfunction
144 144
 nmap <leader>o :call OpenCurrentAsNewTab()<CR>
145 145
 
146
-" git blame, show, and diff
146
+" git blame, show, diff and log
147 147
 " Delete these functions when you install https://github.com/tpope/vim-fugitive
148 148
 function! GitBlame()
149
+    let l:hash = expand('<cword>')
149 150
     let l:currentView = winsaveview()
150
-    let l:fname = expand('%:t')
151
-    exec 'tabnew | r! git blame ' . shellescape(expand('%'))
151
+    " If in a Blame window already, do blame for previous commit
152
+    if l:hash =~ '^[0-9a-f]\{8,40}$' && stridx(expand('%'), ' -- ') != -1
153
+        let l:fname = split(expand('%'), ' -- ')[-1]
154
+        exec 'tabnew | r! git blame ' . l:hash . '^ -- ' . shellescape(l:fname)
155
+        exec 'silent :file git blame ' . l:hash . '^ -- ' . l:fname
156
+    else
157
+        let l:fname = expand('%')
158
+        exec 'tabnew | r! git blame -- ' . shellescape(l:fname)
159
+        exec 'silent :file git blame -- ' . l:fname
160
+    endif
161
+    0d_
152 162
     call winrestview(l:currentView)
153 163
     setl buftype=nofile
154
-    exec 'silent :file git blame ' . l:fname
155 164
 endfunction
156 165
 command Blame :call GitBlame()
157 166
 
158 167
 function! GitShow()
159 168
     let l:hash = expand('<cword>')
169
+    if l:hash =~ '^[0-9a-f]\{8,40}$'
160 170
         exec 'tabnew | r! git show ' . l:hash
161 171
         setl buftype=nofile
162 172
         0d_
163 173
         exec 'silent :file git show ' . l:hash
174
+    else
175
+        echo l:hash . ' is not a git hash.'
176
+    endif
164 177
 endfunction
165 178
 command Show :call GitShow()
166 179
 
... ...
@@ -192,6 +205,7 @@ function! GitLog()
192 205
     let l:fname = expand('%:t')
193 206
     exec 'tabnew | r! git log1 -- ' . shellescape(expand('%'))
194 207
     setl buftype=nofile
208
+    0d_
195 209
     exec 'silent :file git log1 ' . l:fname
196 210
 endfunction
197 211
 command Log :call GitLog()
198 212