hooks.go

 1package backend
 2
 3import (
 4	"context"
 5	"io"
 6)
 7
 8// HookArg is an argument to a git hook.
 9type HookArg struct {
10	OldSha  string
11	NewSha  string
12	RefName string
13}
14
15// Hooks provides an interface for git server-side hooks.
16type Hooks interface {
17	PreReceive(ctx context.Context, stdout io.Writer, stderr io.Writer, repo string, args []HookArg)
18	Update(ctx context.Context, stdout io.Writer, stderr io.Writer, repo string, arg HookArg)
19	PostReceive(ctx context.Context, stdout io.Writer, stderr io.Writer, repo string, args []HookArg)
20	PostUpdate(ctx context.Context, stdout io.Writer, stderr io.Writer, repo string, args ...string)
21}
22
23// PostReceive is called by the git post-receive hook.
24//
25// It implements Hooks.
26func (d *Backend) PostReceive(ctx context.Context, stdout io.Writer, stderr io.Writer, repo string, args []HookArg) {
27	d.logger.Debug("post-receive hook called", "repo", repo, "args", args)
28}
29
30// PreReceive is called by the git pre-receive hook.
31//
32// It implements Hooks.
33func (d *Backend) PreReceive(ctx context.Context, stdout io.Writer, stderr io.Writer, repo string, args []HookArg) {
34	d.logger.Debug("pre-receive hook called", "repo", repo, "args", args)
35}
36
37// Update is called by the git update hook.
38//
39// It implements Hooks.
40func (d *Backend) Update(ctx context.Context, stdout io.Writer, stderr io.Writer, repo string, arg HookArg) {
41	d.logger.Debug("update hook called", "repo", repo, "arg", arg)
42}
43
44// PostUpdate is called by the git post-update hook.
45//
46// It implements Hooks.
47func (d *Backend) PostUpdate(ctx context.Context, stdout io.Writer, stderr io.Writer, repo string, args ...string) {
48	d.logger.Debug("post-update hook called", "repo", repo, "args", args)
49
50	if err := d.Touch(ctx, repo); err != nil {
51		d.logger.Error("failed to touch repo", "repo", repo, "err", err)
52	}
53}