| 1 | package commands |
| 2 | |
| 3 | import ( |
| 4 | "github.com/git-lfs/git-lfs/v3/git" |
| 5 | "github.com/git-lfs/git-lfs/v3/tr" |
| 6 | "github.com/rubyist/tracerx" |
| 7 | "github.com/spf13/cobra" |
| 8 | ) |
| 9 | |
| 10 | // postCommitCommand is run through Git's post-commit hook. The hook passes |
| 11 | // no arguments. |
| 12 | // This hook checks that files which are lockable and not locked are made read-only, |
| 13 | // optimising that based on what was added / modified in the commit. |
| 14 | // This is mainly to catch added files, since modified files should already be |
| 15 | // locked. If we didn't do this, any added files would remain read/write on disk |
| 16 | // even without a lock unless something else checked. |
| 17 | func postCommitCommand(cmd *cobra.Command, args []string) { |
| 18 | |
| 19 | // Skip entire hook if lockable read only feature is disabled |
| 20 | if !cfg.SetLockableFilesReadOnly() { |
| 21 | return |
| 22 | } |
| 23 | |
| 24 | requireGitVersion() |
| 25 | |
| 26 | lockClient := newLockClient() |
| 27 | |
| 28 | // Skip this hook if no lockable patterns have been configured |
| 29 | if len(lockClient.GetLockablePatterns()) == 0 { |
| 30 | return |
| 31 | } |
| 32 | |
| 33 | tracerx.Printf("post-commit: checking file write flags at HEAD") |
| 34 | // We can speed things up by looking at what changed in |
| 35 | // HEAD, and only checking those lockable files |
| 36 | files, err := git.GetFilesChanged("HEAD", "") |
| 37 | |
| 38 | if err != nil { |
| 39 | LoggedError(err, tr.Tr.Get("Warning: post-commit failed: %v", err)) |
| 40 | ExitWithCode(1) |
| 41 | } |
| 42 | tracerx.Printf("post-commit: checking write flags on %v", files) |
| 43 | err = lockClient.FixLockableFileWriteFlags(files) |
| 44 | if err != nil { |
| 45 | LoggedError(err, tr.Tr.Get("Warning: post-commit locked file check failed: %v", err)) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func init() { |
| 50 | RegisterCommand("post-commit", postCommitCommand, nil) |
| 51 | } |