1

I would like a way to press a key binding that limits the git log to the author of the current commit.

Pressing the same key again should clear the author field.


  • Note that I'm aware of how to do this manually by pressing l-A then selecting the authors name. However it's not clear how this can be automated, some kind of function to limit by author, then to clear that limit.

  • The question is quite spesific as I'm not sure which part of magit needs to be customized to support this, if it involves invoking transient or not. I've read over the magit-log.el sources but it's still not clear to me how this should be done.

  • Note that this question relates to: How to filter by author in magit-log?

2 Answers 2

1

This is an updated version of @dalanicolai's answer, the command is my-magit-log-toggle-limit-to-author which can be bound to a key.

Changes include:

  • Improved sha1-at-point check to work in the magit-log view even if the cursor isn't over the SHA1.
  • Avoid a redundant call to magit-rev-format.
  • Only access the author when toggling on.
  • Report an error when the commit can't be detected.
  • Replace string-match with string-prefix-p.
  • Remove use of (magit-git-lines "shortlog" "-n" "-s" "-e" "HEAD") ... on my system it's very slow, rely on the author from the SHA1 instead.
(defun my-magit-log-toggle-limit-to-author ()
  "Toggle an isolated log of the current author."
  (interactive)
  (let* ((args (magit-log-arguments))
         (arg-prefix "--author=")
         (log-setup-buffer-args
          (list
           (magit-log-read-revs t)
           (cond
            ((seq-find (apply-partially #'string-prefix-p arg-prefix) (car args))
             (seq-remove (apply-partially #'string-prefix-p arg-prefix) (car args)))
            (t
             (let ((rev
                    (or (magit-commit-at-point)
                        (magit-section-value-if 'module-commit)
                        (thing-at-point 'git-revision t))))
               (unless rev
                 (error "Could not find rev at point"))
               (let ((author (magit-rev-format "%an <%ae>" rev)))
                 (append (car args) (list (concat arg-prefix author)))))))
           nil)))
    (apply #'magit-log-setup-buffer log-setup-buffer-args)))
1

I am not aware of magit (currently) providing such functionality, however, starting from C-h k l from the magit status buffer, and then further inspecting the code (the steps are a bit too many to write down here), we can come up with the following command:

(defun magit-log-toggle-limit-to-author ()
  (interactive)
  (let* ((args (magit-log-arguments))
         (rev (magit-thing-at-point 'git-revision t))
         (author (format "%s <%s>"
                         (magit-rev-format "%an" rev)
                         (magit-rev-format "%ae" rev))))
    (apply #'magit-log-setup-buffer
       (list (magit-log-read-revs t)
         (if (seq-find (apply-partially #'string-match "--author") (car args))
             (seq-remove (apply-partially #'string-match "--author") (car args))
           (append (car args) (list (concat "--author=" author))))
         nil))))

You can bind it to your preferred key.

Indeed would be nice to see this functionality getting added to magit directly (possibly with some easier/shorter code).

7
  • It frequently picks the wrong author, although not always - nevertheless, this is a great start for me, I'll post if I find a fix for the issue, thanks!
    – ideasman42
    Jun 2 at 11:09
  • Ah interesting, I have tried it a few times on a few commits with different authors now, but I am unable to reproduce it. Maybe some git repo's 'look different'.... Well, good luck hunting it down :) Jun 2 at 11:15
  • 1
    It looks like the cursor needs to be moved to the line-start before magit-thing-at-point sees the sha1, causing rev to be nil.
    – ideasman42
    Jun 2 at 11:17
  • I tried it on a private gitlab repo, but indeed I find that it does not seem to work well on e.g. the Spacemacs repo. Jun 2 at 11:17
  • posted some improvements, would you like to include them in this answer? - if so I'll delete mine.
    – ideasman42
    Jun 2 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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