Skip to content

Commit 87727a2

Browse files
committed
Automatically upgrade old filters instead of requiring —force
Also remove upgrade path for `git-media` since over-general and should not be needed any more
1 parent df4923c commit 87727a2

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

lfs/attribute.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@ package lfs
22

33
import (
44
"fmt"
5-
"regexp"
65
"strings"
76

87
"github.com/github/git-lfs/git"
98
)
109

11-
var (
12-
valueRegexp = regexp.MustCompile("\\Agit[\\-\\s]media")
13-
)
14-
1510
// Attribute wraps the structure and some operations of Git's conception of an
1611
// "attribute", as defined here: http://git-scm.com/docs/gitattributes.
1712
type Attribute struct {
@@ -27,6 +22,8 @@ type Attribute struct {
2722
// The Properties of an Attribute refer to all of the keys and values
2823
// that define that Attribute.
2924
Properties map[string]string
25+
// Previous values of these attributes that can be automatically upgraded
26+
Upgradeables map[string][]string
3027
}
3128

3229
// InstallOptions serves as an argument to Install().
@@ -44,8 +41,13 @@ type InstallOptions struct {
4441
// returned immediately, and the rest of the attributes will not be set.
4542
func (a *Attribute) Install(opt InstallOptions) error {
4643
for k, v := range a.Properties {
44+
var upgradeables []string
45+
if a.Upgradeables != nil {
46+
// use pre-normalised key since caller will have set up the same
47+
upgradeables = a.Upgradeables[k]
48+
}
4749
key := a.normalizeKey(k)
48-
if err := a.set(key, v, opt); err != nil {
50+
if err := a.set(key, v, upgradeables, opt); err != nil {
4951
return err
5052
}
5153
}
@@ -63,7 +65,7 @@ func (a *Attribute) normalizeKey(relative string) string {
6365
// matching key already exists and the value is not equal to the desired value,
6466
// an error will be thrown if force is set to false. If force is true, the value
6567
// will be overridden.
66-
func (a *Attribute) set(key, value string, opt InstallOptions) error {
68+
func (a *Attribute) set(key, value string, upgradeables []string, opt InstallOptions) error {
6769
var currentValue string
6870
if opt.Local {
6971
currentValue = git.Config.FindLocal(key)
@@ -73,7 +75,7 @@ func (a *Attribute) set(key, value string, opt InstallOptions) error {
7375
currentValue = git.Config.FindGlobal(key)
7476
}
7577

76-
if opt.Force || shouldReset(currentValue) {
78+
if opt.Force || shouldReset(currentValue, upgradeables) {
7779
var err error
7880
if opt.Local {
7981
// ignore error for unset, git returns non-zero if missing
@@ -106,11 +108,17 @@ func (a *Attribute) Uninstall() {
106108

107109
// shouldReset determines whether or not a value is resettable given its current
108110
// value on the system. If the value is empty (length = 0), then it will pass.
109-
// Otherwise, it will pass if the below regex matches.
110-
func shouldReset(value string) bool {
111+
// It will also pass if it matches any upgradeable value
112+
func shouldReset(value string, upgradeables []string) bool {
111113
if len(value) == 0 {
112114
return true
113115
}
114116

115-
return valueRegexp.MatchString(value)
117+
for _, u := range upgradeables {
118+
if value == u {
119+
return true
120+
}
121+
}
122+
123+
return false
116124
}

lfs/setup.go

+8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ var (
3030
"smudge": "git-lfs smudge -- %f",
3131
"required": "true",
3232
},
33+
Upgradeables: map[string][]string{
34+
"clean": []string{"git-lfs clean %f"},
35+
"smudge": []string{"git-lfs smudge %f"},
36+
},
3337
}
3438

3539
passFilters = &Attribute{
@@ -39,6 +43,10 @@ var (
3943
"smudge": "git-lfs smudge --skip -- %f",
4044
"required": "true",
4145
},
46+
Upgradeables: map[string][]string{
47+
"clean": []string{"git-lfs clean %f"},
48+
"smudge": []string{"git-lfs smudge --skip %f"},
49+
},
4250
}
4351
)
4452

test/test-install.sh

+19-5
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ begin_test "install again"
1919
)
2020
end_test
2121

22-
begin_test "install with old settings"
22+
begin_test "install with old (non-upgradeable) settings"
2323
(
2424
set -e
2525

26-
git config --global filter.lfs.smudge "git lfs smudge %f"
27-
git config --global filter.lfs.clean "git lfs clean %f"
26+
git config --global filter.lfs.smudge "git-lfs smudge --something %f"
27+
git config --global filter.lfs.clean "git-lfs clean --something %f"
2828

2929
set +e
3030
git lfs install 2> install.log
@@ -37,15 +37,29 @@ begin_test "install with old settings"
3737
grep -E "(clean|smudge) attribute should be" install.log
3838
[ `grep -c "(MISSING)" install.log` = "0" ]
3939

40-
[ "git lfs smudge %f" = "$(git config --global filter.lfs.smudge)" ]
41-
[ "git lfs clean %f" = "$(git config --global filter.lfs.clean)" ]
40+
[ "git-lfs smudge --something %f" = "$(git config --global filter.lfs.smudge)" ]
41+
[ "git-lfs clean --something %f" = "$(git config --global filter.lfs.clean)" ]
4242

4343
git lfs install --force
4444
[ "git-lfs smudge -- %f" = "$(git config --global filter.lfs.smudge)" ]
4545
[ "git-lfs clean -- %f" = "$(git config --global filter.lfs.clean)" ]
4646
)
4747
end_test
4848

49+
begin_test "install with upgradeable settings"
50+
(
51+
set -e
52+
53+
git config --global filter.lfs.smudge "git-lfs smudge %f"
54+
git config --global filter.lfs.clean "git-lfs clean %f"
55+
56+
# should not need force, should upgrade this old style
57+
git lfs install
58+
[ "git-lfs smudge -- %f" = "$(git config --global filter.lfs.smudge)" ]
59+
[ "git-lfs clean -- %f" = "$(git config --global filter.lfs.clean)" ]
60+
)
61+
end_test
62+
4963
begin_test "install updates repo hooks"
5064
(
5165
set -e

0 commit comments

Comments
 (0)