动态"magit"Git可执行文件取决于Emacs lisp中的环境

Bra*_*ght 1 linux macos emacs elisp

我在我的2台Mac上使用Git,每台Mac都有不同的可执行路径(一个是自制程序,一个是make/install'd).我也通过Tramp模式使用Git.

基本上我想要这个,在伪代码中:

if home mac:
    (setq magit-git-executable "~/Projects/homebrew/bin/git")
else if work mac:
    (setq magit-git-executable "/usr/local/git/bin/git")
else [if in tramp mode]:
    (setq magit-git-executable "git") ;; Linux can work this out so don't need a full path
Run Code Online (Sandbox Code Playgroud)

如果Emacs尊重我的setenv("PATH")配置,这将作为最后一个句子正常工作,但它似乎没有.

有没有一种很好的方法可以在Elisp中解决这个问题,这样我emacs.d可以在机器之间保持便携?如果失败了,是否可以在我通过tramp编辑文件时设置变量?

Gar*_*ees 5

如果你坚持使用magit-git-executable一个值,如果你在Tramp缓冲区而另一个值,如果不是,那将会很棘手.保留magit-git-executable其默认值会更容易"git",并exec-path根据您所在的机器调整您的位置.

你如何区分你的机器?按名字?如果是这样,那么可能是这样的:

(require 'cl)
(loop for (name . dir) in '(("home.local" . "~/Projects/homebrew/bin")
                            ("work.local" . "/usr/local/git/bin"))
      if (string= (system-name) name)
      do (push dir exec-path))
Run Code Online (Sandbox Code Playgroud)

  • `PATH`环境变量仅影响通过shell启动的程序.当程序直接启动时(例如通过`call-process`或`process-file`),它就是被搜索的`exec-path`列表.`magit`扩展通过`process-file`启动git,所以你需要设置`exec-path`. (4认同)