Skip to content

Commit b4f9282

Browse files
LemmingAvalanchegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Kristoffer Haugsbakk <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5f31632 commit b4f9282

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

git.c

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
365365
return (*argv) - orig_argv;
366366
}
367367

368-
static int handle_alias(struct strvec *args)
368+
static int handle_alias(struct strvec *args, struct string_list *expanded_aliases)
369369
{
370370
int envchanged = 0, ret = 0, saved_errno = errno;
371371
int count, option_count;
@@ -376,6 +376,8 @@ static int handle_alias(struct strvec *args)
376376
alias_command = args->v[0];
377377
alias_string = alias_lookup(alias_command);
378378
if (alias_string) {
379+
struct string_list_item *seen;
380+
379381
if (args->nr == 2 && !strcmp(args->v[1], "-h"))
380382
fprintf_ln(stderr, _("'%s' is aliased to '%s'"),
381383
alias_command, alias_string);
@@ -423,6 +425,25 @@ static int handle_alias(struct strvec *args)
423425
if (!strcmp(alias_command, new_argv[0]))
424426
die(_("recursive alias: %s"), alias_command);
425427

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+
426447
trace_argv_printf(new_argv,
427448
"trace: alias expansion: %s =>",
428449
alias_command);
@@ -806,8 +827,7 @@ static void execv_dashed_external(const char **argv)
806827
static int run_argv(struct strvec *args)
807828
{
808829
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;
811831

812832
while (1) {
813833
/*
@@ -859,35 +879,17 @@ static int run_argv(struct strvec *args)
859879
/* .. then try the external ones */
860880
execv_dashed_external(args->v);
861881

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-
880882
/*
881883
* It could be an alias -- this works around the insanity
882884
* of overriding "git log" with "git show" by having
883885
* alias.log = show
884886
*/
885-
if (!handle_alias(args))
887+
if (!handle_alias(args, &expanded_aliases))
886888
break;
887889
done_alias = 1;
888890
}
889891

890-
string_list_clear(&cmd_list, 0);
892+
string_list_clear(&expanded_aliases, 0);
891893

892894
return done_alias;
893895
}

0 commit comments

Comments
 (0)