All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tim Schumacher <timschumi@gmx.de>
To: git@vger.kernel.org
Cc: timschumi@gmx.de
Subject: [PATCH 2/2] alias: Move checking code into a seperate function
Date: Sun, 21 Oct 2018 20:46:19 +0200	[thread overview]
Message-ID: <20181021184619.6688-2-timschumi@gmx.de> (raw)
In-Reply-To: <20181021184619.6688-1-timschumi@gmx.de>

We can save a few indentations (and possibly brain cells of people
that don't care about that code) by moving the code that checks for
a looping alias (and that prints the error message if one is found)
into a seperate function.

This restores a lot of readablility to the run_argv() function as
well, because it was only concerned with the high-level routing of
the command and alias logic before.
---
 git.c | 40 +++++++++++++++++++++++-----------------
 1 file changed, 23 insertions(+), 17 deletions(-)

diff --git a/git.c b/git.c
index 0f77bce7d..b6fdd9708 100644
--- a/git.c
+++ b/git.c
@@ -710,11 +710,32 @@ static void add_cmd_history(struct strbuf *env, struct string_list *cmd_list,
 	setenv(COMMAND_HISTORY_ENVIRONMENT, env->buf, 1);
 }
 
+static void cmd_unique_or_die(struct string_list *cmd_list, const char *cmd)
+{
+	struct string_list_item *seen;
+
+	seen = unsorted_string_list_lookup(cmd_list, cmd);
+	if (!seen)
+		return;
+
+	int i;
+	struct strbuf sb = STRBUF_INIT;
+	for (i = 0; i < cmd_list->nr; i++) {
+		struct string_list_item *item = &cmd_list->items[i];
+		strbuf_addf(&sb, "\n  %s", item->string);
+		if (item == seen)
+			strbuf_addstr(&sb, " <==");
+		else if (i == cmd_list->nr - 1)
+			strbuf_addstr(&sb, " ==>");
+	}
+	die(_("alias loop detected: expansion of '%s' does not terminate:%s"),
+	    cmd_list->items[0].string, sb.buf);
+}
+
 static int run_argv(int *argcp, const char ***argv)
 {
 	int done_alias = 0;
 	struct string_list cmd_list = STRING_LIST_INIT_DUP;
-	struct string_list_item *seen;
 	struct strbuf env = STRBUF_INIT;
 
 	init_cmd_history(&env, &cmd_list);
@@ -739,22 +760,7 @@ static int run_argv(int *argcp, const char ***argv)
 		/* .. then try the external ones */
 		execv_dashed_external(*argv);
 
-		seen = unsorted_string_list_lookup(&cmd_list, *argv[0]);
-		if (seen) {
-			int i;
-			struct strbuf sb = STRBUF_INIT;
-			for (i = 0; i < cmd_list.nr; i++) {
-				struct string_list_item *item = &cmd_list.items[i];
-
-				strbuf_addf(&sb, "\n  %s", item->string);
-				if (item == seen)
-					strbuf_addstr(&sb, " <==");
-				else if (i == cmd_list.nr - 1)
-					strbuf_addstr(&sb, " ==>");
-			}
-			die(_("alias loop detected: expansion of '%s' does"
-			      " not terminate:%s"), cmd_list.items[0].string, sb.buf);
-		}
+		cmd_unique_or_die(&cmd_list, *argv[0]);
 
 		add_cmd_history(&env, &cmd_list, *argv[0]);
 
-- 
2.19.1.450.ga4b8ab536


      reply	other threads:[~2018-10-21 18:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-21 18:46 [PATCH 1/2] alias: Rework comment about processing aliases Tim Schumacher
2018-10-21 18:46 ` Tim Schumacher [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181021184619.6688-2-timschumi@gmx.de \
    --to=timschumi@gmx.de \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.