Skip to content

Commit 2b4b5cf

Browse files
committed
git-extra(vimrc): adjust $PATH to remove Git's libexec
As described on the Git mailing list [1] and elsewhere [2], Git adjusts PATH before invoking programs, and those adjustments are inherited by editors if they are invoked as subprocesses. In most cases this is harmless, but it can create confusion when spawning subprocesses from the editor (as is common in Vim) and when the user's shell looks at PATH or the location of a "git" binary. Include portable code to strip these entries from PATH on startup. [1]: https://public-inbox.org/git/CALnO6CDtGRRav8zK2GKi1oHTZWrHFTxZNmnOWu64-ab+oY3_Lw@mail.gmail.com/ [2]: https://benknoble.github.io/blog/2020/05/22/libexec-git-core-on-path/ Signed-off-by: D. Ben Knoble <[email protected]>
1 parent 90cd62f commit 2b4b5cf

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

git-extra/vimrc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,36 @@ if has("autocmd")
5252
autocmd Filetype diff
5353
\ highlight WhiteSpaceEOL ctermbg=red |
5454
\ match WhiteSpaceEOL /\(^+.*\)\@<=\s\+$/
55+
56+
" When Git starts up, it prepends its libexec dir to PATH to allow it to
57+
" find external commands.
58+
"
59+
" Thus, if Vim is invoked via a Git process (such as the contrib git-jump,
60+
" or any other usage of GIT_EDITOR/VISUAL/EDITOR in Git commands, be they
61+
" scripts or internals--with the exception of manually invoking the script
62+
" yourself, without using Git: sh .../git-jump), $PATH will contain
63+
" something like libexec/git-core.
64+
"
65+
" We don't generally want it in Vim's $PATH, though, as it is passed down
66+
" to *all* subprocesses, including shells started with :terminal or
67+
" :shell.
68+
function s:fix_git_path() abort
69+
let slash = exists('+shellslash') && !&shellslash ? '\\' : '/'
70+
let git_core_base = printf('%slib\%%(exec\)\?%sgit-core', slash, slash)
71+
" optimization: early return
72+
if $PATH !~# git_core_base
73+
return
74+
endif
75+
let path_sep = has('win32') ? ';' : ':'
76+
let new_path = split($PATH, path_sep)
77+
\ ->filter({_, d -> d !~# git_core_base..'$' })
78+
\ ->join(path_sep)
79+
let $PATH = new_path
80+
endfunction
81+
82+
augroup fix_git_path
83+
autocmd!
84+
autocmd VimEnter * call s:fix_git_path()
85+
augroup end
86+
5587
endif " has("autocmd")

0 commit comments

Comments
 (0)