From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS31976 209.132.180.0/23 X-Spam-Status: No, score=-4.0 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD shortcircuit=no autolearn=ham autolearn_force=no version=3.4.0 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by dcvr.yhbt.net (Postfix) with ESMTP id 87C4420986 for ; Sat, 22 Oct 2016 23:33:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965031AbcJVXdP (ORCPT ); Sat, 22 Oct 2016 19:33:15 -0400 Received: from mail-it0-f48.google.com ([209.85.214.48]:38056 "EHLO mail-it0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935666AbcJVXdJ (ORCPT ); Sat, 22 Oct 2016 19:33:09 -0400 Received: by mail-it0-f48.google.com with SMTP id 66so74091560itl.1 for ; Sat, 22 Oct 2016 16:33:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=d6OHLLeyyRG0W9mwj1Daqf3Kr7V65pd/1fgS281ux50=; b=e4TY4HFSc7+fQ26BJgEiiFzRile8RYKAuUsiYluW07/Enha3KsC7kiwkCFwbRrq2sw B106n8QGCtPjxTdYk8xAucpxp001A4orAahfmfL395UnghqyrnCEdk8mujT6dwOeeGL6 2Gr1xYX14K2oIQr71VZ9VOovyskKOSONTVOK0GdJc2zVlh5e/sYBnu94zxsITR0lTyZ3 GWnOenHrHjCT7sIcZ2zSldAlhvF5cGF6DUpoAR7MCgChvfvGur/LJrpM+DV9BChPsQTg d/Z/BfdTNuGLH75qPF3abi2d0IQjB/7d4uG0/1Lkq3uIX9IKEtYqQGRScAuVgUaU1ojP axkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=d6OHLLeyyRG0W9mwj1Daqf3Kr7V65pd/1fgS281ux50=; b=Q/jQD1qHIFb5N85eoSCkrWKTowOozJRvBiwSknOM+ScYNgFeNqVIxAt5nCOUlfq3G6 1s3IXqM6uOht4CkIsODWnb10saKQWpzlalx5O6OjEx90RrbGx6T6ZllkpZadhIroVdUl ReXqe4RYcQmwi6mE3wC5rIeyFzyV8B1+YFdGmcVe/TSf87Wp6ItHMcgeACurxn9Th7Jd EOhTNKyQt3DtXempwLPJJryk596aGItiGJO7IDjNNFUngtqKzK9QvzjZaH8nSBZAs7+u uM8HlwpEVYngxhgLkthLXzrN003SqWmIJfPX57yP0MZTPiMI78glsqpxAb9ys02T2jdD 2yfw== X-Gm-Message-State: ABUngveCCM24t6YM8t7D+eGSAu6SA79+00ROQ5jz2Yga92W0XcV+Zu52LrnmywZjepC9eQrA X-Received: by 10.107.142.78 with SMTP id q75mr7901577iod.4.1477179188538; Sat, 22 Oct 2016 16:33:08 -0700 (PDT) Received: from localhost ([2620:0:1000:5b10:4ca0:b1d9:cc22:1c7b]) by smtp.gmail.com with ESMTPSA id 80sm4549564iot.36.2016.10.22.16.33.08 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Sat, 22 Oct 2016 16:33:08 -0700 (PDT) From: Stefan Beller To: gitster@pobox.com Cc: git@vger.kernel.org, bmwill@google.com, pclouds@gmail.com, Stefan Beller Subject: [PATCH 19/36] attr.c: add push_stack() helper Date: Sat, 22 Oct 2016 16:32:08 -0700 Message-Id: <20161022233225.8883-20-sbeller@google.com> X-Mailer: git-send-email 2.10.1.508.g6572022 In-Reply-To: <20161022233225.8883-1-sbeller@google.com> References: <20161022233225.8883-1-sbeller@google.com> Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Junio C Hamano 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 Signed-off-by: Stefan Beller --- attr.c | 71 +++++++++++++++++++++++++++++++----------------------------------- 1 file changed, 33 insertions(+), 38 deletions(-) diff --git a/attr.c b/attr.c index eba582b..34c297d 100644 --- a/attr.c +++ b/attr.c @@ -521,6 +521,18 @@ static int git_attr_system(void) 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) { struct attr_stack *elem; @@ -528,52 +540,35 @@ static void bootstrap_attr_stack(void) if (attr_stack) return; - elem = read_attr_from_array(builtin_attr); - elem->origin = NULL; - elem->prev = attr_stack; - attr_stack = elem; - - if (git_attr_system()) { - elem = read_attr_from_file(git_etc_gitattributes(), 1); - if (elem) { - elem->origin = NULL; - elem->prev = attr_stack; - attr_stack = elem; - } - } + push_stack(&attr_stack, read_attr_from_array(builtin_attr), NULL, 0); + + if (git_attr_system()) + push_stack(&attr_stack, + read_attr_from_file(git_etc_gitattributes(), 1), + NULL, 0); if (!git_attributes_file) git_attributes_file = xdg_config_home("attributes"); - if (git_attributes_file) { - elem = read_attr_from_file(git_attributes_file, 1); - if (elem) { - elem->origin = NULL; - elem->prev = attr_stack; - attr_stack = elem; - } - } + if (git_attributes_file) + push_stack(&attr_stack, + read_attr_from_file(git_attributes_file, 1), + NULL, 0); if (!is_bare_repository() || direction == GIT_ATTR_INDEX) { elem = read_attr(GITATTRIBUTES_FILE, 1); - elem->origin = xstrdup(""); - elem->originlen = 0; - elem->prev = attr_stack; - attr_stack = elem; + push_stack(&attr_stack, elem, xstrdup(""), 0); debug_push(elem); } elem = read_attr_from_file(git_path_info_attributes(), 1); if (!elem) elem = xcalloc(1, sizeof(*elem)); - elem->origin = NULL; - elem->prev = attr_stack; - attr_stack = elem; + push_stack(&attr_stack, elem, NULL, 0); } static void prepare_attr_stack(const char *path, int dirlen) { struct attr_stack *elem, *info; - int len; const char *cp; /* @@ -633,20 +628,21 @@ static void prepare_attr_stack(const char *path, int dirlen) assert(attr_stack->origin); while (1) { - len = strlen(attr_stack->origin); + size_t len = strlen(attr_stack->origin); + char *origin; + if (dirlen <= len) break; cp = memchr(path + len + 1, '/', dirlen - len - 1); if (!cp) cp = path + dirlen; - strbuf_add(&pathbuf, path, cp - path); - strbuf_addch(&pathbuf, '/'); - strbuf_addstr(&pathbuf, GITATTRIBUTES_FILE); + strbuf_addf(&pathbuf, + "%.*s/%s", (int)(cp - path), path, + GITATTRIBUTES_FILE); elem = read_attr(pathbuf.buf, 0); strbuf_setlen(&pathbuf, cp - path); - elem->origin = strbuf_detach(&pathbuf, &elem->originlen); - elem->prev = attr_stack; - attr_stack = elem; + origin = strbuf_detach(&pathbuf, &len); + push_stack(&attr_stack, elem, origin, len); debug_push(elem); } @@ -656,8 +652,7 @@ static void prepare_attr_stack(const char *path, int dirlen) /* * Finally push the "info" one at the top of the stack. */ - info->prev = attr_stack; - attr_stack = info; + push_stack(&attr_stack, info, NULL, 0); } static int path_matches(const char *pathname, int pathlen, -- 2.10.1.508.g6572022