Magit 如何使用 Emacs 或 Terminal 配置好的 Proxy

最近在学习使用 Magit,刚刚尝试了一下通过 P u 来 push 到 origin/master发现无法使用 Proxy。

错误信息:

GitError! unable to access 'https://github.com/willbchang/macos-
dotfiles.git/': Could not resolve proxy: 127.0.0.1  [Type `$' for details]

版本信息:

Magit 20210406.454, Git 2.31.1, Emacs 27.2, darwin

Emacs 的 Proxy 配置可以正常使用,如从 mepla 等地方下载 packages

(setq url-proxy-services '(("no_proxy" . "127.0.0.1")
                          ("http" . "127.0.0.1:7890")
                          ("https" . "127.0.0.1:7890")))

Terminal 的 Proxy 配置可以正常使用,如在 Terminal 里 git push

export https_proxy=http://127.0.0.1:7890 
export http_proxy=http://127.0.0.1:7890 
export all_proxy=socks5://127.0.0.1:7890

经过搜索和尝试后发现为 git 单独配置 proxy 就可以使用了。

git config --global http.proxy http://127.0.0.1:7890

请问有没有办法直接使用 Emacs 或 Terminal 配置好的 Proxy 呢?

用setenv设置https_proxy,http_proxy这种环境变量就行

顺便贴一个从centaur抄来的相关配置

(defun proxy-socks-show ()
  "Show SOCKS proxy."
  (interactive)
  (when (fboundp 'cadddr)
    (if (bound-and-true-p socks-noproxy)
        (message "Current SOCKS%d proxy is %s:%d"
                 (cadddr socks-server) (cadr socks-server) (caddr socks-server))
      (message "No SOCKS proxy"))))

(defun proxy-socks-enable ()
  "Enable SOCKS proxy."
  (interactive)
  (require 'socks)
  (setq url-gateway-method 'socks
        socks-noproxy '("localhost")
        socks-server '("Default server" "127.0.0.1" 7890 5))
  (setenv "all_proxy" "socks5://127.0.0.1:7890")
  (proxy-socks-show))

(defun proxy-socks-disable ()
  "Disable SOCKS proxy."
  (interactive)
  (require 'socks)
  (setq url-gateway-method 'native
        socks-noproxy nil)
  (setenv "all_proxy" "")
  (proxy-socks-show))

(defun proxy-socks-toggle ()
  "Toggle SOCKS proxy."
  (interactive)
  (require 'socks)
  (if (bound-and-true-p socks-noproxy)
      (proxy-socks-disable)
    (proxy-socks-enable)))
2 个赞

感谢你的帮助 :smile:

1 个赞