branch: master
commit 21a88f0f9d0a745025bfe24d611dfe594c59ba52
Author: nathan moreau <[email protected]>
Commit: nathan moreau <[email protected]>

    Extract a helper function to get git toplevel directory.
---
 counsel.el | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/counsel.el b/counsel.el
index 29ea418..fb91842 100644
--- a/counsel.el
+++ b/counsel.el
@@ -1443,11 +1443,17 @@ TREE is the selected candidate."
   "Return worktree from candidate TREE."
   (substring tree 0 (string-match " " tree)))
 
+(defun counsel-git-toplevel ()
+  "Return the base directory of the current git repository."
+  (let ((out (string-trim-right (shell-command-to-string "git rev-parse 
--show-toplevel"))))
+    (and (string-match-p "Not a git repository" out) (error "Not a git 
repository!"))
+    out))
+
 ;;;###autoload
 (defun counsel-git-change-worktree ()
   "Find the file corresponding to the current buffer on a different worktree."
   (interactive)
-  (let ((git-root-dir (string-trim-right (shell-command-to-string "git 
rev-parse --show-toplevel"))))
+  (let ((git-root-dir (counsel-git-toplevel)))
     (ivy-read "Select worktree: "
               (or (cl-delete git-root-dir (counsel-git-worktree-list)
                              :key #'counsel-git-worktree-parse-root :test 
#'string=)

Reply via email to