ignore.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # Copyright (C) 2017 Jelmer Vernooij <jelmer@jelmer.uk>
  2. #
  3. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  4. # General Public License as public by the Free Software Foundation; version 2.0
  5. # or (at your option) any later version. You can redistribute it and/or
  6. # modify it under the terms of either of these two licenses.
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.
  13. #
  14. # You should have received a copy of the licenses; if not, see
  15. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  16. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  17. # License, Version 2.0.
  18. #
  19. """Parsing of gitignore files.
  20. For details for the matching rules, see https://git-scm.com/docs/gitignore
  21. """
  22. import re
  23. def translate(pat):
  24. """Translate a shell PATTERN to a regular expression.
  25. There is no way to quote meta-characters.
  26. Originally copied from fnmatch in Python 2.7, but modified for Dulwich
  27. to cope with features in Git ignore patterns.
  28. """
  29. res = b'(?ms)'
  30. if b'/' not in pat:
  31. # If there's no slash, this is a filename-based match
  32. res = res + b'(.*\/)?'
  33. if pat.startswith(b'**/'):
  34. # Leading **/
  35. pat = pat[2:]
  36. res = res + b'(.*\/)?'
  37. if pat.startswith(b'/'):
  38. pat = pat[1:]
  39. i, n = 0, len(pat)
  40. while i < n:
  41. if pat[i:i+3] == b'/**':
  42. res = res + b'(\\/.*)?'
  43. i = i+3
  44. continue
  45. c = pat[i:i+1]
  46. i = i+1
  47. if c == b'*':
  48. res = res + b'[^\/]+'
  49. elif c == b'?':
  50. res = res + b'.'
  51. elif c == b'[':
  52. j = i
  53. if j < n and pat[j:j+1] == b'!':
  54. j = j+1
  55. if j < n and pat[j:j+1] == b']':
  56. j = j+1
  57. while j < n and pat[j:j+1] != b']':
  58. j = j+1
  59. if j >= n:
  60. res = res + b'\\['
  61. else:
  62. stuff = pat[i:j].replace(b'\\', b'\\\\')
  63. i = j+1
  64. if stuff.startswith(b'!'):
  65. stuff = b'^' + stuff[1:]
  66. elif stuff.startswith(b'^'):
  67. stuff = b'\\' + stuff
  68. res = res + b'[' + stuff + b']'
  69. else:
  70. res = res + re.escape(c)
  71. return res + b'\Z'
  72. def read_ignore_patterns(f):
  73. """Read a git ignore file.
  74. :param f: File-like object to read from
  75. :return: List of patterns
  76. """
  77. for l in f:
  78. l = l.rstrip(b"\n")
  79. # Ignore blank lines, they're used for readability.
  80. if not l:
  81. continue
  82. if l.startswith(b'#'):
  83. # Comment
  84. continue
  85. # Trailing spaces are ignored unless they are quoted with a backslash.
  86. while l.endswith(b' ') and not l.endswith(b'\\ '):
  87. l = l[:-1]
  88. l = l.replace(b'\\ ', b' ')
  89. yield l
  90. def match_pattern(path, pattern):
  91. """Match a gitignore-style pattern against a path.
  92. :param path: Path to match
  93. :param pattern: Pattern to match
  94. :return: bool indicating whether the pattern matched
  95. """
  96. re_pattern = translate(pattern)
  97. return re.match(re_pattern, path)
  98. class IgnoreFilter(object):
  99. def __init__(self, patterns):
  100. self._patterns = []
  101. for pattern in patterns:
  102. self.append_pattern(pattern)
  103. def append_pattern(self, pattern):
  104. """Add a pattern to the set."""
  105. self._patterns.append(pattern)
  106. def is_ignored(self, path):
  107. """Check whether a path is ignored.
  108. For directories, include a trailing slash.
  109. :return: None if file is not mentioned, True if it is included, False
  110. if it is explicitly excluded.
  111. """
  112. status = None
  113. for pattern in self._patterns:
  114. if pattern[0:1] == b'!':
  115. if match_pattern(pattern[1:], path):
  116. # Explicitly excluded.
  117. return False
  118. else:
  119. if pattern[0:1] == b'\\':
  120. pattern = pattern[1:]
  121. if match_pattern(pattern, path):
  122. status = True
  123. return status
  124. class IgnoreFilterStack(object):
  125. """Check for ignore status in multiple filters."""
  126. def __init__(self, filters):
  127. self._filters = filters
  128. def is_ignored(self, path):
  129. """Check whether a path is explicitly included or excluded in ignores.
  130. :param path: Path to check
  131. :return: None if the file is not mentioned, True if it is included,
  132. False if it is explicitly excluded.
  133. """
  134. status = None
  135. for filter in self._filters:
  136. status = filter.is_ignored(path)
  137. if status is not None:
  138. return status
  139. return status