github.com/psexton/git-lfs@v2.1.1-0.20170517224304-289a18b2bc53+incompatible/lfs/setup.go (about)

     1  package lfs
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  )
     7  
     8  var (
     9  	// prePushHook invokes `git lfs pre-push` at the pre-push phase.
    10  	prePushHook = NewStandardHook("pre-push", []string{
    11  		"#!/bin/sh\ngit lfs push --stdin $*",
    12  		"#!/bin/sh\ngit lfs push --stdin \"$@\"",
    13  		"#!/bin/sh\ngit lfs pre-push \"$@\"",
    14  		"#!/bin/sh\ncommand -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\\nThis repository has been set up with Git LFS but Git LFS is not installed.\\n\"; exit 0; }\ngit lfs pre-push \"$@\"",
    15  		"#!/bin/sh\ncommand -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\\nThis repository has been set up with Git LFS but Git LFS is not installed.\\n\"; exit 2; }\ngit lfs pre-push \"$@\"",
    16  	})
    17  	// postCheckoutHook invokes `git lfs post-checkout`
    18  	postCheckoutHook = NewStandardHook("post-checkout", []string{})
    19  	postCommitHook   = NewStandardHook("post-commit", []string{})
    20  	postMergeHook    = NewStandardHook("post-merge", []string{})
    21  
    22  	hooks = []*Hook{
    23  		prePushHook,
    24  		postCheckoutHook,
    25  		postCommitHook,
    26  		postMergeHook,
    27  	}
    28  
    29  	filters = &Attribute{
    30  		Section: "filter.lfs",
    31  		Properties: map[string]string{
    32  			"clean":    "git-lfs clean -- %f",
    33  			"smudge":   "git-lfs smudge -- %f",
    34  			"process":  "git-lfs filter-process",
    35  			"required": "true",
    36  		},
    37  		Upgradeables: map[string][]string{
    38  			"clean":   []string{"git-lfs clean %f"},
    39  			"smudge":  []string{"git-lfs smudge %f"},
    40  			"process": []string{"git-lfs filter"},
    41  		},
    42  	}
    43  
    44  	passFilters = &Attribute{
    45  		Section: "filter.lfs",
    46  		Properties: map[string]string{
    47  			"clean":    "git-lfs clean -- %f",
    48  			"smudge":   "git-lfs smudge --skip -- %f",
    49  			"process":  "git-lfs filter-process --skip",
    50  			"required": "true",
    51  		},
    52  		Upgradeables: map[string][]string{
    53  			"clean":   []string{"git-lfs clean %f"},
    54  			"smudge":  []string{"git-lfs smudge --skip %f"},
    55  			"process": []string{"git-lfs filter --skip"},
    56  		},
    57  	}
    58  )
    59  
    60  // Get user-readable manual install steps for hooks
    61  func GetHookInstallSteps() string {
    62  	var buf bytes.Buffer
    63  	for _, h := range hooks {
    64  		buf.WriteString(fmt.Sprintf("Add the following to .git/hooks/%s :\n\n", h.Type))
    65  		buf.WriteString(h.Contents)
    66  		buf.WriteString("\n")
    67  	}
    68  	return buf.String()
    69  }
    70  
    71  // InstallHooks installs all hooks in the `hooks` var.
    72  func InstallHooks(force bool) error {
    73  	for _, h := range hooks {
    74  		if err := h.Install(force); err != nil {
    75  			return err
    76  		}
    77  	}
    78  
    79  	return nil
    80  }
    81  
    82  // UninstallHooks removes all hooks in range of the `hooks` var.
    83  func UninstallHooks() error {
    84  	for _, h := range hooks {
    85  		if err := h.Uninstall(); err != nil {
    86  			return err
    87  		}
    88  	}
    89  
    90  	return nil
    91  }
    92  
    93  // InstallFilters installs filters necessary for git-lfs to process normal git
    94  // operations. Currently, that list includes:
    95  //   - smudge filter
    96  //   - clean filter
    97  //
    98  // An error will be returned if a filter is unable to be set, or if the required
    99  // filters were not present.
   100  func InstallFilters(opt InstallOptions, passThrough bool) error {
   101  	if passThrough {
   102  		return passFilters.Install(opt)
   103  	}
   104  	return filters.Install(opt)
   105  }
   106  
   107  // UninstallFilters proxies into the Uninstall method on the Filters type to
   108  // remove all installed filters.
   109  func UninstallFilters(opt InstallOptions) error {
   110  	filters.Uninstall(opt)
   111  	return nil
   112  }