Initial upload: gitignore-cli-generator v1.0.0
Some checks failed
CI / test (3.10) (push) Has been cancelled
CI / test (3.11) (push) Has been cancelled
CI / test (3.12) (push) Has been cancelled
CI / test (3.9) (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / type-check (push) Has been cancelled
CI / build (push) Has been cancelled
Some checks failed
CI / test (3.10) (push) Has been cancelled
CI / test (3.11) (push) Has been cancelled
CI / test (3.12) (push) Has been cancelled
CI / test (3.9) (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / type-check (push) Has been cancelled
CI / build (push) Has been cancelled
This commit is contained in:
66
src/gitignore_cli/pattern_merger.py
Normal file
66
src/gitignore_cli/pattern_merger.py
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
"""Pattern merger module for combining and deduplicating .gitignore patterns."""
|
||||||
|
|
||||||
|
from typing import List, Set
|
||||||
|
|
||||||
|
|
||||||
|
class PatternMerger:
|
||||||
|
"""Merges multiple sets of patterns with deduplication."""
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self._seen_patterns: Set[str] = set()
|
||||||
|
|
||||||
|
def merge_patterns(self, patterns_list: List[str]) -> str:
|
||||||
|
"""Merge multiple pattern strings into a single .gitignore content."""
|
||||||
|
merged_lines: List[str] = []
|
||||||
|
self._seen_patterns.clear()
|
||||||
|
|
||||||
|
for patterns in patterns_list:
|
||||||
|
if patterns:
|
||||||
|
for line in patterns.splitlines():
|
||||||
|
normalized = self._normalize_pattern(line)
|
||||||
|
if normalized and normalized not in self._seen_patterns:
|
||||||
|
self._seen_patterns.add(normalized)
|
||||||
|
merged_lines.append(line)
|
||||||
|
|
||||||
|
return "\n".join(merged_lines) + "\n"
|
||||||
|
|
||||||
|
def _normalize_pattern(self, pattern: str) -> str:
|
||||||
|
"""Normalize a pattern for deduplication."""
|
||||||
|
stripped = pattern.strip()
|
||||||
|
if not stripped:
|
||||||
|
return ""
|
||||||
|
if stripped.startswith("#"):
|
||||||
|
return ""
|
||||||
|
if stripped.startswith("!"):
|
||||||
|
return stripped
|
||||||
|
return stripped
|
||||||
|
|
||||||
|
def deduplicate_patterns(self, patterns: str) -> str:
|
||||||
|
"""Remove duplicate patterns from a single pattern string."""
|
||||||
|
self._seen_patterns.clear()
|
||||||
|
unique_lines: List[str] = []
|
||||||
|
|
||||||
|
for line in patterns.splitlines():
|
||||||
|
normalized = self._normalize_pattern(line)
|
||||||
|
if normalized and normalized not in self._seen_patterns:
|
||||||
|
self._seen_patterns.add(normalized)
|
||||||
|
unique_lines.append(line)
|
||||||
|
|
||||||
|
return "\n".join(unique_lines) + "\n"
|
||||||
|
|
||||||
|
def filter_comments(self, patterns: str) -> str:
|
||||||
|
"""Filter out comment lines from patterns."""
|
||||||
|
lines = [line for line in patterns.splitlines() if not line.strip().startswith("#")]
|
||||||
|
return "\n".join(lines) + "\n"
|
||||||
|
|
||||||
|
|
||||||
|
def merge_templates(patterns_list: List[str]) -> str:
|
||||||
|
"""Convenience function to merge multiple pattern strings."""
|
||||||
|
merger = PatternMerger()
|
||||||
|
return merger.merge_patterns(patterns_list)
|
||||||
|
|
||||||
|
|
||||||
|
def deduplicate_patterns(patterns: str) -> str:
|
||||||
|
"""Convenience function to deduplicate patterns."""
|
||||||
|
merger = PatternMerger()
|
||||||
|
return merger.deduplicate_patterns(patterns)
|
||||||
Reference in New Issue
Block a user