Subject: Re: bug#37189: Acknowledgement (25.4.1: vc-hg-ignore implementation is missing)

From: Wolfgang Scherer <[email-masked]>
Date: Tue, 27 Aug 2019 01:25:59 +0200

Patch with commit message attached.

From 7a238036ba08e3b1359e06839ea38b944bd0f2db Mon Sep 17 00:00:00 2001
From: Wolfgang Scherer <[email-masked]>
Date: Tue, 27 Aug 2019 01:22:46 +0200
Subject: [PATCH] Provides vc-hg-ignore to make vc-ignore work correctly

* lisp/vc/vc-hg.el: (vc-hg-ignore) Ignore file of directory.  Add
filepath relative to directory of Mercurial .hgignore file.  The
filepath is quoted according to the active ignore syntax (Bug#37189).
(vc-hg--py-regexp-quote) Quote string as regexp to match exactly
string.

Copyright-paperwork-exempt: yes
---
 lisp/vc/vc-hg.el | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/lisp/vc/vc-hg.el b/lisp/vc/vc-hg.el
index f287adf..f4bddd2 100644
--- a/lisp/vc/vc-hg.el
+++ b/lisp/vc/vc-hg.el
@@ -1153,6 +1153,46 @@ REV is ignored."
   (expand-file-name ".hgignore"
 		    (vc-hg-root file)))

+(defun vc-hg-ignore (file &optional directory remove)
+  "Ignore FILE of DIRECTORY (default is `default-directory').
+
+FILE is a file wildcard, relative to the root directory of DIRECTORY.
+
+If FILE matches the regular expression
+`vc-hg-ignore-detect-wildcard', it is appended to .hgignore as
+is. Otherwise, FILE is escaped/expanded according to the active
+syntax in .hgignore. If the syntax is `regexp', FILE is quoted as
+anchored literal Python regexp and if FILE is a directory, the
+trailing `$' is omitted.  Otherwise, if the syntax is `glob',
+FILE is used unquoted and if FILE is a directory, a `*' is
+appended.
+
+When called from Lisp code, if DIRECTORY is non-nil, the
+repository to use will be deduced by DIRECTORY; if REMOVE is
+non-nil, remove FILE from ignored files."
+  (let ((ignore (vc-hg-find-ignore-file (or directory default-directory)))
+        (pattern file)
+        root-dir file-path syntax)
+    (unless (string-match vc-hg-ignore-detect-wildcard pattern)
+      (setq root-dir (file-name-directory ignore))
+      (setq file-path (expand-file-name file directory))
+      (setq pattern (substring file-path (length root-dir)))
+      (save-match-data
+        (with-current-buffer (find-file-noselect ignore)
+          (goto-char (point-max))
+          (setq syntax
+                (if (re-search-backward "^ *syntax: *\\(regexp\\|glob\\)$" nil t)
+                    (match-string 1)
+                  "regexp")))
+        (setq pattern
+              (if (string= syntax "regexp")
+                  (concat "^" (vc-hg--py-regexp-quote pattern)
+                          (and (not (file-directory-p file-path)) "$"))
+                (concat pattern (and (file-directory-p file-path) "*"))))))
+    (if remove
+        (vc--remove-regexp pattern ignore)
+      (vc--add-line pattern ignore))))
+
 ;; Modeled after the similar function in vc-bzr.el
 (defun vc-hg-checkout (file &optional rev)
   "Retrieve a revision of FILE.
@@ -1451,6 +1491,24 @@ This function differs from vc-do-command in that it invokes
 (defun vc-hg-root (file)
   (vc-find-root file ".hg"))

+(defvar vc-hg--py-regexp-special-chars
+  (mapcar
+   (function
+    (lambda (_c)
+      (cons _c (concat "\\" (char-to-string _c)))))
+   (append "()[]{}?*+-|^$\\.&~# \t\n\r\v\f" nil))
+  "Characters that have special meaning in Python regular expressions.")
+
+(defun vc-hg--py-regexp-quote (string)
+  "Return a Python regexp string which matches exactly STRING and nothing else.
+Ported from Python v3.7"
+  (mapconcat
+   (function
+    (lambda (_c)
+      (or (cdr (assq _c vc-hg--py-regexp-special-chars))
+          (char-to-string _c))))
+   string ""))
+
 (provide 'vc-hg)

 ;;; vc-hg.el ends here
--
2.7.4