attr.c: add push_stack() helper

There are too many repetitious "I have this new attr_stack element;
push it at the top of the stack" sequence.  The new helper function
push_stack() gives us a way to express what is going on at these
places, and as a side effect, halves the number of times we mention
the attr_stack global variable.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Brandon Williams <bmwill@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Junio C Hamano
2017-01-27 18:01:51 -08:00
parent 860a74d9d9
commit 4c0ce0742b

69
attr.c
View File

@ -510,6 +510,18 @@ static int git_attr_system(void)
static GIT_PATH_FUNC(git_path_info_attributes, INFOATTRIBUTES_FILE) static GIT_PATH_FUNC(git_path_info_attributes, INFOATTRIBUTES_FILE)
static void push_stack(struct attr_stack **attr_stack_p,
struct attr_stack *elem, char *origin, size_t originlen)
{
if (elem) {
elem->origin = origin;
if (origin)
elem->originlen = originlen;
elem->prev = *attr_stack_p;
*attr_stack_p = elem;
}
}
static void bootstrap_attr_stack(void) static void bootstrap_attr_stack(void)
{ {
struct attr_stack *elem; struct attr_stack *elem;
@ -517,37 +529,23 @@ static void bootstrap_attr_stack(void)
if (attr_stack) if (attr_stack)
return; return;
elem = read_attr_from_array(builtin_attr); push_stack(&attr_stack, read_attr_from_array(builtin_attr), NULL, 0);
elem->origin = NULL;
elem->prev = attr_stack;
attr_stack = elem;
if (git_attr_system()) { if (git_attr_system())
elem = read_attr_from_file(git_etc_gitattributes(), 1); push_stack(&attr_stack,
if (elem) { read_attr_from_file(git_etc_gitattributes(), 1),
elem->origin = NULL; NULL, 0);
elem->prev = attr_stack;
attr_stack = elem;
}
}
if (!git_attributes_file) if (!git_attributes_file)
git_attributes_file = xdg_config_home("attributes"); git_attributes_file = xdg_config_home("attributes");
if (git_attributes_file) { if (git_attributes_file)
elem = read_attr_from_file(git_attributes_file, 1); push_stack(&attr_stack,
if (elem) { read_attr_from_file(git_attributes_file, 1),
elem->origin = NULL; NULL, 0);
elem->prev = attr_stack;
attr_stack = elem;
}
}
if (!is_bare_repository() || direction == GIT_ATTR_INDEX) { if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
elem = read_attr(GITATTRIBUTES_FILE, 1); elem = read_attr(GITATTRIBUTES_FILE, 1);
elem->origin = xstrdup(""); push_stack(&attr_stack, elem, xstrdup(""), 0);
elem->originlen = 0;
elem->prev = attr_stack;
attr_stack = elem;
debug_push(elem); debug_push(elem);
} }
@ -558,15 +556,12 @@ static void bootstrap_attr_stack(void)
if (!elem) if (!elem)
elem = xcalloc(1, sizeof(*elem)); elem = xcalloc(1, sizeof(*elem));
elem->origin = NULL; push_stack(&attr_stack, elem, NULL, 0);
elem->prev = attr_stack;
attr_stack = elem;
} }
static void prepare_attr_stack(const char *path, int dirlen) static void prepare_attr_stack(const char *path, int dirlen)
{ {
struct attr_stack *elem, *info; struct attr_stack *elem, *info;
int len;
const char *cp; const char *cp;
/* /*
@ -626,20 +621,21 @@ static void prepare_attr_stack(const char *path, int dirlen)
assert(attr_stack->origin); assert(attr_stack->origin);
while (1) { while (1) {
len = strlen(attr_stack->origin); size_t len = strlen(attr_stack->origin);
char *origin;
if (dirlen <= len) if (dirlen <= len)
break; break;
cp = memchr(path + len + 1, '/', dirlen - len - 1); cp = memchr(path + len + 1, '/', dirlen - len - 1);
if (!cp) if (!cp)
cp = path + dirlen; cp = path + dirlen;
strbuf_add(&pathbuf, path, cp - path); strbuf_addf(&pathbuf,
strbuf_addch(&pathbuf, '/'); "%.*s/%s", (int)(cp - path), path,
strbuf_addstr(&pathbuf, GITATTRIBUTES_FILE); GITATTRIBUTES_FILE);
elem = read_attr(pathbuf.buf, 0); elem = read_attr(pathbuf.buf, 0);
strbuf_setlen(&pathbuf, cp - path); strbuf_setlen(&pathbuf, cp - path);
elem->origin = strbuf_detach(&pathbuf, &elem->originlen); origin = strbuf_detach(&pathbuf, &len);
elem->prev = attr_stack; push_stack(&attr_stack, elem, origin, len);
attr_stack = elem;
debug_push(elem); debug_push(elem);
} }
@ -649,8 +645,7 @@ static void prepare_attr_stack(const char *path, int dirlen)
/* /*
* Finally push the "info" one at the top of the stack. * Finally push the "info" one at the top of the stack.
*/ */
info->prev = attr_stack; push_stack(&attr_stack, info, NULL, 0);
attr_stack = info;
} }
static int path_matches(const char *pathname, int pathlen, static int path_matches(const char *pathname, int pathlen,