From a3ce3c3a3837011124b977c5eb5e936bb9154387 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Tue, 14 Feb 2017 19:49:00 -0500 Subject: Extend km/magit-backup-branch to support multiple versions --- init.el | 2 +- lisp/km-magit.el | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/init.el b/init.el index 0044a94..c4e716e 100644 --- a/init.el +++ b/init.el @@ -1148,7 +1148,7 @@ (magit-define-popup-action 'magit-branch-popup ?r "Rename branch" #'km/magit-branch-rename) (magit-define-popup-action 'magit-branch-popup - ?s "Backup current branch" #'km/magit-backup-branch) + ?s "Backup current branch" #'km/magit-branch-backup-current) (magit-define-popup-action 'magit-branch-popup ?t "Local tracking" #'km/magit-checkout-local-tracking)) diff --git a/lisp/km-magit.el b/lisp/km-magit.el index ccac2ed..bf65c4e 100644 --- a/lisp/km-magit.el +++ b/lisp/km-magit.el @@ -207,13 +207,24 @@ START-POINT set to the current branch. (interactive (list (magit-read-string "Branch name"))) (magit-run-git "checkout" "-b" branch)) -(defun km/magit-backup-branch () +(defun km/magit-branch-backup-current () "Create a backup branch for the current branch. -\n(git branch b/)" +\n(git branch b/__vN)" (interactive) - (--if-let (magit-get-current-branch) - (magit-run-git "branch" (concat "b/" it)) - (user-error "No current branch"))) + (let* ((current (or (magit-get-current-branch) + (user-error "No current branch"))) + (version-re (format "\\`b/%s__v\\([[:digit:]]+\\)\\'" current)) + (versions (delq nil + (mapcar + (lambda (s) + (and (string-match version-re s) + (string-to-number + (match-string-no-properties 1 s)))) + (magit-list-local-branch-names)))) + (vbranch (format "b/%s__v%d" + current + (if (null versions) 1 (1+ (apply #'max versions)))))) + (magit-run-git "branch" vbranch))) (defun km/magit-mode-bury-all-windows (&optional kill-buffer) "Run `magit-mode-quit-window' until no longer in Magit buffer." -- cgit v1.2.3