def _add_entry_to_gitignore(self, entry, gitignore): entry = GitWildMatchPattern.escape(entry) with open(gitignore, "a+", encoding="utf-8") as fobj: fobj.seek(0, os.SEEK_END) if fobj.tell() == 0: # Empty file prefix = "" else: fobj.seek(fobj.tell() - 1, os.SEEK_SET) last = fobj.read(1) prefix = "" if last == "\n" else "\n" fobj.write("{}{}\n".format(prefix, entry))
def _add_entry_to_gitignore(self, entry, gitignore): entry = GitWildMatchPattern.escape(entry) with open(gitignore, "a+", encoding="utf-8") as fobj: unique_lines = set(fobj.readlines()) fobj.seek(0, os.SEEK_END) if fobj.tell() == 0: # Empty file prefix = "" else: fobj.seek(fobj.tell() - 1, os.SEEK_SET) last = fobj.read(1) prefix = "" if last == "\n" else "\n" new_entry = f"{prefix}{entry}\n" if new_entry not in unique_lines: fobj.write(new_entry)
def compile(pattern: str, _, definition_file: Path) -> Optional[_IgnoreRule]: """Build an ignore rule from the supplied glob pattern and log a useful warning if it is invalid""" relative_to: Optional[Path] = None if pattern.strip() == "/": # "/" doesn't match anything in gitignore log.warning("Ignoring no-op glob pattern '/' from %s", definition_file) return None if pattern.startswith("/") or "/" in pattern.rstrip("/"): # See https://git-scm.com/docs/gitignore # > If there is a separator at the beginning or middle (or both) of the pattern, then the # > pattern is relative to the directory level of the particular .gitignore file itself. # > Otherwise the pattern may also match at any level below the .gitignore level. relative_to = definition_file.parent ignore_pattern = GitWildMatchPattern(pattern) return _GlobIgnoreRule(ignore_pattern.regex, pattern, ignore_pattern.include, relative_to)
def __init__(self, pattern_list, dirname): if pattern_list: if isinstance(pattern_list[0], str): pattern_list = [ PatternInfo(pattern, "") for pattern in pattern_list ] self.pattern_list = pattern_list self.dirname = dirname self.prefix = self.dirname + os.sep self.regex_pattern_list = [ GitWildMatchPattern.pattern_to_regex(pattern_info.patterns) for pattern_info in pattern_list ] self.ignore_spec = [(ignore, re.compile("|".join(item[0] for item in group))) for ignore, group in groupby( self.regex_pattern_list, lambda x: x[1]) if ignore is not None]