From 9e006275150cde0c1102b11701481597fcd6527d Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 20 Jul 2024 14:42:06 +0200 Subject: Drop Git hook logic This might be re-used later but the new goal is to make craft an independent build automation tool or service which might be called by a later provided git hook. Nevertheless the primary target for now is to build something which executes a build based on a repository reference but not triggered by a Git hook. --- README.md | 17 ++++-------- main.go | 95 +++++++++++---------------------------------------------------- 2 files changed, 22 insertions(+), 90 deletions(-) diff --git a/README.md b/README.md index f9acd86..715e6c6 100644 --- a/README.md +++ b/README.md @@ -10,19 +10,14 @@ required. ## Usage -Craft is used as a Git hook. It can be enabled for a given Git repository and -hook by creating a symbolic link: +Call the `craft` tool like this: ``` -ln -s path/to/craft path/to/repository/.git/hooks/post-receive +./craft -repo https://example.com -commit 8f36088eee1cfbec4ddac4d652101db3b29eed45 ``` -Currently only the `post-receive` hook is supported. This can be performed also -with a bare repository: +This will create a temporary directory, clone the repository including +submodules to that location, execute the `build` function from the `craft.sh` +file inside the root of the repo and remove the temporary directory. -``` -ln -s path/to/craft path/to/bare/repository.git/hooks/post-receive -``` - -Whenever such a repository received updates via `git push` craft will be -executed and prints text to the console of the user who pushed the updates. +Log output is redirected to the calling terminal. diff --git a/main.go b/main.go index 383b398..6408a4a 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,8 @@ package main -// see `man 5 githooks` for details - import ( "bytes" - "errors" - "fmt" - "io" + "flag" "log" "os" "os/exec" @@ -14,29 +10,27 @@ import ( "strings" ) +var ( + repo = "" + commit = "" +) + +func init() { + flag.StringVar(&repo, "repo", "", "") + flag.StringVar(&commit, "commit", "", "") +} + func main() { + flag.Parse() + log.SetFlags(0) defer log.Println("Exiting craft") log.Println("Starting craft") - hookType := getHookType() - log.Printf("Git hook type: %s\n", hookType) - - repo := getRepositoryPath() - log.Printf("Git repository: %s\n", repo) - - for { - eof, update := getUpdate() - if eof { - break - } - log.Printf("Git ref update: %s\n", update) - commit := update.updated - workbench := prepareWorkbench(repo, commit) - defer os.RemoveAll(workbench) - craft(workbench) - } + workbench := prepareWorkbench(repo, commit) + defer os.RemoveAll(workbench) + craft(workbench) } func runCommand(dir string, name string, args ...string) { @@ -111,60 +105,3 @@ func prepareWorkbench(repo string, commit string) string { return workbench } - -func getHookType() string { - args := len(os.Args) - if args != 1 { - log.Fatalf("Expected zero arguments but %d were given", args-1) - } - prefix := `hooks/` - path := os.Args[0] - hookType := strings.TrimPrefix(path, prefix) - if path == hookType { - log.Fatalf("Hook path '%s' has no '%s' prefix", path, prefix) - } - validateHookType(hookType) - return hookType -} - -func validateHookType(hookType string) { - validTypes := [...]string{ - `post-receive`, - } - for _, currentType := range validTypes { - if hookType == currentType { - return - } - } - log.Fatalf("Not supported Git hook type '%s'\n", hookType) -} - -func getRepositoryPath() string { - cwd, err := os.Getwd() - if err != nil { - log.Fatal(err) - } - return cwd -} - -type update struct { - old, updated, ref string -} - -func (u update) String() string { - ret := fmt.Sprintf("'%s' from '%s' to '%s'", u.ref, u.old, u.updated) - return ret -} - -func getUpdate() (bool, update) { - var u update - _, err := fmt.Scanf("%s %s %s\n", &u.old, &u.updated, &u.ref) - if err != nil { - if errors.Is(err, io.EOF) { - return true, u - } else { - log.Fatal(err) - } - } - return false, u -} -- cgit v1.2.3-70-g09d2