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>
Junio C Hamano committed
Jan 27, 2017 at 18:01 UTC
4c0ce0742b19c2a430b809224911b9212313265f
1 file changed
+33
-38
attr.c
+33
-38
@@ -510,6 +510,18 @@ static int git_attr_system(void)
510
511
static GIT_PATH_FUNC(git_path_info_attributes, INFOATTRIBUTES_FILE)
512
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
+
525
static void bootstrap_attr_stack(void)
526
{
527
struct attr_stack *elem;
@@ -517,37 +529,23 @@ static void bootstrap_attr_stack(void)
529
if (attr_stack)
530
return;
531
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);
538
539
if (!git_attributes_file)
540
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);
545
546
if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
547
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);
549
debug_push(elem);
550
}
551
@@ -558,15 +556,12 @@ static void bootstrap_attr_stack(void)
556
557
if (!elem)
558
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);
560
}
561
562
static void prepare_attr_stack(const char *path, int dirlen)
563
{
564
struct attr_stack *elem, *info;
569
- int len;
565
const char *cp;
566
567
/*
@@ -626,20 +621,21 @@ static void prepare_attr_stack(const char *path, int dirlen)
621
622
assert(attr_stack->origin);
623
while (1) {
629
- len = strlen(attr_stack->origin);
624
+ size_t len = strlen(attr_stack->origin);
625
+ char *origin;
626
+
627
if (dirlen <= len)
628
break;
629
cp = memchr(path + len + 1, '/', dirlen - len - 1);
630
if (!cp)
631
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);
635
elem = read_attr(pathbuf.buf, 0);
636
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);
639
debug_push(elem);
640
}
641
@@ -649,8 +645,7 @@ static void prepare_attr_stack(const char *path, int dirlen)
645
/*
646
* Finally push the "info" one at the top of the stack.
647
*/
652
- info->prev = attr_stack;
653
- attr_stack = info;
648
+ push_stack(&attr_stack, info, NULL, 0);
649
}
650
651
static int path_matches(const char *pathname, int pathlen,