From 17c9530ea1daab53c167e89a522c297f24a0fc39 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Mon, 17 Feb 2020 09:18:53 -0500 Subject: [PATCH] gitsrht-update-hook: add pre-receive hook This adds a new pre-receive hook, which is capable of receiving git push options. I have also added support for -o debug, which prints your push UUID for troubleshooting later. --- gitsrht-update-hook/main.go | 5 ++++- gitsrht-update-hook/pre-receive.go | 32 ++++++++++++++++++++++++++++++ gitsrht/repos.py | 4 ++++ scripts/symlink-update-hook.py | 3 ++- 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 gitsrht-update-hook/pre-receive.go diff --git a/gitsrht-update-hook/main.go b/gitsrht-update-hook/main.go index 71037e4..635d539 100644 --- a/gitsrht-update-hook/main.go +++ b/gitsrht-update-hook/main.go @@ -17,11 +17,14 @@ var ( func main() { log.SetFlags(0) + logger.Printf("%v", os.Args) // The update hook is run on the update and post-update git hooks, and also // runs a third stage directly. The first two stages are performance // critical and take place while the user is blocked at their terminal. The // third stage is done in the background. - if os.Args[0] == "hooks/update" { + if os.Args[0] == "hooks/pre-receive" { + preReceive() + } else if os.Args[0] == "hooks/update" { update() } else if os.Args[0] == "hooks/post-update" { postUpdate() diff --git a/gitsrht-update-hook/pre-receive.go b/gitsrht-update-hook/pre-receive.go new file mode 100644 index 0000000..1a3abed --- /dev/null +++ b/gitsrht-update-hook/pre-receive.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "log" + "os" + "strconv" +) + +func preReceive() { + // TODO: This would be a good place to enforce branch update restrictions + // and such, or to check OWNERS, etc. + pushUuid, ok := os.LookupEnv("SRHT_PUSH") + if !ok { + logger.Fatal("Missing SRHT_PUSH in environment, configuration error?") + } + logger.Printf("Running pre-receive for push %s", pushUuid) + + if nopts, ok := os.LookupEnv("GIT_PUSH_OPTION_COUNT"); ok { + n, _ := strconv.Atoi(nopts) + configureOpts(pushUuid, n) + } +} + +func configureOpts(pushUuid string, nopts int) { + for i := 0; i < nopts; i++ { + opt := os.Getenv(fmt.Sprintf("GIT_PUSH_OPTION_%d", i)) + if opt == "debug" { + log.Printf("debug: %s", pushUuid) + } + } +} diff --git a/gitsrht/repos.py b/gitsrht/repos.py index 534ebf1..084386e 100644 --- a/gitsrht/repos.py +++ b/gitsrht/repos.py @@ -91,6 +91,10 @@ class GitRepoApi(SimpleRepoApi): stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) subprocess.run(["git", "config", "srht.repo-id", str(repo.id)], check=True, cwd=repo.path, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + subprocess.run(["ln", "-s", + post_update, + os.path.join(repo.path, "hooks", "pre-receive") + ], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) subprocess.run(["ln", "-s", post_update, os.path.join(repo.path, "hooks", "update") diff --git a/scripts/symlink-update-hook.py b/scripts/symlink-update-hook.py index f90924e..708cdde 100755 --- a/scripts/symlink-update-hook.py +++ b/scripts/symlink-update-hook.py @@ -25,5 +25,6 @@ def migrate(path, link): for repo in Repository.query.all(): if migrate(os.path.join(repo.path, "hooks", "update"), post_update) \ - and migrate(os.path.join(repo.path, "hooks", "post-update"), post_update): + and migrate(os.path.join(repo.path, "hooks", "post-update"), post_update) \ + and migrate(os.path.join(repo.path, "hooks", "pre-receive"), post_update): print("Migrated {}".format(repo.name)) -- 2.38.4