@@ -495,47 +495,48 @@ static int git_attr_system(void)
495495
496496static void bootstrap_attr_stack (void )
497497{
498- if (!attr_stack ) {
499- struct attr_stack * elem ;
498+ struct attr_stack * elem ;
500499
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 ;
505502
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 ;
514507
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 ;
522514 }
515+ }
523516
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 ;
527521 elem -> prev = attr_stack ;
528522 attr_stack = elem ;
529- debug_push (elem );
530523 }
524+ }
531525
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 ("" );
536529 elem -> prev = attr_stack ;
537530 attr_stack = elem ;
531+ debug_push (elem );
538532 }
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 ;
539540}
540541
541542static void prepare_attr_stack (const char * path )
@@ -575,14 +576,17 @@ static void prepare_attr_stack(const char *path)
575576
576577 /*
577578 * 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.
579582 */
580- while (attr_stack && attr_stack -> origin ) {
583+ while (attr_stack -> origin ) {
581584 int namelen = strlen (attr_stack -> origin );
582585
583586 elem = attr_stack ;
584587 if (namelen <= dirlen &&
585- !strncmp (elem -> origin , path , namelen ))
588+ !strncmp (elem -> origin , path , namelen ) &&
589+ (!namelen || path [namelen ] == '/' ))
586590 break ;
587591
588592 debug_pop (elem );
@@ -594,8 +598,15 @@ static void prepare_attr_stack(const char *path)
594598 * Read from parent directories and push them down
595599 */
596600 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+ */
597607 struct strbuf pathbuf = STRBUF_INIT ;
598608
609+ assert (attr_stack -> origin );
599610 while (1 ) {
600611 len = strlen (attr_stack -> origin );
601612 if (dirlen <= len )
0 commit comments