aboutsummaryrefslogtreecommitdiff
path: root/git_hooks_deploy.go
blob: 99e54642fc8ed2cadb0eb5804f3f02f7d32b8779 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileContributor: Runxi Yu <https://runxiyu.org>

package main

import (
	"errors"
	"io"
	"os"
	"path/filepath"
)

// deploy_hooks_to_filesystem deploys the git hooks client to the filesystem.
// The git hooks client is expected to be embedded in resources_fs and must be
// pre-compiled during the build process; see the Makefile.
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
	}

	// Go's embed filesystems do not store permissions; but in any case,
	// they would need to be 0o755:
	err = os.Chmod(filepath.Join(config.Hooks.Execs, "git_hooks_client"), 0o755)
	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
}