Thread (3 messages) flat view 3 messages, 2 authors, 2016-06-15
DORMANTno replies

[PATCH] Add function to checkout a branch in git.el

From: =?utf-8?q?R=C3=A9mi=20Vanicat?= <hidden>
Date: 2016-06-15 22:44:13
Subsystem: the rest · Maintainer: Linus Torvalds

One can now:
- checkout an existing branch with M-x git-change-branch
- create and checkout a new branch with C-u M-x git-change-branch
- do all this from the Git menu
---
It could also be interesting to add some  key biding for those

Also one might want to update the *git-status* buffer after checkout,
but this is not done by this patch
 contrib/emacs/git.el |   45 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 45 insertions(+), 0 deletions(-)
diff --git a/contrib/emacs/git.el b/contrib/emacs/git.el
index a8bf0ef..404c8e6 100644
--- a/contrib/emacs/git.el
+++ b/contrib/emacs/git.el
@@ -1138,6 +1138,50 @@ Return the list of files that haven't been handled."
   (when (eq (window-buffer) (current-buffer))
     (shrink-window-if-larger-than-buffer)))
 
+(defun git-list-branch ()
+  "Return a list of available branch"
+  ;; should I check if I'm in a git repository ?
+  (let ((branchs ()))
+    (with-temp-buffer
+      (git-run-command-buffer (current-buffer) "branch")
+      (goto-char (point-min))
+      (while (re-search-forward "^\\([ *]\\) \\([^\n]*\\)$" () t)
+	(push (list (match-string 2)
+		    (string= (match-string 1) "*"))
+	      branchs)))
+    (nreverse branchs)))
+
+(defun git-change-branch (branch &optional new)
+  "Switch to another branch
+
+With a prefix argument, switch to a new branch, ortherwise use
+an existing one"
+  (interactive
+   (if current-prefix-arg
+       (list (read-from-minibuffer "Branch: ")
+	     current-prefix-arg)
+       (list (completing-read "Branch: " (git-list-branch) () t)
+	     current-prefix-arg)))
+  (with-temp-buffer
+    (if new
+	(git-call-process-display-error "checkout" "-b" branch)
+	(git-call-process-display-error "checkout" branch))
+    (goto-char (point-min))))
+
+(defun git-change-new-branch-menu (branch)
+  "Switch to new branch"
+  (interactive "MBranch: ")
+  (git-change-branch branch 't))
+
+(defun git-change-branch-menu-filter (rest)
+  "define the change branch menu"
+  (append
+   (mapcar
+    (lambda (branch)
+      `[,(car branch) (git-change-branch ,(car branch)) t])
+    (git-list-branch))
+   (list ["change to new" git-change-new-branch-menu t])))
+
 (defun git-diff-file ()
   "Diff the marked file(s) against HEAD."
   (interactive)
@@ -1486,6 +1530,7 @@ amended version of it."
 	["Diff Against Merge Head" git-diff-file-merge-head t]
 	["Diff Against Mine" git-diff-file-mine t]
 	["Diff Against Other" git-diff-file-other t])
+      ("Change branch" :filter git-change-branch-menu-filter)
       "--------"
       ["Add File" git-add-file t]
       ["Revert File" git-revert-file t]
-- 
1.5.4.1.101.gacba
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help