1package backend
2
3import (
4 "context"
5 "io"
6 "sync"
7
8 "github.com/charmbracelet/soft-serve/server/hooks"
9 "github.com/charmbracelet/soft-serve/server/proto"
10)
11
12var _ hooks.Hooks = (*Backend)(nil)
13
14// PostReceive is called by the git post-receive hook.
15//
16// It implements Hooks.
17func (d *Backend) PostReceive(_ context.Context, _ io.Writer, _ io.Writer, repo string, args []hooks.HookArg) {
18 d.logger.Debug("post-receive hook called", "repo", repo, "args", args)
19}
20
21// PreReceive is called by the git pre-receive hook.
22//
23// It implements Hooks.
24func (d *Backend) PreReceive(_ context.Context, _ io.Writer, _ io.Writer, repo string, args []hooks.HookArg) {
25 d.logger.Debug("pre-receive hook called", "repo", repo, "args", args)
26}
27
28// Update is called by the git update hook.
29//
30// It implements Hooks.
31func (d *Backend) Update(_ context.Context, _ io.Writer, _ io.Writer, repo string, arg hooks.HookArg) {
32 d.logger.Debug("update hook called", "repo", repo, "arg", arg)
33}
34
35// PostUpdate is called by the git post-update hook.
36//
37// It implements Hooks.
38func (d *Backend) PostUpdate(ctx context.Context, _ io.Writer, _ io.Writer, repo string, args ...string) {
39 d.logger.Debug("post-update hook called", "repo", repo, "args", args)
40
41 var wg sync.WaitGroup
42
43 // Populate last-modified file.
44 wg.Add(1)
45 go func() {
46 defer wg.Done()
47 if err := populateLastModified(ctx, d, repo); err != nil {
48 d.logger.Error("error populating last-modified", "repo", repo, "err", err)
49 return
50 }
51 }()
52
53 wg.Wait()
54}
55
56func populateLastModified(ctx context.Context, d *Backend, name string) error {
57 var rr *repo
58 _rr, err := d.Repository(ctx, name)
59 if err != nil {
60 return err
61 }
62
63 if r, ok := _rr.(*repo); ok {
64 rr = r
65 } else {
66 return proto.ErrRepoNotFound
67 }
68
69 r, err := rr.Open()
70 if err != nil {
71 return err
72 }
73
74 c, err := r.LatestCommitTime()
75 if err != nil {
76 return err
77 }
78
79 return rr.writeLastModified(c)
80}