@@ -495,47 +495,48 @@ static int git_attr_system(void)
495
495
496
496
static void bootstrap_attr_stack (void )
497
497
{
498
- if (!attr_stack ) {
499
- struct attr_stack * elem ;
498
+ struct attr_stack * elem ;
500
499
501
- elem = read_attr_from_array (builtin_attr );
502
- elem -> origin = NULL ;
503
- elem -> prev = attr_stack ;
504
- attr_stack = elem ;
500
+ if (attr_stack )
501
+ return ;
505
502
506
- if (git_attr_system ()) {
507
- elem = read_attr_from_file (git_etc_gitattributes (), 1 );
508
- if (elem ) {
509
- elem -> origin = NULL ;
510
- elem -> prev = attr_stack ;
511
- attr_stack = elem ;
512
- }
513
- }
503
+ elem = read_attr_from_array (builtin_attr );
504
+ elem -> origin = NULL ;
505
+ elem -> prev = attr_stack ;
506
+ attr_stack = elem ;
514
507
515
- if (git_attributes_file ) {
516
- elem = read_attr_from_file (git_attributes_file , 1 );
517
- if (elem ) {
518
- elem -> origin = NULL ;
519
- elem -> prev = attr_stack ;
520
- attr_stack = elem ;
521
- }
508
+ if (git_attr_system ()) {
509
+ elem = read_attr_from_file (git_etc_gitattributes (), 1 );
510
+ if (elem ) {
511
+ elem -> origin = NULL ;
512
+ elem -> prev = attr_stack ;
513
+ attr_stack = elem ;
522
514
}
515
+ }
523
516
524
- if (!is_bare_repository () || direction == GIT_ATTR_INDEX ) {
525
- elem = read_attr (GITATTRIBUTES_FILE , 1 );
526
- elem -> origin = xstrdup ("" );
517
+ if (git_attributes_file ) {
518
+ elem = read_attr_from_file (git_attributes_file , 1 );
519
+ if (elem ) {
520
+ elem -> origin = NULL ;
527
521
elem -> prev = attr_stack ;
528
522
attr_stack = elem ;
529
- debug_push (elem );
530
523
}
524
+ }
531
525
532
- elem = read_attr_from_file (git_path (INFOATTRIBUTES_FILE ), 1 );
533
- if (!elem )
534
- elem = xcalloc (1 , sizeof (* elem ));
535
- elem -> origin = NULL ;
526
+ if (!is_bare_repository () || direction == GIT_ATTR_INDEX ) {
527
+ elem = read_attr (GITATTRIBUTES_FILE , 1 );
528
+ elem -> origin = xstrdup ("" );
536
529
elem -> prev = attr_stack ;
537
530
attr_stack = elem ;
531
+ debug_push (elem );
538
532
}
533
+
534
+ elem = read_attr_from_file (git_path (INFOATTRIBUTES_FILE ), 1 );
535
+ if (!elem )
536
+ elem = xcalloc (1 , sizeof (* elem ));
537
+ elem -> origin = NULL ;
538
+ elem -> prev = attr_stack ;
539
+ attr_stack = elem ;
539
540
}
540
541
541
542
static void prepare_attr_stack (const char * path )
@@ -575,14 +576,17 @@ static void prepare_attr_stack(const char *path)
575
576
576
577
/*
577
578
* Pop the ones from directories that are not the prefix of
578
- * the path we are checking.
579
+ * the path we are checking. Break out of the loop when we see
580
+ * the root one (whose origin is an empty string "") or the builtin
581
+ * one (whose origin is NULL) without popping it.
579
582
*/
580
- while (attr_stack && attr_stack -> origin ) {
583
+ while (attr_stack -> origin ) {
581
584
int namelen = strlen (attr_stack -> origin );
582
585
583
586
elem = attr_stack ;
584
587
if (namelen <= dirlen &&
585
- !strncmp (elem -> origin , path , namelen ))
588
+ !strncmp (elem -> origin , path , namelen ) &&
589
+ (!namelen || path [namelen ] == '/' ))
586
590
break ;
587
591
588
592
debug_pop (elem );
@@ -594,8 +598,15 @@ static void prepare_attr_stack(const char *path)
594
598
* Read from parent directories and push them down
595
599
*/
596
600
if (!is_bare_repository () || direction == GIT_ATTR_INDEX ) {
601
+ /*
602
+ * bootstrap_attr_stack() should have added, and the
603
+ * above loop should have stopped before popping, the
604
+ * root element whose attr_stack->origin is set to an
605
+ * empty string.
606
+ */
597
607
struct strbuf pathbuf = STRBUF_INIT ;
598
608
609
+ assert (attr_stack -> origin );
599
610
while (1 ) {
600
611
len = strlen (attr_stack -> origin );
601
612
if (dirlen <= len )
0 commit comments