Allow optional flag for gittab's Blame and Show
dblume

dblume commited on 2024-02-26 23:59:03
Showing 1 changed files, with 25 additions and 15 deletions.


For example, "-c" is useful for merge commits
... ...
@@ -24,15 +24,20 @@ function! s:ShowBufInNewSplit(bufname)
24 24
    return 0
25 25
 endfunction
26 26
 
27
-function! s:GitBlame()
27
+function! s:GitBlame(...)
28 28
     let l:hash = expand('<cword>')
29 29
     let l:currentView = winsaveview()
30
+    let l:args = a:1
31
+    if strlen(l:args)
32
+        " Add a space to the end
33
+        let l:args = l:args . " "
34
+    endif
30 35
     " If in a Blame window already, do blame for some prior commit
31 36
     if l:hash =~ '^[0-9a-f]\{7,40}$' && stridx(expand('%'), ' -- ') != -1
32 37
         let l:fname = split(expand('%'), ' -- ')[-1]
33
-        let l:bufname = 'git blame ' . l:hash . '^ -- ' . l:fname
38
+        let l:bufname = 'git blame ' . l:args . l:hash . '^ -- ' . l:fname
34 39
         if !s:ShowBufInNewTab(l:bufname)
35
-            exec 'tabnew | r! git blame ' . l:hash . '^ -- ' . shellescape(l:fname)
40
+            exec 'tabnew | r! git blame ' . l:args . l:hash . '^ -- ' . shellescape(l:fname)
36 41
             exec 'silent :file ' . fnameescape(l:bufname)
37 42
         endif
38 43
     else
... ...
@@ -45,15 +50,15 @@ function! s:GitBlame()
45 50
             let l:hash = split(l:fname_parts[0], ' ')[-1]
46 51
         endif
47 52
         if strlen(l:hash)
48
-            let l:bufname = 'git blame ' . l:hash . ' -- ' . l:fname
53
+            let l:bufname = 'git blame ' . l:args . l:hash . ' -- ' . l:fname
49 54
             if !s:ShowBufInNewTab(l:bufname)
50
-                exec 'tabnew | r! git blame ' . l:hash . ' -- ' . shellescape(l:fname)
55
+                exec 'tabnew | r! git blame ' . l:args . l:hash . ' -- ' . shellescape(l:fname)
51 56
                 exec 'silent :file ' . fnameescape(l:bufname)
52 57
             endif
53 58
         else
54
-            let l:bufname = 'git blame -- ' . l:fname
59
+            let l:bufname = 'git blame ' . l:args . '-- ' . l:fname
55 60
             if !s:ShowBufInNewTab(l:bufname)
56
-                exec 'tabnew | r! git blame -- ' . shellescape(l:fname)
61
+                exec 'tabnew | r! git blame ' . l:args . '-- ' . shellescape(l:fname)
57 62
                 exec 'silent :file ' . fnameescape(l:bufname)
58 63
             endif
59 64
         endif
... ...
@@ -62,30 +67,35 @@ function! s:GitBlame()
62 67
     call winrestview(l:currentView)
63 68
     setl buftype=nofile
64 69
 endfunction
65
-command Blame :call s:GitBlame()
70
+command -nargs=* Blame :call s:GitBlame(<q-args>)
66 71
 
67
-function! s:GitShow(commit_or_file)
72
+function! s:GitShow(commit_or_file, ...)
68 73
     let l:fname = expand('%')
69 74
     let l:hash = expand('<cword>')
70 75
     if l:hash =~ '^[0-9a-f]\{7,40}$'
71 76
         if stridx(l:fname, ' -- ') != -1
72 77
             let l:fname = split(l:fname, ' -- ')[-1]
73 78
         endif
79
+        let l:args = a:1
80
+        if strlen(l:args)
81
+            " Add a space to the end
82
+            let l:args = l:args . " "
83
+        endif
74 84
         if a:commit_or_file != "file"
75
-            let l:bufname = 'git show ' . l:hash . ' -- ' . l:fname
85
+            let l:bufname = 'git show ' . l:args . l:hash . ' -- ' . l:fname
76 86
             if !s:ShowBufInNewTab(l:bufname)
77 87
                 " Have Show show all the affected files, so don't actually use  "--"
78 88
                 " exec 'tabnew | r! git show ' . l:hash . ' -- ' . shellescape(l:fname)
79
-                exec 'tabnew | r! git show ' . l:hash
89
+                exec 'tabnew | r! git show ' . l:args . l:hash
80 90
                 " We lie here (' -- ') to have a filename the other git commands can use.
81 91
                 exec 'silent :file ' . fnameescape(l:bufname)
82 92
             endif
83 93
             0d_
84 94
         else
85 95
             let l:currentView = winsaveview()
86
-            let l:bufname = 'git show ' . l:hash . ':' . l:fname
96
+            let l:bufname = 'git show ' . l:args . l:hash . ':' . l:fname
87 97
             if !s:ShowBufInNewTab(l:bufname)
88
-                exec 'tabnew | r! git show ' . l:hash . ':' . shellescape(l:fname)
98
+                exec 'tabnew | r! git show ' . l:args. l:hash . ':' . shellescape(l:fname)
89 99
                 exec 'silent :file ' . fnameescape(l:bufname)
90 100
             endif
91 101
             0d_
... ...
@@ -96,8 +106,8 @@ function! s:GitShow(commit_or_file)
96 106
         echo l:hash . ' is not a git hash.'
97 107
     endif
98 108
 endfunction
99
-command Show :call s:GitShow("commit")
100
-command ShowFile :call s:GitShow("file")
109
+command -nargs=* Show :call s:GitShow("commit", <q-args>)
110
+command -nargs=* ShowFile :call s:GitShow("file", <q-args>)
101 111
 
102 112
 function! s:GitDiff()
103 113
     let l:fname = expand('%:.')
104 114