3

In the same fashion magit can offer the list of recent commits in a section of status buffer. I'd like to have a list of recent checkout/visited branches to be able to quickly switch to it (M-xbb).

One of current patterns I have is :

  • work on my topic branch
  • find a bug unrelated to my topic
  • switch to master and pull latest
  • create a spinoff branch from master (checkout -b )
  • fix bug
  • push & pull-request
  • go back to my topic branch

For last step I have to remember the name of my branch ; which is not handy for me.

1

I experimented with this feature after digging through magit source code.

TO ENABLE it, as @tarsius, magit maintainer, points out in the comment below, use the function magit-add-section-hook.

add exp-feat/magit-insert-recent-branches to magit-status-sections-hook (preferably using customize interface since you would like to decide this section location in your magit-status buffer).

Here it goes (copied lots of stuffs from magit source code 😹):

(defvar exp-feat/recent-branches (make-hash-table :test 'equal))

(defcustom exp-feat/recent-branches-limits 3
  "Limits" :type 'integer :risky t)

(defun exp-feat/magit-insert-recent-branches nil
  "Insert recent branches"
  (let* ((dir (magit-toplevel))
         (curr-branch (magit-get-current-branch))
         (prev-branch (magit-get-previous-branch))
         (rbs (--> (gethash dir exp-feat/recent-branches)
                   (nconc (list prev-branch curr-branch) it)
                   (-distinct it)
                   (-filter (lambda (a) (and a (not (equal a curr-branch)))) it))))
    (when rbs
      (when (> (length rbs) exp-feat/recent-branches-limits)
        (--> (1- exp-feat/recent-branches-limits)
             (nthcdr it rbs)
             (setcdr it nil)))
      (puthash dir rbs exp-feat/recent-branches)
      (magit-insert-section (rb "rb")
        (magit-insert-heading "Recent branches")
        (dolist (it-branch rbs)
          (let ((output (magit-rev-format "%h %s" it-branch)))
            (string-match "^\\([^ ]+\\) \\(.*\\)" output)
            (magit-bind-match-strings (commit summary) output
              (when (and t (equal summary ""))
                (setq summary "(no commit message)"))
              (magit-insert-section (branch it-branch)
                (insert (propertize commit
                                    'font-lock-face 'magit-hash) ?\s)
                (insert (propertize it-branch
                                    'font-lock-face 'magit-branch-local) ?\s)
                (insert (funcall magit-log-format-message-function
                                 it-branch summary) ?\n)))))))))
2
  • 1
    You should use magit-add-section-hook to add to a section hook. If you use Custom then you deprive yourself of any future changes to the default value.
    – tarsius
    Mar 5 at 14:03
  • @tarsius Edited. Thank you for pointing that out!
    – TerryTsao
    Mar 6 at 3:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.