2024-12-08 14:34:31 +01:00
|
|
|
package spitfire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2024-12-08 15:39:19 +01:00
|
|
|
// ApplyPatches handles cloning, updating, and applying patches
|
2024-12-08 14:34:31 +01:00
|
|
|
func ApplyPatches(sourcePath string, patchesRepo string) error {
|
|
|
|
// Define the patches repository and clone directory
|
|
|
|
patchesCloneDir := filepath.Join(sourcePath, "patches")
|
|
|
|
fmt.Printf("Source Path: %s\n", sourcePath)
|
|
|
|
fmt.Printf("Patches Clone Directory: %s\n", patchesCloneDir)
|
|
|
|
fmt.Printf("Patches Repository URL: %s\n", patchesRepo)
|
|
|
|
|
|
|
|
// Check if the patches directory already exists
|
|
|
|
if _, err := os.Stat(patchesCloneDir); os.IsNotExist(err) {
|
|
|
|
// Clone the patches repository
|
|
|
|
fmt.Println("Patches directory does not exist. Proceeding to clone.")
|
|
|
|
cmdClone := exec.Command("git", "clone", patchesRepo, patchesCloneDir)
|
|
|
|
cmdClone.Stdout = os.Stdout
|
|
|
|
cmdClone.Stderr = os.Stderr
|
|
|
|
if err := cmdClone.Run(); err != nil {
|
|
|
|
return fmt.Errorf("failed to clone patches repository: %v", err)
|
|
|
|
}
|
|
|
|
fmt.Println("Patches repository cloned successfully.")
|
|
|
|
} else {
|
2024-12-08 15:39:19 +01:00
|
|
|
// If the directory exists, fetch and pull the latest changes
|
|
|
|
fmt.Println("Patches directory already exists. Fetching latest changes.")
|
|
|
|
cmdFetch := exec.Command("git", "fetch", "--all")
|
|
|
|
cmdFetch.Dir = patchesCloneDir
|
|
|
|
cmdFetch.Stdout = os.Stdout
|
|
|
|
cmdFetch.Stderr = os.Stderr
|
|
|
|
if err := cmdFetch.Run(); err != nil {
|
|
|
|
return fmt.Errorf("failed to fetch updates for patches repository: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdReset := exec.Command("git", "reset", "--hard", "origin/main")
|
|
|
|
cmdReset.Dir = patchesCloneDir
|
|
|
|
cmdReset.Stdout = os.Stdout
|
|
|
|
cmdReset.Stderr = os.Stderr
|
|
|
|
if err := cmdReset.Run(); err != nil {
|
|
|
|
return fmt.Errorf("failed to reset patches repository to latest state: %v", err)
|
|
|
|
}
|
|
|
|
fmt.Println("Patches repository updated successfully.")
|
2024-12-08 14:34:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the directory exists
|
|
|
|
if _, err := os.Stat(patchesCloneDir); os.IsNotExist(err) {
|
2024-12-08 15:39:19 +01:00
|
|
|
return fmt.Errorf("patches directory not found after update: %s", patchesCloneDir)
|
2024-12-08 14:34:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the patches using `go run apply.go`
|
|
|
|
applyCmd := exec.Command("go", "run", "apply.go", "--path="+sourcePath)
|
|
|
|
applyCmd.Dir = patchesCloneDir // Run from the patches directory
|
|
|
|
applyCmd.Stdout = os.Stdout
|
|
|
|
applyCmd.Stderr = os.Stderr
|
|
|
|
fmt.Println("Applying patches...")
|
|
|
|
if err := applyCmd.Run(); err != nil {
|
|
|
|
return fmt.Errorf("failed to apply patches: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Patches applied successfully.")
|
|
|
|
return nil
|
|
|
|
}
|