Skip to content

Commit

Permalink
Added support for flattening directly from a git repo without needing…
Browse files Browse the repository at this point in the history
… a local clone first
  • Loading branch information
btfranklin committed Jun 29, 2024
1 parent b789484 commit 3462788
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
50 changes: 47 additions & 3 deletions src/coderoller/flatten_repo.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,58 @@
import os
import sys
import shutil
import tempfile
from git import Repo
from coderoller.source_repo_flattener import flatten_repo


def get_repo_name(input_path: str) -> str:
"""
Infer the repository name from the input path or URL.
Args:
input_path (str): The path or URL of the repository.
Returns:
str: The inferred repository name.
"""
if (
input_path.startswith("http://")
or input_path.startswith("https://")
or input_path.startswith("git@")
):
repo_name = os.path.basename(input_path).replace(".git", "")
else:
repo_name = os.path.basename(os.path.normpath(input_path))
return repo_name


def main():
if len(sys.argv) != 2:
print("Usage: coderoller-flatten-repo <root_folder>")
print("Usage: coderoller-flatten-repo <root_folder_or_git_url>")
sys.exit(1)

root_folder = sys.argv[1]
flatten_repo(root_folder)
input_path = sys.argv[1]
repo_name = get_repo_name(input_path)

# Check if the input is a Git URL
if (
input_path.startswith("http://")
or input_path.startswith("https://")
or input_path.startswith("git@")
):
# Clone the repository to a temporary directory
temp_dir = tempfile.mkdtemp()
try:
print(f"Cloning repository from {input_path} to {temp_dir}")
Repo.clone_from(input_path, temp_dir)
flatten_repo(temp_dir, repo_name=repo_name)
finally:
# Clean up the temporary directory
shutil.rmtree(temp_dir)
print(f"Deleted temporary directory {temp_dir}")
else:
flatten_repo(input_path, repo_name=repo_name)


if __name__ == "__main__":
Expand Down
8 changes: 6 additions & 2 deletions src/coderoller/source_repo_flattener.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@ def find_readme(root_folder: str) -> str:
return ""


def flatten_repo(root_folder: str, output_folder: str | None = None):
def flatten_repo(
root_folder: str, output_folder: str | None = None, repo_name: str | None = None
):
"""
Flatten the source repository into a single markdown file.
Args:
root_folder (str): The root folder of the repository.
output_folder (str | None): The folder to save the flattened file. Defaults to the current working directory.
repo_name (str | None): The name of the repository.
"""
repo_name = os.path.basename(os.path.normpath(root_folder))
if repo_name is None:
repo_name = os.path.basename(os.path.normpath(root_folder))
if output_folder is None:
output_folder = os.getcwd()
flattened_file_path = os.path.join(output_folder, f"{repo_name}.flat.md")
Expand Down

0 comments on commit 3462788

Please sign in to comment.