Skip to content

Commit 3462788

Browse files
committed
Added support for flattening directly from a git repo without needing a local clone first
1 parent b789484 commit 3462788

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/coderoller/flatten_repo.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,58 @@
1+
import os
12
import sys
3+
import shutil
4+
import tempfile
5+
from git import Repo
26
from coderoller.source_repo_flattener import flatten_repo
37

48

9+
def get_repo_name(input_path: str) -> str:
10+
"""
11+
Infer the repository name from the input path or URL.
12+
13+
Args:
14+
input_path (str): The path or URL of the repository.
15+
16+
Returns:
17+
str: The inferred repository name.
18+
"""
19+
if (
20+
input_path.startswith("http://")
21+
or input_path.startswith("https://")
22+
or input_path.startswith("git@")
23+
):
24+
repo_name = os.path.basename(input_path).replace(".git", "")
25+
else:
26+
repo_name = os.path.basename(os.path.normpath(input_path))
27+
return repo_name
28+
29+
530
def main():
631
if len(sys.argv) != 2:
7-
print("Usage: coderoller-flatten-repo <root_folder>")
32+
print("Usage: coderoller-flatten-repo <root_folder_or_git_url>")
833
sys.exit(1)
934

10-
root_folder = sys.argv[1]
11-
flatten_repo(root_folder)
35+
input_path = sys.argv[1]
36+
repo_name = get_repo_name(input_path)
37+
38+
# Check if the input is a Git URL
39+
if (
40+
input_path.startswith("http://")
41+
or input_path.startswith("https://")
42+
or input_path.startswith("git@")
43+
):
44+
# Clone the repository to a temporary directory
45+
temp_dir = tempfile.mkdtemp()
46+
try:
47+
print(f"Cloning repository from {input_path} to {temp_dir}")
48+
Repo.clone_from(input_path, temp_dir)
49+
flatten_repo(temp_dir, repo_name=repo_name)
50+
finally:
51+
# Clean up the temporary directory
52+
shutil.rmtree(temp_dir)
53+
print(f"Deleted temporary directory {temp_dir}")
54+
else:
55+
flatten_repo(input_path, repo_name=repo_name)
1256

1357

1458
if __name__ == "__main__":

src/coderoller/source_repo_flattener.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,19 @@ def find_readme(root_folder: str) -> str:
4848
return ""
4949

5050

51-
def flatten_repo(root_folder: str, output_folder: str | None = None):
51+
def flatten_repo(
52+
root_folder: str, output_folder: str | None = None, repo_name: str | None = None
53+
):
5254
"""
5355
Flatten the source repository into a single markdown file.
5456
5557
Args:
5658
root_folder (str): The root folder of the repository.
5759
output_folder (str | None): The folder to save the flattened file. Defaults to the current working directory.
60+
repo_name (str | None): The name of the repository.
5861
"""
59-
repo_name = os.path.basename(os.path.normpath(root_folder))
62+
if repo_name is None:
63+
repo_name = os.path.basename(os.path.normpath(root_folder))
6064
if output_folder is None:
6165
output_folder = os.getcwd()
6266
flattened_file_path = os.path.join(output_folder, f"{repo_name}.flat.md")

0 commit comments

Comments
 (0)