@matheusbsbs / matheus-bagliano / commits / a907e1b6ec
Fork 0

git.c: make autocorrected aliases work

help_unknown_cmd() is able to autocorrect a command to an alias, and not only to internal or external commands. However, main() was not passing the autocorrected command through handle_alias(), hence it failed if it was an alias. This commit makes the autocorrected command go through handle_alias(), once handle_internal_command() and execv_dashed_external() have been tried. Since this is done twice in main() now, moved that logic to a new run_argv() function. Also, print the same "Expansion of alias 'x' failed" message when the alias was autocorrected, rather than a generic "Failed to run command 'x'". Signed-off-by: Adeodato Simó <dato@net.com.org.es> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Adeodato Simó committed Jan 4, 2009 at 18:16 UTC a907e1b6ec3038306df68dc1275edad7457bba4b
1 file changed +33 -20
git.c
+33 -20
@@ -416,12 +416,34 @@ static void execv_dashed_external(const char **argv)
416 strbuf_release(&cmd);
417 }
418
419 +static int run_argv(int *argcp, const char ***argv)
420 +{
421 + int done_alias = 0;
422 +
423 + while (1) {
424 + /* See if it's an internal command */
425 + handle_internal_command(*argcp, *argv);
426 +
427 + /* .. then try the external ones */
428 + execv_dashed_external(*argv);
429 +
430 + /* It could be an alias -- this works around the insanity
431 + * of overriding "git log" with "git show" by having
432 + * alias.log = show
433 + */
434 + if (done_alias || !handle_alias(argcp, argv))
435 + break;
436 + done_alias = 1;
437 + }
438 +
439 + return done_alias;
440 +}
441 +
442
443 int main(int argc, const char **argv)
444 {
445 const char *cmd = argv[0] && *argv[0] ? argv[0] : "git-help";
446 char *slash = (char *)cmd + strlen(cmd);
424 - int done_alias = 0;
447
448 /*
449 * Take the basename of argv[0] as the command
@@ -479,31 +501,22 @@ int main(int argc, const char **argv)
501 setup_path();
502
503 while (1) {
482 - /* See if it's an internal command */
483 - handle_internal_command(argc, argv);
484 -
485 - /* .. then try the external ones */
486 - execv_dashed_external(argv);
487 -
488 - /* It could be an alias -- this works around the insanity
489 - * of overriding "git log" with "git show" by having
490 - * alias.log = show
491 - */
492 - if (done_alias || !handle_alias(&argc, &argv))
504 + static int done_help = 0;
505 + static int was_alias = 0;
506 + was_alias = run_argv(&argc, &argv);
507 + if (errno != ENOENT)
508 break;
494 - done_alias = 1;
495 - }
496 -
497 - if (errno == ENOENT) {
498 - if (done_alias) {
509 + if (was_alias) {
510 fprintf(stderr, "Expansion of alias '%s' failed; "
511 "'%s' is not a git-command\n",
512 cmd, argv[0]);
513 exit(1);
514 }
504 - argv[0] = help_unknown_cmd(cmd);
505 - handle_internal_command(argc, argv);
506 - execv_dashed_external(argv);
515 + if (!done_help) {
516 + cmd = argv[0] = help_unknown_cmd(cmd);
517 + done_help = 1;
518 + } else
519 + break;
520 }
521
522 fprintf(stderr, "Failed to run command '%s': %s\n",