From: Wolfgang Scherer Date: Mon, 26 Aug 2019 22:45:48 +0000 (+0200) Subject: Improve vc--add-line, vc--remove-regexp X-Git-Tag: emacs-27.0.90~287 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=c3be58a8f599de32e15fd350dc2b8730a13c1923;p=emacs.git Improve vc--add-line, vc--remove-regexp * lisp/vc/vc.el (vc--add-line): Create file if it does not exist. Use existing buffer to avoid discrepancies with filesytem. Make sure that the file ends with a newline. (vc--remove-line): Do not create file if it does not exist. Use existing buffer to avoid discrepancies with filesytem. (bug#37185) --- diff --git a/lisp/vc/vc.el b/lisp/vc/vc.el index 132278e8230..c5584188b31 100644 --- a/lisp/vc/vc.el +++ b/lisp/vc/vc.el @@ -1460,20 +1460,22 @@ Argument BACKEND is the backend you are using." ;; Subroutine for `vc-git-ignore' and `vc-hg-ignore'. (defun vc--add-line (string file) "Add STRING as a line to FILE." - (with-temp-buffer - (insert-file-contents file) + (with-current-buffer (find-file-noselect file) + (goto-char (point-min)) (unless (re-search-forward (concat "^" (regexp-quote string) "$") nil t) (goto-char (point-max)) - (insert (concat "\n" string)) - (write-region (point-min) (point-max) file)))) + (unless (bolp) (insert "\n")) + (insert string "\n") + (save-buffer)))) (defun vc--remove-regexp (regexp file) "Remove all matching for REGEXP in FILE." - (with-temp-buffer - (insert-file-contents file) - (while (re-search-forward regexp nil t) - (replace-match "")) - (write-region (point-min) (point-max) file))) + (if (file-exists-p file) + (with-current-buffer (find-file-noselect file) + (goto-char (point-min)) + (while (re-search-forward regexp nil t) + (replace-match "")) + (save-buffer)))) (defun vc-checkout (file &optional rev) "Retrieve a copy of the revision REV of FILE.