AzLearn

تمرين إعادة البناء

مخطط استنساخ مستودعات

اقرأ روابط مستودعات وأنشئ خطة أوامر git clone بدون تنفيذها.

python ~15 دقيقة متوسط
أعد بناء الكود Rebuild

هذا هو الكود. اكتبه بنفسك.

الكود المرجعي
import argparse
import shlex
from pathlib import Path
from urllib.parse import urlparse


def valid_repo_url(url: str) -> bool:
    parsed = urlparse(url)
    return parsed.scheme in {"http", "https", "ssh"} or url.startswith("git@")


def folder_name(url: str) -> str:
    cleaned = url.rstrip("/").removesuffix(".git")
    return cleaned.rsplit("/", 1)[-1].replace(":", "-")


def main() -> None:
    parser = argparse.ArgumentParser(description="Print git clone commands from a repo list without running them.")
    parser.add_argument("repo_list", type=Path)
    parser.add_argument("--dest", type=Path, default=Path("repos"))
    args = parser.parse_args()

    for line in args.repo_list.read_text(encoding="utf-8").splitlines():
        url = line.strip()
        if not url or url.startswith("#"):
            continue
        if not valid_repo_url(url):
            print(f"Skip invalid URL: {url}")
            continue
        target = args.dest / folder_name(url)
        print(shlex.join(["git", "clone", url, str(target)]))


if __name__ == "__main__":
    main()
اكتب هنا