diff options
author | xengineering <me@xengineering.eu> | 2024-07-06 11:35:47 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-07-06 12:18:30 +0200 |
commit | ac426d988737f3a6161c93767b4801243998e8c7 (patch) | |
tree | ba878e9ca0f53bb79b3a5cd520aab3cd549d1c3a | |
parent | b8f4ee66c9bcf1e31b233a9beaa0aa693eae2a89 (diff) | |
download | craft-ac426d988737f3a6161c93767b4801243998e8c7.tar craft-ac426d988737f3a6161c93767b4801243998e8c7.tar.zst craft-ac426d988737f3a6161c93767b4801243998e8c7.zip |
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.
-rw-r--r-- | main.go | 48 |
1 files changed, 46 insertions, 2 deletions
@@ -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 { |