| 1 | package commands |
| 2 | |
| 3 | import ( |
| 4 | "github.com/git-lfs/git-lfs/v3/git" |
| 5 | "github.com/git-lfs/git-lfs/v3/locking" |
| 6 | "github.com/git-lfs/git-lfs/v3/tr" |
| 7 | "github.com/rubyist/tracerx" |
| 8 | "github.com/spf13/cobra" |
| 9 | ) |
| 10 | |
| 11 | // postCheckoutCommand is run through Git's post-checkout hook. The hook passes |
| 12 | // up to 3 arguments on the command line: |
| 13 | // |
| 14 | // 1. SHA of previous commit before the checkout |
| 15 | // 2. SHA of commit just checked out |
| 16 | // 3. Flag ("0" or "1") - 1 if a branch/tag/SHA was checked out, 0 if a file was |
| 17 | // In the case of a file being checked out, the pre/post SHA are the same |
| 18 | // |
| 19 | // This hook checks that files which are lockable and not locked are made read-only, |
| 20 | // optimising that as best it can based on the available information. |
| 21 | func postCheckoutCommand(cmd *cobra.Command, args []string) { |
| 22 | if len(args) != 3 { |
| 23 | Print(tr.Tr.Get("This should be run through Git's post-checkout hook. Run `git lfs update` to install it.")) |
| 24 | ExitWithCode(1) |
| 25 | } |
| 26 | |
| 27 | // Skip entire hook if lockable read only feature is disabled |
| 28 | if !cfg.SetLockableFilesReadOnly() { |
| 29 | return |
| 30 | } |
| 31 | |
| 32 | requireGitVersion() |
| 33 | |
| 34 | lockClient := newLockClient() |
| 35 | |
| 36 | // Skip this hook if no lockable patterns have been configured |
| 37 | if len(lockClient.GetLockablePatterns()) == 0 { |
| 38 | return |
| 39 | } |
| 40 | |
| 41 | if args[2] == "1" && args[0] != "0000000000000000000000000000000000000000" { |
| 42 | postCheckoutRevChange(lockClient, args[0], args[1]) |
| 43 | } else { |
| 44 | postCheckoutFileChange(lockClient) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func postCheckoutRevChange(client *locking.Client, pre, post string) { |
| 49 | tracerx.Printf("post-checkout: changes between %v and %v", pre, post) |
| 50 | // We can speed things up by looking at the difference between previous HEAD |
| 51 | // and current HEAD, and only checking lockable files that are different |
| 52 | files, err := git.GetFilesChanged(pre, post) |
| 53 | |
| 54 | if err != nil { |
| 55 | LoggedError(err, "%s\n%s", tr.Tr.Get("Warning: post-checkout rev diff %v:%v failed: %v", pre, post, err), tr.Tr.Get("Falling back on full scan.")) |
| 56 | postCheckoutFileChange(client) |
| 57 | } |
| 58 | tracerx.Printf("post-checkout: checking write flags on %v", files) |
| 59 | err = client.FixLockableFileWriteFlags(files) |
| 60 | if err != nil { |
| 61 | LoggedError(err, tr.Tr.Get("Warning: post-checkout locked file check failed: %v", err)) |
| 62 | } |
| 63 | |
| 64 | } |
| 65 | |
| 66 | func postCheckoutFileChange(client *locking.Client) { |
| 67 | tracerx.Printf("post-checkout: checking write flags for all lockable files") |
| 68 | // Sadly we don't get any information about what files were checked out, |
| 69 | // so we have to check the entire repo |
| 70 | err := client.FixAllLockableFileWriteFlags() |
| 71 | if err != nil { |
| 72 | LoggedError(err, tr.Tr.Get("Warning: post-checkout locked file check failed: %v", err)) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func init() { |
| 77 | RegisterCommand("post-checkout", postCheckoutCommand, nil) |
| 78 | } |