repo.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. """Repository management module for syncing library repositories."""
  2. from __future__ import annotations
  3. import logging
  4. import shutil
  5. import subprocess
  6. from pathlib import Path
  7. from rich.progress import SpinnerColumn, TextColumn
  8. from rich.table import Table
  9. from typer import Argument, Option, Typer
  10. from ..core.config import ConfigManager, LibraryConfig, normalize_git_url
  11. from ..core.display import DisplayManager, IconManager
  12. from ..core.exceptions import ConfigError
  13. logger = logging.getLogger(__name__)
  14. display = DisplayManager()
  15. app = Typer(help="Manage library repositories")
  16. def _run_git_command(args: list[str], cwd: Path | None = None) -> tuple[bool, str, str]:
  17. """Run a git command and return the result.
  18. Args:
  19. args: Git command arguments (without 'git' prefix)
  20. cwd: Working directory for the command
  21. Returns:
  22. Tuple of (success, stdout, stderr)
  23. """
  24. try:
  25. result = subprocess.run(
  26. ["git", *args],
  27. check=False,
  28. cwd=cwd,
  29. capture_output=True,
  30. text=True,
  31. timeout=300, # 5 minute timeout
  32. )
  33. return result.returncode == 0, result.stdout, result.stderr
  34. except subprocess.TimeoutExpired:
  35. return False, "", "Command timed out after 5 minutes"
  36. except FileNotFoundError:
  37. return False, "", "Git command not found. Please install git."
  38. except Exception as e:
  39. return False, "", str(e)
  40. def _clone_or_pull_repo(
  41. name: str,
  42. url: str,
  43. target_path: Path,
  44. branch: str | None = None,
  45. sparse_dir: str | None = None,
  46. ) -> tuple[bool, str]:
  47. """Clone or pull a git repository with optional sparse-checkout.
  48. Args:
  49. name: Library name
  50. url: Git repository URL
  51. target_path: Target directory for the repository
  52. branch: Git branch to clone/pull (optional)
  53. sparse_dir: Directory to sparse-checkout (optional, use None or "." for full clone)
  54. Returns:
  55. Tuple of (success, message)
  56. """
  57. if target_path.exists() and (target_path / ".git").exists():
  58. remote_url = _get_repo_remote_url(target_path)
  59. if not remote_url:
  60. return _replace_repo_checkout(
  61. name,
  62. url,
  63. target_path,
  64. branch,
  65. sparse_dir,
  66. reason="existing checkout has no readable origin remote",
  67. )
  68. if normalize_git_url(remote_url) != normalize_git_url(url):
  69. return _replace_repo_checkout(
  70. name,
  71. url,
  72. target_path,
  73. branch,
  74. sparse_dir,
  75. reason=f"configured remote changed from {remote_url} to {url}",
  76. )
  77. success, message = _pull_repo_updates(name, target_path, branch)
  78. if success:
  79. return success, message
  80. if _is_recoverable_pull_failure(message):
  81. return _replace_repo_checkout(
  82. name,
  83. url,
  84. target_path,
  85. branch,
  86. sparse_dir,
  87. reason="managed checkout diverged from origin",
  88. )
  89. return success, message
  90. return _clone_new_repo(name, url, target_path, branch, sparse_dir)
  91. def _get_repo_remote_url(target_path: Path) -> str | None:
  92. """Return the current origin remote URL for an existing checkout."""
  93. success, stdout, stderr = _run_git_command(["remote", "get-url", "origin"], cwd=target_path)
  94. if not success:
  95. logger.warning("Failed to read origin remote for '%s': %s", target_path, stderr or stdout)
  96. return None
  97. return stdout.strip() or None
  98. def _is_recoverable_pull_failure(message: str) -> bool:
  99. """Identify pull failures that can be fixed by replacing the managed checkout."""
  100. recoverable_markers = (
  101. "Not possible to fast-forward",
  102. "Diverging branches can't be fast-forwarded",
  103. "refusing to merge unrelated histories",
  104. "fatal: bad object",
  105. "couldn't find remote ref",
  106. )
  107. return any(marker in message for marker in recoverable_markers)
  108. def _replace_repo_checkout(
  109. name: str,
  110. url: str,
  111. target_path: Path,
  112. branch: str | None,
  113. sparse_dir: str | None,
  114. *,
  115. reason: str,
  116. ) -> tuple[bool, str]:
  117. """Replace a managed checkout with a clean clone from the configured remote."""
  118. logger.warning("Replacing managed library checkout '%s' at %s: %s", name, target_path, reason)
  119. try:
  120. shutil.rmtree(target_path)
  121. except OSError as exc:
  122. logger.error("Failed to remove managed library checkout '%s': %s", name, exc)
  123. return False, f"{reason}; failed to remove old checkout: {exc}"
  124. success, message = _clone_new_repo(name, url, target_path, branch, sparse_dir)
  125. if not success:
  126. return False, f"{reason}; {message}"
  127. return True, f"Re-cloned successfully after {reason}"
  128. def _pull_repo_updates(name: str, target_path: Path, branch: str | None) -> tuple[bool, str]:
  129. """Pull updates for an existing repository."""
  130. logger.debug(f"Pulling updates for library '{name}' at {target_path}")
  131. pull_branch = branch if branch else "main"
  132. success, stdout, stderr = _run_git_command(["pull", "--ff-only", "origin", pull_branch], cwd=target_path)
  133. if not success:
  134. error_msg = stderr or stdout
  135. logger.error(f"Failed to pull library '{name}': {error_msg}")
  136. return False, f"Pull failed: {error_msg}"
  137. if "Already up to date" in stdout or "Already up-to-date" in stdout:
  138. return True, "Already up to date"
  139. return True, "Updated successfully"
  140. def _clone_new_repo(
  141. name: str, url: str, target_path: Path, branch: str | None, sparse_dir: str | None
  142. ) -> tuple[bool, str]:
  143. """Clone a new repository, optionally with sparse-checkout."""
  144. logger.debug(f"Cloning library '{name}' from {url} to {target_path}")
  145. target_path.parent.mkdir(parents=True, exist_ok=True)
  146. use_sparse = sparse_dir and sparse_dir != "."
  147. if use_sparse:
  148. return _clone_sparse_repo(url, target_path, branch, sparse_dir)
  149. return _clone_full_repo(name, url, target_path, branch)
  150. def _clone_sparse_repo(url: str, target_path: Path, branch: str | None, sparse_dir: str) -> tuple[bool, str]:
  151. """Clone repository with sparse-checkout."""
  152. logger.debug(f"Using sparse-checkout for directory: {sparse_dir}")
  153. target_path.mkdir(parents=True, exist_ok=True)
  154. # Define git operations to perform sequentially
  155. operations = [
  156. (["init"], "Failed to initialize repo"),
  157. (["remote", "add", "origin", url], "Failed to add remote"),
  158. (["sparse-checkout", "init", "--no-cone"], "Failed to enable sparse-checkout"),
  159. (
  160. ["sparse-checkout", "set", f"{sparse_dir}/*"],
  161. "Failed to set sparse-checkout directory",
  162. ),
  163. ]
  164. # Execute initial operations
  165. for cmd, error_msg in operations:
  166. success, stdout, stderr = _run_git_command(cmd, cwd=target_path)
  167. if not success:
  168. return False, f"{error_msg}: {stderr or stdout}"
  169. # Fetch and checkout
  170. fetch_branch = branch if branch else "main"
  171. success, stdout, stderr = _run_git_command(["fetch", "--depth", "1", "origin", fetch_branch], cwd=target_path)
  172. if not success:
  173. return False, f"Fetch failed: {stderr or stdout}"
  174. success, stdout, stderr = _run_git_command(["checkout", fetch_branch], cwd=target_path)
  175. result_success = success
  176. result_msg = "Cloned successfully (sparse)" if success else f"Checkout failed: {stderr or stdout}"
  177. return result_success, result_msg
  178. def _clone_full_repo(name: str, url: str, target_path: Path, branch: str | None) -> tuple[bool, str]:
  179. """Clone full repository."""
  180. clone_args = ["clone", "--depth", "1"]
  181. if branch:
  182. clone_args.extend(["--branch", branch])
  183. clone_args.extend([url, str(target_path)])
  184. success, stdout, stderr = _run_git_command(clone_args)
  185. if success:
  186. return True, "Cloned successfully"
  187. error_msg = stderr or stdout
  188. logger.error(f"Failed to clone library '{name}': {error_msg}")
  189. return False, f"Clone failed: {error_msg}"
  190. def _process_library_update(lib: dict, libraries_path: Path, progress, verbose: bool) -> tuple[str, str, bool]:
  191. """Process a single library update and return result."""
  192. name = lib.get("name")
  193. lib_type = lib.get("type", "git")
  194. enabled = lib.get("enabled", True)
  195. if not enabled:
  196. if verbose:
  197. display.text(f"Skipping disabled library: {name}", style="dim")
  198. return (name, "Skipped (disabled)", False)
  199. if lib_type == "static":
  200. if verbose:
  201. display.text(f"Skipping static library: {name} (no sync needed)", style="dim")
  202. return (name, "N/A (static)", True)
  203. # Handle git libraries
  204. url = lib.get("url")
  205. branch = lib.get("branch")
  206. directory = lib.get("directory", "library")
  207. task = progress.add_task(f"Updating {name}...", total=None)
  208. target_path = libraries_path / name
  209. success, message = _clone_or_pull_repo(name, url, target_path, branch, directory)
  210. progress.remove_task(task)
  211. if verbose:
  212. if success:
  213. display.success(f"{name}: {message}")
  214. else:
  215. display.error(f"{name}: {message}")
  216. return (name, message, success)
  217. def _display_update_summary(results: list[tuple[str, str, bool]]) -> None:
  218. """Display update summary."""
  219. total = len(results)
  220. successful = sum(1 for _, _, success in results if success)
  221. display.text("")
  222. if successful == total:
  223. display.text(f"All libraries updated successfully ({successful}/{total})", style="green")
  224. elif successful > 0:
  225. display.text(
  226. f"Partially successful: {successful}/{total} libraries updated",
  227. style="yellow",
  228. )
  229. else:
  230. display.text("Failed to update libraries", style="red")
  231. @app.command()
  232. def update(
  233. library_name: str | None = Argument(None, help="Name of specific library to update (updates all if not specified)"),
  234. verbose: bool = Option(False, "--verbose", "-v", help="Show detailed output"),
  235. ) -> None:
  236. """Update library repositories by cloning or pulling from git.
  237. This command syncs all configured libraries from their git repositories.
  238. If a library doesn't exist locally, it will be cloned. If it exists, it will be pulled.
  239. """
  240. config = ConfigManager()
  241. libraries = config.get_libraries()
  242. if not libraries:
  243. display.warning("No libraries configured")
  244. display.text("Libraries are auto-configured on first run with a default library.")
  245. return
  246. # Filter to specific library if requested
  247. if library_name:
  248. libraries = [lib for lib in libraries if lib.get("name") == library_name]
  249. if not libraries:
  250. display.error(f"Library '{library_name}' not found in configuration")
  251. return
  252. libraries_path = config.get_libraries_path()
  253. results = []
  254. with display.progress(SpinnerColumn(), TextColumn("[progress.description]{task.description}")) as progress:
  255. for lib in libraries:
  256. result = _process_library_update(lib, libraries_path, progress, verbose)
  257. results.append(result)
  258. # Display summary table
  259. if not verbose:
  260. display.display_status_table("Library Update Summary", results, columns=("Library", "Status"))
  261. _display_update_summary(results)
  262. def _get_library_path_for_git(lib: dict, libraries_path: Path, name: str) -> Path:
  263. """Get library path for git library type."""
  264. directory = lib.get("directory", "library")
  265. library_base = libraries_path / name
  266. if directory and directory != ".":
  267. configured_path = library_base / directory
  268. fallback_path = _fallback_template_root(configured_path)
  269. return fallback_path or configured_path
  270. return library_base
  271. def _get_library_path_for_static(lib: dict, config: ConfigManager) -> Path:
  272. """Get library path for static library type."""
  273. url_or_path = lib.get("path", "")
  274. library_path = Path(url_or_path).expanduser()
  275. if not library_path.is_absolute():
  276. library_path = (config.config_path.parent / library_path).resolve()
  277. fallback_path = _fallback_template_root(library_path)
  278. return fallback_path or library_path
  279. def _looks_like_template_root(path: Path) -> bool:
  280. """Check whether a path looks like the root of a templates repository."""
  281. if not path.is_dir():
  282. return False
  283. try:
  284. return any(item.is_dir() for item in path.iterdir())
  285. except OSError:
  286. return False
  287. def _fallback_template_root(path: Path) -> Path | None:
  288. """Resolve old-style /library paths to the actual template repo root."""
  289. if path.exists():
  290. if _looks_like_template_root(path):
  291. return path
  292. if path.name == "library" and _looks_like_template_root(path.parent):
  293. return path.parent
  294. return None
  295. if path.name == "library" and _looks_like_template_root(path.parent):
  296. return path.parent
  297. return None
  298. def _get_library_info(lib: dict, config: ConfigManager, libraries_path: Path) -> tuple[str, str, str, str, str, str]:
  299. """Extract library information based on type."""
  300. name = lib.get("name", "")
  301. lib_type = lib.get("type", "git")
  302. enabled = lib.get("enabled", True)
  303. if lib_type == "git":
  304. url_or_path = lib.get("url", "")
  305. branch = lib.get("branch", "main")
  306. directory = lib.get("directory", "library")
  307. library_path = _get_library_path_for_git(lib, libraries_path, name)
  308. exists = library_path.exists()
  309. type_icon = IconManager.UI_LIBRARY_GIT
  310. elif lib_type == "static":
  311. url_or_path = lib.get("path", "")
  312. branch = "-"
  313. directory = "-"
  314. library_path = _get_library_path_for_static(lib, config)
  315. exists = library_path.exists()
  316. type_icon = IconManager.UI_LIBRARY_STATIC
  317. else:
  318. # Unknown type
  319. url_or_path = "<unknown type>"
  320. branch = "-"
  321. directory = "-"
  322. exists = False
  323. type_icon = "?"
  324. # Build status string
  325. status_parts = []
  326. if not enabled:
  327. status_parts.append("[dim]disabled[/dim]")
  328. elif exists:
  329. status_parts.append("[green]available[/green]")
  330. else:
  331. status_parts.append("[yellow]not found[/yellow]")
  332. status = " ".join(status_parts)
  333. type_display = f"{type_icon} {lib_type}"
  334. return url_or_path, branch, directory, type_display, type_icon, status
  335. @app.command()
  336. def list() -> None:
  337. """List all configured libraries."""
  338. config = ConfigManager()
  339. libraries = config.get_libraries()
  340. if not libraries:
  341. display.text("No libraries configured.", style="yellow")
  342. return
  343. settings = display.settings
  344. table = Table(
  345. title="Configured Libraries",
  346. show_header=True,
  347. header_style=settings.STYLE_TABLE_HEADER,
  348. )
  349. table.add_column("Name", style="cyan", no_wrap=True)
  350. table.add_column("URL/Path", style="blue")
  351. table.add_column("Branch", style="yellow")
  352. table.add_column("Directory", style="magenta")
  353. table.add_column("Type", style="cyan")
  354. table.add_column("Status", style="green")
  355. libraries_path = config.get_libraries_path()
  356. for lib in libraries:
  357. name = lib.get("name", "")
  358. url_or_path, branch, directory, type_display, _type_icon, status = _get_library_info(
  359. lib, config, libraries_path
  360. )
  361. table.add_row(name, url_or_path, branch, directory, type_display, status)
  362. display.print_table(table)
  363. @app.command()
  364. def add(
  365. name: str = Argument(..., help="Unique name for the library"),
  366. *,
  367. library_type: str | None = None,
  368. url: str | None = None,
  369. branch: str = "main",
  370. directory: str = "library",
  371. path: str | None = None,
  372. enabled: bool = Option(True, "--enabled/--disabled", help="Enable or disable the library"),
  373. sync: bool = Option(True, "--sync/--no-sync", help="Sync after adding (git only)"),
  374. ) -> None:
  375. """Add a new library to the configuration.
  376. Examples:
  377. # Add a git library
  378. repo add mylib --type git --url https://github.com/user/templates.git
  379. # Add a static library
  380. repo add local --type static --path ~/my-templates
  381. """
  382. config = ConfigManager()
  383. try:
  384. if library_type == "git":
  385. if not url:
  386. display.error("--url is required for git libraries")
  387. return
  388. lib_config = LibraryConfig(
  389. name=name,
  390. library_type="git",
  391. url=url,
  392. branch=branch,
  393. directory=directory,
  394. enabled=enabled,
  395. )
  396. elif library_type == "static":
  397. if not path:
  398. display.error("--path is required for static libraries")
  399. return
  400. lib_config = LibraryConfig(
  401. name=name,
  402. library_type="static",
  403. path=path,
  404. enabled=enabled,
  405. )
  406. else:
  407. display.error(f"Invalid library type: {library_type}. Must be 'git' or 'static'.")
  408. return
  409. config.add_library(lib_config)
  410. display.success(f"Added {library_type} library '{name}'")
  411. if library_type == "git" and sync and enabled:
  412. display.text(f"\nSyncing library '{name}'...")
  413. update(library_name=name, verbose=True)
  414. elif library_type == "static":
  415. display.info(f"Static library points to: {path}")
  416. except ConfigError as e:
  417. display.error(str(e))
  418. @app.command()
  419. def remove(
  420. name: str = Argument(..., help="Name of the library to remove"),
  421. keep_files: bool = Option(False, "--keep-files", help="Keep the local library files (don't delete)"),
  422. ) -> None:
  423. """Remove a library from the configuration and delete its local files."""
  424. config = ConfigManager()
  425. try:
  426. # Remove from config
  427. config.remove_library(name)
  428. display.success(f"Removed library '{name}' from configuration")
  429. # Delete local files unless --keep-files is specified
  430. if not keep_files:
  431. libraries_path = config.get_libraries_path()
  432. library_path = libraries_path / name
  433. if library_path.exists():
  434. shutil.rmtree(library_path)
  435. display.success(f"Deleted local files at {library_path}")
  436. else:
  437. display.info(f"No local files found at {library_path}")
  438. except ConfigError as e:
  439. display.error(str(e))
  440. # Register the repo command with the CLI
  441. def register_cli(parent_app: Typer) -> None:
  442. """Register the repo command with the parent Typer app."""
  443. parent_app.add_typer(app, name="repo", rich_help_panel="Configuration Commands")