git: move seen-alias bookkeeping into handle_alias(...)

We are about to complicate the command handling by allowing *deprecated* builtins to be shadowed by aliases. We need to organize the code in order to facilitate that.[1] The code in the `while(1)` speculatively adds commands to the list before finding out if it’s an alias. Let’s instead move it inside `handle_alias(...)`—where it conceptually belongs anyway—and in turn only run this logic when we have found an alias.[2] [1]: We will do that with an additional call to `handle_alias(1)` inside the loop. *Not* moving this code leaves a blind spot; we will miss alias looping crafted via deprecated builtin names [2]: Also rename the list to a more descriptive name Based-on-patch-by: Jeff King <peff@peff.net> Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kristoffer Haugsbakk committed Sep 17, 2025 at 22:24 UTC b4f9282d8db88619b2becac7f4ee2cad75a72ff9
1 file changed +25 -23
git.c
+25 -23
@@ -365,7 +365,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
365 return (*argv) - orig_argv;
366 }
367
368 -static int handle_alias(struct strvec *args)
368 +static int handle_alias(struct strvec *args, struct string_list *expanded_aliases)
369 {
370 int envchanged = 0, ret = 0, saved_errno = errno;
371 int count, option_count;
@@ -376,6 +376,8 @@ static int handle_alias(struct strvec *args)
376 alias_command = args->v[0];
377 alias_string = alias_lookup(alias_command);
378 if (alias_string) {
379 + struct string_list_item *seen;
380 +
381 if (args->nr == 2 && !strcmp(args->v[1], "-h"))
382 fprintf_ln(stderr, _("'%s' is aliased to '%s'"),
383 alias_command, alias_string);
@@ -423,6 +425,25 @@ static int handle_alias(struct strvec *args)
425 if (!strcmp(alias_command, new_argv[0]))
426 die(_("recursive alias: %s"), alias_command);
427
428 + string_list_append(expanded_aliases, alias_command);
429 + seen = unsorted_string_list_lookup(expanded_aliases,
430 + new_argv[0]);
431 +
432 + if (seen) {
433 + struct strbuf sb = STRBUF_INIT;
434 + for (size_t i = 0; i < expanded_aliases->nr; i++) {
435 + struct string_list_item *item = &expanded_aliases->items[i];
436 +
437 + strbuf_addf(&sb, "\n %s", item->string);
438 + if (item == seen)
439 + strbuf_addstr(&sb, " <==");
440 + else if (i == expanded_aliases->nr - 1)
441 + strbuf_addstr(&sb, " ==>");
442 + }
443 + die(_("alias loop detected: expansion of '%s' does"
444 + " not terminate:%s"), expanded_aliases->items[0].string, sb.buf);
445 + }
446 +
447 trace_argv_printf(new_argv,
448 "trace: alias expansion: %s =>",
449 alias_command);
@@ -806,8 +827,7 @@ static void execv_dashed_external(const char **argv)
827 static int run_argv(struct strvec *args)
828 {
829 int done_alias = 0;
809 - struct string_list cmd_list = STRING_LIST_INIT_DUP;
810 - struct string_list_item *seen;
830 + struct string_list expanded_aliases = STRING_LIST_INIT_DUP;
831
832 while (1) {
833 /*
@@ -859,35 +879,17 @@ static int run_argv(struct strvec *args)
879 /* .. then try the external ones */
880 execv_dashed_external(args->v);
881
862 - seen = unsorted_string_list_lookup(&cmd_list, args->v[0]);
863 - if (seen) {
864 - struct strbuf sb = STRBUF_INIT;
865 - for (size_t i = 0; i < cmd_list.nr; i++) {
866 - struct string_list_item *item = &cmd_list.items[i];
867 -
868 - strbuf_addf(&sb, "\n %s", item->string);
869 - if (item == seen)
870 - strbuf_addstr(&sb, " <==");
871 - else if (i == cmd_list.nr - 1)
872 - strbuf_addstr(&sb, " ==>");
873 - }
874 - die(_("alias loop detected: expansion of '%s' does"
875 - " not terminate:%s"), cmd_list.items[0].string, sb.buf);
876 - }
877 -
878 - string_list_append(&cmd_list, args->v[0]);
879 -
882 /*
883 * It could be an alias -- this works around the insanity
884 * of overriding "git log" with "git show" by having
885 * alias.log = show
886 */
885 - if (!handle_alias(args))
887 + if (!handle_alias(args, &expanded_aliases))
888 break;
889 done_alias = 1;
890 }
891
890 - string_list_clear(&cmd_list, 0);
892 + string_list_clear(&expanded_aliases, 0);
893
894 return done_alias;
895 }