forked from git-lfs/git-lfs
/
setup.go
127 lines (104 loc) · 2.82 KB
/
setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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)
}