Function: vc-revert

vc-revert is an autoloaded, interactive and byte-compiled function defined in vc.el.gz.

Signature

(vc-revert)

Documentation

Revert working copies of the selected fileset to their repository contents.

This asks for confirmation if the buffer contents are not identical to the working revision (except for keyword expansion).

View in manual

Probably introduced at or before Emacs version 24.1.

Key Bindings

Aliases

vc-restore

Source Code

;; Defined in /usr/src/emacs/lisp/vc/vc.el.gz
;;;###autoload
(defun vc-revert ()
  "Revert working copies of the selected fileset to their repository contents.
This asks for confirmation if the buffer contents are not identical
to the working revision (except for keyword expansion)."
  (interactive)
  (let* ((vc-fileset (vc-deduce-fileset))
         (backend (car vc-fileset))
	 (files (cadr vc-fileset))
	 (queried nil)
	 diff-buffer)
    ;; If any of the files is visited by the current buffer, make sure
    ;; buffer is saved.  If the user says `no', abort since we cannot
    ;; show the changes and ask for confirmation to discard them.
    (when-let* ((n (buffer-file-name))
                ((or (not files) (member n files))))
      (vc-buffer-sync nil))
    (save-some-buffers nil (lambda ()
                             (and-let* ((n (buffer-file-name)))
                               (member n files))))
    (let (needs-save)
      (dolist (file files)
        (let ((buf (get-file-buffer file)))
	  (when (and buf (buffer-modified-p buf))
            (push buf needs-save)))
        (when (vc-up-to-date-p file)
	  (if (yes-or-no-p (format "%s seems up-to-date.  Revert anyway? "
                                   file))
	      (setq queried t)
	    (error "Revert canceled"))))
      (when needs-save
        (error "Cannot revert with these buffers unsaved: %s"
               (string-join (mapcar #'buffer-name needs-save) ", "))))
    (unwind-protect
	(when (if vc-revert-show-diff
		  (progn
		    (setq diff-buffer (generate-new-buffer "*vc-diff*"))
		    (vc-diff-internal vc-allow-async-revert vc-fileset
				      nil nil nil diff-buffer))
		;; Avoid querying the user again.
		(null queried))
	  (unless (yes-or-no-p
		   (format "Discard changes in %s? "
			   (let ((str (vc-delistify files))
				 (nfiles (length files)))
			     (if (length< str 50)
				 str
                               (format (ngettext "%d file" "%d files"
                                                 nfiles)
                                       nfiles)))))
	    (error "Revert cancelled")))
      (when diff-buffer
	(quit-windows-on diff-buffer (eq vc-revert-show-diff 'kill))))
    (vc-revert-files backend files)))