git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
David Kågedal <davidk@lysator.liu.se>
Date
Feb 24, 2009, 20:39 UTC
Message-ID
<87vdqztvrl.fsf_-_@krank.kagedal.org>
In-Reply-To
<87bpsrx1j1.fsf@wine.dyndns.org>
Signed-off-by: David Kågedal <davidk@lysator.liu.se>
---

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
Previous: Alexandre JulliardNext: Alexandre Julliard
Message 9 of 12 in “git.el: Only show completions from refs/heads, refs/remotes and refs/tags”
  1. git.el: Only show completions from refs/heads, refs/remotes and refs/tagsDavid Kågedal, Feb 24, 2009
  2. David KågedalFeb 24, 2009
  3. David KågedalFeb 24, 2009
  4. Alexandre JulliardFeb 24, 2009
  5. David KågedalFeb 24, 2009
  6. Alexandre JulliardFeb 24, 2009
  7. David KågedalFeb 24, 2009
  8. Alexandre JulliardFeb 24, 2009
  9. git.el: Only show completions from refs/headsDavid Kågedal, Feb 24, 2009
  10. Alexandre JulliardMar 1, 2009
  11. David KågedalMar 2, 2009
  12. Alexandre JulliardMar 3, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.