github.com/git-lfs/git-lfs@v2.5.2+incompatible/commands/command_post_merge.go (about)

     1  package commands
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/rubyist/tracerx"
     7  	"github.com/spf13/cobra"
     8  )
     9  
    10  // postMergeCommand is run through Git's post-merge hook.
    11  //
    12  // This hook checks that files which are lockable and not locked are made read-only,
    13  // optimising that as best it can based on the available information.
    14  func postMergeCommand(cmd *cobra.Command, args []string) {
    15  	if len(args) != 1 {
    16  		Print("This should be run through Git's post-merge hook.  Run `git lfs update` to install it.")
    17  		os.Exit(1)
    18  	}
    19  
    20  	// Skip entire hook if lockable read only feature is disabled
    21  	if !cfg.SetLockableFilesReadOnly() {
    22  		os.Exit(0)
    23  	}
    24  
    25  	requireGitVersion()
    26  
    27  	lockClient := newLockClient()
    28  
    29  	// Skip this hook if no lockable patterns have been configured
    30  	if len(lockClient.GetLockablePatterns()) == 0 {
    31  		os.Exit(0)
    32  	}
    33  
    34  	// The only argument this hook receives is a flag indicating whether the
    35  	// merge was a squash merge; we don't know what files changed.
    36  	// Whether it's squash or not is irrelevant, either way it could have
    37  	// reset the read-only flag on files that got merged.
    38  
    39  	tracerx.Printf("post-merge: checking write flags for all lockable files")
    40  	// Sadly we don't get any information about what files were checked out,
    41  	// so we have to check the entire repo
    42  	err := lockClient.FixAllLockableFileWriteFlags()
    43  	if err != nil {
    44  		LoggedError(err, "Warning: post-merge locked file check failed: %v", err)
    45  	}
    46  }
    47  
    48  func init() {
    49  	RegisterCommand("post-merge", postMergeCommand, nil)
    50  }