|
| 1 | +import os |
1 | 2 | import sys
|
| 3 | +import shutil |
| 4 | +import tempfile |
| 5 | +from git import Repo |
2 | 6 | from coderoller.source_repo_flattener import flatten_repo
|
3 | 7 |
|
4 | 8 |
|
| 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 | + |
5 | 30 | def main():
|
6 | 31 | if len(sys.argv) != 2:
|
7 |
| - print("Usage: coderoller-flatten-repo <root_folder>") |
| 32 | + print("Usage: coderoller-flatten-repo <root_folder_or_git_url>") |
8 | 33 | sys.exit(1)
|
9 | 34 |
|
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) |
12 | 56 |
|
13 | 57 |
|
14 | 58 | if __name__ == "__main__":
|
|
0 commit comments