branch: elpa/magit
commit 9db2055689bc078afd654af40a252de7e548d03e
Author: Jonas Bernoulli <[email protected]>
Commit: Jonas Bernoulli <[email protected]>

    magit-{ref,branch,local-branch,remote-branch}: Speed up
    
    "heads/<name>" is now recognized as a local branch, if appropriate.
    `magit-ref-p' is more permissive, using "git rev-parse --verify
    --symbolic-full-name", but still disallowing symbolic references.
    `magit-remote-branch-p', and thus `magit-branch-p', now recognize
    "remotes/<remote>/<branch>".
    
    Also consistently return a boolean and add docstrings.  Do this for
    `magit-symbolic-ref-p', `magit-tag-p' and `magit-remote-p' as well.
    
    Consistently name the argument STRING.  Some of these functions would
    return an incorrect result if STRING where nil, which would be a bug
    in the caller.  Guard against that.
---
 lisp/magit-git.el | 61 +++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 39 insertions(+), 22 deletions(-)

diff --git a/lisp/magit-git.el b/lisp/magit-git.el
index 662199fa84..fc6b34de65 100644
--- a/lisp/magit-git.el
+++ b/lisp/magit-git.el
@@ -2291,30 +2291,47 @@ specified using `core.worktree'."
                    (if (> (length line) 8) (substring line 9) t)))))
     (nreverse worktrees)))
 
-(defun magit-symbolic-ref-p (name)
-  (magit-git-success "symbolic-ref" "--quiet" name))
-
-(defun magit-ref-p (rev)
-  (or (car (member rev (magit-list-refs "refs/")))
-      (car (member rev (magit-list-refnames "refs/")))))
-
-(defun magit-branch-p (rev)
-  (or (car (member rev (magit-list-branches)))
-      (car (member rev (magit-list-branch-names)))))
-
-(defun magit-local-branch-p (rev)
-  (or (car (member rev (magit-list-local-branches)))
-      (car (member rev (magit-list-local-branch-names)))))
-
-(defun magit-remote-branch-p (rev)
-  (or (car (member rev (magit-list-remote-branches)))
-      (car (member rev (magit-list-remote-branch-names)))))
-
-(defun magit-tag-p (obj)
-  (equal (magit-object-type obj) "tag"))
+(defun magit-symbolic-ref-p (string)
+  "Return t if STRING is a reference, nil otherwise."
+  (cl-assert (stringp string))
+  (magit-git-success "symbolic-ref" "--quiet" string))
+
+(defun magit-ref-p (string)
+  "Return t if STRING is a reference, nil otherwise."
+  (cl-assert (stringp string))
+  (and (magit-ref-fullname string)
+       (not (magit-symbolic-ref-p string))))
+
+(defun magit-branch-p (string)
+  "Return t if STRING is a local or remote-tracking branch, nil otherwise."
+  (cl-assert (stringp string))
+  (or (magit-local-branch-p string)
+      (magit-remote-branch-p string)))
+
+(defun magit-local-branch-p (string)
+  "Return t if STRING is a local branch."
+  (cl-assert (stringp string))
+  (magit-git-success "show-ref" "--quiet" "--branches" string))
+
+(defun magit-remote-branch-p (string)
+  "Return t if STRING is a remote-tracking branch, nil otherwise."
+  (cl-assert (stringp string))
+  (seq-some (lambda (line)
+              (let ((ref (cadr (split-string line " "))))
+                (or (equal string ref)
+                    (equal string (substring ref 5))
+                    (equal string (substring ref 13)))))
+            (magit-git-lines "show-ref" string)))
+
+(defun magit-tag-p (string)
+  "Return t if STRING is a tag, nil otherwise."
+  (cl-assert (stringp string))
+  (magit-git-success "show-ref" "--quiet" "--tags" string))
 
 (defun magit-remote-p (string)
-  (car (member string (magit-list-remotes))))
+  "Return t if STRING is a remote, nil otherwise."
+  (cl-assert (stringp string))
+  (and (member string (magit-list-remotes)) t))
 
 (defun magit-branch-set-face (branch)
   (magit--propertize-face branch (if (magit-local-branch-p branch)

Reply via email to