diff options
Diffstat (limited to 'main.go')
-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 { |