Skip to content

Commit

Permalink
refractor
Browse files Browse the repository at this point in the history
  • Loading branch information
bitranox committed Sep 20, 2020
1 parent 897d28c commit bf6ff61
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions igittigitt/igittigitt.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ def parse_rule_files(
self._parse_rule_file(rule_file)

def _parse_rule_file(
self, rule_file: PathLikeOrString, base_dir: Optional[PathLikeOrString] = None,
self,
rule_file: PathLikeOrString,
base_dir: Optional[PathLikeOrString] = None,
) -> None:
"""
parse a git ignore file, create rules from a gitignore file
Expand Down Expand Up @@ -354,8 +356,8 @@ def get_rules_from_git_pattern(
>>> # level of the particular .gitignore file itself.
>>> # Otherwise the pattern may also match at any level
>>> # below the .gitignore level.
>>> assert not match_also_sub_directories('/some/thing/')
>>> assert match_also_sub_directories('something/')
>>> assert get_match_anchored('/some/thing/')
>>> assert not get_match_anchored('something/')
>>> # test match at any level (no leading /)
>>> get_rules_from_git_pattern(git_pattern='test', path_base_dir=pathlib.Path('/base_dir/'))
Expand Down Expand Up @@ -400,11 +402,11 @@ def get_rules_from_git_pattern(
match_file = False

git_pattern = git_pattern.rstrip("/")
match_also_subdirs = match_also_sub_directories(git_pattern)
match_anchored = get_match_anchored(git_pattern)
git_pattern = git_pattern.lstrip("/")

if git_pattern.startswith("**/"):
match_also_subdirs = True
match_anchored = False
git_pattern = git_pattern[3:]

if git_pattern.endswith("/**"):
Expand All @@ -418,7 +420,7 @@ def get_rules_from_git_pattern(
path_base_dir=path_base_dir,
match_file=match_file,
match_dirs_and_content=match_dirs_and_content,
match_also_subdirs=match_also_subdirs,
match_anchored=match_anchored,
is_negation_rule=is_negation_rule,
source_file=path_source_file,
source_line_number=source_line_number,
Expand Down Expand Up @@ -446,7 +448,7 @@ def git_pattern_handle_blanks(git_pattern: str) -> str:
return "\\ ".join(parts)


def match_also_sub_directories(git_pattern: str) -> bool:
def get_match_anchored(git_pattern: str) -> bool:
"""
is the pattern relative to the ignore file base directory
Expand All @@ -459,15 +461,15 @@ def match_also_sub_directories(git_pattern: str) -> bool:
Otherwise the pattern may also match at any level
below the .gitignore level.
>>> assert match_also_sub_directories('')
>>> assert match_also_sub_directories('something')
>>> assert not match_also_sub_directories('some/thing')
>>> assert not match_also_sub_directories('/some/thing')
>>> assert not match_also_sub_directories('/some/thing/')
>>> assert match_also_sub_directories('something/')
>>> assert not get_match_anchored('')
>>> assert not get_match_anchored('something')
>>> assert get_match_anchored('some/thing')
>>> assert get_match_anchored('/some/thing')
>>> assert get_match_anchored('/some/thing/')
>>> assert not get_match_anchored('something/')
"""
return "/" not in git_pattern.rstrip("/")
return "/" in git_pattern.rstrip("/")


def get_match_files(git_pattern: str) -> bool:
Expand All @@ -490,7 +492,7 @@ def create_rule_variations(
path_base_dir: pathlib.Path,
match_file: bool,
match_dirs_and_content: bool,
match_also_subdirs: bool,
match_anchored: bool,
is_negation_rule: bool,
source_file: Optional[pathlib.Path],
source_line_number: Optional[int],
Expand All @@ -504,10 +506,10 @@ def create_rule_variations(
str_path_base_dir = str(path_base_dir).replace("\\", "/")
l_rules: List[IgnoreRule] = list()

if match_also_subdirs:
pattern_resolved = str_path_base_dir + "/**/" + pattern
else:
if match_anchored:
pattern_resolved = str_path_base_dir + "/" + pattern
else:
pattern_resolved = str_path_base_dir + "/**/" + pattern

# match the pattern, .../.../pattern
# if match_file = True, it will also match on Files, otherwise only on directories
Expand Down

0 comments on commit bf6ff61

Please sign in to comment.