github.com/stffabi/git-lfs@v2.3.5-0.20180214015214-8eeaa8d88902+incompatible/commands/command_post_commit.go (about)

     1  package commands
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/git-lfs/git-lfs/git"
     7  	"github.com/rubyist/tracerx"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  // postCommitCommand is run through Git's post-commit hook. The hook passes
    12  // no arguments.
    13  // This hook checks that files which are lockable and not locked are made read-only,
    14  // optimising that based on what was added / modified in the commit.
    15  // This is mainly to catch added files, since modified files should already be
    16  // locked. If we didn't do this, any added files would remain read/write on disk
    17  // even without a lock unless something else checked.
    18  func postCommitCommand(cmd *cobra.Command, args []string) {
    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  	tracerx.Printf("post-commit: checking file write flags at HEAD")
    35  	// We can speed things up by looking at what changed in
    36  	// HEAD, and only checking those lockable files
    37  	files, err := git.GetFilesChanged("HEAD", "")
    38  
    39  	if err != nil {
    40  		LoggedError(err, "Warning: post-commit failed: %v", err)
    41  		os.Exit(1)
    42  	}
    43  	tracerx.Printf("post-commit: checking write flags on %v", files)
    44  	err = lockClient.FixLockableFileWriteFlags(files)
    45  	if err != nil {
    46  		LoggedError(err, "Warning: post-commit locked file check failed: %v", err)
    47  	}
    48  
    49  }
    50  
    51  func init() {
    52  	RegisterCommand("post-commit", postCommitCommand, nil)
    53  }