forked from git-lfs/git-lfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_post_commit.go
54 lines (43 loc) · 1.58 KB
/
command_post_commit.go
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
package commands
import (
"os"
"github.com/git-lfs/git-lfs/v3/git"
"github.com/git-lfs/git-lfs/v3/tr"
"github.com/rubyist/tracerx"
"github.com/spf13/cobra"
)
// postCommitCommand is run through Git's post-commit hook. The hook passes
// no arguments.
// This hook checks that files which are lockable and not locked are made read-only,
// optimising that based on what was added / modified in the commit.
// This is mainly to catch added files, since modified files should already be
// locked. If we didn't do this, any added files would remain read/write on disk
// even without a lock unless something else checked.
func postCommitCommand(cmd *cobra.Command, args []string) {
// Skip entire hook if lockable read only feature is disabled
if !cfg.SetLockableFilesReadOnly() {
os.Exit(0)
}
requireGitVersion()
lockClient := newLockClient()
// Skip this hook if no lockable patterns have been configured
if len(lockClient.GetLockablePatterns()) == 0 {
os.Exit(0)
}
tracerx.Printf("post-commit: checking file write flags at HEAD")
// We can speed things up by looking at what changed in
// HEAD, and only checking those lockable files
files, err := git.GetFilesChanged("HEAD", "")
if err != nil {
LoggedError(err, tr.Tr.Get("Warning: post-commit failed: %v", err))
os.Exit(1)
}
tracerx.Printf("post-commit: checking write flags on %v", files)
err = lockClient.FixLockableFileWriteFlags(files)
if err != nil {
LoggedError(err, tr.Tr.Get("Warning: post-commit locked file check failed: %v", err))
}
}
func init() {
RegisterCommand("post-commit", postCommitCommand, nil)
}