@@ -747,25 +747,28 @@ static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)
747747
748748static struct attr_stack * read_attr (const char * path , int macro_ok )
749749{
750- struct attr_stack * res ;
750+ struct attr_stack * res = NULL ;
751751
752- if (direction == GIT_ATTR_CHECKOUT ) {
752+ if (direction == GIT_ATTR_INDEX ) {
753753 res = read_attr_from_index (path , macro_ok );
754- if (!res )
755- res = read_attr_from_file (path , macro_ok );
756- }
757- else if (direction == GIT_ATTR_CHECKIN ) {
758- res = read_attr_from_file (path , macro_ok );
759- if (!res )
760- /*
761- * There is no checked out .gitattributes file there, but
762- * we might have it in the index. We allow operation in a
763- * sparsely checked out work tree, so read from it.
764- */
754+ } else if (!is_bare_repository ()) {
755+ if (direction == GIT_ATTR_CHECKOUT ) {
765756 res = read_attr_from_index (path , macro_ok );
757+ if (!res )
758+ res = read_attr_from_file (path , macro_ok );
759+ } else if (direction == GIT_ATTR_CHECKIN ) {
760+ res = read_attr_from_file (path , macro_ok );
761+ if (!res )
762+ /*
763+ * There is no checked out .gitattributes file
764+ * there, but we might have it in the index.
765+ * We allow operation in a sparsely checked out
766+ * work tree, so read from it.
767+ */
768+ res = read_attr_from_index (path , macro_ok );
769+ }
766770 }
767- else
768- res = read_attr_from_index (path , macro_ok );
771+
769772 if (!res )
770773 res = xcalloc (1 , sizeof (* res ));
771774 return res ;
@@ -857,10 +860,7 @@ static void bootstrap_attr_stack(struct attr_stack **stack)
857860 }
858861
859862 /* root directory */
860- if (!is_bare_repository () || direction == GIT_ATTR_INDEX )
861- e = read_attr (GITATTRIBUTES_FILE , 1 );
862- else
863- e = xcalloc (1 , sizeof (struct attr_stack ));
863+ e = read_attr (GITATTRIBUTES_FILE , 1 );
864864 push_stack (stack , e , xstrdup ("" ), 0 );
865865
866866 /* info frame */
@@ -877,6 +877,7 @@ static void prepare_attr_stack(const char *path, int dirlen,
877877 struct attr_stack * * stack )
878878{
879879 struct attr_stack * info ;
880+ struct strbuf pathbuf = STRBUF_INIT ;
880881
881882 /*
882883 * At the bottom of the attribute stack is the built-in
@@ -923,54 +924,47 @@ static void prepare_attr_stack(const char *path, int dirlen,
923924 }
924925
925926 /*
926- * Read from parent directories and push them down
927+ * bootstrap_attr_stack() should have added, and the
928+ * above loop should have stopped before popping, the
929+ * root element whose attr_stack->origin is set to an
930+ * empty string.
927931 */
928- if (!is_bare_repository () || direction == GIT_ATTR_INDEX ) {
929- /*
930- * bootstrap_attr_stack() should have added, and the
931- * above loop should have stopped before popping, the
932- * root element whose attr_stack->origin is set to an
933- * empty string.
934- */
935- struct strbuf pathbuf = STRBUF_INIT ;
936-
937- assert ((* stack )-> origin );
938- strbuf_addstr (& pathbuf , (* stack )-> origin );
939- /* Build up to the directory 'path' is in */
940- while (pathbuf .len < dirlen ) {
941- size_t len = pathbuf .len ;
942- struct attr_stack * next ;
943- char * origin ;
944-
945- /* Skip path-separator */
946- if (len < dirlen && is_dir_sep (path [len ]))
947- len ++ ;
948- /* Find the end of the next component */
949- while (len < dirlen && !is_dir_sep (path [len ]))
950- len ++ ;
951-
952- if (pathbuf .len > 0 )
953- strbuf_addch (& pathbuf , '/' );
954- strbuf_add (& pathbuf , path + pathbuf .len ,
955- (len - pathbuf .len ));
956- strbuf_addf (& pathbuf , "/%s" , GITATTRIBUTES_FILE );
957-
958- next = read_attr (pathbuf .buf , 0 );
959-
960- /* reset the pathbuf to not include "/.gitattributes" */
961- strbuf_setlen (& pathbuf , len );
962-
963- origin = xstrdup (pathbuf .buf );
964- push_stack (stack , next , origin , len );
965-
966- }
967- strbuf_release (& pathbuf );
932+ assert ((* stack )-> origin );
933+
934+ strbuf_addstr (& pathbuf , (* stack )-> origin );
935+ /* Build up to the directory 'path' is in */
936+ while (pathbuf .len < dirlen ) {
937+ size_t len = pathbuf .len ;
938+ struct attr_stack * next ;
939+ char * origin ;
940+
941+ /* Skip path-separator */
942+ if (len < dirlen && is_dir_sep (path [len ]))
943+ len ++ ;
944+ /* Find the end of the next component */
945+ while (len < dirlen && !is_dir_sep (path [len ]))
946+ len ++ ;
947+
948+ if (pathbuf .len > 0 )
949+ strbuf_addch (& pathbuf , '/' );
950+ strbuf_add (& pathbuf , path + pathbuf .len , (len - pathbuf .len ));
951+ strbuf_addf (& pathbuf , "/%s" , GITATTRIBUTES_FILE );
952+
953+ next = read_attr (pathbuf .buf , 0 );
954+
955+ /* reset the pathbuf to not include "/.gitattributes" */
956+ strbuf_setlen (& pathbuf , len );
957+
958+ origin = xstrdup (pathbuf .buf );
959+ push_stack (stack , next , origin , len );
968960 }
969961
970962 /*
971963 * Finally push the "info" one at the top of the stack.
972964 */
973965 push_stack (stack , info , NULL , 0 );
966+
967+ strbuf_release (& pathbuf );
974968}
975969
976970static int path_matches (const char * pathname , int pathlen ,
0 commit comments