From 5dc1cbdb7f7aa9aad7592595147a36bf7db09187 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Mon, 17 Feb 2025 11:54:14 +0800 Subject: git_hooks_deploy.go: Deploy hooks to filesystem --- git_hooks_deploy.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 5 +++++ 2 files changed, 55 insertions(+) create mode 100644 git_hooks_deploy.go diff --git a/git_hooks_deploy.go b/git_hooks_deploy.go new file mode 100644 index 0000000..01be550 --- /dev/null +++ b/git_hooks_deploy.go @@ -0,0 +1,50 @@ +package main + +import ( + "errors" + "io" + "os" + "path/filepath" +) + +func deploy_hooks_to_filesystem() (err error) { + err = func() error { + src_fd, err := resources_fs.Open("git_hooks_client/git_hooks_client") + if err != nil { + return err + } + defer src_fd.Close() + + dst_fd, err := os.OpenFile(filepath.Join(config.Hooks.Execs, "git_hooks_client"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755) + if err != nil { + return err + } + defer dst_fd.Close() + + _, err = io.Copy(dst_fd, src_fd) + if err != nil { + return err + } + + return nil + }() + if err != nil { + return err + } + + err = os.Chmod(filepath.Join(config.Hooks.Execs, "git_hooks_client"), 0755) + if err != nil { + return err + } + + for _, hook_name := range []string{ + "pre-receive", + } { + err = os.Symlink(filepath.Join(config.Hooks.Execs, "git_hooks_client"), filepath.Join(config.Hooks.Execs, hook_name)) + if err != nil && !errors.Is(err, os.ErrExist) { + return err + } + } + + return nil +} diff --git a/main.go b/main.go index 23ee558..90dfaee 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,11 @@ func main() { clog.Fatal(1, "Loading configuration: "+err.Error()) } + err = deploy_hooks_to_filesystem() + if err != nil { + clog.Fatal(1, "Deploying hooks to filesystem: "+err.Error()) + } + err = load_templates() if err != nil { clog.Fatal(1, "Loading templates: "+err.Error()) -- cgit v1.2.3