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)
This commit is contained in:
Wolfgang Scherer 2019-08-27 00:45:48 +02:00 committed by Dmitry Gutov
parent 9ea9ac9a61
commit c3be58a8f5

View 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.