plugin/fugitive.vim (26733B) - Raw
1 " fugitive.vim - A Git wrapper so awesome, it should be illegal 2 " Maintainer: Tim Pope <http://tpo.pe/> 3 " Version: 3.3 4 " GetLatestVimScripts: 2975 1 :AutoInstall: fugitive.vim 5 6 if exists('g:loaded_fugitive') 7 finish 8 endif 9 let g:loaded_fugitive = 1 10 11 let s:bad_git_dir = '/$\|^fugitive:' 12 13 " FugitiveGitDir() returns the detected Git dir for the given buffer number, 14 " or the current buffer if no argument is passed. This will be an empty 15 " string if no Git dir was found. Use !empty(FugitiveGitDir()) to check if 16 " Fugitive is active in the current buffer. Do not rely on this for direct 17 " filesystem access; use FugitiveFind('.git/whatever') instead. 18 function! FugitiveGitDir(...) abort 19 if v:version < 704 20 return '' 21 elseif !a:0 || type(a:1) == type(0) && a:1 < 0 22 if exists('g:fugitive_event') 23 return g:fugitive_event 24 endif 25 let dir = get(b:, 'git_dir', '') 26 if empty(dir) && (empty(bufname('')) || &buftype =~# '^\%(nofile\|acwrite\|quickfix\|terminal\|prompt\)$') 27 return FugitiveExtractGitDir(getcwd()) 28 elseif (!exists('b:git_dir') || b:git_dir =~# s:bad_git_dir) && empty(&buftype) 29 let b:git_dir = FugitiveExtractGitDir(expand('%:p')) 30 return b:git_dir 31 endif 32 return dir =~# s:bad_git_dir ? '' : dir 33 elseif type(a:1) == type(0) 34 if a:1 == bufnr('') && (!exists('b:git_dir') || b:git_dir =~# s:bad_git_dir) && empty(&buftype) 35 let b:git_dir = FugitiveExtractGitDir(expand('%:p')) 36 endif 37 let dir = getbufvar(a:1, 'git_dir') 38 return dir =~# s:bad_git_dir ? '' : dir 39 elseif type(a:1) == type('') 40 return substitute(s:Slash(a:1), '/$', '', '') 41 elseif type(a:1) == type({}) 42 return get(a:1, 'git_dir', '') 43 else 44 return '' 45 endif 46 endfunction 47 48 " FugitiveReal() takes a fugitive:// URL and returns the corresponding path in 49 " the work tree. This may be useful to get a cleaner path for inclusion in 50 " the statusline, for example. Note that the file and its parent directories 51 " are not guaranteed to exist. 52 " 53 " This is intended as an abstract API to be used on any "virtual" path. For a 54 " buffer named foo://bar, check for a function named FooReal(), and if it 55 " exists, call FooReal("foo://bar"). 56 function! FugitiveReal(...) abort 57 let file = a:0 ? a:1 : @% 58 if file =~# '^\a\a\+:' || a:0 > 1 59 return call('fugitive#Real', [file] + a:000[1:-1]) 60 elseif file =~# '^/\|^\a:\|^$' 61 return file 62 else 63 return fnamemodify(file, ':p' . (file =~# '[\/]$' ? '' : ':s?[\/]$??')) 64 endif 65 endfunction 66 67 " FugitiveFind() takes a Fugitive object and returns the appropriate Vim 68 " buffer name. You can use this to generate Fugitive URLs ("HEAD:README") or 69 " to get the absolute path to a file in the Git dir (".git/HEAD"), the common 70 " dir (".git/config"), or the work tree (":(top)Makefile"). 71 " 72 " An optional second argument provides the Git dir, or the buffer number of a 73 " buffer with a Git dir. The default is the current buffer. 74 function! FugitiveFind(...) abort 75 return fugitive#Find(a:0 ? a:1 : bufnr(''), FugitiveGitDir(a:0 > 1 ? a:2 : -1)) 76 endfunction 77 78 function! FugitivePath(...) abort 79 if a:0 > 1 80 return fugitive#Path(a:1, a:2, FugitiveGitDir(a:0 > 2 ? a:3 : -1)) 81 else 82 return FugitiveReal(a:0 ? a:1 : @%) 83 endif 84 endfunction 85 86 " FugitiveParse() takes a fugitive:// URL and returns a 2 element list 87 " containing an object name ("commit:file") and the Git dir. It's effectively 88 " the inverse of FugitiveFind(). 89 function! FugitiveParse(...) abort 90 let path = s:Slash(a:0 ? a:1 : @%) 91 if path !~# '^fugitive:' 92 return ['', ''] 93 endif 94 let vals = matchlist(path, '\c^fugitive:\%(//\)\=\(.\{-\}\)\%(//\|::\)\(\x\{40,\}\|[0-3]\)\(/.*\)\=$') 95 if len(vals) 96 return [(vals[2] =~# '^.$' ? ':' : '') . vals[2] . substitute(vals[3], '^/', ':', ''), vals[1]] 97 endif 98 let v:errmsg = 'fugitive: invalid Fugitive URL ' . path 99 throw v:errmsg 100 endfunction 101 102 " FugitiveResult() returns an object encapsulating the result of the most 103 " recent :Git command. Will be empty if no result is available. During a 104 " User FugitiveChanged event, this is guaranteed to correspond to the :Git 105 " command that triggered the event, or be empty if :Git was not the trigger. 106 " Pass in the name of a temp buffer to get the result object for that command 107 " instead. Contains the following keys: 108 " 109 " * "args": List of command arguments, starting with the subcommand. Will be 110 " empty for usages like :Git --help. 111 " * "dir": Git dir of the relevant repository. 112 " * "exit_status": The integer exit code of the process. 113 " * "flags": Flags passed directly to Git, like -c and --help. 114 " * "file": Path to file containing command output. Not guaranteed to exist, 115 " so verify with filereadable() before trying to access it. 116 function! FugitiveResult(...) abort 117 return call('fugitive#Result', a:000) 118 endfunction 119 120 " FugitivePrepare() constructs a Git command string which can be executed with 121 " functions like system() and commands like :!. Integer arguments will be 122 " treated as buffer numbers, and the appropriate relative path inserted in 123 " their place. 124 " 125 " If the first argument is a string that looks like a path or an empty string, 126 " it will be used as the Git dir. If it's a buffer number, the Git dir for 127 " that buffer will be used. The default is the current buffer. 128 function! FugitivePrepare(...) abort 129 return call('fugitive#Prepare', a:000) 130 endfunction 131 132 " FugitiveConfig() get returns an opaque structure that can be passed to other 133 " FugitiveConfig functions in lieu of a Git directory. This can be faster 134 " when performing multiple config queries. Do not rely on the internal 135 " structure of the return value as it is not guaranteed. If you want a full 136 " dictionary of every config value, use FugitiveConfigGetRegexp('.*'). 137 " 138 " An optional argument provides the Git dir, or the buffer number of a 139 " buffer with a Git dir. The default is the current buffer. Pass a blank 140 " string to limit to the global config. 141 function! FugitiveConfig(...) abort 142 return call('fugitive#Config', a:000) 143 endfunction 144 145 " FugitiveConfigGet() retrieves a Git configuration value. An optional second 146 " argument can be either the object returned by FugitiveConfig(), or a Git 147 " dir or buffer number to be passed along to FugitiveConfig(). 148 function! FugitiveConfigGet(name, ...) abort 149 return get(call('FugitiveConfigGetAll', [a:name] + (a:0 ? [a:1] : [])), 0, get(a:, 2, '')) 150 endfunction 151 152 " FugitiveConfigGetAll() is like FugitiveConfigGet() but returns a list of 153 " all values. 154 function! FugitiveConfigGetAll(name, ...) abort 155 return call('fugitive#ConfigGetAll', [a:name] + a:000) 156 endfunction 157 158 " FugitiveConfigGetRegexp() retrieves a dictionary of all configuration values 159 " with a key matching the given pattern. Like git config --get-regexp, but 160 " using a Vim regexp. Second argument has same semantics as 161 " FugitiveConfigGet(). 162 function! FugitiveConfigGetRegexp(pattern, ...) abort 163 return call('fugitive#ConfigGetRegexp', [a:pattern] + a:000) 164 endfunction 165 166 " FugitiveRemoteUrl() retrieves the remote URL for the given remote name, 167 " defaulting to the current branch's remote or "origin" if no argument is 168 " given. Similar to `git remote get-url`, but also attempts to resolve HTTP 169 " redirects and SSH host aliases. 170 " 171 " An optional second argument provides the Git dir, or the buffer number of a 172 " buffer with a Git dir. The default is the current buffer. 173 function! FugitiveRemoteUrl(...) abort 174 return call('fugitive#RemoteUrl', a:000) 175 endfunction 176 177 " FugitiveHead() retrieves the name of the current branch. If the current HEAD 178 " is detached, FugitiveHead() will return the empty string, unless the 179 " optional argument is given, in which case the hash of the current commit 180 " will be truncated to the given number of characters. 181 " 182 " An optional second argument provides the Git dir, or the buffer number of a 183 " buffer with a Git dir. The default is the current buffer. 184 function! FugitiveHead(...) abort 185 let dir = FugitiveGitDir(a:0 > 1 ? a:2 : -1) 186 if empty(dir) 187 return '' 188 endif 189 return fugitive#Head(a:0 ? a:1 : 0, dir) 190 endfunction 191 192 function! FugitiveStatusline(...) abort 193 if empty(get(b:, 'git_dir', '')) 194 return '' 195 endif 196 return fugitive#Statusline() 197 endfunction 198 199 function! FugitiveCommonDir(...) abort 200 let dir = FugitiveGitDir(a:0 ? a:1 : -1) 201 if empty(dir) 202 return '' 203 endif 204 return fugitive#Find('.git/refs/..', dir) 205 endfunction 206 207 function! FugitiveWorkTree(...) abort 208 let tree = s:Tree(FugitiveGitDir(a:0 ? a:1 : -1)) 209 if tree isnot# 0 || a:0 > 1 210 return tree 211 else 212 return '' 213 endif 214 endfunction 215 216 function! FugitiveIsGitDir(path) abort 217 let path = substitute(a:path, '[\/]$', '', '') . '/' 218 return len(a:path) && getfsize(path.'HEAD') > 10 && ( 219 \ isdirectory(path.'objects') && isdirectory(path.'refs') || 220 \ getftype(path.'commondir') ==# 'file') 221 endfunction 222 223 let s:worktree_for_dir = {} 224 let s:dir_for_worktree = {} 225 function! s:Tree(path) abort 226 let dir = a:path 227 if dir =~# '/\.git$' 228 return len(dir) ==# 5 ? '/' : dir[0:-6] 229 elseif dir ==# '' 230 return '' 231 endif 232 if !has_key(s:worktree_for_dir, dir) 233 let s:worktree_for_dir[dir] = '' 234 let config_file = dir . '/config' 235 if filereadable(config_file) 236 let config = readfile(config_file,'',10) 237 let wt_config = filter(copy(config),'v:val =~# "^\\s*worktree *="') 238 if len(wt_config) == 1 239 let worktree = FugitiveVimPath(matchstr(wt_config[0], '= *\zs.*')) 240 else 241 call filter(config,'v:val =~# "^\\s*bare *= *false *$"') 242 if len(config) 243 let s:worktree_for_dir[dir] = 0 244 endif 245 endif 246 elseif filereadable(dir . '/gitdir') 247 let worktree = fnamemodify(FugitiveVimPath(readfile(dir . '/gitdir')[0]), ':h') 248 if worktree ==# '.' 249 unlet! worktree 250 endif 251 endif 252 if exists('worktree') 253 let s:worktree_for_dir[dir] = s:Slash(resolve(worktree)) 254 let s:dir_for_worktree[s:worktree_for_dir[dir]] = dir 255 endif 256 endif 257 if s:worktree_for_dir[dir] =~# '^\.' 258 return simplify(dir . '/' . s:worktree_for_dir[dir]) 259 else 260 return s:worktree_for_dir[dir] 261 endif 262 endfunction 263 264 function! s:CeilingDirectories() abort 265 if !exists('s:ceiling_directories') 266 let s:ceiling_directories = [] 267 let resolve = 1 268 for dir in split($GIT_CEILING_DIRECTORIES, has('win32') ? ';' : ':', 1) 269 if empty(dir) 270 let resolve = 0 271 elseif resolve 272 call add(s:ceiling_directories, resolve(dir)) 273 else 274 call add(s:ceiling_directories, dir) 275 endif 276 endfor 277 endif 278 return s:ceiling_directories + get(g:, 'ceiling_directories', []) 279 endfunction 280 281 function! FugitiveExtractGitDir(path) abort 282 let path = s:Slash(a:path) 283 if path =~# '^fugitive:' 284 return matchstr(path, '\C^fugitive:\%(//\)\=\zs.\{-\}\ze\%(//\|::\|$\)') 285 elseif empty(path) 286 return '' 287 elseif isdirectory(path) 288 let path = fnamemodify(path, ':p:s?/$??') 289 else 290 let path = fnamemodify(path, ':p:h:s?/$??') 291 endif 292 let pre = substitute(matchstr(path, '^\a\a\+\ze:'), '^.', '\u&', '') 293 if len(pre) && exists('*' . pre . 'Real') 294 let path = s:Slash({pre}Real(path)) 295 endif 296 let root = resolve(path) 297 if root !=# path 298 silent! exe (haslocaldir() ? 'lcd' : exists(':tcd') && haslocaldir(-1) ? 'tcd' : 'cd') '.' 299 endif 300 let previous = "" 301 let env_git_dir = len($GIT_DIR) ? s:Slash(simplify(fnamemodify(FugitiveVimPath($GIT_DIR), ':p:s?[\/]$??'))) : '' 302 call s:Tree(env_git_dir) 303 while root !=# previous 304 if root =~# '\v^//%([^/]+/?)?$' 305 break 306 endif 307 if index(s:CeilingDirectories(), root) >= 0 308 break 309 endif 310 if root ==# $GIT_WORK_TREE && FugitiveIsGitDir(env_git_dir) 311 return env_git_dir 312 elseif has_key(s:dir_for_worktree, root) 313 return s:dir_for_worktree[root] 314 endif 315 let dir = substitute(root, '[\/]$', '', '') . '/.git' 316 let type = getftype(dir) 317 if type ==# 'dir' && FugitiveIsGitDir(dir) 318 return dir 319 elseif type ==# 'link' && FugitiveIsGitDir(dir) 320 return resolve(dir) 321 elseif type !=# '' && filereadable(dir) 322 let line = get(readfile(dir, '', 1), 0, '') 323 let file_dir = s:Slash(FugitiveVimPath(matchstr(line, '^gitdir: \zs.*'))) 324 if file_dir !~# '^/\|^\a:' && FugitiveIsGitDir(root . '/' . file_dir) 325 return simplify(root . '/' . file_dir) 326 elseif len(file_dir) && FugitiveIsGitDir(file_dir) 327 return file_dir 328 endif 329 elseif FugitiveIsGitDir(root) 330 return root 331 endif 332 let previous = root 333 let root = fnamemodify(root, ':h') 334 endwhile 335 return '' 336 endfunction 337 338 function! FugitiveDetect(path) abort 339 if v:version < 704 340 return '' 341 endif 342 if exists('b:git_dir') && b:git_dir =~# '^$\|' . s:bad_git_dir 343 unlet b:git_dir 344 endif 345 if !exists('b:git_dir') 346 let b:git_dir = FugitiveExtractGitDir(a:path) 347 endif 348 if empty(b:git_dir) || !exists('#User#Fugitive') 349 return '' 350 endif 351 if v:version >= 704 || (v:version == 703 && has('patch442')) 352 doautocmd <nomodeline> User Fugitive 353 elseif &modelines > 0 354 let modelines = &modelines 355 try 356 set modelines=0 357 doautocmd User Fugitive 358 finally 359 let &modelines = modelines 360 endtry 361 else 362 doautocmd User Fugitive 363 endif 364 return '' 365 endfunction 366 367 function! FugitiveVimPath(path) abort 368 if exists('+shellslash') && !&shellslash 369 return tr(a:path, '/', '\') 370 else 371 return a:path 372 endif 373 endfunction 374 375 function! FugitiveGitPath(path) abort 376 return s:Slash(a:path) 377 endfunction 378 379 if exists('+shellslash') 380 function! s:Slash(path) abort 381 return tr(a:path, '\', '/') 382 endfunction 383 else 384 function! s:Slash(path) abort 385 return a:path 386 endfunction 387 endif 388 389 function! s:ProjectionistDetect() abort 390 let file = s:Slash(get(g:, 'projectionist_file', '')) 391 let dir = FugitiveExtractGitDir(file) 392 let base = matchstr(file, '^fugitive://.\{-\}//\x\+') 393 if empty(base) 394 let base = s:Tree(dir) 395 endif 396 if !empty(base) 397 if exists('+shellslash') && !&shellslash 398 let base = tr(base, '/', '\') 399 endif 400 let file = FugitiveCommonDir(dir) . '/info/projections.json' 401 if filereadable(file) 402 call projectionist#append(base, file) 403 endif 404 endif 405 endfunction 406 407 let s:addr_other = has('patch-8.1.560') ? '-addr=other' : '' 408 let s:addr_tabs = has('patch-7.4.542') ? '-addr=tabs' : '' 409 let s:addr_wins = has('patch-7.4.542') ? '-addr=windows' : '' 410 411 if exists(':G') != 2 412 command! -bang -nargs=? -range=-1 -complete=customlist,fugitive#Complete G exe fugitive#Command(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>) 413 endif 414 command! -bang -nargs=? -range=-1 -complete=customlist,fugitive#Complete Git exe fugitive#Command(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>) 415 416 if exists(':Gstatus') != 2 && get(g:, 'fugitive_legacy_commands', 1) 417 exe 'command! -bang -bar -range=-1' s:addr_other 'Gstatus exe fugitive#Command(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>)' 418 \ '|echohl WarningMSG|echomsg ":Gstatus is deprecated in favor of :Git (with no arguments)"|echohl NONE' 419 endif 420 421 for s:cmd in ['Commit', 'Revert', 'Merge', 'Rebase', 'Pull', 'Push', 'Fetch', 'Blame'] 422 if exists(':G' . tolower(s:cmd)) != 2 && get(g:, 'fugitive_legacy_commands', 1) 423 exe 'command! -bang -nargs=? -range=-1 -complete=customlist,fugitive#' . s:cmd . 'Complete G' . tolower(s:cmd) 424 \ 'echohl WarningMSG|echomsg ":G' . tolower(s:cmd) . ' is deprecated in favor of :Git ' . tolower(s:cmd) . '"|echohl NONE|' 425 \ 'exe fugitive#Command(<line1>, <count>, +"<range>", <bang>0, "<mods>", "' . tolower(s:cmd) . ' " . <q-args>)' 426 endif 427 endfor 428 unlet s:cmd 429 430 exe "command! -bar -bang -nargs=? -complete=customlist,fugitive#CdComplete Gcd exe fugitive#Cd(<q-args>, 0)" 431 exe "command! -bar -bang -nargs=? -complete=customlist,fugitive#CdComplete Glcd exe fugitive#Cd(<q-args>, 1)" 432 433 exe 'command! -bang -nargs=? -range=-1' s:addr_wins '-complete=customlist,fugitive#GrepComplete Ggrep exe fugitive#GrepCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>)' 434 exe 'command! -bang -nargs=? -range=-1' s:addr_wins '-complete=customlist,fugitive#GrepComplete Gcgrep exe fugitive#GrepCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>)' 435 exe 'command! -bang -nargs=? -range=-1' s:addr_wins '-complete=customlist,fugitive#GrepComplete Glgrep exe fugitive#GrepCommand(0, <count> > 0 ? <count> : 0, +"<range>", <bang>0, "<mods>", <q-args>)' 436 437 if exists(':Glog') != 2 && get(g:, 'fugitive_legacy_commands', 1) 438 exe 'command! -bang -nargs=? -range=-1 -complete=customlist,fugitive#LogComplete Glog :exe fugitive#LogCommand(<line1>,<count>,+"<range>",<bang>0,"<mods>",<q-args>, "")' 439 \ '|echohl WarningMSG|echomsg ":Glog is deprecated in favor of :Gclog"|echohl NONE' 440 endif 441 exe 'command! -bang -nargs=? -range=-1 -complete=customlist,fugitive#LogComplete Gclog :exe fugitive#LogCommand(<line1>,<count>,+"<range>",<bang>0,"<mods>",<q-args>, "c")' 442 exe 'command! -bang -nargs=? -range=-1 -complete=customlist,fugitive#LogComplete GcLog :exe fugitive#LogCommand(<line1>,<count>,+"<range>",<bang>0,"<mods>",<q-args>, "c")' 443 exe 'command! -bang -nargs=? -range=-1 -complete=customlist,fugitive#LogComplete Gllog :exe fugitive#LogCommand(<line1>,<count>,+"<range>",<bang>0,"<mods>",<q-args>, "l")' 444 exe 'command! -bang -nargs=? -range=-1 -complete=customlist,fugitive#LogComplete GlLog :exe fugitive#LogCommand(<line1>,<count>,+"<range>",<bang>0,"<mods>",<q-args>, "l")' 445 446 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#EditComplete Ge exe fugitive#Open("edit<bang>", 0, "<mods>", <q-args>, [<f-args>])' 447 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#EditComplete Gedit exe fugitive#Open("edit<bang>", 0, "<mods>", <q-args>, [<f-args>])' 448 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#ReadComplete Gpedit exe fugitive#Open("pedit", <bang>0, "<mods>", <q-args>, [<f-args>])' 449 exe 'command! -bar -bang -nargs=* -range=-1' s:addr_other '-complete=customlist,fugitive#ReadComplete Gsplit exe fugitive#Open((<count> > 0 ? <count> : "").(<count> ? "split" : "edit"), <bang>0, "<mods>", <q-args>, [<f-args>])' 450 exe 'command! -bar -bang -nargs=* -range=-1' s:addr_other '-complete=customlist,fugitive#ReadComplete Gvsplit exe fugitive#Open((<count> > 0 ? <count> : "").(<count> ? "vsplit" : "edit!"), <bang>0, "<mods>", <q-args>, [<f-args>])' 451 exe 'command! -bar -bang -nargs=* -range=-1' s:addr_tabs '-complete=customlist,fugitive#ReadComplete Gtabedit exe fugitive#Open((<count> >= 0 ? <count> : "")."tabedit", <bang>0, "<mods>", <q-args>, [<f-args>])' 452 453 if exists(':Gr') != 2 454 exe 'command! -bar -bang -nargs=* -range=-1 -complete=customlist,fugitive#ReadComplete Gr exe fugitive#ReadCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])' 455 endif 456 exe 'command! -bar -bang -nargs=* -range=-1 -complete=customlist,fugitive#ReadComplete Gread exe fugitive#ReadCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])' 457 458 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#EditComplete Gdiffsplit exe fugitive#Diffsplit(1, <bang>0, "<mods>", <q-args>, [<f-args>])' 459 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#EditComplete Ghdiffsplit exe fugitive#Diffsplit(0, <bang>0, "<mods>", <q-args>, [<f-args>])' 460 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#EditComplete Gvdiffsplit exe fugitive#Diffsplit(0, <bang>0, "vertical <mods>", <q-args>, [<f-args>])' 461 462 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#EditComplete Gw exe fugitive#WriteCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])' 463 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#EditComplete Gwrite exe fugitive#WriteCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])' 464 exe 'command! -bar -bang -nargs=* -complete=customlist,fugitive#EditComplete Gwq exe fugitive#WqCommand( <line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])' 465 466 exe 'command! -bar -bang -nargs=0 GRemove exe fugitive#RemoveCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])' 467 exe 'command! -bar -bang -nargs=0 GDelete exe fugitive#DeleteCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])' 468 exe 'command! -bar -bang -nargs=1 -complete=customlist,fugitive#CompleteObject GMove exe fugitive#MoveCommand( <line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])' 469 exe 'command! -bar -bang -nargs=1 -complete=customlist,fugitive#RenameComplete GRename exe fugitive#RenameCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])' 470 if exists(':Gremove') != 2 && get(g:, 'fugitive_legacy_commands', 1) 471 exe 'command! -bar -bang -nargs=0 Gremove exe fugitive#RemoveCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])' 472 \ '|echohl WarningMSG|echomsg ":Gremove is deprecated in favor of :GRemove"|echohl NONE' 473 endif 474 if exists(':Gdelete') != 2 && get(g:, 'fugitive_legacy_commands', 1) 475 exe 'command! -bar -bang -nargs=0 Gdelete exe fugitive#DeleteCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])' 476 \ '|echohl WarningMSG|echomsg ":Gdelete is deprecated in favor of :GDelete"|echohl NONE' 477 endif 478 if exists(':Gmove') != 2 && get(g:, 'fugitive_legacy_commands', 1) 479 exe 'command! -bar -bang -nargs=1 -complete=customlist,fugitive#CompleteObject Gmove exe fugitive#MoveCommand( <line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])' 480 \ '|echohl WarningMSG|echomsg ":Gmove is deprecated in favor of :GMove"|echohl NONE' 481 endif 482 if exists(':Grename') != 2 && get(g:, 'fugitive_legacy_commands', 1) 483 exe 'command! -bar -bang -nargs=1 -complete=customlist,fugitive#RenameComplete Grename exe fugitive#RenameCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])' 484 \ '|echohl WarningMSG|echomsg ":Grename is deprecated in favor of :GRename"|echohl NONE' 485 endif 486 487 exe 'command! -bar -bang -range=-1 -nargs=* -complete=customlist,fugitive#CompleteObject GBrowse exe fugitive#BrowseCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])' 488 if exists(':Gbrowse') != 2 && get(g:, 'fugitive_legacy_commands', 1) 489 exe 'command! -bar -bang -range=-1 -nargs=* -complete=customlist,fugitive#CompleteObject Gbrowse exe fugitive#BrowseCommand(<line1>, <count>, +"<range>", <bang>0, "<mods>", <q-args>, [<f-args>])' 490 \ '|if <bang>1|redraw!|endif|echohl WarningMSG|echomsg ":Gbrowse is deprecated in favor of :GBrowse"|echohl NONE' 491 endif 492 493 if v:version < 704 494 finish 495 endif 496 497 let g:io_fugitive = { 498 \ 'simplify': function('fugitive#simplify'), 499 \ 'resolve': function('fugitive#resolve'), 500 \ 'getftime': function('fugitive#getftime'), 501 \ 'getfsize': function('fugitive#getfsize'), 502 \ 'getftype': function('fugitive#getftype'), 503 \ 'filereadable': function('fugitive#filereadable'), 504 \ 'filewritable': function('fugitive#filewritable'), 505 \ 'isdirectory': function('fugitive#isdirectory'), 506 \ 'getfperm': function('fugitive#getfperm'), 507 \ 'setfperm': function('fugitive#setfperm'), 508 \ 'readfile': function('fugitive#readfile'), 509 \ 'writefile': function('fugitive#writefile'), 510 \ 'glob': function('fugitive#glob'), 511 \ 'delete': function('fugitive#delete'), 512 \ 'Real': function('FugitiveReal')} 513 514 augroup fugitive 515 autocmd! 516 517 autocmd BufNewFile,BufReadPost * call FugitiveDetect(expand('<amatch>:p')) 518 autocmd FileType netrw call FugitiveDetect(fnamemodify(get(b:, 'netrw_curdir', expand('<amatch>')), ':p')) 519 520 autocmd FileType git 521 \ call fugitive#MapCfile() 522 autocmd FileType gitcommit 523 \ call fugitive#MapCfile('fugitive#MessageCfile()') 524 autocmd FileType git,gitcommit 525 \ if &foldtext ==# 'foldtext()' | 526 \ setlocal foldtext=fugitive#Foldtext() | 527 \ endif 528 autocmd FileType fugitive 529 \ call fugitive#MapCfile('fugitive#StatusCfile()') 530 autocmd FileType gitrebase 531 \ let &l:include = '^\%(pick\|squash\|edit\|reword\|fixup\|drop\|[pserfd]\)\>' | 532 \ if &l:includeexpr !~# 'Fugitive' | 533 \ let &l:includeexpr = 'v:fname =~# ''^\x\{4,\}$'' && len(FugitiveGitDir()) ? FugitiveFind(v:fname) : ' . 534 \ (len(&l:includeexpr) ? &l:includeexpr : 'v:fname') | 535 \ endif | 536 \ let b:undo_ftplugin = get(b:, 'undo_ftplugin', 'exe') . '|setl inex= inc=' 537 538 autocmd BufReadCmd index{,.lock} 539 \ if FugitiveIsGitDir(expand('<amatch>:p:h')) | 540 \ let b:git_dir = s:Slash(expand('<amatch>:p:h')) | 541 \ exe fugitive#BufReadStatus() | 542 \ elseif filereadable(expand('<amatch>')) | 543 \ silent doautocmd BufReadPre | 544 \ keepalt read <amatch> | 545 \ 1delete_ | 546 \ silent doautocmd BufReadPost | 547 \ else | 548 \ silent doautocmd BufNewFile | 549 \ endif 550 551 autocmd BufReadCmd fugitive://*//* exe fugitive#BufReadCmd() | 552 \ if &path =~# '^\.\%(,\|$\)' | 553 \ let &l:path = substitute(&path, '^\.,\=', '', '') | 554 \ endif 555 autocmd BufWriteCmd fugitive://*//[0-3]/* exe fugitive#BufWriteCmd() 556 autocmd FileReadCmd fugitive://*//* exe fugitive#FileReadCmd() 557 autocmd FileWriteCmd fugitive://*//[0-3]/* exe fugitive#FileWriteCmd() 558 if exists('##SourceCmd') 559 autocmd SourceCmd fugitive://*//* nested exe fugitive#SourceCmd() 560 endif 561 562 autocmd User Flags call Hoist('buffer', function('FugitiveStatusline')) 563 564 autocmd User ProjectionistDetect call s:ProjectionistDetect() 565 augroup END 566 567 if get(g:, 'fugitive_no_maps') 568 finish 569 endif 570 571 let s:nowait = v:version >= 704 ? '<nowait>' : '' 572 573 function! s:Map(mode, lhs, rhs, ...) abort 574 for mode in split(a:mode, '\zs') 575 let flags = (a:0 ? a:1 : '') . (a:rhs =~# '<Plug>' ? '' : '<script>') 576 let head = a:lhs 577 let tail = '' 578 let keys = get(g:, mode.'remap', {}) 579 if type(keys) == type([]) 580 return 581 endif 582 while !empty(head) 583 if has_key(keys, head) 584 let head = keys[head] 585 if empty(head) 586 return 587 endif 588 break 589 endif 590 let tail = matchstr(head, '<[^<>]*>$\|.$') . tail 591 let head = substitute(head, '<[^<>]*>$\|.$', '', '') 592 endwhile 593 if flags !~# '<unique>' || empty(mapcheck(head.tail, mode)) 594 exe mode.'map' s:nowait flags head.tail a:rhs 595 endif 596 endfor 597 endfunction 598 599 call s:Map('c', '<C-R><C-G>', 'fnameescape(fugitive#Object(@%))', '<expr>') 600 call s:Map('n', 'y<C-G>', ':<C-U>call setreg(v:register, fugitive#Object(@%))<CR>', '<silent>')