summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lisp/km-magit.el81
1 files changed, 71 insertions, 10 deletions
diff --git a/lisp/km-magit.el b/lisp/km-magit.el
index 2f8709f..0cfbf2c 100644
--- a/lisp/km-magit.el
+++ b/lisp/km-magit.el
@@ -208,6 +208,18 @@ START-POINT set to the current branch.
(concat it "-tmp")))))
(magit-run-git "checkout" "-b" branch))
+(defun km/magit-branch--get-versions (branch suffix)
+ (let ((version-re (rx (group (one-or-more digit)) string-end)))
+ (delq nil
+ (mapcar
+ (lambda (s)
+ (unless (string= branch s)
+ (if (string-match version-re s)
+ (string-to-number
+ (match-string-no-properties 1 s))
+ (error "bug: version re should always match"))))
+ (km/magit-branch-get-set branch suffix)))))
+
(defun km/magit-branch-backup-current (&optional choose-suffix)
"Create a backup branch for the current branch.
Name it as '<current-branch>__v<n>', where <n> is incremented
@@ -222,16 +234,7 @@ argument CHOOSE-SUFFIX, prompt for a suffix to use instead of
(let* ((suffix (if choose-suffix
(magit-read-string-ns "Backup suffix")
"v"))
- (version-re (format "\\`%s__%s\\([[:digit:]]+\\)\\'"
- (regexp-quote current)
- (regexp-quote suffix)))
- (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)))))
+ (versions (km/magit-branch--get-versions current suffix)))
(format "%s__%s%d"
current
suffix
@@ -270,6 +273,64 @@ argument CHOOSE-SUFFIX, prompt for a suffix to use instead of
(caar branches)
(format "%d branches" num-branches))))))
+(defun km/magit-match-versioned (member)
+ (string-match (rx (group (one-or-more (not space)))
+ "__"
+ (group (one-or-more (not (any space digit))))
+ (one-or-more digit)
+ string-end)
+ member))
+
+(defun km/magit-branch-get-set (member &optional suffix)
+ (let* ((rx-form (if (km/magit-match-versioned member)
+ `(and string-start
+ ,(match-string 1 member)
+ "__"
+ ,(match-string 2 member)
+ (one-or-more digit)
+ string-end)
+ `(and string-start
+ ,member
+ (zero-or-more
+ "__"
+ ,(or suffix '(one-or-more (not space)))
+ (one-or-more digit))
+ string-end)))
+ (re (rx-to-string rx-form 'no-group)))
+ (cl-remove-if-not (lambda (b) (string-match-p re b))
+ (magit-list-local-branch-names))))
+
+(defun km/magit-branch--set-action (action fn member)
+ (when-let ((branch-set (km/magit-branch-get-set member)))
+ (when (y-or-n-p (concat " "
+ (mapconcat #'identity branch-set "\n ")
+ "\n" action "? "))
+ (funcall fn branch-set))))
+
+(defun km/magit-branch-delete-set (member)
+ "Delete a set of branches identified by MEMBER."
+ (interactive
+ (list (magit-completing-read
+ "Delete set with member" (magit-list-refnames "refs/heads")
+ nil 'require nil nil
+ (or (magit-branch-at-point) (magit-get-previous-branch)))))
+ (km/magit-branch--set-action
+ "Delete"
+ (lambda (branch-set) (magit-run-git "branch" "-D" branch-set))
+ member))
+
+(defun km/magit-branch-archive-set (member)
+ "Archive a set of branches identified by MEMBER."
+ (interactive
+ (list (magit-completing-read
+ "Archive set with member" (magit-list-refnames "refs/heads")
+ nil 'require nil nil
+ (or (magit-branch-at-point) (magit-get-previous-branch)))))
+ (km/magit-branch--set-action
+ "Archive"
+ #'km/magit-branch-archive
+ member))
+
(defun km/magit-mode-bury-all-windows (&optional kill-buffer)
"Run `magit-mode-quit-window' until no longer in Magit buffer."
(interactive "P")