Debian Codegraph @

git/gitattr/tree.go

raw symbols SCIP
1 package gitattr
2
3 import (
4 "io"
5 "os"
6 "strings"
7
8 "github.com/git-lfs/git-lfs/v3/errors"
9 "github.com/git-lfs/git-lfs/v3/tr"
10 "github.com/git-lfs/gitobj/v2"
11 )
12
13 // Environment is a restricted version of config.Environment that only provides
14 // a single method.
15 type Environment interface {
16 // Get is shorthand for calling `e.Fetcher.Get(key)`.
17 Get(key string) (val string, ok bool)
18 }
19
20 // Tree represents the .gitattributes file at one layer of the tree in a Git
21 // repository.
22 type Tree struct {
23 mp *MacroProcessor
24
25 systemAttributes *Tree
26
27 userAttributes *Tree
28 // lines are the lines of the .gitattributes at this level of the tree.
29 lines []Line
30 // children are the named child directories in the repository.
31 children map[string]*Tree
32
33 repoAttributes *Tree
34 }
35
36 // New constructs a *Tree starting at the given tree "t" and reading objects
37 // from the given ObjectDatabase. If a tree was not able to be read, an error
38 // will be propagated up accordingly.
39 func New(db *gitobj.ObjectDatabase, t *gitobj.Tree) (*Tree, error) {
40 processor := NewMacroProcessor()
41 return newFromGitTree(db, t, processor)
42 }
43
44 func newFromGitTree(db *gitobj.ObjectDatabase, t *gitobj.Tree, mp *MacroProcessor) (*Tree, error) {
45 children := make(map[string]*Tree)
46 tree, err := linesInTree(db, t, mp)
47
48 if err != nil {
49 return nil, err
50 }
51
52 for _, entry := range t.Entries {
53 if entry.Type() != gitobj.TreeObjectType {
54 continue
55 }
56
57 // For every entry in the current tree, parse its sub-trees to
58 // see if they might contain a .gitattributes.
59 subGitTree, err := db.Tree(entry.Oid)
60 if err != nil {
61 return nil, err
62 }
63
64 subTree, err := newFromGitTree(db, subGitTree, mp)
65 if err != nil {
66 return nil, err
67 }
68
69 if len(subTree.children) > 0 || len(subTree.lines) > 0 {
70 // Only include entries that have either (1) a
71 // .gitattributes in their tree, or (2) a .gitattributes
72 // in a sub-tree.
73 children[entry.Name] = subTree
74 }
75 }
76 tree.children = children
77
78 return tree, nil
79 }
80
81 func NewFromReader(mp *MacroProcessor, rdr io.Reader) (*Tree, error) {
82 lines, _, err := ParseLines(rdr)
83 if err != nil {
84 return nil, err
85 }
86 return &Tree{
87 mp: mp,
88 lines: lines,
89 }, nil
90 }
91
92 func (t *Tree) FindSpecialAttributes(gitEnv, osEnv Environment, gitDir string) error {
93 systemPath, err := GetSystemAttributeFilePath(osEnv)
94 if err != nil {
95 return err
96 }
97
98 if len(systemPath) != 0 {
99 systemReader, err := os.Open(systemPath)
100 if err != nil {
101 return err
102 }
103 defer systemReader.Close()
104 t.systemAttributes, err = NewFromReader(t.mp, systemReader)
105
106 if err != nil {
107 return err
108 }
109 }
110
111 userPath, err := GetUserAttributeFilePath(gitEnv)
112 if err != nil {
113 return err
114 }
115
116 if len(userPath) != 0 {
117 userReader, err := os.Open(userPath)
118 if err != nil {
119 return err
120 }
121 defer userReader.Close()
122 if t.userAttributes, err = NewFromReader(t.mp, userReader); err != nil {
123 return err
124 }
125 }
126
127 repoPath, err := GetRepoAttributeFilePath(gitDir)
128 if err != nil {
129 return err
130 }
131
132 if len(repoPath) != 0 {
133 repoReader, err := os.Open(repoPath)
134 if err != nil {
135 return err
136 }
137 defer repoReader.Close()
138 t.repoAttributes, err = NewFromReader(t.mp, repoReader)
139 }
140
141 return err
142 }
143
144 // linesInTree parses a given tree's .gitattributes and returns a slice of lines
145 // in that .gitattributes, or an error. If no .gitattributes blob was found,
146 // return nil.
147 func linesInTree(db *gitobj.ObjectDatabase, t *gitobj.Tree, mp *MacroProcessor) (*Tree, error) {
148 var at int = -1
149 for i, e := range t.Entries {
150 if e.Name == ".gitattributes" {
151 if e.IsLink() {
152 return nil, errors.New(tr.Tr.Get("expected '.gitattributes' to be a file, got a symbolic link"))
153 }
154 at = i
155 break
156 }
157 }
158
159 if at < 0 {
160 return &Tree{mp: mp}, nil
161 }
162
163 blob, err := db.Blob(t.Entries[at].Oid)
164 if err != nil {
165 return nil, err
166 }
167 defer blob.Close()
168
169 return NewFromReader(mp, blob.Contents)
170 }
171
172 // Applied returns a slice of attributes applied to the given path, relative to
173 // the receiving tree. It traverse through sub-trees in a topological ordering,
174 // if there are relevant .gitattributes matching that path.
175 func (t *Tree) Applied(to string) []*Attr {
176 var attrs []*Attr
177 if !t.mp.didReadMacros {
178 if t.systemAttributes != nil {
179 t.mp.ProcessMacros(t.systemAttributes.lines)
180 }
181 if t.userAttributes != nil {
182 t.mp.ProcessMacros(t.userAttributes.lines)
183 }
184 t.mp.ProcessMacros(t.lines)
185
186 if t.repoAttributes != nil {
187 t.mp.ProcessMacros(t.repoAttributes.lines)
188 }
189 }
190
191 if t.systemAttributes != nil {
192 attrs = append(attrs, t.systemAttributes.applied(to)...)
193 }
194
195 if t.userAttributes != nil {
196 attrs = append(attrs, t.userAttributes.applied(to)...)
197 }
198
199 attrs = append(attrs, t.applied(to)...)
200
201 if t.repoAttributes != nil {
202 attrs = append(attrs, t.repoAttributes.applied(to)...)
203 }
204
205 return attrs
206 }
207
208 func (t *Tree) applied(to string) []*Attr {
209 var attrs []*Attr
210
211 for _, line := range t.mp.ProcessLines(t.lines, false) {
212 if line.Pattern().Match(to) {
213 attrs = append(attrs, line.Attrs()...)
214 }
215 }
216
217 dirPath, remainingPath, found := strings.Cut(to, "/")
218 if found {
219 if child, ok := t.children[dirPath]; ok {
220 attrs = append(attrs, child.applied(remainingPath)...)
221 }
222 }
223
224 return attrs
225 }