969684088a558a394b040d6911a2948a17e5d726
dblume Move vim gittab commands to...

dblume authored 10 months ago

1) if exists("g:loaded_gittab") || &cp | finish | endif
2) let g:loaded_gittab = 1
3) 
4) "A helper function that tries to show a buffer if it already exists
5) function! s:ShowBufInNewTab(bufname)
6)    let l:bnr = bufnr(a:bufname)
7)    if l:bnr > 0
8)        tabnew
9)        exec 'buffer ' . l:bnr
10)        return 1
11)    endif
12)    return 0
13) endfunction
14) 
15) "A helper function that tries to show a buffer if it already exists
16) function! s:ShowBufInNewSplit(bufname)
17)    let l:bnr = bufnr(a:bufname)
18)    if l:bnr > 0
19) "       bo vne
20)        vne
21)        exec 'buffer ' . l:bnr
22)        return 1
23)    endif
24)    return 0
25) endfunction
26) 
dblume Allow optional flag for git...

dblume authored 8 months ago

27) function! s:GitBlame(...)
dblume Move vim gittab commands to...

dblume authored 10 months ago

28)     let l:hash = expand('<cword>')
29)     let l:currentView = winsaveview()
dblume Allow optional flag for git...

dblume authored 8 months ago

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
dblume Move vim gittab commands to...

dblume authored 10 months ago

35)     " If in a Blame window already, do blame for some prior commit
36)     if l:hash =~ '^[0-9a-f]\{7,40}$' && stridx(expand('%'), ' -- ') != -1
37)         let l:fname = split(expand('%'), ' -- ')[-1]
dblume Allow optional flag for git...

dblume authored 8 months ago

38)         let l:bufname = 'git blame ' . l:args . l:hash . '^ -- ' . l:fname
dblume Move vim gittab commands to...

dblume authored 10 months ago

39)         if !s:ShowBufInNewTab(l:bufname)
dblume Allow optional flag for git...

dblume authored 8 months ago

40)             exec 'tabnew | r! git blame ' . l:args . l:hash . '^ -- ' . shellescape(l:fname)
dblume Move vim gittab commands to...

dblume authored 10 months ago

41)             exec 'silent :file ' . fnameescape(l:bufname)
42)         endif
43)     else
44)         let l:fname = expand('%')
45)         let l:hash = ''
46)         " Show fnames will have ':' in them.
47)         if stridx(l:fname, ':') != -1
48)             let l:fname_parts = split(l:fname, ':')
49)             let l:fname = l:fname_parts[-1]
50)             let l:hash = split(l:fname_parts[0], ' ')[-1]
51)         endif
52)         if strlen(l:hash)
dblume Allow optional flag for git...

dblume authored 8 months ago

53)             let l:bufname = 'git blame ' . l:args . l:hash . ' -- ' . l:fname
dblume Move vim gittab commands to...

dblume authored 10 months ago

54)             if !s:ShowBufInNewTab(l:bufname)
dblume Allow optional flag for git...

dblume authored 8 months ago

55)                 exec 'tabnew | r! git blame ' . l:args . l:hash . ' -- ' . shellescape(l:fname)
dblume Move vim gittab commands to...

dblume authored 10 months ago

56)                 exec 'silent :file ' . fnameescape(l:bufname)
57)             endif
58)         else
dblume Allow optional flag for git...

dblume authored 8 months ago

59)             let l:bufname = 'git blame ' . l:args . '-- ' . l:fname
dblume Move vim gittab commands to...

dblume authored 10 months ago

60)             if !s:ShowBufInNewTab(l:bufname)
dblume Allow optional flag for git...

dblume authored 8 months ago

61)                 exec 'tabnew | r! git blame ' . l:args . '-- ' . shellescape(l:fname)
dblume Move vim gittab commands to...

dblume authored 10 months ago

62)                 exec 'silent :file ' . fnameescape(l:bufname)
63)             endif
64)         endif
65)     endif
66)     0d_
67)     call winrestview(l:currentView)
68)     setl buftype=nofile
69) endfunction
dblume Allow optional flag for git...

dblume authored 8 months ago

70) command -nargs=* Blame :call s:GitBlame(<q-args>)
dblume Move vim gittab commands to...

dblume authored 10 months ago

71) 
dblume Allow optional flag for git...

dblume authored 8 months ago

72) function! s:GitShow(commit_or_file, ...)
dblume Move vim gittab commands to...

dblume authored 10 months ago

73)     let l:fname = expand('%')
74)     let l:hash = expand('<cword>')
75)     if l:hash =~ '^[0-9a-f]\{7,40}$'
76)         if stridx(l:fname, ' -- ') != -1
77)             let l:fname = split(l:fname, ' -- ')[-1]
78)         endif
dblume Allow optional flag for git...

dblume authored 8 months ago

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
dblume Move vim gittab commands to...

dblume authored 10 months ago

84)         if a:commit_or_file != "file"
dblume Allow optional flag for git...

dblume authored 8 months ago

85)             let l:bufname = 'git show ' . l:args . l:hash . ' -- ' . l:fname
dblume Move vim gittab commands to...

dblume authored 10 months ago

86)             if !s:ShowBufInNewTab(l:bufname)
87)                 " Have Show show all the affected files, so don't actually use  "--"
88)                 " exec 'tabnew | r! git show ' . l:hash . ' -- ' . shellescape(l:fname)
dblume Allow optional flag for git...

dblume authored 8 months ago

89)                 exec 'tabnew | r! git show ' . l:args . l:hash
dblume Move vim gittab commands to...

dblume authored 10 months ago

90)                 " We lie here (' -- ') to have a filename the other git commands can use.
91)                 exec 'silent :file ' . fnameescape(l:bufname)
92)             endif
93)             0d_
94)         else
95)             let l:currentView = winsaveview()
dblume Allow optional flag for git...

