File: command_post_checkout.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (81 lines) | stat: -rw-r--r-- 2,691 bytes parent folder | download | duplicates (2)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package commands

import (
	"os"

	"github.com/git-lfs/git-lfs/v3/git"
	"github.com/git-lfs/git-lfs/v3/locking"
	"github.com/git-lfs/git-lfs/v3/tr"
	"github.com/rubyist/tracerx"
	"github.com/spf13/cobra"
)

// postCheckoutCommand is run through Git's post-checkout hook. The hook passes
// up to 3 arguments on the command line:
//
//  1. SHA of previous commit before the checkout
//  2. SHA of commit just checked out
//  3. Flag ("0" or "1") - 1 if a branch/tag/SHA was checked out, 0 if a file was
//     In the case of a file being checked out, the pre/post SHA are the same
//
// This hook checks that files which are lockable and not locked are made read-only,
// optimising that as best it can based on the available information.
func postCheckoutCommand(cmd *cobra.Command, args []string) {
	if len(args) != 3 {
		Print(tr.Tr.Get("This should be run through Git's post-checkout hook.  Run `git lfs update` to install it."))
		os.Exit(1)
	}

	// 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)
	}

	if args[2] == "1" && args[0] != "0000000000000000000000000000000000000000" {
		postCheckoutRevChange(lockClient, args[0], args[1])
	} else {
		postCheckoutFileChange(lockClient)
	}

}

func postCheckoutRevChange(client *locking.Client, pre, post string) {
	tracerx.Printf("post-checkout: changes between %v and %v", pre, post)
	// We can speed things up by looking at the difference between previous HEAD
	// and current HEAD, and only checking lockable files that are different
	files, err := git.GetFilesChanged(pre, post)

	if err != nil {
		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."))
		postCheckoutFileChange(client)
	}
	tracerx.Printf("post-checkout: checking write flags on %v", files)
	err = client.FixLockableFileWriteFlags(files)
	if err != nil {
		LoggedError(err, tr.Tr.Get("Warning: post-checkout locked file check failed: %v", err))
	}

}

func postCheckoutFileChange(client *locking.Client) {
	tracerx.Printf("post-checkout: checking write flags for all lockable files")
	// Sadly we don't get any information about what files were checked out,
	// so we have to check the entire repo
	err := client.FixAllLockableFileWriteFlags()
	if err != nil {
		LoggedError(err, tr.Tr.Get("Warning: post-checkout locked file check failed: %v", err))
	}
}

func init() {
	RegisterCommand("post-checkout", postCheckoutCommand, nil)
}