~edwargix/git.sr.ht

17c9530ea1daab53c167e89a522c297f24a0fc39 — Drew DeVault 5 years ago f09b537
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.
M gitsrht-update-hook/main.go => gitsrht-update-hook/main.go +4 -1
@@ 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()

A gitsrht-update-hook/pre-receive.go => gitsrht-update-hook/pre-receive.go +32 -0
@@ 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)
		}
	}
}

M gitsrht/repos.py => gitsrht/repos.py +4 -0
@@ 93,6 93,10 @@ class GitRepoApi(SimpleRepoApi):
            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")
            ], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        subprocess.run(["ln", "-s",

M scripts/symlink-update-hook.py => scripts/symlink-update-hook.py +2 -1
@@ 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))