Commit 21888a94 authored by Jonathan Tomer's avatar Jonathan Tomer Committed by Michael Albinus

Don't rewrite buffer contents after saving by rename (Bug#35497)

* 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.
parent cd8a1d6b
Pipeline #1605 failed with stage
in 52 minutes and 50 seconds
......@@ -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)
......@@ -5274,7 +5274,7 @@ Before and after saving the buffer, this function runs
(and setmodes (not success)
(rename-file (nth 2 setmodes) buffer-file-name t)
(setq buffer-backed-up nil))))))
(setq buffer-backed-up nil)))))))
(declare-function diff-no-select "diff"
......@@ -1244,5 +1244,20 @@ See <>."
(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)))))
(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
......@@ -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))
(advice (lambda (_start _end filename &rest _r)
(push filename written-files))))
(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)
;; 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))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment