Debian Codegraph @

lfs/attribute.go

raw symbols SCIP
1 package lfs
2
3 import (
4 "errors"
5 "fmt"
6 "strings"
7
8 "github.com/git-lfs/git-lfs/v3/git"
9 "github.com/git-lfs/git-lfs/v3/tr"
10 )
11
12 // Attribute wraps the structure and some operations of Git's conception of an
13 // "attribute", as defined here: http://git-scm.com/docs/gitattributes.
14 type Attribute struct {
15 // The Section of an Attribute refers to the location at which all
16 // properties are relative to. For example, for a Section with the value
17 // "core", Git will produce something like:
18 //
19 // [core]
20 // autocrlf = true
21 // ...
22 Section string
23
24 // The Properties of an Attribute refer to all of the keys and values
25 // that define that Attribute.
26 Properties map[string]string
27 // Previous values of these attributes that can be automatically upgraded
28 Upgradeables map[string][]string
29 }
30
31 // FilterOptions serves as an argument to Install().
32 type FilterOptions struct {
33 GitConfig *git.Configuration
34 Force bool
35 File string
36 Local bool
37 Worktree bool
38 System bool
39 SkipSmudge bool
40 }
41
42 func (o *FilterOptions) Install() error {
43 if o.SkipSmudge {
44 return skipSmudgeFilterAttribute().Install(o)
45 }
46 return filterAttribute().Install(o)
47 }
48
49 func (o *FilterOptions) Uninstall() error {
50 attrs := filterAttribute()
51 if err := attrs.Uninstall(o); err != nil {
52 return err
53 }
54 for k := range attrs.Properties {
55 name := fmt.Sprintf("%s.%s", attrs.Section, k)
56 if len(o.GitConfig.Find(name)) > 0 {
57 return errors.New(tr.Tr.Get("some filter configuration was not removed (found %s)", name))
58 }
59 }
60 return nil
61 }
62
63 func filterAttribute() *Attribute {
64 return &Attribute{
65 Section: "filter.lfs",
66 Properties: map[string]string{
67 "clean": "git-lfs clean -- %f",
68 "smudge": "git-lfs smudge -- %f",
69 "process": "git-lfs filter-process",
70 "required": "true",
71 },
72 Upgradeables: map[string][]string{
73 "clean": []string{
74 "git-lfs clean %f",
75 },
76 "smudge": []string{
77 "git-lfs smudge %f",
78 "git-lfs smudge --skip %f",
79 "git-lfs smudge --skip -- %f",
80 },
81 "process": []string{
82 "git-lfs filter",
83 "git-lfs filter --skip",
84 "git-lfs filter-process --skip",
85 },
86 },
87 }
88 }
89
90 func skipSmudgeFilterAttribute() *Attribute {
91 return &Attribute{
92 Section: "filter.lfs",
93 Properties: map[string]string{
94 "clean": "git-lfs clean -- %f",
95 "smudge": "git-lfs smudge --skip -- %f",
96 "process": "git-lfs filter-process --skip",
97 "required": "true",
98 },
99 Upgradeables: map[string][]string{
100 "clean": []string{
101 "git-lfs clean -- %f",
102 },
103 "smudge": []string{
104 "git-lfs smudge %f",
105 "git-lfs smudge --skip %f",
106 "git-lfs smudge -- %f",
107 },
108 "process": []string{
109 "git-lfs filter",
110 "git-lfs filter --skip",
111 "git-lfs filter-process",
112 },
113 },
114 }
115 }
116
117 // Install instructs Git to set all keys and values relative to the root
118 // location of this Attribute. For any particular key/value pair, if a matching
119 // key is already set, it will be overridden if it is either a) empty, or b) the
120 // `force` argument is passed as true. If an attribute is already set to a
121 // different value than what is given, and force is false, an error will be
122 // returned immediately, and the rest of the attributes will not be set.
123 func (a *Attribute) Install(opt *FilterOptions) error {
124 for k, v := range a.Properties {
125 var upgradeables []string
126 if a.Upgradeables != nil {
127 // use pre-normalised key since caller will have set up the same
128 upgradeables = a.Upgradeables[k]
129 }
130 key := a.normalizeKey(k)
131 if err := a.set(opt.GitConfig, key, v, upgradeables, opt); err != nil {
132 return err
133 }
134 }
135
136 return nil
137 }
138
139 // normalizeKey makes an absolute path out of a partial relative one. For a
140 // relative path of "foo", and a root Section of "bar", "bar.foo" will be returned.
141 func (a *Attribute) normalizeKey(relative string) string {
142 return strings.Join([]string{a.Section, relative}, ".")
143 }
144
145 // set attempts to set a single key/value pair portion of this Attribute. If a
146 // matching key already exists and the value is not equal to the desired value,
147 // an error will be thrown if force is set to false. If force is true, the value
148 // will be overridden.
149 func (a *Attribute) set(gitConfig *git.Configuration, key, value string, upgradeables []string, opt *FilterOptions) error {
150 var currentValue string
151 if opt.Local {
152 currentValue = gitConfig.FindLocal(key)
153 } else if opt.Worktree {
154 currentValue = gitConfig.FindWorktree(key)
155 } else if opt.System {
156 currentValue = gitConfig.FindSystem(key)
157 } else if opt.File != "" {
158 currentValue = gitConfig.FindFile(opt.File, key)
159 } else {
160 currentValue = gitConfig.FindGlobal(key)
161 }
162
163 if opt.Force || shouldReset(currentValue, upgradeables) {
164 var err error
165 if opt.Local {
166 _, err = gitConfig.SetLocal(key, value)
167 } else if opt.Worktree {
168 _, err = gitConfig.SetWorktree(key, value)
169 } else if opt.System {
170 _, err = gitConfig.SetSystem(key, value)
171 } else if opt.File != "" {
172 _, err = gitConfig.SetFile(opt.File, key, value)
173 } else {
174 _, err = gitConfig.SetGlobal(key, value)
175 }
176 return err
177 } else if currentValue != value {
178 return errors.New(tr.Tr.Get("the %q attribute should be %q but is %q",
179 key, value, currentValue))
180 }
181
182 return nil
183 }
184
185 // Uninstall removes all properties in the path of this property.
186 func (a *Attribute) Uninstall(opt *FilterOptions) error {
187 var err error
188 if opt.Local {
189 _, err = opt.GitConfig.UnsetLocalSection(a.Section)
190 } else if opt.Worktree {
191 _, err = opt.GitConfig.UnsetWorktreeSection(a.Section)
192 } else if opt.System {
193 _, err = opt.GitConfig.UnsetSystemSection(a.Section)
194 } else if opt.File != "" {
195 _, err = opt.GitConfig.UnsetFileSection(opt.File, a.Section)
196 } else {
197 _, err = opt.GitConfig.UnsetGlobalSection(a.Section)
198 }
199 return err
200 }
201
202 // shouldReset determines whether or not a value is resettable given its current
203 // value on the system. If the value is empty (length = 0), then it will pass.
204 // It will also pass if it matches any upgradeable value
205 func shouldReset(value string, upgradeables []string) bool {
206 if len(value) == 0 {
207 return true
208 }
209
210 for _, u := range upgradeables {
211 if value == u {
212 return true
213 }
214 }
215
216 return false
217 }