Skip to content

Commit 4c0ce07

Browse files
committed
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 <[email protected]> Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Brandon Williams <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 860a74d commit 4c0ce07

File tree

1 file changed

+33
-38
lines changed

1 file changed

+33
-38
lines changed

attr.c

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -510,44 +510,42 @@ static int git_attr_system(void)
510510

511511
static GIT_PATH_FUNC(git_path_info_attributes, INFOATTRIBUTES_FILE)
512512

513+
static void push_stack(struct attr_stack **attr_stack_p,
514+
struct attr_stack *elem, char *origin, size_t originlen)
515+
{
516+
if (elem) {
517+
elem->origin = origin;
518+
if (origin)
519+
elem->originlen = originlen;
520+
elem->prev = *attr_stack_p;
521+
*attr_stack_p = elem;
522+
}
523+
}
524+
513525
static void bootstrap_attr_stack(void)
514526
{
515527
struct attr_stack *elem;
516528

517529
if (attr_stack)
518530
return;
519531

520-
elem = read_attr_from_array(builtin_attr);
521-
elem->origin = NULL;
522-
elem->prev = attr_stack;
523-
attr_stack = elem;
524-
525-
if (git_attr_system()) {
526-
elem = read_attr_from_file(git_etc_gitattributes(), 1);
527-
if (elem) {
528-
elem->origin = NULL;
529-
elem->prev = attr_stack;
530-
attr_stack = elem;
531-
}
532-
}
532+
push_stack(&attr_stack, read_attr_from_array(builtin_attr), NULL, 0);
533+
534+
if (git_attr_system())
535+
push_stack(&attr_stack,
536+
read_attr_from_file(git_etc_gitattributes(), 1),
537+
NULL, 0);
533538

534539
if (!git_attributes_file)
535540
git_attributes_file = xdg_config_home("attributes");
536-
if (git_attributes_file) {
537-
elem = read_attr_from_file(git_attributes_file, 1);
538-
if (elem) {
539-
elem->origin = NULL;
540-
elem->prev = attr_stack;
541-
attr_stack = elem;
542-
}
543-
}
541+
if (git_attributes_file)
542+
push_stack(&attr_stack,
543+
read_attr_from_file(git_attributes_file, 1),
544+
NULL, 0);
544545

545546
if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
546547
elem = read_attr(GITATTRIBUTES_FILE, 1);
547-
elem->origin = xstrdup("");
548-
elem->originlen = 0;
549-
elem->prev = attr_stack;
550-
attr_stack = elem;
548+
push_stack(&attr_stack, elem, xstrdup(""), 0);
551549
debug_push(elem);
552550
}
553551

@@ -558,15 +556,12 @@ static void bootstrap_attr_stack(void)
558556

559557
if (!elem)
560558
elem = xcalloc(1, sizeof(*elem));
561-
elem->origin = NULL;
562-
elem->prev = attr_stack;
563-
attr_stack = elem;
559+
push_stack(&attr_stack, elem, NULL, 0);
564560
}
565561

566562
static void prepare_attr_stack(const char *path, int dirlen)
567563
{
568564
struct attr_stack *elem, *info;
569-
int len;
570565
const char *cp;
571566

572567
/*
@@ -626,20 +621,21 @@ static void prepare_attr_stack(const char *path, int dirlen)
626621

627622
assert(attr_stack->origin);
628623
while (1) {
629-
len = strlen(attr_stack->origin);
624+
size_t len = strlen(attr_stack->origin);
625+
char *origin;
626+
630627
if (dirlen <= len)
631628
break;
632629
cp = memchr(path + len + 1, '/', dirlen - len - 1);
633630
if (!cp)
634631
cp = path + dirlen;
635-
strbuf_add(&pathbuf, path, cp - path);
636-
strbuf_addch(&pathbuf, '/');
637-
strbuf_addstr(&pathbuf, GITATTRIBUTES_FILE);
632+
strbuf_addf(&pathbuf,
633+
"%.*s/%s", (int)(cp - path), path,
634+
GITATTRIBUTES_FILE);
638635
elem = read_attr(pathbuf.buf, 0);
639636
strbuf_setlen(&pathbuf, cp - path);
640-
elem->origin = strbuf_detach(&pathbuf, &elem->originlen);
641-
elem->prev = attr_stack;
642-
attr_stack = elem;
637+
origin = strbuf_detach(&pathbuf, &len);
638+
push_stack(&attr_stack, elem, origin, len);
643639
debug_push(elem);
644640
}
645641

@@ -649,8 +645,7 @@ static void prepare_attr_stack(const char *path, int dirlen)
649645
/*
650646
* Finally push the "info" one at the top of the stack.
651647
*/
652-
info->prev = attr_stack;
653-
attr_stack = info;
648+
push_stack(&attr_stack, info, NULL, 0);
654649
}
655650

656651
static int path_matches(const char *pathname, int pathlen,

0 commit comments

Comments
 (0)