.git 项目下,emacs, shell-command-to-string, git fatal?

emacs --version
GNU Emacs 29.4

.git 项目下,请问大家遇到了吗?

/usr/bin/emacs -Q --batch --eval '(let ((status (shell-command-to-string "/usr/bin/git status"))) (print status))'
"fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
"
GIT_DISCOVERY_ACROSS_FILESYSTEM=1 /usr/bin/emacs -Q --batch --eval '(let ((status (shell-command-to-string "/usr/bin/git status"))) (print status))'
"fatal: not a git repository (or any of the parent directories): .git
"

:person_tipping_hand:

有可能是这里的emacs的default-directory是 /usr/bin/emacs/而不是你运行这条命令的路径

谢谢,学习中…

直接在let里面bind一下default-directory应该就行了,不过如果你是想在终端里面运行,可能要看下怎么让emacs启动的时候自动把启动的路径设置为default-directory…,或者换个思路,看看内置的vc.el有没有相关api能拿来的

有道理,(shell-command-to-string “ls”), 显示 /tmp/ 下路径。

看了源码,shell-command-to-string 依赖 (with-current-buffer standard-output …),我这是 /tmp/

不应该 shell-command-to-string → git …

不能这样去拿 git-repo-root-path

(let* ((git-repo-path (shell-command-to-string "git rev-parse --show-toplevel"))) ... )