Add vim command ShowFile for git show HASH:fname
dblume

dblume commited on 2023-06-07 09:49:30
Showing 1 changed files, with 10 additions and 4 deletions.


Show does "git show HASH" to display commit message
ShowFile does "git show HASH:fname" to display old version of file
... ...
@@ -164,25 +164,31 @@ function! GitBlame()
164 164
 endfunction
165 165
 command Blame :call GitBlame()
166 166
 
167
-function! GitShow()
167
+function! GitShow(commit_or_file)
168 168
     let l:fname = expand('%')
169 169
     let l:hash = expand('<cword>')
170 170
     if l:hash =~ '^[0-9a-f]\{7,40}$'
171 171
         if stridx(l:fname, ' -- ') != -1
172 172
             let l:fname = split(l:fname, ' -- ')[-1]
173 173
         endif
174
+        if a:commit_or_file != "file"
174 175
             " Have Show show all the affected files, so don't actually use  "--"
175 176
             " exec 'tabnew | r! git show ' . l:hash . ' -- ' . shellescape(l:fname)
176 177
             exec 'tabnew | r! git show ' . l:hash
177
-        setl buftype=nofile
178
-        0d_
179 178
             " We lie here (' -- ') to have a filename the other git commands can use.
180 179
             exec 'silent :file git show ' . l:hash . ' -- ' . l:fname
180
+        else
181
+            exec 'tabnew | r! git show ' . l:hash . ':' . shellescape(l:fname)
182
+            exec 'silent :file git show ' . l:hash . ':' . l:fname
183
+        endif
184
+        setl buftype=nofile
185
+        0d_
181 186
     else
182 187
         echo l:hash . ' is not a git hash.'
183 188
     endif
184 189
 endfunction
185
-command Show :call GitShow()
190
+command Show :call GitShow("commit")
191
+command ShowFile :call GitShow("file")
186 192
 
187 193
 function! GitDiff()
188 194
     let l:fname = expand('%:.')
189 195