Include base directory of current git repository in eshell prompt

This allows to see of which git repository the current working directory is part
of.

This is also included in Howard Abrams configuration, but he instead determines
the name of the current git repository by looking at `remote.origin.url`.
However, my upstream branch is not always called `origin`, and sometimes there
is no upstream repository at all.  Using the name of the current directory (not
the whole path, though) instead seems to be a good compromise from my point of
view.
This commit is contained in:
Daniel Borchmann 2022-08-27 16:17:14 +02:00
parent a6f876d4f4
commit 925a55de1e
No known key found for this signature in database
GPG Key ID: 784AA8DF0CCDF625

View File

@ -56,16 +56,25 @@
(defun db/eshell-git-branch-string () (defun db/eshell-git-branch-string ()
;; Inspired by https://github.com/howardabrams/dot-files/blob/master/emacs-eshell.org#special-prompt ;; Inspired by https://github.com/howardabrams/dot-files/blob/master/emacs-eshell.org#special-prompt
"Return name of git branch of current directory, as a string. "Return name of git branch of current directory, as a string.
The format will be BASE-DIR::BRANCH-NAME, where BASE-DIR is the
directory containing the .git directory or link file of the
current git repository, and BRANCH-NAME is the name of the
current branch.
Return the empty string if the current directory is not part of a Return the empty string if the current directory is not part of a
git repository." git repository."
(interactive) (interactive)
(let ((pwd (eshell/pwd))) (let ((pwd (eshell/pwd))
repo-dir)
(when (and (not (file-remote-p pwd)) (when (and (not (file-remote-p pwd))
(eshell-search-path "git") (eshell-search-path "git")
(locate-dominating-file pwd ".git")) (setq repo-dir (locate-dominating-file pwd ".git")))
(save-match-data (save-match-data
(string-trim (let* ((git-branch (string-trim
(shell-command-to-string "git rev-parse --abbrev-ref HEAD")))))) (shell-command-to-string "git rev-parse --abbrev-ref HEAD")))
(base-dir (file-name-base (string-trim-right repo-dir "/?"))))
(format "%s::%s" base-dir git-branch))))))
(defun eshell/default-prompt-function () (defun eshell/default-prompt-function ()
"A prompt for eshell of the form "A prompt for eshell of the form
@ -73,9 +82,10 @@ git repository."
[$USER@$HOST] [$PWD] (current-git-branch) [$USER@$HOST] [$PWD] (current-git-branch)
Information about the current git branch will be empty when Information about the current git branch will be empty when the
the current directory is not part of a git repository. current directory is not part of a git repository. See
" `db/eshell-git-branch-string' for more details about the
formatting."
(let ((head-face '(:foreground "#859900"))) (let ((head-face '(:foreground "#859900")))
(concat (propertize "┌─" 'face head-face) (concat (propertize "┌─" 'face head-face)
(user-login-name) (user-login-name)