git-lfs/lfs/setup.go

112 lines
3.0 KiB
Go
Raw Normal View History

2015-03-19 19:30:55 +00:00
package lfs
import (
"bytes"
"fmt"
)
2014-06-04 14:29:19 +00:00
var (
// prePushHook invokes `git lfs pre-push` at the pre-push phase.
prePushHook = NewStandardHook("pre-push", []string{
"#!/bin/sh\ngit lfs push --stdin $*",
"#!/bin/sh\ngit lfs push --stdin \"$@\"",
"#!/bin/sh\ngit lfs pre-push \"$@\"",
"#!/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 \"$@\"",
"#!/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 \"$@\"",
})
// postCheckoutHook invokes `git lfs post-checkout`
postCheckoutHook = NewStandardHook("post-checkout", []string{})
postCommitHook = NewStandardHook("post-commit", []string{})
hooks = []*Hook{
prePushHook,
postCheckoutHook,
postCommitHook,
}
filters = &Attribute{
Section: "filter.lfs",
Properties: map[string]string{
"clean": "git-lfs clean -- %f",
"smudge": "git-lfs smudge -- %f",
"process": "git-lfs filter-process",
"required": "true",
},
Upgradeables: map[string][]string{
2016-11-08 14:24:36 +00:00
"clean": []string{"git-lfs clean %f"},
"smudge": []string{"git-lfs smudge %f"},
"process": []string{"git-lfs filter"},
},
2015-07-10 16:43:20 +00:00
}
2015-09-23 18:20:30 +00:00
passFilters = &Attribute{
Section: "filter.lfs",
Properties: map[string]string{
"clean": "git-lfs clean -- %f",
"smudge": "git-lfs smudge --skip -- %f",
"process": "git-lfs filter-process --skip",
2015-09-23 18:20:30 +00:00
"required": "true",
},
Upgradeables: map[string][]string{
2016-11-08 14:24:36 +00:00
"clean": []string{"git-lfs clean %f"},
"smudge": []string{"git-lfs smudge --skip %f"},
"process": []string{"git-lfs filter --skip"},
},
2015-09-23 18:20:30 +00:00
}
)
2015-07-10 16:43:20 +00:00
// Get user-readable manual install steps for hooks
func GetHookInstallSteps() string {
var buf bytes.Buffer
for _, h := range hooks {
buf.WriteString(fmt.Sprintf("Add the following to .git/hooks/%s :\n\n", h.Type))
buf.WriteString(h.Contents)
buf.WriteString("\n")
}
return buf.String()
}
// InstallHooks installs all hooks in the `hooks` var.
func InstallHooks(force bool) error {
for _, h := range hooks {
if err := h.Install(force); err != nil {
return err
}
2015-07-10 16:43:20 +00:00
}
return nil
}
2015-10-01 19:44:28 +00:00
// UninstallHooks removes all hooks in range of the `hooks` var.
func UninstallHooks() error {
for _, h := range hooks {
if err := h.Uninstall(); err != nil {
return err
}
}
return nil
}
// InstallFilters installs filters necessary for git-lfs to process normal git
// operations. Currently, that list includes:
// - smudge filter
// - clean filter
//
// An error will be returned if a filter is unable to be set, or if the required
// filters were not present.
2015-09-23 18:20:30 +00:00
func InstallFilters(opt InstallOptions, passThrough bool) error {
if passThrough {
return passFilters.Install(opt)
}
2015-09-23 17:58:16 +00:00
return filters.Install(opt)
2015-07-10 16:43:20 +00:00
}
// UninstallFilters proxies into the Uninstall method on the Filters type to
// remove all installed filters.
func UninstallFilters() error {
filters.Uninstall()
2014-06-05 18:48:23 +00:00
return nil
}