Avoid duplicate buffer name error by using orig buffer
dblume

dblume commited on 2023-06-17 22:46:13
Showing 1 changed files, with 33 additions and 6 deletions.

... ...
@@ -145,18 +145,36 @@ nmap <leader>o :call OpenCurrentAsNewTab()<CR>
145 145
 
146 146
 " git blame, show, diff and log
147 147
 " Delete these functions when you install https://github.com/tpope/vim-fugitive
148
+
149
+"A helper function that tries to show a buffer if it already exists
150
+function! ShowBufInNewTab(bufname)
151
+   let l:bnr = bufnr(a:bufname)
152
+   if l:bnr > 0
153
+       tabnew
154
+       exec 'buffer ' . l:bnr
155
+       return 1
156
+   endif
157
+   return 0
158
+endfunction
159
+
148 160
 function! GitBlame()
149 161
     let l:hash = expand('<cword>')
150 162
     let l:currentView = winsaveview()
151
-    " If in a Blame window already, do blame for previous commit
163
+    " If in a Blame window already, do blame for some prior commit
152 164
     if l:hash =~ '^[0-9a-f]\{7,40}$' && stridx(expand('%'), ' -- ') != -1
153 165
         let l:fname = split(expand('%'), ' -- ')[-1]
166
+        let l:bufname = 'git blame ' . l:hash . '^ -- ' . l:fname
167
+        if !ShowBufInNewTab(l:bufname)
154 168
             exec 'tabnew | r! git blame ' . l:hash . '^ -- ' . shellescape(l:fname)
155
-        exec 'silent :file git blame ' . l:hash . '^ -- ' . l:fname
169
+            exec 'silent :file ' . l:bufname
170
+        endif
156 171
     else
157 172
         let l:fname = expand('%')
173
+        let l:bufname = 'git blame -- ' . l:fname
174
+        if !ShowBufInNewTab(l:bufname)
158 175
             exec 'tabnew | r! git blame -- ' . shellescape(l:fname)
159
-        exec 'silent :file git blame -- ' . l:fname
176
+            exec 'silent :file ' . l:bufname
177
+        endif
160 178
     endif
161 179
     0d_
162 180
     call winrestview(l:currentView)
... ...
@@ -172,14 +190,20 @@ function! GitShow(commit_or_file)
172 190
             let l:fname = split(l:fname, ' -- ')[-1]
173 191
         endif
174 192
         if a:commit_or_file != "file"
193
+            let l:bufname = 'git show ' . l:hash . ' -- ' . l:fname
194
+            if !ShowBufInNewTab(l:bufname)
175 195
                 " Have Show show all the affected files, so don't actually use  "--"
176 196
                 " exec 'tabnew | r! git show ' . l:hash . ' -- ' . shellescape(l:fname)
177 197
                 exec 'tabnew | r! git show ' . l:hash
178 198
                 " We lie here (' -- ') to have a filename the other git commands can use.
179
-            exec 'silent :file git show ' . l:hash . ' -- ' . l:fname
199
+                exec 'silent :file ' . l:bufname
200
+            endif
180 201
         else
202
+            let l:bufname = 'git show ' . l:hash . ':' . l:fname
203
+            if !ShowBufInNewTab(l:bufname)
181 204
                 exec 'tabnew | r! git show ' . l:hash . ':' . shellescape(l:fname)
182
-            exec 'silent :file git show ' . l:hash . ':' . l:fname
205
+                exec 'silent :file ' . l:bufname
206
+            endif
183 207
         endif
184 208
         setl buftype=nofile
185 209
         0d_
... ...
@@ -234,10 +258,13 @@ function! GitLog()
234 258
     if stridx(l:fname, ' -- ') != -1
235 259
         let l:fname = split(l:fname, ' -- ')[-1]
236 260
     endif
261
+    let l:bufname = 'git log1 -- ' . l:fname
262
+    if !ShowBufInNewTab(l:bufname)
237 263
         exec 'tabnew | r! git log1 -- ' . shellescape(l:fname)
238 264
         setl buftype=nofile
239 265
         0d_
240
-    exec 'silent :file git log1 -- ' . l:fname
266
+        exec 'silent :file ' . l:bufname
267
+    endif
241 268
 endfunction
242 269
 command Log :call GitLog()
243 270
 
244 271