MAJOR=10
MINOR=231
DEVNAME=snapshot
  B   ˆl–äY>”ÊeJeA3p\eÅ1hß�þDV„³r‰ðÈV^‘ÐùË„^h_Ê_ŠI|¥�+Wé3ž¯ÉÈ .B    # Copyright (C) 2017 Jelmer Vernooij <jelmer@jelmer.uk>
#
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
# General Public License as public by the Free Software Foundation; version 2.0
# or (at your option) any later version. You can redistribute it and/or
# modify it under the terms of either of these two licenses.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# You should have received a copy of the licenses; if not, see
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
# License, Version 2.0.
#

"""Parsing of gitignore files.

For details for the matching rules, see https://git-scm.com/docs/gitignore
"""

import os.path
import re
from typing import (
    BinaryIO,
    Iterable,
    List,
    Optional,
    TYPE_CHECKING,
    Dict,
    Union,
)

if TYPE_CHECKING:
    from dulwich.repo import Repo

from dulwich.config import get_xdg_config_home_path, Config


def _translate_segment(segment: bytes) -> bytes:
    if segment == b"*":
        return b"[^/]+"
    res = b""
    i, n = 0, len(segment)
    while i < n:
        c = segment[i : i + 1]
        i = i + 1
        if c == b"*":
            res += b"[^/]*"
        elif c == b"?":
            res += b"[^/]"
        elif c == b"\\":
            res += re.escape(segment[i : i + 1])
            i += 1
        elif c == b"[":
            j = i
            if j < n and segment[j : j + 1] == b"!":
                j = j + 1
            if j < n and segment[j : j + 1] == b"]":
                j = j + 1
            while j < n and segment[j : j + 1] != b"]":
                j = j + 1
            if j >= n:
                res += b"\\["
            else:
                stuff = segment[i:j].replace(b"\\", b"\\\\")
                i = j + 1
                if stuff.startswith(b"!"):
                    stuff = b"^" + stuff[1:]
                elif stuff.startswith(b"^"):
                    stuff = b"\\" + stuff
                res += b"[" + stuff + b"]"
        else:
            res += re.escape(c)
    return res


def translate(pat: bytes) -> bytes:
    """Translate a shell PATTERN to a regular expression.

    There is no way to quote meta-characters.

    Originally copied from fnmatch in Python 2.7, but modified for Dulwich
    to cope with features in Git ignore patterns.
    """

    res = b"(?ms)"

    if b"/" not in pat[:-1]:
        # If there's no slash, this is a filename-based match
        res += b"(.*/)?"

    if pat.startswith(b"**/"):
        # Leading **/
        pat = pat[2:]
        res += b"(.*/)?"

    if pat.startswith(b"/"):
        pat = pat[1:]

    for i, segment in enumerate(pat.split(b"/")):
        if segment == b"**":
            res += b"(/.*)?"
            continue
        else:
            res += (re.escape(b"/") if i > 0 else b"") + _translate_segment(segment)

    if not pat.endswith(b"/"):
        res += b"/?"

    return res + b"\\Z"


def read_ignore_patterns(f: BinaryIO) -> Iterable[bytes]:
    """Read a git ignore file.

    Args:
      f: File-like object to read from
    Returns: List of patterns
    """

    for line in f:
        line = line.rstrip(b"\r\n")

        # Ignore blank lines, they're used for readability.
        if not line.strip():
            continue

        if line.startswith(b"#"):
            # Comment
            continue

        # Trailing spaces are ignored unless they are quoted with a backslash.
        while line.endswith(b" ") and not line.endswith(b"\\ "):
            line = line[:-1]
        line = line.replace(b"\\ ", b" ")

        yield line


def match_pattern(path: bytes, pattern: byt