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

[PATCH 02/03] Adding checkout function for commitish in git.el

From
Rémi Vanicat <vanicat@debian.org>
Date
Feb 16, 2008, 19:30 UTC
Message-ID
<87abm0u2ao.dlv_-_@maison.homelinux.org>
In-Reply-To
<87lk5ku2wn.dlv_-_@maison.homelinux.org>
M-x git-checkout ask for a commitish, and checkout it as in
  git checkout commitish --
  key binding: "C"
M-x git-branch ask for the name of a new branch, create it and
checkout it as in
  git checkout -b name
  key binding: "b"
M-x git-create-branch ask for a new branchname, a startpoint (a
commitish) and create a new branch as in
  git branch branchname startpoint
  key binding "B"
A menu is also available for just switching branch
---
The main problem that I still see, is the error message when one try
to checkout a branch/tag... that doesn't exist: it is a cryptic error
message... 
 contrib/emacs/git.el |   42 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 42 insertions(+), 0 deletions(-)
diff --git a/contrib/emacs/git.el b/contrib/emacs/git.el
index 16d9771..f180421 100644
--- a/contrib/emacs/git.el
+++ b/contrib/emacs/git.el
@@ -1175,6 +1175,44 @@ nil otherwise"
 					 (git-list-tags)))
 		   () () () () default))
 
+(defun git-checkout (branch &optional merge)
+  "checkout a branch, tag, or any commitish
+
+use a prefix arg if git should merge while checking out"
+  (interactive
+   (list (git-read-commitish "Branch: ")
+	 current-prefix-arg))
+  (let ((args (list branch "--")))
+    (if merge (push "-m" args))
+    (if (apply #'git-call-process-display-error "checkout" args)
+	(git-refresh-status))))
+
+(defun git-branch (branch)
+  "branch from current commit and checkout the new branch"
+  (interactive "MBranch: ")
+  (if (git-call-process-display-error "checkout" "-b" branch)
+      (git-refresh-status)))
+
+(defun git-create-branch (branch start-point)
+  "create a new branch named [branch] from the [start-point]"
+  (interactive
+   (list (read-string "New branch: ")
+	 (git-read-commitish "Start point(HEAD): " "HEAD")))
+  (git-call-process-display-error "branch" branch start-point))
+
+(defun git-checkout-menu-filter (rest)
+  "define the change branch menu"
+  (append
+   (mapcar
+    (lambda (branch)
+      `[,(car branch)
+	 (git-checkout ,(car branch))
+	 :style radio
+	 :selected ,(cdr branch)])
+    (git-list-branches))
+   (list ["checkout any commitish" git-checkout t]
+	 ["change to new" git-branch t])))
+
 (defun git-diff-file ()
   "Diff the marked file(s) against HEAD."
   (interactive)
@@ -1466,6 +1504,9 @@ amended version of it."
     (define-key map "a"   'git-add-file)
     (define-key map "c"   'git-commit-file)
     (define-key map "\C-c" commit-map)
+    (define-key map "C"   'git-checkout)
+    (define-key map "b"   'git-branch)
+    (define-key map "B"   'git-create-branch)
     (define-key map "d"    diff-map)
     (define-key map "="   'git-diff-file)
     (define-key map "f"   'git-find-file)
@@ -1513,6 +1554,7 @@ amended version of it."
     `("Git"
       ["Refresh" git-refresh-status t]
       ["Commit" git-commit-file t]
+      ("Checkout Branch" :filter git-checkout-menu-filter)
       ("Merge"
 	["Next Unmerged File" git-next-unmerged-file t]
 	["Prev Unmerged File" git-prev-unmerged-file t]
-- 
1.5.4.1.123.gcb68-dirty


-- 
Rémi Vanicat
Previous: Alexandre JulliardNext: Alexandre Julliard
Message 11 of 14 in “Add function to checkout a branch in git.el”
  1. Add function to checkout a branch in git.el=?utf-8?q?R=C3=A9mi=20Vanicat?=, Feb 13, 2008
  2. Karl HasselströmFeb 13, 2008
  3. Karl HasselströmFeb 13, 2008
  4. Remi VanicatFeb 13, 2008
  5. Sergei OrganovFeb 13, 2008
  6. Karl HasselströmFeb 13, 2008
  7. Sergei OrganovFeb 14, 2008
  8. switching and creating branch, merging and rebasing with Emacs=?utf-8?q?R=C3=A9mi=20Vanicat?=, Feb 16, 2008
  9. 01/03 Add helper function for branch tag and commitish to git.elRémi Vanicat, Feb 16, 2008
  10. Alexandre JulliardFeb 17, 2008
  11. 02/03 Adding checkout function for commitish in git.elRémi Vanicat, Feb 16, 2008
  12. Alexandre JulliardFeb 17, 2008
  13. 03/03 Adding command for merging in git.elRémi Vanicat, Feb 16, 2008
  14. Sergei OrganovFeb 13, 2008

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.