[PATCH] git.el: Only show completions from refs/heads

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: David Kågedal <davidk@xxxxxxxxxxxxxx>
---

Here is a version that can to both commit name lookup and branch name
lookup, and with a configuration parameter that determines how to find
branch names to complete on.

contrib/emacs/git.el |   34 +++++++++++++++++++++++++---------
 1 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/contrib/emacs/git.el b/contrib/emacs/git.el
index feb229c..a5138d7 100644
--- a/contrib/emacs/git.el
+++ b/contrib/emacs/git.el
@@ -118,6 +118,12 @@ if there is already one that displays the same directory."
   :group 'git
   :type 'boolean)
 
+(defcustom git-complete-branch-patterns
+  '("refs/heads" "refs/tags" "refs/remotes")
+  "Which patterns to use when completing branch names."
+  :group 'git
+  :type '(repeat string))
+
 
 (defface git-status-face
   '((((class color) (background light)) (:foreground "purple"))
@@ -1385,17 +1391,27 @@ With a prefix arg, diff the marked files instead."
         (push (match-string 1) files)))
     files))
 
-(defun git-read-commit-name (prompt &optional default)
-  "Ask for a commit name, with completion for local branch, remote branch and tag."
-  (completing-read prompt
-                   (list* "HEAD" "ORIG_HEAD" "FETCH_HEAD" (mapcar #'car (git-for-each-ref)))
-		   nil nil nil nil default))
+(defun git-read-commit-name (prompt specials &optional ref-patterns)
+  "Ask for a commit name, with completion.
+If SPECIALS is non-nil, add HEAD and similar names to the list of possible
+completions. The patterns in REF-PATTERNS are passed to `git-for-each-ref'
+to get a list of completions."
+  (let ((refs (apply #'git-for-each-ref ref-patterns)))
+    (completing-read prompt (if specials (list* '("HEAD" . nil)
+                                                '("FETCH_HEAD" . nil)
+                                                '("ORIG_HEAD" . nil)
+                                                refs)
+                              refs))))
+
+(defun git-read-branch-name (prompt)
+  "Ask for a branch name, with completion."
+  (git-read-commit-name prompt nil git-complete-branch-patterns))
 
 (defun git-checkout (branch &optional merge)
   "Checkout a branch, tag, or any commit.
 Use a prefix arg if git should merge while checking out."
   (interactive
-   (list (git-read-commit-name "Checkout: ")
+   (list (git-read-branch-name "Checkout: ")
          current-prefix-arg))
   (unless git-status (error "Not in git-status buffer."))
   (let ((args (list branch "--")))
@@ -1405,7 +1421,7 @@ Use a prefix arg if git should merge while checking out."
 
 (defun git-branch (branch)
   "Create a branch from the current HEAD and switch to it."
-  (interactive (list (git-read-commit-name "Branch: ")))
+  (interactive (list (git-read-branch-name "Branch: ")))
   (unless git-status (error "Not in git-status buffer."))
   (if (git-rev-parse (concat "refs/heads/" branch))
       (if (yes-or-no-p (format "Branch %s already exists, replace it? " branch))
@@ -1433,7 +1449,7 @@ amended version of it."
 
 (defun git-cherry-pick-commit (arg)
   "Cherry-pick a commit."
-  (interactive (list (git-read-commit-name "Cherry-pick commit: ")))
+  (interactive (list (git-read-commit-name "Cherry-pick commit: " t)))
   (unless git-status (error "Not in git-status buffer."))
   (let ((commit (git-rev-parse (concat arg "^0"))))
     (unless commit (error "Not a valid commit '%s'." arg))
@@ -1452,7 +1468,7 @@ amended version of it."
 
 (defun git-revert-commit (arg)
   "Revert a commit."
-  (interactive (list (git-read-commit-name "Revert commit: ")))
+  (interactive (list (git-read-commit-name "Revert commit: " t)))
   (unless git-status (error "Not in git-status buffer."))
   (let ((commit (git-rev-parse (concat arg "^0"))))
     (unless commit (error "Not a valid commit '%s'." arg))
-- 
1.6.2.rc1.21.gda6d


-- 
David Kågedal
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux