xcad 4 månader sedan
förälder
incheckning
1961c2edcc
3 ändrade filer med 49 tillägg och 42 borttagningar
  1. 8 4
      cli/core/library.py
  2. 40 37
      cli/core/repo.py
  3. 1 1
      library/compose/traefik/template.yaml

+ 8 - 4
cli/core/library.py

@@ -145,11 +145,15 @@ class LibraryManager:
         continue
       
       name = lib_config.get("name")
+      directory = lib_config.get("directory", ".")
       
-      # Build path to library: ~/.config/boilerplates/libraries/{name}/
-      # The 'directory' config is just metadata about the repo structure,
-      # the actual cloned repo is always at libraries/{name}/
-      library_path = libraries_path / name
+      # Build path to library: ~/.config/boilerplates/libraries/{name}/{directory}/
+      # For sparse-checkout, files remain in the specified directory
+      library_base = libraries_path / name
+      if directory and directory != ".":
+        library_path = library_base / directory
+      else:
+        library_path = library_base
       
       # Check if library path exists
       if not library_path.exists():

+ 40 - 37
cli/core/repo.py

@@ -66,13 +66,14 @@ def _clone_or_pull_repo(name: str, url: str, target_path: Path, branch: Optional
         # Repository exists, pull updates
         logger.debug(f"Pulling updates for library '{name}' at {target_path}")
         
-        # If branch is specified, checkout the branch first
-        if branch:
-            success, stdout, stderr = _run_git_command(["checkout", branch], cwd=target_path)
-            if not success:
-                logger.warning(f"Failed to checkout branch '{branch}' for library '{name}': {stderr}")
+        # Determine which branch to pull
+        pull_branch = branch if branch else "main"
         
-        success, stdout, stderr = _run_git_command(["pull", "--ff-only"], cwd=target_path)
+        # Pull updates from specific branch
+        success, stdout, stderr = _run_git_command(
+            ["pull", "--ff-only", "origin", pull_branch],
+            cwd=target_path
+        )
         
         if success:
             # Check if anything was updated
@@ -114,45 +115,43 @@ def _clone_or_pull_repo(name: str, url: str, target_path: Path, branch: Optional
                 if not success:
                     return False, f"Failed to add remote: {stderr or stdout}"
                 
-                # Enable sparse-checkout
-                success, stdout, stderr = _run_git_command(["config", "core.sparseCheckout", "true"], cwd=target_path)
+                # Enable sparse-checkout (non-cone mode to exclude root files)
+                success, stdout, stderr = _run_git_command(
+                    ["sparse-checkout", "init", "--no-cone"], 
+                    cwd=target_path
+                )
                 if not success:
                     return False, f"Failed to enable sparse-checkout: {stderr or stdout}"
                 
-                # Create sparse-checkout file
-                sparse_checkout_file = target_path / ".git" / "info" / "sparse-checkout"
-                sparse_checkout_file.parent.mkdir(parents=True, exist_ok=True)
-                with open(sparse_checkout_file, "w") as f:
-                    f.write(f"{sparse_dir}/*\n")
+                # Set sparse-checkout to specific directory (non-cone uses patterns)
+                success, stdout, stderr = _run_git_command(
+                    ["sparse-checkout", "set", f"{sparse_dir}/*"],
+                    cwd=target_path
+                )
+                if not success:
+                    return False, f"Failed to set sparse-checkout directory: {stderr or stdout}"
                 
-                # Pull specific branch with sparse-checkout
-                pull_args = ["pull", "--depth", "1", "origin"]
+                # Fetch specific branch
+                fetch_args = ["fetch", "--depth", "1", "origin"]
                 if branch:
-                    pull_args.append(branch)
+                    fetch_args.append(f"{branch}:{branch}")
                 else:
-                    pull_args.append("main")
+                    fetch_args.append("main:main")
                 
-                success, stdout, stderr = _run_git_command(pull_args, cwd=target_path)
+                success, stdout, stderr = _run_git_command(fetch_args, cwd=target_path)
                 if not success:
-                    return False, f"Sparse-checkout failed: {stderr or stdout}"
+                    return False, f"Fetch failed: {stderr or stdout}"
                 
-                # Move contents of sparse directory to root
-                sparse_path = target_path / sparse_dir
-                if sparse_path.exists() and sparse_path.is_dir():
-                    # Move all contents from sparse_dir to target_path root
-                    import shutil
-                    for item in sparse_path.iterdir():
-                        dest = target_path / item.name
-                        if dest.exists():
-                            if dest.is_dir():
-                                shutil.rmtree(dest)
-                            else:
-                                dest.unlink()
-                        shutil.move(str(item), str(target_path))
-                    
-                    # Remove empty sparse directory
-                    sparse_path.rmdir()
+                # Checkout the branch
+                checkout_branch = branch if branch else "main"
+                success, stdout, stderr = _run_git_command(
+                    ["checkout", checkout_branch],
+                    cwd=target_path
+                )
+                if not success:
+                    return False, f"Checkout failed: {stderr or stdout}"
                 
+                # Done! Files are in target_path/sparse_dir/
                 return True, "Cloned successfully (sparse)"
             else:
                 return False, f"Failed to initialize: {stderr or stdout}"
@@ -290,8 +289,12 @@ def list() -> None:
         directory = lib.get("directory", "library")
         enabled = lib.get("enabled", True)
         
-        # Check if library exists locally (check base path, not directory subdirectory)
-        library_path = libraries_path / name
+        # Check if library exists locally
+        library_base = libraries_path / name
+        if directory and directory != ".":
+            library_path = library_base / directory
+        else:
+            library_path = library_base
         exists = library_path.exists()
         
         status_parts = []

+ 1 - 1
library/compose/traefik/template.yaml

@@ -56,7 +56,7 @@ metadata:
        - Review and limit network exposure
 
     For more information, visit: https://doc.traefik.io/traefik/
-  draft: true
+  draft: false
 spec:
   general:
     title: "General"