dblume authored 8 months ago

96)             let l:bufname = 'git show ' . l:args . l:hash . ':' . l:fname
dblume Move vim gittab commands to...

dblume authored 10 months ago

97)             if !s:ShowBufInNewTab(l:bufname)
dblume Allow optional flag for git...

dblume authored 8 months ago

98)                 exec 'tabnew | r! git show ' . l:args. l:hash . ':' . shellescape(l:fname)
dblume Move vim gittab commands to...

dblume authored 10 months ago

99)                 exec 'silent :file ' . fnameescape(l:bufname)
100)             endif
101)             0d_
102)             call winrestview(l:currentView)
103)         endif
104)         setl buftype=nofile
105)     else
106)         echo l:hash . ' is not a git hash.'
107)     endif
108) endfunction
dblume Allow optional flag for git...

dblume authored 8 months ago

109) command -nargs=* Show :call s:GitShow("commit", <q-args>)
110) command -nargs=* ShowFile :call s:GitShow("file", <q-args>)
dblume Move vim gittab commands to...

dblume authored 10 months ago

111) 
112) function! s:GitDiff()
113)     let l:fname = expand('%:.')
114)     let l:buf = winbufnr(0)
115)     let l:commit = 'HEAD'
116)     let l:hash = expand('<cword>')
117)     let l:currentView = winsaveview()
118) 
119)     " If the current word is a hash, then diff that vs. previous
120)     if l:hash =~ '^[0-9a-f]\{7,40}$' && stridx(expand('%'), ' -- ') != -1
121)         let l:fname = split(expand('%'), ' -- ')[-1]
122)         let l:bufname = 'git show ' . l:hash . '^:' . l:fname
123)         if !s:ShowBufInNewTab(l:bufname)
124)             exec ':tabnew | silent r! git show ' . l:hash . '^:$(git rev-parse --show-prefix)' . shellescape(l:fname)
125)             setl buftype=nofile
126)             0d_
127)             exec 'silent :file ' . fnameescape(l:bufname)
dblume Allow optional flag for git...

dblume authored 8 months ago

128)         endif
dblume Move vim gittab commands to...

dblume authored 10 months ago

129) 
130)         let l:bufname = 'git show ' . l:hash . ':' . l:fname
131)         if !s:ShowBufInNewSplit(l:bufname)
132)             exec 'vne | silent r! git show ' . l:hash . ':$(git rev-parse --show-prefix)' . shellescape(l:fname)
133)             setl buftype=nofile
134)             exec 'silent :file ' . fnameescape(l:bufname)
135)             0d_
136)         endif
137)     elseif stridx(expand('%'), ':') != -1
138)         " If we're in a 'git show' buffer, then extract fname and hash from there
139)         let l:fname_parts = split(l:fname, ':')
140)         let l:fname = l:fname_parts[-1]
141)         let l:hash = split(l:fname_parts[0], ' ')[-1]
dblume Allow optional flag for git...

dblume authored 8 months ago

142)         " TODO: Below few lines are identical to above, so remove dupes.
dblume Move vim gittab commands to...

dblume authored 10 months ago

143)         let l:bufname = 'git show ' . l:hash . '^:' . l:fname
144)         if !s:ShowBufInNewTab(l:bufname)
145)             exec ':tabnew | silent r! git show ' . l:hash . '^:$(git rev-parse --show-prefix)' . shellescape(l:fname)
146)             setl buftype=nofile
147)             0d_
148)             exec 'silent :file ' . fnameescape(l:bufname)
dblume Allow optional flag for git...

dblume authored 8 months ago

149)         endif
dblume Move vim gittab commands to...

dblume authored 10 months ago

150) 
151)         let l:bufname = 'git show ' . l:hash . ':' . l:fname
152)         if !s:ShowBufInNewSplit(l:bufname)
153)             exec 'vne | silent r! git show ' . l:hash . ':$(git rev-parse --show-prefix)' . shellescape(l:fname)
154)             setl buftype=nofile
155)             exec 'silent :file ' . fnameescape(l:bufname)
156)             0d_
157)         endif
158)     else
159)         " If the buffer is not different then repo, then diff HEAD vs file's previous commit
160)         let l:o = system("git status --porcelain | grep " . l:fname)
161)         if v:shell_error != 0
162)             let l:commit = system('git log -2 --pretty=format:"%h" -- ' . l:fname . ' | tail -n 1')
163)         endif
164) 
165)         let l:bufname = 'git show ' . l:commit . ':' . l:fname
166)         " Bug if l:filename includes ".."
167)         if !s:ShowBufInNewTab(l:bufname)
168)             exec ':tabnew | r! git show ' . l:commit . ':$(git rev-parse --show-prefix)' . l:fname
169)             setl buftype=nofile
170)             0d_
171)             exec 'silent :file ' . fnameescape(l:bufname)
172)         endif
173)         exec 'vert sb '.l:buf
174)     endif
175)     call winrestview(l:currentView)
176)     windo diffthis
177)     setl buftype=nofile
178)     wincmd r
179)     wincmd l
180) endfunction
181) command Diff :call s:GitDiff()
182) 
183) function! s:GitLog(...)
184)     let l:fname = expand('%')
185)     if stridx(l:fname, ' -- ') != -1
186)         let l:fname = split(l:fname, ' -- ')[-1]
187)     elseif stridx(l:fname, ':') != -1
188)         let l:fname = split(l:fname, ':')[-1]
189)     endif
190)     let l:args = a:1
191)     if strlen(l:args)
192)         " Add a space to the end
dblume Allow optional flag for git...

dblume authored 8 months ago

193)         let l:args = l:args . " "