Subject: bug#11490: vc-next-action overwrites changes in non-checked-out RCS file

From: Chong Yidong <[email-masked]>
Date: Tue, 02 Apr 2013 17:30:54 +0800

Jonathan Kamens <[email-masked]> writes:

> Please read the WHOLE LIST OF COMMANDS I SENT in my test case.
>
> I made the file writable in that list only to modify it. If you read
> the whole list of commands, you will see that I make it unwritable
> again before editing it with emacs. And that's exactly the problem...
> if the file isn't writable, emacs assumes that it's not checked out
> and not modified it and checks it out when you hit C-x v v,
> overwriting the changes in it.

OK, so the original bug is fixed by changing `vc-mistrust-permissions'
to t, indeed.

As for the problem Glenn just pointed out, it arises because VC caches
the state of the file when it is first visited (via vc-state-refresh).
The following patch (against trunk) should fix this, but I haven't had
time to give it much testing yet.

=== modified file 'lisp/vc/vc-hooks.el'
*** lisp/vc/vc-hooks.el	2013-01-02 16:13:04 +0000
--- lisp/vc/vc-hooks.el	2013-04-02 09:23:22 +0000
***************
*** 703,719 ****
    ;; the state to 'edited and redisplay the mode line.
    (let* ((file buffer-file-name)
           (backend (vc-backend file)))
!     (and backend
! 	 (or (and (equal (vc-file-getprop file 'vc-checkout-time)
! 			 (nth 5 (file-attributes file)))
! 		  ;; File has been saved in the same second in which
! 		  ;; it was checked out.  Clear the checkout-time
! 		  ;; to avoid confusion.
! 		  (vc-file-setprop file 'vc-checkout-time nil))
! 	     t)
!          (eq (vc-checkout-model backend (list file)) 'implicit)
!          (vc-state-refresh file backend)
! 	 (vc-mode-line file backend))
      ;; Try to avoid unnecessary work, a *vc-dir* buffer is
      ;; present if this is true.
      (when vc-dir-buffers
--- 703,723 ----
    ;; the state to 'edited and redisplay the mode line.
    (let* ((file buffer-file-name)
           (backend (vc-backend file)))
!     (when backend
!       (if (eq (vc-checkout-model backend (list file)) 'implicit)
! 	  (progn
! 	    ;; If the file was saved in the same second in which it
! 	    ;; was checked out, clear the checkout-time to avoid
! 	    ;; confusion.
! 	    (if (equal (vc-file-getprop file 'vc-checkout-time)
! 		       (nth 5 (file-attributes file)))
! 		(vc-file-setprop file 'vc-checkout-time nil))
! 	    (if (vc-state-refresh file backend)
! 		(vc-mode-line file backend)))
! 	;; If we saved an unlocked file on a locking based VCS, that
! 	;; file is not longer up-to-date.
! 	(if (eq (vc-file-getprop file 'vc-state) 'up-to-date)
! 	    (vc-file-setprop file 'vc-state nil))))
      ;; Try to avoid unnecessary work, a *vc-dir* buffer is
      ;; present if this is true.
      (when vc-dir-buffers

=== modified file 'lisp/vc/vc.el'
*** lisp/vc/vc.el	2013-01-02 16:13:04 +0000
--- lisp/vc/vc.el	2013-04-02 09:30:28 +0000
***************
*** 1072,1077 ****
--- 1072,1087 ----
           ;; among all the `files'.
  	 (model (nth 4 vc-fileset)))
  
+     ;; If a buffer has unsaved changes, a checkout would discard them.
+     (when (and (not (eq model 'implicit))
+ 	       (eq state 'up-to-date))
+       (let ((files files)
+ 	    buffer)
+ 	(while files
+ 	  (setq buffer (get-file-buffer (car files)))
+ 	  (and buffer (buffer-modified-p buffer)
+ 	       (setq state 'unlocked-changes files nil)))))
+ 
      ;; Do the right thing
      (cond
       ((eq state 'missing)