From ac426d988737f3a6161c93767b4801243998e8c7 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 6 Jul 2024 11:35:47 +0200 Subject: Prepare simple workbench After detecting an update of a Git ref craft should create a throw-away Git clone of the repository with the new Git commit checked out. This is the starting point for executing build and test steps. Improving performance by tree-less or shallow clones and updating submodules is not yet implemented. --- main.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 08a97b8..86b689d 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,8 @@ import ( "io" "log" "os" + "os/exec" + "path/filepath" "strings" ) @@ -16,8 +18,12 @@ func main() { defer log.Println("Exiting craft") log.Println("Starting craft") - log.Printf("Git hook type: %s\n", getHookType()) - log.Printf("Git repository: %s\n", getRepositoryPath()) + + hookType := getHookType() + log.Printf("Git hook type: %s\n", hookType) + + repo := getRepositoryPath() + log.Printf("Git repository: %s\n", repo) for { eof, update := getUpdate() @@ -25,9 +31,47 @@ func main() { break } log.Printf("Git ref update: %s\n", update) + commit := update.updated + _ = prepareWorkbench(repo, commit) + } +} + +func runCommand(name string, args ...string) { + log.Printf("%s %s\n", name, strings.Join(args, " ")) + command := exec.Command(name, args...) + err := command.Run() + if err != nil { + log.Fatal(err) } } +func prepareWorkbench(repo string, commit string) string { + workbench, err := os.MkdirTemp("", "*-craft") + if err != nil { + log.Fatal(err) + } + defer os.RemoveAll(workbench) + + runCommand( + "git", + "clone", + repo, + workbench, + ) + + runCommand( + "git", + "--git-dir", + filepath.Join(workbench, ".git"), + "--work-tree", + workbench, + "checkout", + commit, + ) + + return workbench +} + func getHookType() string { args := len(os.Args) if args != 1 { -- cgit v1.2.3-70-g09d2