From 89ddf31ecad885e5491e8d6b71b48c1591b3faec Mon Sep 17 00:00:00 2001 From: benjamin Date: Fri, 14 Sep 2018 23:00:46 +0200 Subject: use magit's function to obtain git branch The old method was just incredibly hacky and bad. If magit is not installed, we still use the old method as fallback (but who doesn't use magit anyway) --- feebleline.el | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'feebleline.el') diff --git a/feebleline.el b/feebleline.el index a62c77a..af4fff9 100644 --- a/feebleline.el +++ b/feebleline.el @@ -115,17 +115,28 @@ FORMAT-ARGS (a list) will be expanded as the rest of `format' arguments. If PROPS is given, it should be a list which will be sent to `add-text-properties'.") -(defun feebleline--git-branch-string () - "Return current git branch as a string, or the empty string if pwd is not in a git repo (or the git command is not found)." - (interactive) +(require 'magit nil t) +(if (fboundp 'magit-get-current-branch) + (defun feebleline--git-branch-string () + "Return current git branch as a string, or the empty string if pwd is not in a git repo (or the git command is not found)." + (interactive) + (require 'esh-ext) + (let ((git-output (magit-get-current-branch))) + (if (> (length git-output) 0) + git-output + "(no branch)"))) + + (message "Warning: Feebleline couldn't find magit! Using hacky version to obtain git branch.") (require 'esh-ext) - (when (and (eshell-search-path "git") - (locate-dominating-file default-directory ".git")) - (let ((git-output (shell-command-to-string (concat "cd " default-directory " && git branch | grep '\\*' | sed -e 's/^\\* //'")))) - (if (> (length git-output) 0) - (substring git-output 0 -1) - ;; (concat " :" (substring git-output 0 -1)) - "(no branch)")))) + (defun feebleline--git-branch-string () + "Return current git branch as a string, or the empty string if pwd is not in a git repo (or the git command is not found)." + (interactive) + (when (and (eshell-search-path "git") + (locate-dominating-file default-directory ".git")) + (let ((git-output (shell-command-to-string (concat "cd " default-directory " && git branch | grep '\\*' | sed -e 's/^\\* //'")))) + (if (> (length git-output) 0) + (substring git-output 0 -1) + "(no branch)"))))) (defvar feebleline--home-dir nil) -- cgit v1.2.3