]> git.eshelyaron.com Git - emacs.git/commitdiff
Don't rewrite buffer contents after saving by rename (Bug#35497)
authorJonathan Tomer <jktomer@google.com>
Wed, 8 May 2019 07:13:58 +0000 (09:13 +0200)
committerMichael Albinus <michael.albinus@gmx.de>
Wed, 8 May 2019 07:13:58 +0000 (09:13 +0200)
* lisp/files.el (basic-save-buffer-2): Don't rewrite file contents
after saving-by-renaming.  (Bug#35497)

* test/lisp/files-tests.el (files-tests-dont-rewrite-precious-files):
* test/lisp/net/tramp-tests.el
(tramp-test10-write-region-file-precious-flag): Regression tests
for this change.

lisp/files.el
test/lisp/files-tests.el
test/lisp/net/tramp-tests.el

index 518394fdcd7130a279e7407bc8a8696372f2714c..8477c227bcc53323c13bd2b75ac229a99788e9f0 100644 (file)
@@ -5258,7 +5258,7 @@ Before and after saving the buffer, this function runs
                     (set-file-extended-attributes buffer-file-name
                                                   (nth 1 setmodes)))
                 (set-file-modes buffer-file-name
-                                (logior (car setmodes) 128))))))
+                                (logior (car setmodes) 128)))))
        (let (success)
          (unwind-protect
              (progn
@@ -5274,7 +5274,7 @@ Before and after saving the buffer, this function runs
            (and setmodes (not success)
                 (progn
                   (rename-file (nth 2 setmodes) buffer-file-name t)
-                  (setq buffer-backed-up nil))))))
+                  (setq buffer-backed-up nil)))))))
     setmodes))
 
 (declare-function diff-no-select "diff"
index ae8ea41a797ccb5b41aaaa389f672a6f4f386018..fe2e958f1c30824c52cbd450d7d6afc6d9f18323 100644 (file)
@@ -1244,5 +1244,20 @@ See <https://debbugs.gnu.org/35241>."
                     (executable-find (file-name-nondirectory tmpfile))))))
       (delete-file tmpfile))))
 
+(ert-deftest files-tests-dont-rewrite-precious-files ()
+  "Test that `file-precious-flag' forces files to be saved by
+renaming only, rather than modified in-place."
+  (let* ((temp-file-name (make-temp-file "files-tests"))
+         (advice (lambda (_start _end filename &rest _r)
+                   (should-not (string= filename temp-file-name)))))
+    (unwind-protect
+        (with-current-buffer (find-file-noselect temp-file-name)
+          (advice-add #'write-region :before advice)
+          (setq-local file-precious-flag t)
+          (insert "foobar")
+          (should (null (save-buffer))))
+      (ignore-errors (advice-remove #'write-region advice))
+      (ignore-errors (delete-file temp-file-name)))))
+
 (provide 'files-tests)
 ;;; files-tests.el ends here
index 7d3c43408d48afb72eeb52b8d85f03912c382e67..e35be0e3121de56868ee47aa51590b6ca83b81fe 100644 (file)
@@ -41,6 +41,7 @@
 
 ;;; Code:
 
+(require 'cl-seq)
 (require 'dired)
 (require 'ert)
 (require 'ert-x)
@@ -2270,6 +2271,34 @@ This checks also `file-name-as-directory', `file-name-directory',
        ;; Cleanup.
        (ignore-errors (delete-file tmp-name))))))
 
+(ert-deftest tramp-test10-write-region-file-precious-flag ()
+  "Check that `file-precious-flag' is respected with Tramp in use."
+  (skip-unless (tramp--test-enabled))
+  (skip-unless (tramp--test-sh-p))
+
+  (let* ((tmp-name (tramp--test-make-temp-name))
+         written-files
+         (advice (lambda (_start _end filename &rest _r)
+                   (push filename written-files))))
+
+    (unwind-protect
+        (with-current-buffer (find-file-noselect tmp-name)
+          ;; Write initial contents.  Adapt `visited-file-modtime'
+          ;; in order to suppress confirmation.
+          (insert "foo")
+          (write-region nil nil tmp-name)
+          (set-visited-file-modtime)
+          ;; Run the test.
+          (advice-add 'write-region :before advice)
+          (setq-local file-precious-flag t)
+          (insert "bar")
+          (should (null (save-buffer)))
+          (should-not (cl-member tmp-name written-files :test #'string=)))
+
+      ;; Cleanup.
+      (ignore-errors (advice-remove 'write-region advice))
+      (ignore-errors (delete-file tmp-name)))))
+
 (ert-deftest tramp-test11-copy-file ()
   "Check `copy-file'."
   (skip-unless (tramp--test-enabled))