]> git.eshelyaron.com Git - emacs.git/commitdiff
Improve vc--add-line, vc--remove-regexp
authorWolfgang Scherer <wolfgang.scherer@gmx.de>
Mon, 26 Aug 2019 22:45:48 +0000 (00:45 +0200)
committerDmitry Gutov <dgutov@yandex.ru>
Tue, 24 Dec 2019 22:33:36 +0000 (00:33 +0200)
* 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)

lisp/vc/vc.el

index 132278e82306137173fe7e5827f51013693ecdd3..c5584188b31025785dafd6c6442117f1c01d6481 100644 (file)
@@ -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.