git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] alias: Move checking code into a seperate function

From
Tim Schumacher <timschumi@gmx.de>
Date
Oct 21, 2018, 18:46 UTC
Message-ID
<20181021184619.6688-2-timschumi@gmx.de>
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
Previous: Tim Schumacher
Message 2 of 2 in “alias: Rework comment about processing aliases”
  1. 1/2 alias: Rework comment about processing aliasesTim Schumacher, Oct 21, 2018
  2. 2/2 alias: Move checking code into a seperate functionTim Schumacher, Oct 21, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.