git-lfs/lfs/setup.go
rubyist b363c569bd Make the pre-push hook check that git-lfs exists first
If git-lfs does not exist (e.g. it has been uninstalled) the pre-push
hook should still exit cleanly so the push can continue. The script will
output a warning stating that the repo has been set up for Git LFS but
that it is not installed.
2015-05-27 11:43:06 -04:00

128 lines
2.8 KiB
Go

package lfs
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/github/git-lfs/git"
)
var (
valueRegexp = regexp.MustCompile("\\Agit[\\-\\s]media")
NotInARepositoryError = errors.New("Not in a repository")
prePushHook = "#!/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 \"$@\""
prePushUpgrades = map[string]bool{
"#!/bin/sh\ngit lfs push --stdin $*": true,
"#!/bin/sh\ngit lfs push --stdin \"$@\"": true,
"#!/bin/sh\ngit lfs pre-push \"$@\"": true,
}
)
type HookExists struct {
Name string
Path string
Contents string
}
func (e *HookExists) Error() string {
return fmt.Sprintf("Hook already exists: %s\n\n%s\n", e.Name, e.Contents)
}
func InstallHooks(force bool) error {
if !InRepo() {
return NotInARepositoryError
}
if err := os.MkdirAll(filepath.Join(LocalGitDir, "hooks"), 0755); err != nil {
return err
}
hookPath := filepath.Join(LocalGitDir, "hooks", "pre-push")
if _, err := os.Stat(hookPath); err == nil && !force {
return upgradeHookOrError(hookPath, "pre-push", prePushHook, prePushUpgrades)
}
return ioutil.WriteFile(hookPath, []byte(prePushHook+"\n"), 0755)
}
func upgradeHookOrError(hookPath, hookName, hook string, upgrades map[string]bool) error {
file, err := os.Open(hookPath)
if err != nil {
return err
}
by, err := ioutil.ReadAll(io.LimitReader(file, 1024))
file.Close()
if err != nil {
return err
}
contents := strings.TrimSpace(string(by))
if contents == hook {
return nil
}
if upgrades[contents] {
return ioutil.WriteFile(hookPath, []byte(hook+"\n"), 0755)
}
return &HookExists{hookName, hookPath, contents}
}
func InstallFilters() error {
if err := setFilter("clean"); err != nil {
return err
}
if err := setFilter("smudge"); err != nil {
return err
}
if err := requireFilters(); err != nil {
return err
}
return nil
}
func setFilter(filterName string) error {
key := fmt.Sprintf("filter.lfs.%s", filterName)
value := fmt.Sprintf("git lfs %s %%f", filterName)
existing := git.Config.Find(key)
if shouldReset(existing) {
git.Config.UnsetGlobal(key)
git.Config.SetGlobal(key, value)
} else if existing != value {
return fmt.Errorf("The %s filter should be \"%s\" but is \"%s\"", filterName, value, existing)
}
return nil
}
func requireFilters() error {
key := "filter.lfs.required"
value := "true"
existing := git.Config.Find(key)
if shouldReset(existing) {
git.Config.UnsetGlobal(key)
git.Config.SetGlobal(key, value)
} else if existing != value {
return errors.New("Git LFS filters should be required but are not.")
}
return nil
}
func shouldReset(value string) bool {
if len(value) == 0 {
return true
}
return valueRegexp.MatchString(value)
}