-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fixed the issue which prevented snapping repositories without .giti…
…gnore file - Added possibility to snap subfloder of a repo instead of a whole repo
- Loading branch information
andrey.goloborodko
committed
Sep 16, 2024
1 parent
5cdf4ee
commit c4e1325
Showing
9 changed files
with
152 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,64 @@ | ||
# src/reposnap/core/collector.py | ||
|
||
from .file_system import FileSystem | ||
from .git_repo import GitRepo | ||
from .markdown_generator import MarkdownGenerator | ||
import pathspec | ||
import logging | ||
from pathlib import Path | ||
import pathspec | ||
from .git_repo import GitRepo | ||
from .file_system import FileSystem | ||
from .markdown_generator import MarkdownGenerator | ||
|
||
|
||
class ProjectContentCollector: | ||
def __init__(self, root_dir: str, output_file: str, structure_only: bool, gitignore_patterns: list): | ||
self.logger = logging.getLogger(__name__) | ||
self.root_dir = Path(root_dir).resolve() | ||
self.output_file = Path(output_file).resolve() | ||
self.structure_only = structure_only | ||
self.gitignore_patterns = gitignore_patterns | ||
self.spec = pathspec.PathSpec.from_lines(pathspec.patterns.GitWildMatchPattern, gitignore_patterns) | ||
self.file_system = FileSystem(self.root_dir) | ||
|
||
# Initialize components | ||
self.git_repo = GitRepo(self.root_dir) | ||
self.file_system = FileSystem(self.root_dir) | ||
self.markdown_generator = MarkdownGenerator( | ||
root_dir=self.root_dir, | ||
output_file=self.output_file, | ||
structure_only=self.structure_only | ||
) | ||
self.logger = logging.getLogger(__name__) | ||
|
||
def collect_and_generate(self): | ||
self.logger.info("Starting project content collection.") | ||
# Collect files and build tree during initialization | ||
self.files = self.collect_files() | ||
self.tree_structure = self.build_tree_structure() | ||
|
||
def collect_files(self): | ||
""" | ||
Collects and filters files to be included in the documentation. | ||
""" | ||
self.logger.info("Collecting git files.") | ||
git_files = self.git_repo.get_git_files() | ||
tree_structure = self.file_system.build_tree_structure(git_files) | ||
self.markdown_generator.generate_markdown(tree_structure, git_files, self.spec) | ||
self.logger.debug(f"Git files before filtering: {git_files}") | ||
|
||
# Filter files based on .gitignore patterns | ||
filtered_files = [ | ||
f for f in git_files if not self.spec.match_file(str(f)) | ||
] | ||
self.logger.debug(f"Git files after filtering: {filtered_files}") | ||
|
||
return filtered_files # Paths relative to root_dir | ||
|
||
def build_tree_structure(self): | ||
""" | ||
Builds the tree structure from the collected files. | ||
""" | ||
self.logger.info("Building tree structure.") | ||
tree = self.file_system.build_tree_structure(self.files) | ||
self.logger.debug(f"Tree structure: {tree}") | ||
return tree | ||
|
||
def collect_and_generate(self): | ||
""" | ||
Initiates the markdown generation process. | ||
""" | ||
self.logger.info("Starting markdown generation.") | ||
self.markdown_generator.generate_markdown(self.tree_structure, self.files) | ||
self.logger.info("Markdown generation completed.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,30 @@ | ||
# src/reposnap/core/git_repo.py | ||
|
||
import logging | ||
from pathlib import Path | ||
from git import Repo, InvalidGitRepositoryError | ||
|
||
class GitRepo: | ||
def __init__(self, repo_path: str): | ||
self.repo_path = repo_path | ||
def __init__(self, repo_path: Path): | ||
self.repo_path = repo_path.resolve() | ||
self.logger = logging.getLogger(__name__) | ||
|
||
def get_git_files(self): | ||
try: | ||
repo = Repo(self.repo_path) | ||
files = repo.git.ls_files().splitlines() | ||
logging.debug(f"\n--- Retrieved Git Files from {repo.working_tree_dir} ---") | ||
for file in files: | ||
logging.debug(f" - {file}") | ||
logging.debug("--- End of Git Files ---\n") | ||
return files | ||
repo = Repo(self.repo_path, search_parent_directories=True) | ||
repo_root = Path(repo.working_tree_dir).resolve() | ||
git_files = repo.git.ls_files().splitlines() | ||
self.logger.debug(f"Git files from {repo_root}: {git_files}") | ||
git_files_relative = [] | ||
for f in git_files: | ||
absolute_path = (repo_root / f).resolve() | ||
try: | ||
relative_path = absolute_path.relative_to(self.repo_path) | ||
git_files_relative.append(relative_path) | ||
except ValueError: | ||
# Skip files not under root_dir | ||
continue | ||
return git_files_relative | ||
except InvalidGitRepositoryError: | ||
logging.debug(f"Invalid Git repository at: {self.repo_path}") | ||
self.logger.error(f"Invalid Git repository at: {self.repo_path}") | ||
return [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.