attr: push the bare repo check into read_attr()

Push the bare repository check into the 'read_attr()' function. This avoids needing to have extra logic which creates an empty stack frame when inside a bare repo as a similar bit of logic already exists in the 'read_attr()' function. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Jan 27, 2017 at 18:02 UTC 0787dafdccfb09b26501293f72db74638c3834ad
1 file changed +54 -60
attr.c
+54 -60
@@ -747,25 +747,28 @@ static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)
747
748 static struct attr_stack *read_attr(const char *path, int macro_ok)
749 {
750 - struct attr_stack *res;
750 + struct attr_stack *res = NULL;
751
752 - if (direction == GIT_ATTR_CHECKOUT) {
752 + if (direction == GIT_ATTR_INDEX) {
753 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) {
756 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 + }
770 }
767 - else
768 - res = read_attr_from_index(path, macro_ok);
771 +
772 if (!res)
773 res = xcalloc(1, sizeof(*res));
774 return res;
@@ -857,10 +860,7 @@ static void bootstrap_attr_stack(struct attr_stack **stack)
860 }
861
862 /* 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);
864 push_stack(stack, e, xstrdup(""), 0);
865
866 /* info frame */
@@ -877,6 +877,7 @@ static void prepare_attr_stack(const char *path, int dirlen,
877 struct attr_stack **stack)
878 {
879 struct attr_stack *info;
880 + struct strbuf pathbuf = STRBUF_INIT;
881
882 /*
883 * 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,
924 }
925
926 /*
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.
931 */
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);
960 }
961
962 /*
963 * Finally push the "info" one at the top of the stack.
964 */
965 push_stack(stack, info, NULL, 0);
966 +
967 + strbuf_release(&pathbuf);
968 }
969
970 static int path_matches(const char *pathname, int pathlen,