From mboxrd@z Thu Jan 1 00:00:00 1970 From: Junio C Hamano Subject: [PATCH 1/5] attr.c: add push_stack() helper Date: Wed, 8 Jun 2016 15:58:14 -0700 Message-ID: <20160608225818.726-2-gitster@pobox.com> References: <20160608225818.726-1-gitster@pobox.com> To: git@vger.kernel.org X-From: git-owner@vger.kernel.org Thu Jun 09 00:58:30 2016 Return-path: Envelope-to: gcvg-git-2@plane.gmane.org Received: from vger.kernel.org ([209.132.180.67]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1bAmQe-00059M-WD for gcvg-git-2@plane.gmane.org; Thu, 09 Jun 2016 00:58:29 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1423786AbcFHW6Y (ORCPT ); Wed, 8 Jun 2016 18:58:24 -0400 Received: from pb-smtp1.pobox.com ([64.147.108.70]:63238 "EHLO sasl.smtp.pobox.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1161950AbcFHW6X (ORCPT ); Wed, 8 Jun 2016 18:58:23 -0400 Received: from sasl.smtp.pobox.com (unknown [127.0.0.1]) by pb-smtp1.pobox.com (Postfix) with ESMTP id 44FC521EE7; Wed, 8 Jun 2016 18:58:22 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=pobox.com; h=from:to :subject:date:message-id:in-reply-to:references; s=sasl; bh=fQaX B9t78XYe19UF2RW1RTNS+yk=; b=UOJDA9yrlGYgj9k+4dz24kpXFHfXxbRsfqNy ChDquKi9is3osE7Q1hbLYomKTeEZ2MOY7dM49WzOdgGVfUSLINl9LMpWdNQAjKF0 sYRXOZOblZBnhq5QHQgF7/Posi5EViutJi+DXJxWJwHlc/IHsY9oSGOh5qU6K8kc ulUrw8c= DomainKey-Signature: a=rsa-sha1; c=nofws; d=pobox.com; h=from:to:subject :date:message-id:in-reply-to:references; q=dns; s=sasl; b=qFA9I5 ue8mRvkGTIiYT/Q+cNo6GURh9DYkhinVcGGVUpn2eFKBj9f3VQxBwFQ8C7VZRWGq ccfk492VjgHyhMVgWqZNkI92nEakOX6LTybCgwiUSIOtoLJYJMqZaF5lfI9/pgjz Ic69GjzFAn0UuP+i3cJeUxPJ4e81+0Aq7nYNM= Received: from pb-smtp1.nyi.icgroup.com (unknown [127.0.0.1]) by pb-smtp1.pobox.com (Postfix) with ESMTP id 3B9FF21EE6; Wed, 8 Jun 2016 18:58:22 -0400 (EDT) Received: from pobox.com (unknown [104.132.0.95]) (using TLSv1.2 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by pb-smtp1.pobox.com (Postfix) with ESMTPSA id B3EB721EE5; Wed, 8 Jun 2016 18:58:21 -0400 (EDT) X-Mailer: git-send-email 2.9.0-rc2-262-g9161bbf In-Reply-To: <20160608225818.726-1-gitster@pobox.com> X-Pobox-Relay-ID: 7F86405A-2DCC-11E6-A4AE-89D312518317-77302942!pb-smtp1.pobox.com Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Archived-At: 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 --- attr.c | 71 +++++++++++++++++++++++++++++++----------------------------------- 1 file changed, 33 insertions(+), 38 deletions(-) diff --git a/attr.c b/attr.c index fc63712..26228ce 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.9.0-rc2-262-g9161bbf