| 1 | package commands |
| 2 | |
| 3 | import ( |
| 4 | "github.com/git-lfs/git-lfs/v3/tr" |
| 5 | "github.com/rubyist/tracerx" |
| 6 | "github.com/spf13/cobra" |
| 7 | ) |
| 8 | |
| 9 | // postMergeCommand is run through Git's post-merge hook. |
| 10 | // |
| 11 | // This hook checks that files which are lockable and not locked are made read-only, |
| 12 | // optimising that as best it can based on the available information. |
| 13 | func postMergeCommand(cmd *cobra.Command, args []string) { |
| 14 | if len(args) != 1 { |
| 15 | Print(tr.Tr.Get("This should be run through Git's post-merge hook. Run `git lfs update` to install it.")) |
| 16 | ExitWithCode(1) |
| 17 | } |
| 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 | // The only argument this hook receives is a flag indicating whether the |
| 34 | // merge was a squash merge; we don't know what files changed. |
| 35 | // Whether it's squash or not is irrelevant, either way it could have |
| 36 | // reset the read-only flag on files that got merged. |
| 37 | |
| 38 | tracerx.Printf("post-merge: checking write flags for all lockable files") |
| 39 | // Sadly we don't get any information about what files were checked out, |
| 40 | // so we have to check the entire repo |
| 41 | err := lockClient.FixAllLockableFileWriteFlags() |
| 42 | if err != nil { |
| 43 | LoggedError(err, tr.Tr.Get("Warning: post-merge locked file check failed: %v", err)) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func init() { |
| 48 | RegisterCommand("post-merge", postMergeCommand, nil) |
| 49 | } |