list mirror (unofficial, one of many)
 help / color / Atom feed
* libexec/git-core on PATH in editors and other subprocesses (and a fix for vim)
@ 2020-05-21 20:31 D. Ben Knoble
  0 siblings, 0 replies; only message in thread
From: D. Ben Knoble @ 2020-05-21 20:31 UTC (permalink / raw)
  To: git

Hello all,

I noticed something odd today--I was working in vim and spawned a new terminal
with `:terminal`. My shell startup files, which use the location of the git
binary to reconstruct paths to things like
`etc/bash_completion.d/` and `contrib/git-jump`, suddenly choked.
The location of the git program was suddenly different than usual!

After some debugging, I came to determine that this occurred, not because of
vim, but because vim was launched from a git-invoked process (in this case,

The salient git code is `setup_path()` in exec-cmd.c [1] and it's call in git.c
[2]. They appear to coordinate to prepend the libexec dir to PATH. This causes
the location of the git binary to be (e.g.) /usr/local/libexec/git-core/git, and
not (e.g.) /usr/local/bin/git; since I traverse up two directories from the
location of git, the former breaks my startup scripts. (You would be well
withing your rights to argue that I should not use the location of git to find
these scripts. I have my reasons.)

Anyway, the net result is that if I run, say, `git jump grep foo` and then try
to invoke `:terminal` or `:shell`, my shell complains.

Unfortunately, this modification propagates to all child processes, as this
simple test-case demonstrates:

    $ printf '%s\n' '#!/bin/sh' "printenv PATH | tr : '\n' | grep
git-core" > git-show-env
    $ chmod u+x git-show-env
    $ PATH=.:$PATH git show-env

While it would be *nice* if git didn't modify PATH in such a way that it
affected all subprocesses (i.e., if it was somehow scoped to only processes that
need it), I suspect this is at best difficult and at worst highly-error prone or
likely to break things.

In the (possibly eternal) interim, I would like to share some vimscript that
"fixes" $PATH in vim when it detects this case. The easiest use is to drop the
code in your vimrc file (usually ~/.vim/vimrc or ~/.vimrc). I've tried to keep
it portable in terms of path separators and file paths, but I do not have a
Windows box to test on.

Actually, it's a little more aggressive than I suggested; it strips out *all*
PATH entries ending in libexec/git-core, not just the first. But I never have
libexec/git-core on my PATH anyway, so I'm not bothered by that. One could
modify this function to only check the first entry (the one git would have

Ben Knoble

P.S. Does anyone know what the libexec/git-core equivalent is on Windows? [3]
alludes to libexec\git-core, which I *think* is handled by my code.

P.P.S. If your vim is old and does not have `const`, you can use `let` instead.
You may need to change out the lambda {_, d -> ... } for a "v:val" string as



" When git starts up, it prepends it's libexec dir to PATH to allow it to find
" external commands.
" Thus, if vim is invoked via a git process (such as the contrib git-jump, my
" own git-ed, or any other usage of GIT_EDITOR/VISUAL/EDITOR in git commands, be
" they scripts or internals--with the exception of manually invoking the script
" yourself, without using git: sh .../git-jump), $PATH will contain something
" like libexec/git-core.
" We don't generally want it in vim's $PATH, though, as it is passed down to
" *all* subprocesses, including shells started with :terminal or :shell.
function s:fix_git_path() abort
  const slash = has('win32') ? '\' : '/'
  const git_core_base = printf('libexec%sgit-core', slash)
  " optimization: early return
  if $PATH !~# '.*'.git_core_base.'.*'
  const path_sep = has('win32') ? ';' : ':'
  const path = split($PATH, path_sep)
  const path_sans_libexec_git_core = filter(path, {_, d -> d !~#
  const new_path = join(path_sans_libexec_git_core, path_sep)
  let $PATH = new_path

augroup fix_git_path
  autocmd VimEnter * call s:fix_git_path()
augroup END


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, back to index

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-21 20:31 libexec/git-core on PATH in editors and other subprocesses (and a fix for vim) D. Ben Knoble list mirror (unofficial, one of many)

Archives are clonable:
	git clone --mirror
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

Example config snippet for mirrors

Newsgroups are available over NNTP:

 note: .onion URLs require Tor:

AGPL code for this site: git clone