dblume commited on 2023-12-12 15:20:44
Showing 1 changed files, with 49 additions and 3 deletions.
| ... | ... |
@@ -180,6 +180,18 @@ function! ShowBufInNewTab(bufname) |
| 180 | 180 |
return 0 |
| 181 | 181 |
endfunction |
| 182 | 182 |
|
| 183 |
+"A helper function that tries to show a buffer if it already exists |
|
| 184 |
+function! ShowBufInNewSplit(bufname) |
|
| 185 |
+ let l:bnr = bufnr(a:bufname) |
|
| 186 |
+ if l:bnr > 0 |
|
| 187 |
+" bo vne |
|
| 188 |
+ vne |
|
| 189 |
+ exec 'buffer ' . l:bnr |
|
| 190 |
+ return 1 |
|
| 191 |
+ endif |
|
| 192 |
+ return 0 |
|
| 193 |
+endfunction |
|
| 194 |
+ |
|
| 183 | 195 |
function! GitBlame() |
| 184 | 196 |
let l:hash = expand('<cword>')
|
| 185 | 197 |
let l:currentView = winsaveview() |
| ... | ... |
@@ -260,19 +272,47 @@ function! GitDiff() |
| 260 | 272 |
let l:buf = winbufnr(0) |
| 261 | 273 |
let l:commit = 'HEAD' |
| 262 | 274 |
let l:hash = expand('<cword>')
|
| 275 |
+ let l:currentView = winsaveview() |
|
| 263 | 276 |
|
| 264 | 277 |
" If the current word is a hash, then diff that vs. previous |
| 265 | 278 |
if l:hash =~ '^[0-9a-f]\{7,40}$' && stridx(expand('%'), ' -- ') != -1
|
| 266 | 279 |
let l:fname = split(expand('%'), ' -- ')[-1]
|
| 280 |
+ let l:bufname = 'git show ' . l:hash . '^:' . l:fname |
|
| 281 |
+ if !ShowBufInNewTab(l:bufname) |
|
| 282 |
+ exec ':tabnew | silent r! git show ' . l:hash . '^:$(git rev-parse --show-prefix)' . shellescape(l:fname) |
|
| 283 |
+ setl buftype=nofile |
|
| 284 |
+ 0d_ |
|
| 285 |
+ exec 'silent :file ' . fnameescape(l:bufname) |
|
| 286 |
+ endif |
|
| 287 |
+ |
|
| 288 |
+ let l:bufname = 'git show ' . l:hash . ':' . l:fname |
|
| 289 |
+ if !ShowBufInNewSplit(l:bufname) |
|
| 290 |
+ exec 'vne | silent r! git show ' . l:hash . ':$(git rev-parse --show-prefix)' . shellescape(l:fname) |
|
| 291 |
+ setl buftype=nofile |
|
| 292 |
+ exec 'silent :file ' . fnameescape(l:bufname) |
|
| 293 |
+ 0d_ |
|
| 294 |
+ endif |
|
| 295 |
+ elseif stridx(expand('%'), ':') != -1
|
|
| 296 |
+ " If we're in a 'git show' buffer, then extract fname and hash from there |
|
| 297 |
+ let l:fname_parts = split(l:fname, ':') |
|
| 298 |
+ let l:fname = l:fname_parts[-1] |
|
| 299 |
+ let l:hash = split(l:fname_parts[0], ' ')[-1] |
|
| 300 |
+ " TODO: Below few lines are identical to above, so remove dupes. |
|
| 301 |
+ let l:bufname = 'git show ' . l:hash . '^:' . l:fname |
|
| 302 |
+ if !ShowBufInNewTab(l:bufname) |
|
| 267 | 303 |
exec ':tabnew | silent r! git show ' . l:hash . '^:$(git rev-parse --show-prefix)' . shellescape(l:fname) |
| 268 | 304 |
setl buftype=nofile |
| 269 | 305 |
0d_ |
| 270 |
- exec 'silent :file ' . fnameescape('git show '.l:hash .'^:'.l:fname)
|
|
| 306 |
+ exec 'silent :file ' . fnameescape(l:bufname) |
|
| 307 |
+ endif |
|
| 271 | 308 |
|
| 309 |
+ let l:bufname = 'git show ' . l:hash . ':' . l:fname |
|
| 310 |
+ if !ShowBufInNewSplit(l:bufname) |
|
| 272 | 311 |
exec 'vne | silent r! git show ' . l:hash . ':$(git rev-parse --show-prefix)' . shellescape(l:fname) |
| 273 | 312 |
setl buftype=nofile |
| 274 |
- exec 'silent :file ' . fnameescape('git show '.l:hash.':'.l:fname)
|
|
| 313 |
+ exec 'silent :file ' . fnameescape(l:bufname) |
|
| 275 | 314 |
0d_ |
| 315 |
+ endif |
|
| 276 | 316 |
else |
| 277 | 317 |
" If the buffer is not different then repo, then diff HEAD vs file's previous commit |
| 278 | 318 |
let l:o = system("git status --porcelain | grep " . l:fname)
|
| ... | ... |
@@ -280,13 +320,17 @@ function! GitDiff() |
| 280 | 320 |
let l:commit = system('git log -2 --pretty=format:"%h" -- ' . l:fname . ' | tail -n 1')
|
| 281 | 321 |
endif |
| 282 | 322 |
|
| 323 |
+ let l:bufname = 'git show ' . l:commit . ':' . l:fname |
|
| 283 | 324 |
" Bug if l:filename includes ".." |
| 325 |
+ if !ShowBufInNewTab(l:bufname) |
|
| 284 | 326 |
exec ':tabnew | r! git show ' . l:commit . ':$(git rev-parse --show-prefix)' . l:fname |
| 285 | 327 |
setl buftype=nofile |
| 286 | 328 |
0d_ |
| 287 |
- exec 'silent :file ' . fnameescape('git show '.l:commit.':'.l:fname)
|
|
| 329 |
+ exec 'silent :file ' . fnameescape(l:bufname) |
|
| 330 |
+ endif |
|
| 288 | 331 |
exec 'vert sb '.l:buf |
| 289 | 332 |
endif |
| 333 |
+ call winrestview(l:currentView) |
|
| 290 | 334 |
windo diffthis |
| 291 | 335 |
setl buftype=nofile |
| 292 | 336 |
wincmd r |
| ... | ... |
@@ -298,6 +342,8 @@ function! GitLog(flags) |
| 298 | 342 |
let l:fname = expand('%')
|
| 299 | 343 |
if stridx(l:fname, ' -- ') != -1 |
| 300 | 344 |
let l:fname = split(l:fname, ' -- ')[-1] |
| 345 |
+ elseif stridx(l:fname, ':') != -1 |
|
| 346 |
+ let l:fname = split(l:fname, ':')[-1] |
|
| 301 | 347 |
endif |
| 302 | 348 |
let l:bufname = 'git log ' . a:flags . '-- ' . l:fname |
| 303 | 349 |
if !ShowBufInNewTab(l:bufname) |
| 304 | 350 |