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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
/*
* Copyright (C) 2015, 2022 Ivan Motsch <ivan.motsch@bsiag.com> and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.attributes;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.function.Supplier;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.attributes.Attribute.State;
import org.eclipse.jgit.dircache.DirCacheIterator;
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.treewalk.AbstractTreeIterator;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.TreeWalk.OperationType;
import org.eclipse.jgit.treewalk.WorkingTreeIterator;
/**
* The attributes handler knows how to retrieve, parse and merge attributes from
* the various gitattributes files. Furthermore it collects and expands macro
* expressions. The method {@link #getAttributes()} yields the ready processed
* attributes for the current path represented by the
* {@link org.eclipse.jgit.treewalk.TreeWalk}
* <p>
* The implementation is based on the specifications in
* http://git-scm.com/docs/gitattributes
*
* @since 4.3
*/
public class AttributesHandler {
private static final String MACRO_PREFIX = "[attr]"; //$NON-NLS-1$
private static final String BINARY_RULE_KEY = "binary"; //$NON-NLS-1$
/**
* This is the default <b>binary</b> rule that is present in any git folder
* <code>[attr]binary -diff -merge -text</code>
*/
private static final List<Attribute> BINARY_RULE_ATTRIBUTES = new AttributesRule(
MACRO_PREFIX + BINARY_RULE_KEY, "-diff -merge -text") //$NON-NLS-1$
.getAttributes();
private final TreeWalk treeWalk;
private final Supplier<CanonicalTreeParser> attributesTree;
private final AttributesNode globalNode;
private final AttributesNode infoNode;
private final Map<String, List<Attribute>> expansions = new HashMap<>();
/**
* Create an {@link org.eclipse.jgit.attributes.AttributesHandler} with
* default rules as well as merged rules from global, info and worktree root
* attributes
*
* @param treeWalk
* a {@link org.eclipse.jgit.treewalk.TreeWalk}
* @throws java.io.IOException
* if an IO error occurred
* @deprecated since 6.1, use {@link #AttributesHandler(TreeWalk, Supplier)}
* instead
*/
@Deprecated
public AttributesHandler(TreeWalk treeWalk) throws IOException {
this(treeWalk, () -> treeWalk.getTree(CanonicalTreeParser.class));
}
/**
* Create an {@link org.eclipse.jgit.attributes.AttributesHandler} with
* default rules as well as merged rules from global, info and worktree root
* attributes
*
* @param treeWalk
* a {@link org.eclipse.jgit.treewalk.TreeWalk}
* @param attributesTree
* the tree to read .gitattributes from
* @throws java.io.IOException
* if an IO error occurred
* @since 6.1
*/
public AttributesHandler(TreeWalk treeWalk,
Supplier<CanonicalTreeParser> attributesTree) throws IOException {
this.treeWalk = treeWalk;
this.attributesTree = attributesTree;
AttributesNodeProvider attributesNodeProvider = treeWalk
.getAttributesNodeProvider();
this.globalNode = attributesNodeProvider != null
? attributesNodeProvider.getGlobalAttributesNode() : null;
this.infoNode = attributesNodeProvider != null
? attributesNodeProvider.getInfoAttributesNode() : null;
AttributesNode rootNode = attributesNode(treeWalk,
rootOf(treeWalk.getTree(WorkingTreeIterator.class)),
rootOf(treeWalk.getTree(DirCacheIterator.class)),
rootOf(attributesTree.get()));
expansions.put(BINARY_RULE_KEY, BINARY_RULE_ATTRIBUTES);
for (AttributesNode node : new AttributesNode[] { globalNode, rootNode,
infoNode }) {
if (node == null) {
continue;
}
for (AttributesRule rule : node.getRules()) {
if (rule.getPattern().startsWith(MACRO_PREFIX)) {
expansions.put(rule.getPattern()
.substring(MACRO_PREFIX.length()).trim(),
rule.getAttributes());
}
}
}
}
/**
* See {@link org.eclipse.jgit.treewalk.TreeWalk#getAttributes()}
*
* @return the {@link org.eclipse.jgit.attributes.Attributes} for the
* current path represented by the
* {@link org.eclipse.jgit.treewalk.TreeWalk}
* @throws java.io.IOException
* if an IO error occurred
*/
public Attributes getAttributes() throws IOException {
String entryPath = treeWalk.getPathString();
boolean isDirectory = (treeWalk.getFileMode() == FileMode.TREE);
Attributes attributes = new Attributes();
// Gets the info attributes
mergeInfoAttributes(entryPath, isDirectory, attributes);
// Gets the attributes located on the current entry path
mergePerDirectoryEntryAttributes(entryPath, entryPath.lastIndexOf('/'),
isDirectory,
treeWalk.getTree(WorkingTreeIterator.class),
treeWalk.getTree(DirCacheIterator.class),
attributesTree.get(),
attributes);
// Gets the attributes located in the global attribute file
mergeGlobalAttributes(entryPath, isDirectory, attributes);
// now after all attributes are collected - in the correct hierarchy
// order - remove all unspecified entries (the ! marker)
for (Attribute a : attributes.getAll()) {
if (a.getState() == State.UNSPECIFIED)
attributes.remove(a.getKey());
}
return attributes;
}
/**
* Merges the matching GLOBAL attributes for an entry path.
*
* @param entryPath
* the path to test. The path must be relative to this attribute
* node's own repository path, and in repository path format
* (uses '/' and not '\').
* @param isDirectory
* true if the target item is a directory.
* @param result
* that will hold the attributes matching this entry path. This
* method will NOT override any existing entry in attributes.
*/
private void mergeGlobalAttributes(String entryPath, boolean isDirectory,
Attributes result) {
mergeAttributes(globalNode, entryPath, isDirectory, result);
}
/**
* Merges the matching INFO attributes for an entry path.
*
* @param entryPath
* the path to test. The path must be relative to this attribute
* node's own repository path, and in repository path format
* (uses '/' and not '\').
* @param isDirectory
* true if the target item is a directory.
* @param result
* that will hold the attributes matching this entry path. This
* method will NOT override any existing entry in attributes.
*/
private void mergeInfoAttributes(String entryPath, boolean isDirectory,
Attributes result) {
mergeAttributes(infoNode, entryPath, isDirectory, result);
}
/**
* Merges the matching working directory attributes for an entry path.
*
* @param entryPath
* the path to test. The path must be relative to this attribute
* node's own repository path, and in repository path format
* (uses '/' and not '\').
* @param nameRoot
* index of the '/' preceeding the current level, or -1 if none
* @param isDirectory
* true if the target item is a directory.
* @param workingTreeIterator
* the working tree iterator
* @param dirCacheIterator
* the dircache iterator
* @param otherTree
* another tree
* @param result
* that will hold the attributes matching this entry path. This
* method will NOT override any existing entry in attributes.
* @throws IOException
* if an IO error occurred
*/
private void mergePerDirectoryEntryAttributes(String entryPath,
int nameRoot, boolean isDirectory,
@Nullable WorkingTreeIterator workingTreeIterator,
@Nullable DirCacheIterator dirCacheIterator,
@Nullable CanonicalTreeParser otherTree, Attributes result)
throws IOException {
// Prevents infinite recurrence
if (workingTreeIterator != null || dirCacheIterator != null
|| otherTree != null) {
AttributesNode attributesNode = attributesNode(
treeWalk, workingTreeIterator, dirCacheIterator, otherTree);
if (attributesNode != null) {
mergeAttributes(attributesNode,
entryPath.substring(nameRoot + 1), isDirectory,
result);
}
mergePerDirectoryEntryAttributes(entryPath,
entryPath.lastIndexOf('/', nameRoot - 1), isDirectory,
parentOf(workingTreeIterator), parentOf(dirCacheIterator),
parentOf(otherTree), result);
}
}
/**
* Merges the matching node attributes for an entry path.
*
* @param node
* the node to scan for matches to entryPath
* @param entryPath
* the path to test. The path must be relative to this attribute
* node's own repository path, and in repository path format
* (uses '/' and not '\').
* @param isDirectory
* true if the target item is a directory.
* @param result
* that will hold the attributes matching this entry path. This
* method will NOT override any existing entry in attributes.
*/
protected void mergeAttributes(@Nullable AttributesNode node,
String entryPath,
boolean isDirectory, Attributes result) {
if (node == null)
return;
List<AttributesRule> rules = node.getRules();
// Parse rules in the reverse order that they were read since the last
// entry should be used
ListIterator<AttributesRule> ruleIterator = rules
.listIterator(rules.size());
while (ruleIterator.hasPrevious()) {
AttributesRule rule = ruleIterator.previous();
if (rule.isMatch(entryPath, isDirectory)) {
ListIterator<Attribute> attributeIte = rule.getAttributes()
.listIterator(rule.getAttributes().size());
// Parses the attributes in the reverse order that they were
// read since the last entry should be used
while (attributeIte.hasPrevious()) {
expandMacro(attributeIte.previous(), result);
}
}
}
}
/**
* Expand a macro
*
* @param attr
* a {@link org.eclipse.jgit.attributes.Attribute}
* @param result
* contains the (recursive) expanded and merged macro attributes
* including the attribute iself
*/
protected void expandMacro(Attribute attr, Attributes result) {
// loop detection = exists check
if (result.containsKey(attr.getKey()))
return;
// also add macro to result set, same does native git
result.put(attr);
List<Attribute> expansion = expansions.get(attr.getKey());
if (expansion == null) {
return;
}
switch (attr.getState()) {
case UNSET: {
for (Attribute e : expansion) {
switch (e.getState()) {
case SET:
expandMacro(new Attribute(e.getKey(), State.UNSET), result);
break;
case UNSET:
expandMacro(new Attribute(e.getKey(), State.SET), result);
break;
case UNSPECIFIED:
expandMacro(new Attribute(e.getKey(), State.UNSPECIFIED),
result);
break;
case CUSTOM:
default:
expandMacro(e, result);
}
}
break;
}
case CUSTOM: {
for (Attribute e : expansion) {
switch (e.getState()) {
case SET:
case UNSET:
case UNSPECIFIED:
expandMacro(e, result);
break;
case CUSTOM:
default:
expandMacro(new Attribute(e.getKey(), attr.getValue()),
result);
}
}
break;
}
case UNSPECIFIED: {
for (Attribute e : expansion) {
expandMacro(new Attribute(e.getKey(), State.UNSPECIFIED),
result);
}
break;
}
case SET:
default:
for (Attribute e : expansion) {
expandMacro(e, result);
}
break;
}
}
/**
* Get the {@link AttributesNode} for the current entry.
* <p>
* This method implements the fallback mechanism between the index and the
* working tree depending on the operation type
* </p>
*
* @param treeWalk
* used to walk trees
* @param workingTreeIterator
* used to walk the working tree
* @param dirCacheIterator
* used to walk the dircache
* @param otherTree
* another tree
* @return a {@link AttributesNode} of the current entry,
* {@link NullPointerException} otherwise.
* @throws IOException
* It raises an {@link IOException} if a problem appears while
* parsing one on the attributes file.
*/
private static AttributesNode attributesNode(TreeWalk treeWalk,
@Nullable WorkingTreeIterator workingTreeIterator,
@Nullable DirCacheIterator dirCacheIterator,
@Nullable CanonicalTreeParser otherTree) throws IOException {
AttributesNode attributesNode = null;
switch (treeWalk.getOperationType()) {
case CHECKIN_OP:
if (workingTreeIterator != null) {
attributesNode = workingTreeIterator.getEntryAttributesNode();
}
if (attributesNode == null && dirCacheIterator != null) {
attributesNode = dirCacheIterator
.getEntryAttributesNode(treeWalk.getObjectReader());
}
if (attributesNode == null && otherTree != null) {
attributesNode = otherTree
.getEntryAttributesNode(treeWalk.getObjectReader());
}
break;
case CHECKOUT_OP:
if (otherTree != null) {
attributesNode = otherTree
.getEntryAttributesNode(treeWalk.getObjectReader());
}
if (attributesNode == null && dirCacheIterator != null) {
attributesNode = dirCacheIterator
.getEntryAttributesNode(treeWalk.getObjectReader());
}
if (attributesNode == null && workingTreeIterator != null) {
attributesNode = workingTreeIterator.getEntryAttributesNode();
}
break;
default:
throw new IllegalStateException(
"The only supported operation types are:" //$NON-NLS-1$
+ OperationType.CHECKIN_OP + "," //$NON-NLS-1$
+ OperationType.CHECKOUT_OP);
}
return attributesNode;
}
private static <T extends AbstractTreeIterator> T parentOf(@Nullable T node) {
if(node==null) return null;
@SuppressWarnings("unchecked")
Class<T> type = (Class<T>) node.getClass();
AbstractTreeIterator parent = node.parent;
if (type.isInstance(parent)) {
return type.cast(parent);
}
return null;
}
private static <T extends AbstractTreeIterator> T rootOf(
@Nullable T node) {
if(node==null) return null;
AbstractTreeIterator t=node;
while (t!= null && t.parent != null) {
t= t.parent;
}
@SuppressWarnings("unchecked")
Class<T> type = (Class<T>) node.getClass();
if (type.isInstance(t)) {
return type.cast(t);
}
return null;
}
}
|