Commit a51cd8e6f6724079a552b75e014f792f3f68e158

Russell Belfer 2012-01-16T16:58:27

Fix handling of relative paths for attrs Per issue #533, the handling of relative paths in attribute and ignore files was not right. Fixed this by pre-joining the relative path of the attribute/ignore file onto the match string when a full path match is required. Unfortunately, fixing this required a bit more code than I would have liked because I had to juggle things around so that the fnmatch parser would have sufficient information to prepend the relative path when it was needed.

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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
diff --git a/src/attr.c b/src/attr.c
index dc42379..3fe76d1 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -214,7 +214,7 @@ int git_attr_cache__push_file(
 	git_vector     *stack,
 	const char     *base,
 	const char     *filename,
-	int (*loader)(git_repository *, const char *, git_attr_file **))
+	int (*loader)(git_repository *, const char *, git_attr_file *))
 {
 	int error = GIT_SUCCESS;
 	git_attr_cache *cache = &repo->attrcache;
@@ -231,11 +231,12 @@ int git_attr_cache__push_file(
 	/* either get attr_file from cache or read from disk */
 	file = git_hashtable_lookup(cache->files, filename);
 	if (file == NULL && git_futils_exists(filename) == GIT_SUCCESS) {
-		error = (*loader)(repo, filename, &file);
+		if ((error = git_attr_file__new(&file)) == GIT_SUCCESS)
+			error = (*loader)(repo, filename, file);
 		add_to_cache = (error == GIT_SUCCESS);
 	}
 
-	if (file != NULL) {
+	if (error == GIT_SUCCESS && file != NULL) {
 		/* add file to vector, if we found it */
 		error = git_vector_insert(stack, file);
 
diff --git a/src/attr.h b/src/attr.h
index 5edff30..a758cc4 100644
--- a/src/attr.h
+++ b/src/attr.h
@@ -25,6 +25,6 @@ extern int git_attr_cache__push_file(
 	git_vector     *stack,
 	const char     *base,
 	const char     *filename,
-	int (*loader)(git_repository *, const char *, git_attr_file **));
+	int (*loader)(git_repository *, const char *, git_attr_file *));
 
 #endif
diff --git a/src/attr_file.c b/src/attr_file.c
index 5ea07c9..f6eaad6 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -31,21 +31,46 @@ int git_attr_file__new(git_attr_file **attrs_ptr)
 	return error;
 }
 
+int git_attr_file__set_path(
+	git_repository *repo, const char *path, git_attr_file *file)
+{
+	if (file->path != NULL) {
+		git__free(file->path);
+		file->path = NULL;
+	}
+
+	if (repo == NULL)
+		file->path = git__strdup(path);
+	else {
+		const char *workdir = git_repository_workdir(repo);
+
+		if (workdir && git__prefixcmp(path, workdir) == 0)
+			file->path = git__strdup(path + strlen(workdir));
+		else
+			file->path = git__strdup(path);
+	}
+
+	return (file->path == NULL) ? GIT_ENOMEM : GIT_SUCCESS;
+}
+
 int git_attr_file__from_buffer(
-	git_repository *repo, const char *buffer, git_attr_file **out)
+	git_repository *repo, const char *buffer, git_attr_file *attrs)
 {
 	int error = GIT_SUCCESS;
-	git_attr_file *attrs = NULL;
 	const char *scan = NULL;
+	char *context = NULL;
 	git_attr_rule *rule = NULL;
 
-	*out = NULL;
-
-	if ((error = git_attr_file__new(&attrs)) < GIT_SUCCESS)
-		goto cleanup;
+	assert(buffer && attrs);
 
 	scan = buffer;
 
+	if (attrs->path && git__suffixcmp(attrs->path, GIT_ATTR_FILE) == 0) {
+		context = git__strndup(attrs->path,
+			strlen(attrs->path) - strlen(GIT_ATTR_FILE));
+		if (!context) error = GIT_ENOMEM;
+	}
+
 	while (error == GIT_SUCCESS && *scan) {
 		/* allocate rule if needed */
 		if (!rule && !(rule = git__calloc(1, sizeof(git_attr_rule)))) {
@@ -54,7 +79,7 @@ int git_attr_file__from_buffer(
 		}
 
 		/* parse the next "pattern attr attr attr" line */
-		if (!(error = git_attr_fnmatch__parse(&rule->match, &scan)) &&
+		if (!(error = git_attr_fnmatch__parse(&rule->match, context, &scan)) &&
 			!(error = git_attr_assignment__parse(repo, &rule->assigns, &scan)))
 		{
 			if (rule->match.flags & GIT_ATTR_FNMATCH_MACRO)
@@ -76,35 +101,30 @@ int git_attr_file__from_buffer(
 		}
 	}
 
-cleanup:
-	if (error != GIT_SUCCESS) {
-		git_attr_rule__free(rule);
-		git_attr_file__free(attrs);
-	} else {
-		*out = attrs;
-	}
+	git_attr_rule__free(rule);
+	git__free(context);
 
 	return error;
 }
 
 int git_attr_file__from_file(
-	git_repository *repo, const char *path, git_attr_file **out)
+	git_repository *repo, const char *path, git_attr_file *file)
 {
 	int error = GIT_SUCCESS;
 	git_fbuffer fbuf = GIT_FBUFFER_INIT;
 
-	*out = NULL;
+	assert(path && file);
 
-	if ((error = git_futils_readbuffer(&fbuf, path)) < GIT_SUCCESS ||
-		(error = git_attr_file__from_buffer(repo, fbuf.data, out)) < GIT_SUCCESS)
-	{
-		git__rethrow(error, "Could not open attribute file '%s'", path);
-	} else {
-		/* save path (okay to fail) */
-		(*out)->path = git__strdup(path);
-	}
+	if (file->path == NULL)
+		error = git_attr_file__set_path(repo, path, file);
+
+	if (error == GIT_SUCCESS &&
+		(error = git_futils_readbuffer(&fbuf, path)) == GIT_SUCCESS)
+		error = git_attr_file__from_buffer(repo, fbuf.data, file);
 
 	git_futils_freebuffer(&fbuf);
+	if (error != GIT_SUCCESS)
+		git__rethrow(error, "Could not open attribute file '%s'", path);
 
 	return error;
 }
@@ -267,6 +287,7 @@ int git_attr_path__init(
  */
 int git_attr_fnmatch__parse(
 	git_attr_fnmatch *spec,
+	const char *source,
 	const char **base)
 {
 	const char *pattern, *scan;
@@ -312,32 +333,40 @@ int git_attr_fnmatch__parse(
 	*base = scan;
 
 	spec->length = scan - pattern;
-	spec->pattern = git__strndup(pattern, spec->length);
-
-	if (!spec->pattern) {
-		*base = git__next_line(pattern);
-		return GIT_ENOMEM;
-	} else {
-		/* remove '\' that might have be used for internal whitespace */
-		char *from = spec->pattern, *to = spec->pattern;
-		while (*from) {
-			if (*from == '\\') {
-				from++;
-				spec->length--;
-			}
-			*to++ = *from++;
-		}
-		*to = '\0';
-	}
 
 	if (pattern[spec->length - 1] == '/') {
 		spec->length--;
-		spec->pattern[spec->length] = '\0';
 		spec->flags = spec->flags | GIT_ATTR_FNMATCH_DIRECTORY;
 		if (--slash_count <= 0)
 			spec->flags = spec->flags & ~GIT_ATTR_FNMATCH_FULLPATH;
 	}
 
+	if ((spec->flags & GIT_ATTR_FNMATCH_FULLPATH) != 0 &&
+		source != NULL && git_path_root(pattern) < 0)
+	{
+		size_t sourcelen = strlen(source);
+		/* given an unrooted fullpath match from a file inside a repo,
+		 * prefix the pattern with the relative directory of the source file
+		 */
+		spec->pattern = git__malloc(sourcelen + spec->length + 1);
+		if (spec->pattern) {
+			memcpy(spec->pattern, source, sourcelen);
+			memcpy(spec->pattern + sourcelen, pattern, spec->length);
+			spec->length += sourcelen;
+			spec->pattern[spec->length] = '\0';
+		}
+	} else {
+		spec->pattern = git__strndup(pattern, spec->length);
+	}
+
+	if (!spec->pattern) {
+		*base = git__next_line(pattern);
+		return GIT_ENOMEM;
+	} else {
+		/* remove '\' that might have be used for internal whitespace */
+		spec->length = git__removechar(spec->pattern, '\\');
+	}
+
 	return GIT_SUCCESS;
 }
 
diff --git a/src/attr_file.h b/src/attr_file.h
index 7190c4c..304c7a8 100644
--- a/src/attr_file.h
+++ b/src/attr_file.h
@@ -62,13 +62,16 @@ typedef struct {
  * git_attr_file API
  */
 
+extern int git_attr_file__new(git_attr_file **attrs_ptr);
+extern void git_attr_file__free(git_attr_file *file);
+
 extern int git_attr_file__from_buffer(
-	git_repository *repo, const char *buf, git_attr_file **out);
+	git_repository *repo, const char *buf, git_attr_file *file);
 extern int git_attr_file__from_file(
-	git_repository *repo, const char *path, git_attr_file **out);
+	git_repository *repo, const char *path, git_attr_file *file);
 
-extern int git_attr_file__new(git_attr_file **attrs_ptr);
-extern void git_attr_file__free(git_attr_file *file);
+extern int git_attr_file__set_path(
+	git_repository *repo, const char *path, git_attr_file *file);
 
 extern int git_attr_file__lookup_one(
 	git_attr_file *file,
@@ -90,6 +93,7 @@ extern unsigned long git_attr_file__name_hash(const char *name);
 
 extern int git_attr_fnmatch__parse(
 	git_attr_fnmatch *spec,
+	const char *source,
 	const char **base);
 
 extern int git_attr_fnmatch__match(
diff --git a/src/ignore.c b/src/ignore.c
index 1040574..388a4b2 100644
--- a/src/ignore.c
+++ b/src/ignore.c
@@ -8,22 +8,25 @@
 #define GIT_IGNORE_CONFIG		"core.excludesfile"
 
 static int load_ignore_file(
-	git_repository *GIT_UNUSED(repo), const char *path, git_attr_file **out)
+	git_repository *repo, const char *path, git_attr_file *ignores)
 {
 	int error = GIT_SUCCESS;
 	git_fbuffer fbuf = GIT_FBUFFER_INIT;
-	git_attr_file *ignores = NULL;
 	git_attr_fnmatch *match = NULL;
 	const char *scan = NULL;
+	char *context = NULL;
 
-	GIT_UNUSED_ARG(repo);
+	if (ignores->path == NULL)
+		error = git_attr_file__set_path(repo, path, ignores);
 
-	*out = NULL;
-
-	if ((error = git_futils_readbuffer(&fbuf, path)) == GIT_SUCCESS)
-		error = git_attr_file__new(&ignores);
+	if (git__suffixcmp(ignores->path, GIT_IGNORE_FILE) == 0) {
+		context = git__strndup(ignores->path,
+			strlen(ignores->path) - strlen(GIT_IGNORE_FILE));
+		if (!context) error = GIT_ENOMEM;
+	}
 
-	ignores->path = git__strdup(path);
+	if (error == GIT_SUCCESS)
+		error = git_futils_readbuffer(&fbuf, path);
 
 	scan = fbuf.data;
 
@@ -33,7 +36,7 @@ static int load_ignore_file(
 			break;
 		}
 
-		if (!(error = git_attr_fnmatch__parse(match, &scan))) {
+		if (!(error = git_attr_fnmatch__parse(match, context, &scan))) {
 			match->flags = match->flags | GIT_ATTR_FNMATCH_IGNORE;
 			scan = git__next_line(scan);
 			error = git_vector_insert(&ignores->rules, match);
@@ -52,13 +55,10 @@ static int load_ignore_file(
 
 	git_futils_freebuffer(&fbuf);
 	git__free(match);
+	git__free(context);
 
-	if (error != GIT_SUCCESS) {
+	if (error != GIT_SUCCESS)
 		git__rethrow(error, "Could not open ignore file '%s'", path);
-		git_attr_file__free(ignores);
-	} else {
-		*out = ignores;
-	}
 
 	return error;
 }
diff --git a/src/util.c b/src/util.c
index 1ca9d85..f47de9e 100644
--- a/src/util.c
+++ b/src/util.c
@@ -156,6 +156,23 @@ void git__strtolower(char *str)
 	git__strntolower(str, strlen(str));
 }
 
+size_t git__removechar(char *str, char remove)
+{
+	char *from = str, *to = str;
+
+	while (*from) {
+		if (*from == remove)
+			from++;
+		if (to != from)
+			*to = *from;
+		to++;
+		from++;
+	}
+	*to = '\0';
+
+	return (to - str);
+}
+
 int git__prefixcmp(const char *str, const char *prefix)
 {
 	for (;;) {
diff --git a/src/util.h b/src/util.h
index 6c929cf..818e6f0 100644
--- a/src/util.h
+++ b/src/util.h
@@ -102,6 +102,8 @@ extern char *git__strtok(char **end, const char *sep);
 extern void git__strntolower(char *str, size_t len);
 extern void git__strtolower(char *str);
 
+extern size_t git__removechar(char *str, char remove);
+
 GIT_INLINE(const char *) git__next_line(const char *s)
 {
 	while (*s && *s != '\n') s++;
diff --git a/tests-clay/attr/file.c b/tests-clay/attr/file.c
index acca0c6..652ee27 100644
--- a/tests-clay/attr/file.c
+++ b/tests-clay/attr/file.c
@@ -6,11 +6,12 @@
 
 void test_attr_file__simple_read(void)
 {
-	git_attr_file *file = NULL;
+	git_attr_file *file;
 	git_attr_assignment *assign;
 	git_attr_rule *rule;
 
-	cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr0"), &file));
+	cl_git_pass(git_attr_file__new(&file));
+	cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr0"), file));
 	cl_assert_strequal(cl_fixture("attr/attr0"), file->path);
 	cl_assert(file->rules.length == 1);
 
@@ -32,11 +33,12 @@ void test_attr_file__simple_read(void)
 
 void test_attr_file__match_variants(void)
 {
-	git_attr_file *file = NULL;
+	git_attr_file *file;
 	git_attr_rule *rule;
 	git_attr_assignment *assign;
 
-	cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr1"), &file));
+	cl_git_pass(git_attr_file__new(&file));
+	cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr1"), file));
 	cl_assert_strequal(cl_fixture("attr/attr1"), file->path);
 	cl_assert(file->rules.length == 10);
 
@@ -119,11 +121,12 @@ static void check_one_assign(
 
 void test_attr_file__assign_variants(void)
 {
-	git_attr_file *file = NULL;
+	git_attr_file *file;
 	git_attr_rule *rule;
 	git_attr_assignment *assign;
 
-	cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr2"), &file));
+	cl_git_pass(git_attr_file__new(&file));
+	cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr2"), file));
 	cl_assert_strequal(cl_fixture("attr/attr2"), file->path);
 	cl_assert(file->rules.length == 11);
 
@@ -184,11 +187,12 @@ void test_attr_file__assign_variants(void)
 
 void test_attr_file__check_attr_examples(void)
 {
-	git_attr_file *file = NULL;
+	git_attr_file *file;
 	git_attr_rule *rule;
 	git_attr_assignment *assign;
 
-	cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr3"), &file));
+	cl_git_pass(git_attr_file__new(&file));
+	cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr3"), file));
 	cl_assert_strequal(cl_fixture("attr/attr3"), file->path);
 	cl_assert(file->rules.length == 3);
 
diff --git a/tests-clay/attr/lookup.c b/tests-clay/attr/lookup.c
index fcade52..b251562 100644
--- a/tests-clay/attr/lookup.c
+++ b/tests-clay/attr/lookup.c
@@ -3,11 +3,12 @@
 
 void test_attr_lookup__simple(void)
 {
-	git_attr_file *file = NULL;
+	git_attr_file *file;
 	git_attr_path path;
 	const char *value = NULL;
 
-	cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr0"), &file));
+	cl_git_pass(git_attr_file__new(&file));
+	cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr0"), file));
 	cl_assert_strequal(cl_fixture("attr/attr0"), file->path);
 	cl_assert(file->rules.length == 1);
 
@@ -60,7 +61,7 @@ static void run_test_cases(git_attr_file *file, test_case *cases)
 
 void test_attr_lookup__match_variants(void)
 {
-	git_attr_file *file = NULL;
+	git_attr_file *file;
 	git_attr_path path;
 	test_case cases[] = {
 		/* pat0 -> simple match */
@@ -132,7 +133,8 @@ void test_attr_lookup__match_variants(void)
 		{ NULL, NULL, NULL, 0, 0 }
 	};
 
-	cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr1"), &file));
+	cl_git_pass(git_attr_file__new(&file));
+	cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr1"), file));
 	cl_assert_strequal(cl_fixture("attr/attr1"), file->path);
 	cl_assert(file->rules.length == 10);
 
@@ -146,7 +148,7 @@ void test_attr_lookup__match_variants(void)
 
 void test_attr_lookup__assign_variants(void)
 {
-	git_attr_file *file = NULL;
+	git_attr_file *file;
 	test_case cases[] = {
 		/* pat0 -> simple assign */
 		{ "pat0", "simple", GIT_ATTR_TRUE, 0, 0 },
@@ -190,7 +192,8 @@ void test_attr_lookup__assign_variants(void)
 		{ NULL, NULL, NULL, 0, 0 }
 	};
 
-	cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr2"), &file));
+	cl_git_pass(git_attr_file__new(&file));
+	cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr2"), file));
 	cl_assert(file->rules.length == 11);
 
 	run_test_cases(file, cases);
@@ -200,7 +203,7 @@ void test_attr_lookup__assign_variants(void)
 
 void test_attr_lookup__check_attr_examples(void)
 {
-	git_attr_file *file = NULL;
+	git_attr_file *file;
 	test_case cases[] = {
 		{ "foo.java", "diff", "java", 1, 0 },
 		{ "foo.java", "crlf", GIT_ATTR_FALSE, 0, 0 },
@@ -224,7 +227,8 @@ void test_attr_lookup__check_attr_examples(void)
 		{ NULL, NULL, NULL, 0, 0 }
 	};
 
-	cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr3"), &file));
+	cl_git_pass(git_attr_file__new(&file));
+	cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr3"), file));
 	cl_assert(file->rules.length == 3);
 
 	run_test_cases(file, cases);
@@ -234,7 +238,7 @@ void test_attr_lookup__check_attr_examples(void)
 
 void test_attr_lookup__from_buffer(void)
 {
-	git_attr_file *file = NULL;
+	git_attr_file *file;
 	test_case cases[] = {
 		{ "abc", "foo", GIT_ATTR_TRUE, 0, 0 },
 		{ "abc", "bar", GIT_ATTR_TRUE, 0, 0 },
@@ -248,7 +252,8 @@ void test_attr_lookup__from_buffer(void)
 		{ NULL, NULL, NULL, 0, 0 }
 	};
 
-	cl_git_pass(git_attr_file__from_buffer(NULL, "a* foo\nabc bar\n* baz", &file));
+	cl_git_pass(git_attr_file__new(&file));
+	cl_git_pass(git_attr_file__from_buffer(NULL, "a* foo\nabc bar\n* baz", file));
 	cl_assert(file->rules.length == 3);
 
 	run_test_cases(file, cases);
diff --git a/tests-clay/attr/repo.c b/tests-clay/attr/repo.c
index 3e9b9de..13f28ca 100644
--- a/tests-clay/attr/repo.c
+++ b/tests-clay/attr/repo.c
@@ -45,19 +45,24 @@ void test_attr_repo__get_one(void)
 		{ "root_test3", "rootattr", NULL },
 		{ "root_test3", "multiattr", "3" },
 		{ "root_test3", "multi2", NULL },
-		{ "subdir/subdir_test1", "repoattr", GIT_ATTR_TRUE },
-		{ "subdir/subdir_test1", "rootattr", GIT_ATTR_TRUE },
-		{ "subdir/subdir_test1", "missingattr", NULL },
-		{ "subdir/subdir_test1", "subattr", "yes" },
-		{ "subdir/subdir_test1", "negattr", GIT_ATTR_FALSE },
-		{ "subdir/subdir_test1", "another", NULL },
-		{ "subdir/subdir_test2.txt", "repoattr", GIT_ATTR_TRUE },
-		{ "subdir/subdir_test2.txt", "rootattr", GIT_ATTR_TRUE },
-		{ "subdir/subdir_test2.txt", "missingattr", NULL },
-		{ "subdir/subdir_test2.txt", "subattr", "yes" },
-		{ "subdir/subdir_test2.txt", "negattr", GIT_ATTR_FALSE },
-		{ "subdir/subdir_test2.txt", "another", "one" },
+		{ "sub/subdir_test1", "repoattr", GIT_ATTR_TRUE },
+		{ "sub/subdir_test1", "rootattr", GIT_ATTR_TRUE },
+		{ "sub/subdir_test1", "missingattr", NULL },
+		{ "sub/subdir_test1", "subattr", "yes" },
+		{ "sub/subdir_test1", "negattr", GIT_ATTR_FALSE },
+		{ "sub/subdir_test1", "another", NULL },
+		{ "sub/subdir_test2.txt", "repoattr", GIT_ATTR_TRUE },
+		{ "sub/subdir_test2.txt", "rootattr", GIT_ATTR_TRUE },
+		{ "sub/subdir_test2.txt", "missingattr", NULL },
+		{ "sub/subdir_test2.txt", "subattr", "yes" },
+		{ "sub/subdir_test2.txt", "negattr", GIT_ATTR_FALSE },
+		{ "sub/subdir_test2.txt", "another", "zero" },
+		{ "sub/subdir_test2.txt", "reposub", GIT_ATTR_TRUE },
+		{ "sub/sub/subdir.txt", "another", "one" },
+		{ "sub/sub/subdir.txt", "reposubsub", GIT_ATTR_TRUE },
+		{ "sub/sub/subdir.txt", "reposub", NULL },
 		{ "does-not-exist", "foo", "yes" },
+		{ "sub/deep/file", "deepdeep", GIT_ATTR_TRUE },
 		{ NULL, NULL, NULL }
 	}, *scan;
 
@@ -105,7 +110,7 @@ void test_attr_repo__get_many(void)
 	cl_assert(values[2] == NULL);
 	cl_assert(values[3] == NULL);
 
-	cl_git_pass(git_attr_get_many(g_repo, "subdir/subdir_test1", 4, names, values));
+	cl_git_pass(git_attr_get_many(g_repo, "sub/subdir_test1", 4, names, values));
 
 	cl_assert(values[0] == GIT_ATTR_TRUE);
 	cl_assert(values[1] == GIT_ATTR_TRUE);
@@ -136,33 +141,33 @@ void test_attr_repo__foreach(void)
 	cl_assert(count == 2);
 
 	count = 0;
-	cl_git_pass(git_attr_foreach(g_repo, "subdir/subdir_test1",
+	cl_git_pass(git_attr_foreach(g_repo, "sub/subdir_test1",
 		&count_attrs, &count));
 	cl_assert(count == 4); /* repoattr, rootattr, subattr, negattr */
 
 	count = 0;
-	cl_git_pass(git_attr_foreach(g_repo, "subdir/subdir_test2.txt",
+	cl_git_pass(git_attr_foreach(g_repo, "sub/subdir_test2.txt",
 		&count_attrs, &count));
-	cl_assert(count == 5); /* repoattr, rootattr, subattr, negattr, another */
+	cl_assert(count == 6); /* repoattr, rootattr, subattr, reposub, negattr, another */
 }
 
 void test_attr_repo__manpage_example(void)
 {
 	const char *value;
 
-	cl_git_pass(git_attr_get(g_repo, "subdir/abc", "foo", &value));
+	cl_git_pass(git_attr_get(g_repo, "sub/abc", "foo", &value));
 	cl_assert(value == GIT_ATTR_TRUE);
 
-	cl_git_pass(git_attr_get(g_repo, "subdir/abc", "bar", &value));
+	cl_git_pass(git_attr_get(g_repo, "sub/abc", "bar", &value));
 	cl_assert(value == NULL);
 
-	cl_git_pass(git_attr_get(g_repo, "subdir/abc", "baz", &value));
+	cl_git_pass(git_attr_get(g_repo, "sub/abc", "baz", &value));
 	cl_assert(value == GIT_ATTR_FALSE);
 
-	cl_git_pass(git_attr_get(g_repo, "subdir/abc", "merge", &value));
+	cl_git_pass(git_attr_get(g_repo, "sub/abc", "merge", &value));
 	cl_assert_strequal("filfre", value);
 
-	cl_git_pass(git_attr_get(g_repo, "subdir/abc", "frotz", &value));
+	cl_git_pass(git_attr_get(g_repo, "sub/abc", "frotz", &value));
 	cl_assert(value == NULL);
 }
 
diff --git a/tests-clay/status/ignore.c b/tests-clay/status/ignore.c
new file mode 100644
index 0000000..f14e10c
--- /dev/null
+++ b/tests-clay/status/ignore.c
@@ -0,0 +1,49 @@
+#include "clay_libgit2.h"
+#include "fileops.h"
+#include "git2/attr.h"
+
+static git_repository *g_repo = NULL;
+
+void test_status_ignore__initialize(void)
+{
+	/* Before each test, instantiate the attr repo from the fixtures and
+	 * rename the .gitted to .git so it is a repo with a working dir.  Also
+	 * rename gitignore to .gitignore.
+	 */
+	cl_fixture_sandbox("attr");
+	cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
+	cl_git_pass(p_rename("attr/gitignore", "attr/.gitignore"));
+	cl_git_pass(git_repository_open(&g_repo, "attr/.git"));
+}
+
+void test_status_ignore__cleanup(void)
+{
+	git_repository_free(g_repo);
+	g_repo = NULL;
+	cl_fixture_cleanup("attr");
+}
+
+void test_status_ignore__0(void)
+{
+	struct {
+		const char *path;
+		int expected;
+	} test_cases[] = {
+		{ "file", 0 },
+		{ "ign", 1 },
+		{ "sub", 1 },
+		{ "sub/file", 0 },
+		{ "sub/ign", 1 },
+		{ "sub/sub", 1 },
+		{ "sub/sub/file", 0 },
+		{ "sub/sub/ign", 1 },
+		{ "sub/sub/sub", 1 },
+		{ NULL, 0 }
+	}, *one_test;
+
+	for (one_test = test_cases; one_test->path != NULL; one_test++) {
+		int ignored;
+		cl_git_pass(git_status_should_ignore(g_repo, one_test->path, &ignored));
+		cl_assert_(ignored == one_test->expected, one_test->path);
+	}
+}
diff --git a/tests/resources/attr/.gitted/info/attributes b/tests/resources/attr/.gitted/info/attributes
index 2e9643a..5fe62a3 100644
--- a/tests/resources/attr/.gitted/info/attributes
+++ b/tests/resources/attr/.gitted/info/attributes
@@ -1,2 +1,4 @@
 * repoattr
 a*	foo !bar -baz
+sub/*.txt reposub
+sub/sub/*.txt reposubsub
diff --git a/tests/resources/attr/file b/tests/resources/attr/file
new file mode 100644
index 0000000..45b983b
--- /dev/null
+++ b/tests/resources/attr/file
@@ -0,0 +1 @@
+hi
diff --git a/tests/resources/attr/gitignore b/tests/resources/attr/gitignore
new file mode 100644
index 0000000..66f7769
--- /dev/null
+++ b/tests/resources/attr/gitignore
@@ -0,0 +1,2 @@
+sub
+ign
diff --git a/tests/resources/attr/ign b/tests/resources/attr/ign
new file mode 100644
index 0000000..592fd25
--- /dev/null
+++ b/tests/resources/attr/ign
@@ -0,0 +1 @@
+ignore me
diff --git a/tests/resources/attr/sub/.gitattributes b/tests/resources/attr/sub/.gitattributes
new file mode 100644
index 0000000..329c1c5
--- /dev/null
+++ b/tests/resources/attr/sub/.gitattributes
@@ -0,0 +1,7 @@
+* subattr=yes -negattr
+*.txt     another=zero
+sub/*.txt another=one
+ab*			 merge=filfre
+abc			 -foo -bar
+*.c			 frotz
+deep/file	 deepdeep
diff --git a/tests/resources/attr/sub/abc b/tests/resources/attr/sub/abc
new file mode 100644
index 0000000..3e42ffc
--- /dev/null
+++ b/tests/resources/attr/sub/abc
@@ -0,0 +1,37 @@
+# Test file from gitattributes(5) example:
+
+If you have these three gitattributes file:
+
+   (in $GIT_DIR/info/attributes)
+
+	a*      foo !bar -baz
+
+	(in .gitattributes)
+	abc     foo bar baz
+
+	(in t/.gitattributes)
+	ab*     merge=filfre
+	abc     -foo -bar
+	*.c     frotz
+
+the attributes given to path t/abc are computed as follows:
+
+1. By examining t/.gitattributes (which is in the same directory as the path
+   in question), git finds that the first line matches. merge attribute is
+   set. It also finds that the second line matches, and attributes foo and
+   bar are unset.
+2. Then it examines .gitattributes (which is in the parent directory), and
+   finds that the first line matches, but t/.gitattributes file already
+   decided how merge, foo and bar attributes should be given to this path,
+   so it leaves foo and bar unset. Attribute baz is set.
+3. Finally it examines $GIT_DIR/info/attributes. This file is used to
+   override the in-tree settings. The first line is a match, and foo is set,
+   bar is reverted to unspecified state, and baz is unset.
+
+As the result, the attributes assignment to t/abc becomes:
+
+	foo     set to true
+	bar     unspecified
+	baz     set to false
+	merge   set to string value "filfre"
+	frotz   unspecified
diff --git a/tests/resources/attr/sub/file b/tests/resources/attr/sub/file
new file mode 100644
index 0000000..45b983b
--- /dev/null
+++ b/tests/resources/attr/sub/file
@@ -0,0 +1 @@
+hi
diff --git a/tests/resources/attr/sub/ign b/tests/resources/attr/sub/ign
new file mode 100644
index 0000000..592fd25
--- /dev/null
+++ b/tests/resources/attr/sub/ign
@@ -0,0 +1 @@
+ignore me
diff --git a/tests/resources/attr/sub/sub/file b/tests/resources/attr/sub/sub/file
new file mode 100644
index 0000000..45b983b
--- /dev/null
+++ b/tests/resources/attr/sub/sub/file
@@ -0,0 +1 @@
+hi
diff --git a/tests/resources/attr/sub/sub/subsub.txt b/tests/resources/attr/sub/sub/subsub.txt
new file mode 100644
index 0000000..9e5bdc4
--- /dev/null
+++ b/tests/resources/attr/sub/sub/subsub.txt
@@ -0,0 +1 @@
+subsub
diff --git a/tests/resources/attr/sub/subdir_test1 b/tests/resources/attr/sub/subdir_test1
new file mode 100644
index 0000000..e563cf4
--- /dev/null
+++ b/tests/resources/attr/sub/subdir_test1
@@ -0,0 +1,2 @@
+Hello from the subdir
+
diff --git a/tests/resources/attr/sub/subdir_test2.txt b/tests/resources/attr/sub/subdir_test2.txt
new file mode 100644
index 0000000..fb5067b
--- /dev/null
+++ b/tests/resources/attr/sub/subdir_test2.txt
@@ -0,0 +1 @@
+Hello again
diff --git a/tests/resources/attr/subdir/.gitattributes b/tests/resources/attr/subdir/.gitattributes
deleted file mode 100644
index 99eae47..0000000
--- a/tests/resources/attr/subdir/.gitattributes
+++ /dev/null
@@ -1,5 +0,0 @@
-* subattr=yes -negattr
-subdir/*.txt another=one
-ab*			 merge=filfre
-abc			 -foo -bar
-*.c			 frotz
diff --git a/tests/resources/attr/subdir/abc b/tests/resources/attr/subdir/abc
deleted file mode 100644
index 3e42ffc..0000000
--- a/tests/resources/attr/subdir/abc
+++ /dev/null
@@ -1,37 +0,0 @@
-# Test file from gitattributes(5) example:
-
-If you have these three gitattributes file:
-
-   (in $GIT_DIR/info/attributes)
-
-	a*      foo !bar -baz
-
-	(in .gitattributes)
-	abc     foo bar baz
-
-	(in t/.gitattributes)
-	ab*     merge=filfre
-	abc     -foo -bar
-	*.c     frotz
-
-the attributes given to path t/abc are computed as follows:
-
-1. By examining t/.gitattributes (which is in the same directory as the path
-   in question), git finds that the first line matches. merge attribute is
-   set. It also finds that the second line matches, and attributes foo and
-   bar are unset.
-2. Then it examines .gitattributes (which is in the parent directory), and
-   finds that the first line matches, but t/.gitattributes file already
-   decided how merge, foo and bar attributes should be given to this path,
-   so it leaves foo and bar unset. Attribute baz is set.
-3. Finally it examines $GIT_DIR/info/attributes. This file is used to
-   override the in-tree settings. The first line is a match, and foo is set,
-   bar is reverted to unspecified state, and baz is unset.
-
-As the result, the attributes assignment to t/abc becomes:
-
-	foo     set to true
-	bar     unspecified
-	baz     set to false
-	merge   set to string value "filfre"
-	frotz   unspecified
diff --git a/tests/resources/attr/subdir/subdir_test1 b/tests/resources/attr/subdir/subdir_test1
deleted file mode 100644
index e563cf4..0000000
--- a/tests/resources/attr/subdir/subdir_test1
+++ /dev/null
@@ -1,2 +0,0 @@
-Hello from the subdir
-
diff --git a/tests/resources/attr/subdir/subdir_test2.txt b/tests/resources/attr/subdir/subdir_test2.txt
deleted file mode 100644
index fb5067b..0000000
--- a/tests/resources/attr/subdir/subdir_test2.txt
+++ /dev/null
@@ -1 +0,0 @@
-Hello again
diff --git a/tests/resources/attr/subdir2/subdir2_test1 b/tests/resources/attr/subdir2/subdir2_test1
deleted file mode 100644
index dccada4..0000000
--- a/tests/resources/attr/subdir2/subdir2_test1
+++ /dev/null
@@ -1 +0,0 @@
-Hello from subdir2