Repository: adamchainz/pygments-git (language: python)

Issue:
git ignore files
### Description

https://git-scm.com/docs/gitignore


Discussion / hints:

Fix patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 3c5f6f3..6ec76b9 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -2,6 +2,8 @@
 Changelog
 =========
 
+* Add ``git-ignore`` lexer.
+
 1.2.0 (2023-04-16)
 ------------------
 
diff --git a/setup.cfg b/setup.cfg
index b4f1dbf..1054e9f 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -48,6 +48,7 @@ pygments.lexers =
     git-commit-edit-msg=pygments_git:GitCommitEditMsgLexer
     git-conflict-markers=pygments_git:GitConflictMarkersLexer
     git-console=pygments_git:GitBashSessionLexer
+    git-ignore=pygments_git:GitIgnoreLexer
     git-rebase-todo=pygments_git:GitRebaseTodoLexer
 
 [coverage:run]
diff --git a/src/pygments_git/__init__.py b/src/pygments_git/__init__.py
index 8e64833..c113f9e 100644
--- a/src/pygments_git/__init__.py
+++ b/src/pygments_git/__init__.py
@@ -302,6 +302,29 @@ class GitBashSessionLexer(RegexLexer):
     }
 
 
+class GitIgnoreLexer(RegexLexer):
+    name = "Git Ignore"
+    aliases = ("git-ignore",)
+    flags = re.MULTILINE
+
+    tokens = {
+        "root": [
+            (r"^#.*$", Comment),
+            (r"^", Text, "pattern"),
+        ],
+        "pattern": [
+            (r"\\!", Text),
+            (r"([!?*]|\*\*)", Keyword),
+            (
+                r"(\[)(.*?)(\])",
+                bygroups(Keyword, Text, Keyword),  # type: ignore [no-untyped-call]
+            ),
+            (r"\n", Text, "#pop"),
+            (r".", Text),
+        ],
+    }
+
+
 class GitRebaseTodoLexer(RegexLexer):
     name = "Git Rebase TODO"
     aliases = ("git-rebase-todo",)


Test patch:
diff --git a/tests/index.html b/tests/index.html
index 0896807..39f7a2b 100644
--- a/tests/index.html
+++ b/tests/index.html
@@ -1,6 +1,8 @@
 <!doctype html>
 <html>
 <head>
+<title>pygments-git tests</title>
+<link rel="icon" href="data:image/svg+xml,&lt;svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22&gt;&lt;text y=%22.9em%22 font-size=%2290%22&gt;🎨&lt;/text&gt;&lt;/svg&gt;">
 <style>
 :root {
   font-family: "PT Sans", sans-serif;
@@ -320,6 +322,37 @@ <h2 id=test_git_console>
 <span class="nl">[main </span><span class="mh">0bcdb8f</span><span class="nl">]</span><span class="go"> Spin it</span>
 </pre></div>
 </div>
+<h2 id=test_git_ignore>
+test_git_ignore
+<a href=#test_git_ignore>#</a>
+</h2>
+<p>
+Lexer: git-ignore
+</p>
+<div class=compare><code>
+<pre>
+# Hello ignorance
+/apples.py
+*.pyc
+!apples.pyc
+\!literal-exclamation.apple
+a??les.pyx
+[Aa]pples.txt
+
+fruits/**
+</pre>
+</code>
+<div class="highlight"><pre><span></span><span class="c"># Hello ignorance</span>
+/apples.py
+<span class="k">*</span>.pyc
+<span class="k">!</span>apples.pyc
+\!literal-exclamation.apple
+a<span class="k">??</span>les.pyx
+<span class="k">[</span>Aa<span class="k">]</span>pples.txt
+
+fruits/<span class="k">**</span>
+</pre></div>
+</div>
 <h2 id=test_git_rebase_todo>
 test_git_rebase_todo
 <a href=#test_git_rebase_todo>#</a>
diff --git a/tests/test_pygments_git.py b/tests/test_pygments_git.py
index 3904539..d4259b7 100644
--- a/tests/test_pygments_git.py
+++ b/tests/test_pygments_git.py
@@ -60,6 +60,13 @@ def check(self, testid: str, lexer: str, given: str) -> None:
             "<!doctype html>",
             "<html>",
             "<head>",
+            "<title>pygments-git tests</title>",
+            (
+                '<link rel="icon" href="data:image/svg+xml,&lt;svg xmlns=%22'
+                + "http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22&gt;"
+                + "&lt;text y=%22.9em%22 font-size=%2290%22&gt;🎨&lt;/text&gt;"
+                + '&lt;/svg&gt;">'
+            ),
             "<style>",
             dedent(
                 """\
@@ -240,6 +247,24 @@ def test_git_console(golden_file):
     )
 
 
+def test_git_ignore(golden_file):
+    golden_file.check(
+        "test_git_ignore",
+        "git-ignore",
+        """\
+        # Hello ignorance
+        /apples.py
+        *.pyc
+        !apples.pyc
+        \\!literal-exclamation.apple
+        a??les.pyx
+        [Aa]pples.txt
+
+        fruits/**
+        """,
+    )
+
+
 def test_git_rebase_todo(golden_file):
     golden_file.check(
         "test_git_rebase_todo",