How can I get the root directory of the current git repo? I could run
shell-command-to-string on something like
git rev-parse --show-toplevel. Is there a better way? Does
magit or any other git front-end expose this info?
In Magit this is available as
magit-toplevel (but I agree with
@abo-abo that it makes sense to use
vc-root-dir. Works for more than git.
As mentioned by abo-abo, in Emacs 25, there is a function called
vc-root-diff that does what you need in a backend-agnostic manner. For earlier versions of Emacs, the following is a suitable replacement:
(defun vc-root-dir () (let ((backend (vc-deduce-backend))) (and backend (ignore-errors (vc-call-backend backend 'root default-directory)))))
As mentioned by Kyle, Magit provides the function
magit-get-top-dir, which simply calls
git rev-parse --show-cdup and interprets the result.