- C-xvl to view the file's history.
- n and p to move between commits.
- f to visit the file as of the commit at point.
That's bound to log-view-find-revision
, and if we look at the code we see the critical bit is:
(switch-to-buffer (vc-find-revision file revision)))
So we could wrap that in a custom function like so:
(defun my-vc-visit-file-revision (file revision)
"Visit FILE as it was at REVISION."
(interactive
(list (expand-file-name
(read-file-name (if (buffer-file-name)
(format "File (%s): " (file-name-nondirectory
(buffer-file-name)))
"File: ")))
(read-string "Revision: ")))
(require 'vc)
(switch-to-buffer
(vc-find-revision file revision)))
Edit: Stefan has provided a better answer, but if you liked being able to select the file as well as the revision, here's a version of my function which maintains the interactive file selection, but uses the code from vc-revision-other-window
for the revision handling.
I concluded that using other-window by default does indeed make more sense, so I've done the same here -- unless you provide a prefix argument in which case it uses the current window.
(defun my-vc-visit-file-revision (file rev)
"Visit revision REV of FILE in another window.
With prefix argument, uses the current window instead.
If the current file is named `F', the revision is named `F.~REV~'.
If `F.~REV~' already exists, use it instead of checking it out again."
;; based on `vc-revision-other-window'.
(interactive
(let ((file (expand-file-name
(read-file-name
(if (buffer-file-name)
(format "File (%s): " (file-name-nondirectory
(buffer-file-name)))
"File: ")))))
(require 'vc)
(list file (if (vc-backend file)
(vc-read-revision
"Revision to visit (default is working revision): "
(list file))
(vc-read-revision "Revision to visit: " t
(or (vc-deduce-backend)
(vc-responsible-backend file)))))))
(require 'vc)
(let ((revision (if (string-equal rev "")
(if (vc-backend file)
(vc-working-revision file)
(error "No revision specified for unregistered file %s"
file))
rev))
(backend (or (vc-backend file)
(vc-deduce-backend)
(vc-responsible-backend file)))
(visit (if current-prefix-arg
'switch-to-buffer
'switch-to-buffer-other-window)))
(condition-case err
(funcall visit (vc-find-revision file revision backend))
;; The errors which can result when we request an invalid combination of
;; file and revision tend to be opaque side-effects of some unexpected
;; failure within the backend; so we simply trap everything and signal a
;; replacement error indicting the assumed cause.
(error (error "File not found at revision %s: %s" revision file)))))
I bind this command to C-xvC-f
M-x magit-find-file
? It asks for a revision first and then lets you open a file... – Wayside