2

I have following problem. In a unfamiliar project I need to inspect the history of changes in a given file. So I open the file and run M-x magit-log-buffer-file. That opens a log buffer listing commits in which that file was modified. Now I move to the commit I want to inspect and press RET to show the revision buffer.

In that buffer point is placed at the beginning of the buffer, but I would like if Magit allowed jumping right to the diff of that one file I am interested in directly from the log buffer.

Currently, once in the revision buffer, I have search for the file in the list of files that changed, press j, then press C-l C-l, and finally the diff for the interesting file appears at the top of revision buffer.

  • What's the end goal? I think I do something like you describe differently, but not sure if I do it for the same reason. I use git-timemachine to browse the revisions of the same file. – wvxvw Aug 17 '16 at 16:11
  • @wvxvw can git-time mashine show me diffs? Like + followed by added code and - followed by removed code. My impression was git-timemachine only shows what the file looked like. – ruby_object Aug 17 '16 at 16:26
  • I don't think git time machine can show diffs. And I don't think I have a better workflow for you either. I'd probably just copied the path to file and fed it to =f option in diff popup. – wvxvw Aug 17 '16 at 16:38
  • I am also having difficulties trying to understand what you are trying to do - and also what you are actually doing. C-| C-| isn't a standard Magit binding; what is it bound to? What is "revision history"? The diff of a particular commit or a log (list) of commits? It sounds to me that you are in a log buffer, then you press RET to show the revision buffer, and now you want to go back to the log buffer. Is that correct? – tarsius Aug 19 '16 at 12:38
  • @ tarsius it's not C-| but C-l, in other words Control - "el" or lower case letter in the alphabet after letter k. It's a standards shortcut on emacs bound to recenter-top-bottom. – ruby_object Aug 20 '16 at 9:26
3

I have thrown together a quick-and-dirty implementation, which does this for diff buffers (not revision buffers as you requested), so you have to press d d instead of RET.

Put this in your init file:

(with-eval-after-load 'magit-diff
  (defvar magit-diff-focus-file nil)

  (defun magit-diff-setup (rev-or-range const args files)
    (require 'magit)
    (magit-mode-setup #'magit-diff-mode rev-or-range const args files)
    (--when-let (and magit-diff-focus-file
                     (magit-get-section
                      (append `((file . ,magit-diff-focus-file))
                              (magit-section-ident magit-root-section))))
      (magit-section-goto it)
      (recenter 0)))

  (defun magit-diff (rev-or-range &optional args files)
    (interactive (cons (magit-diff-read-range-or-commit "Diff for range"
                                                        nil current-prefix-arg)
                       (magit-diff-arguments)))
    (let ((magit-diff-focus-file
           (and (derived-mode-p 'magit-log-mode)
                (= (length (car (cddr magit-refresh-args))) 1)
                (caar (cddr magit-refresh-args)))))
      (magit-diff-setup rev-or-range nil args files)))
  )

I might eventually add something like this to Magit. To make it less likely that I will forget, please open a feature request.

  • It's a step in the right direction. I'll play with it tomorrow. Thank you for your help. – ruby_object Sep 2 '16 at 21:40

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.