git-module

git clone git://git.lin.moe/fork/git-module.git

 1package git
 2
 3import (
 4	"os"
 5	"path/filepath"
 6)
 7
 8// DefaultHooksDir is the default directory for Git hooks.
 9const DefaultHooksDir = "hooks"
10
11// NewHook creates and returns a new hook with given name. Update method must be
12// called to actually save the hook to disk.
13func (r *Repository) NewHook(dir string, name HookName) *Hook {
14	return &Hook{
15		name: name,
16		path: filepath.Join(r.path, dir, string(name)),
17	}
18}
19
20// Hook returns a Git hook by given name in the repository. Giving empty
21// directory will use the default directory. It returns an os.ErrNotExist if
22// both active and sample hook do not exist.
23func (r *Repository) Hook(dir string, name HookName) (*Hook, error) {
24	if dir == "" {
25		dir = DefaultHooksDir
26	}
27	// 1. Check if there is an active hook.
28	fpath := filepath.Join(r.path, dir, string(name))
29	if isFile(fpath) {
30		p, err := os.ReadFile(fpath)
31		if err != nil {
32			return nil, err
33		}
34		return &Hook{
35			name:    name,
36			path:    fpath,
37			content: string(p),
38		}, nil
39	}
40
41	// 2. Check if sample content exists.
42	sample := ServerSideHookSamples[name]
43	if sample != "" {
44		return &Hook{
45			name:     name,
46			path:     fpath,
47			isSample: true,
48			content:  sample,
49		}, nil
50	}
51
52	return nil, os.ErrNotExist
53}
54
55// Hooks returns a list of Git hooks found in the repository. Giving empty
56// directory will use the default directory. It may return an empty slice when
57// no hooks found.
58func (r *Repository) Hooks(dir string) ([]*Hook, error) {
59	hooks := make([]*Hook, 0, len(ServerSideHooks))
60	for _, name := range ServerSideHooks {
61		h, err := r.Hook(dir, name)
62		if err != nil {
63			if err == os.ErrNotExist {
64				continue
65			}
66			return nil, err
67		}
68		hooks = append(hooks, h)
69	}
70	return hooks, nil
71}