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

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

 



M-x git-checkout ask for a commitish, and checkout it as in
  git checkout commitish --
  key binding: "C-cC-o"
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: "C-cC-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 C"
A menu is also available for just switching branch
---
 contrib/emacs/git.el |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 52 insertions(+), 0 deletions(-)

diff --git a/contrib/emacs/git.el b/contrib/emacs/git.el
index 5166fca..d35f837 100644
--- a/contrib/emacs/git.el
+++ b/contrib/emacs/git.el
@@ -1209,6 +1209,51 @@ if it is :tags return tags"
 				 (git-list-refs :all))
 		   () () () () 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"
+  (let ((head (git-symbolic-ref "HEAD")))
+    (git-refs-build-menu
+     :before `(["detached head" ignore
+				:visible ,(not head)
+				:selected ,(not head)
+				:style radio
+				:key-sequence nil])
+     :branch (lambda (short-name ref-name)
+	       `[,short-name (git-checkout ,short-name)
+			      :style radio
+			      :selected ,(string= head ref-name)])
+     :remote (lambda (short-name ref-name)
+	       `[,short-name (git-checkout ,ref-name)])
+     :tag (lambda (short-name ref-name)
+	    `[,short-name (git-checkout ,ref-name)])
+     :after `(["checkout any commitish" git-checkout t]
+	      ["change to new" git-branch t]))))
+
 (defun git-diff-file ()
   "Diff the marked file(s) against HEAD."
   (interactive)
@@ -1491,6 +1536,7 @@ amended version of it."
 (unless git-status-mode-map
   (let ((map (make-keymap))
         (commit-map (make-sparse-keymap))
+        (branch-map (make-sparse-keymap))
         (diff-map (make-sparse-keymap))
         (toggle-map (make-sparse-keymap)))
     (suppress-keymap map)
@@ -1498,6 +1544,7 @@ amended version of it."
     (define-key map "h"   'git-help)
     (define-key map " "   'git-next-file)
     (define-key map "a"   'git-add-file)
+    (define-key map "B"    branch-map)
     (define-key map "c"   'git-commit-file)
     (define-key map "\C-c" commit-map)
     (define-key map "d"    diff-map)
@@ -1525,8 +1572,12 @@ amended version of it."
     (define-key map "x"   'git-remove-handled)
     (define-key map "\C-?" 'git-unmark-file-up)
     (define-key map "\M-\C-?" 'git-unmark-all)
+    ; the branch map
+    (define-key branch-map "C" 'git-create-branch)
     ; the commit submap
     (define-key commit-map "\C-a" 'git-amend-commit)
+    (define-key commit-map "\C-o" 'git-checkout)
+    (define-key commit-map "\C-b" 'git-branch)
     ; the diff submap
     (define-key diff-map "b" 'git-diff-file-base)
     (define-key diff-map "c" 'git-diff-file-combined)
@@ -1547,6 +1598,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

-
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