From cf4a651a2ae799efd3c11ac2ccb83fbb1f2683b5 Mon Sep 17 00:00:00 2001 From: Wolfgang Scherer Date: Tue, 27 Aug 2019 00:45:48 +0200 Subject: [PATCH] vc--add-line, vc--remove-regexp enhanced * lisp/vc/vc.el (vc--add-line): Create file, if it does not exist. Use existing buffer to avoid discrepancies with filesytem. Make sure, 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) Copyright-paperwork-exempt: yes --- lisp/vc/vc.el | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lisp/vc/vc.el b/lisp/vc/vc.el index 4cac153..d97e7a2 100644 --- a/lisp/vc/vc.el +++ b/lisp/vc/vc.el @@ -1449,20 +1449,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. -- 2.7.4