0

When I'm editing a Git commit message, I sometimes like to browse recent commits to the same file(s). That is, I'd like to view the output of git log $list_of_files_with_staged_changes.

Git commit mode (from Magit) provides some nice features, including a history of my recent commit messages (M-p/M-n = git-commit-prev-message/git-commit-next-message). But these are only commits that I've done in the same session, not necessarily in the same repository. Here I only want to see commit messages that are relevant to the files I'm committing, and I want them no matter how old they are. I also want the commit IDs.

I also have Egg (Emacs's Got Git) installed, and it provides a quick way to access the log for all the files in the repository (C-x v l: egg-log). But that's too much noise.

While editing a Git commit message, how can I view the log of the files I'm committing? I can use both Egg and Magit if convenient, as well as the built-in vc-git.

1

The following function does the job in the nominal case, when committing files in the index.

(defun magit-log-staged-files (&optional args use-vc-log)
  "Show the change log of the files in the Git index.
If no files are staged in the index, log all changed files.

If USE-VC-LOG is non-nil (interactively: with a prefix argument),
call `vc-git-print-log' to show the log. Otherwise call `magit-log'."
  (interactive (list magit-log-arguments current-prefix-arg))
  (let ((files (or (magit-staged-files)
                   (magit-changed-files "HEAD"))))
    (if use-vc-log
        (vc-print-log-internal 'Git files "HEAD")
      (magit-log '("HEAD") args files))))
(define-key git-commit-mode-map "\C-c\C-l" 'magit-log-staged-files)

These aren't the right files when committing a set of files explicitly (git commit FILENAME…), which I think Magit doesn't do.

  • You should use magit-log-arguments instead of (car magit-log-arguments). If you use the function magit-log-arguments, then you need to use car, but that would be overkill in this case. – tarsius Oct 30 '17 at 19:25

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.