From e2fdbc2d011dec04ed4fc2a0cce128624e0e7e98 Mon Sep 17 00:00:00 2001 From: Marius Vollmer Date: Sat, 6 Jun 2009 14:54:30 +0300 Subject: [PATCH] Offer to create tracking branches when switching to a remote branch. * magit.el (magit-ref-exists-p): New. (magit-maybe-create-localtracking-branch): New. (magit-checkout): Use it. --- magit.el | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/magit.el b/magit.el index c3e163b8..665eb911 100644 --- a/magit.el +++ b/magit.el @@ -303,6 +303,9 @@ Many Magit faces inherit from this one by default." (substring head 11) nil))) +(defun magit-ref-exists-p (ref) + (magit-git-exit-code "show-ref" "--verify" "--quiet" ref)) + (defun magit-read-top-dir () (file-name-as-directory (read-directory-name "Git repository: " @@ -1944,10 +1947,22 @@ in log buffer." ;;; Branches +(defun magit-maybe-create-localtracking-branch (rev) + (if (string-match "^refs/remotes/\\(.*\\)/\\([^/]*\\)" rev) + (let ((remote (match-string 1 rev)) + (branch (match-string 2 rev))) + (when (and (not (magit-ref-exists-p (concat "refs/heads/" branch))) + (yes-or-no-p + (format "Create local tracking branch for %s? " branch))) + (magit-run-git "checkout" "-b" branch rev) + t)) + nil)) + (defun magit-checkout (rev) (interactive (list (magit-read-rev "Switch to" (magit-default-rev)))) (if rev - (magit-run-git "checkout" (magit-rev-to-git rev)))) + (if (not (magit-maybe-create-localtracking-branch rev)) + (magit-run-git "checkout" (magit-rev-to-git rev))))) (defun magit-read-create-branch-args () (let* ((cur-branch (magit-get-current-branch))