ignore.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 os.path
  23. import re
  24. import sys
  25. def translate(pat):
  26. """Translate a shell PATTERN to a regular expression.
  27. There is no way to quote meta-characters.
  28. Originally copied from fnmatch in Python 2.7, but modified for Dulwich
  29. to cope with features in Git ignore patterns.
  30. """
  31. res = b'(?ms)'
  32. if b'/' not in pat[:-1]:
  33. # If there's no slash, this is a filename-based match
  34. res = res + b'(.*/)?'
  35. if pat.startswith(b'**/'):
  36. # Leading **/
  37. pat = pat[2:]
  38. res = res + b'(.*/)?'
  39. if pat.startswith(b'/'):
  40. pat = pat[1:]
  41. i, n = 0, len(pat)
  42. while i < n:
  43. if pat[i:i+3] == b'/**':
  44. res = res + b'(/.*)?'
  45. i = i+3
  46. continue
  47. c = pat[i:i+1]
  48. i = i+1
  49. if c == b'*':
  50. res = res + b'[^/]+'
  51. elif c == b'?':
  52. res = res + b'.'
  53. elif c == b'[':
  54. j = i
  55. if j < n and pat[j:j+1] == b'!':
  56. j = j+1
  57. if j < n and pat[j:j+1] == b']':
  58. j = j+1
  59. while j < n and pat[j:j+1] != b']':
  60. j = j+1
  61. if j >= n:
  62. res = res + b'\\['
  63. else:
  64. stuff = pat[i:j].replace(b'\\', b'\\\\')
  65. i = j+1
  66. if stuff.startswith(b'!'):
  67. stuff = b'^' + stuff[1:]
  68. elif stuff.startswith(b'^'):
  69. stuff = b'\\' + stuff
  70. res = res + b'[' + stuff + b']'
  71. else:
  72. res = res + re.escape(c)
  73. if not res.endswith(b'/'):
  74. res = res + b'/?'
  75. return res + b'\Z'
  76. def read_ignore_patterns(f):
  77. """Read a git ignore file.
  78. :param f: File-like object to read from
  79. :return: List of patterns
  80. """
  81. for l in f:
  82. l = l.rstrip(b"\n")
  83. # Ignore blank lines, they're used for readability.
  84. if not l:
  85. continue
  86. if l.startswith(b'#'):
  87. # Comment
  88. continue
  89. # Trailing spaces are ignored unless they are quoted with a backslash.
  90. while l.endswith(b' ') and not l.endswith(b'\\ '):
  91. l = l[:-1]
  92. l = l.replace(b'\\ ', b' ')
  93. yield l
  94. def match_pattern(path, pattern):
  95. """Match a gitignore-style pattern against a path.
  96. :param path: Path to match
  97. :param pattern: Pattern to match
  98. :return: bool indicating whether the pattern matched
  99. """
  100. re_pattern = translate(pattern)
  101. return re.match(re_pattern, path)
  102. class IgnoreFilter(object):
  103. def __init__(self, patterns):
  104. self._patterns = []
  105. for pattern in patterns:
  106. self.append_pattern(pattern)
  107. def append_pattern(self, pattern):
  108. """Add a pattern to the set."""
  109. self._patterns.append(pattern)
  110. def is_ignored(self, path):
  111. """Check whether a path is ignored.
  112. For directories, include a trailing slash.
  113. :return: None if file is not mentioned, True if it is included, False
  114. if it is explicitly excluded.
  115. """
  116. if not isinstance(path, bytes):
  117. path = path.encode(sys.getfilesystemencoding())
  118. status = None
  119. for pattern in self._patterns:
  120. if pattern[0:1] == b'!':
  121. if match_pattern(path, pattern[1:]):
  122. status = False
  123. else:
  124. if pattern[0:1] == b'\\':
  125. pattern = pattern[1:]
  126. if match_pattern(path, pattern):
  127. status = True
  128. return status
  129. @classmethod
  130. def from_path(cls, path):
  131. with open(path, 'rb') as f:
  132. ret = cls(read_ignore_patterns(f))
  133. ret._path = path
  134. return ret
  135. def __repr__(self):
  136. if getattr(self, '_path', None) is None:
  137. return "<%s>" % (type(self).__name__)
  138. else:
  139. return "%s.from_path(%r)" % (type(self).__name__, self._path)
  140. class IgnoreFilterStack(object):
  141. """Check for ignore status in multiple filters."""
  142. def __init__(self, filters):
  143. self._filters = filters
  144. def is_ignored(self, path):
  145. """Check whether a path is explicitly included or excluded in ignores.
  146. :param path: Path to check
  147. :return: None if the file is not mentioned, True if it is included,
  148. False if it is explicitly excluded.
  149. """
  150. status = None
  151. for filter in self._filters:
  152. status = filter.is_ignored(path)
  153. if status is not None:
  154. return status
  155. return status
  156. def default_user_ignore_filter_path(config):
  157. """Return default user ignore filter path.
  158. :param config: A Config object
  159. :return: Path to a global ignore file
  160. """
  161. try:
  162. return config.get(('core', ), 'excludesFile')
  163. except KeyError:
  164. pass
  165. xdg_config_home = os.environ.get(
  166. "XDG_CONFIG_HOME", os.path.expanduser("~/.config/"),
  167. )
  168. return os.path.join(xdg_config_home, 'git', 'ignore')
  169. class IgnoreFilterManager(object):
  170. """Ignore file manager."""
  171. def __init__(self, top_path, global_filters):
  172. self._path_filters = {}
  173. self._top_path = top_path
  174. self._global_filters = global_filters
  175. def _load_path(self, path):
  176. try:
  177. return self._path_filters[path]
  178. except KeyError:
  179. pass
  180. p = os.path.join(path, '.gitignore')
  181. try:
  182. self._path_filters[path] = IgnoreFilter.from_path(p)
  183. except IOError:
  184. self._path_filters[path] = None
  185. return self._path_filters[path]
  186. def is_ignored(self, path):
  187. """Check whether a path is explicitly included or excluded in ignores.
  188. :param path: Path to check
  189. :return: None if the file is not mentioned, True if it is included,
  190. False if it is explicitly excluded.
  191. """
  192. dirname = path
  193. while dirname not in (self._top_path, '/'):
  194. dirname = os.path.dirname(dirname)
  195. ignore_filter = self._load_path(dirname)
  196. if ignore_filter is not None:
  197. relpath = os.path.relpath(path, dirname)
  198. status = ignore_filter.is_ignored(relpath)
  199. if status is not None:
  200. return status
  201. for ignore_filter in self._global_filters:
  202. relpath = os.path.relpath(path, dirname)
  203. status = ignore_filter.is_ignored(relpath)
  204. if status is not None:
  205. return status
  206. return None
  207. @classmethod
  208. def from_repo(cls, repo):
  209. global_filters = []
  210. for p in [
  211. os.path.join(repo.controldir(), 'info', 'exclude'),
  212. default_user_ignore_filter_path(repo.get_config_stack())]:
  213. try:
  214. global_filters.append(IgnoreFilter.from_path(p))
  215. except IOError:
  216. pass
  217. return cls(repo.path, global_filters)