Skip to content

Commit 506ee60

Browse files
committed
Merge branch 'ts/alias-of-alias'
An alias that expands to another alias has so far been forbidden, but now it is allowed to create such an alias. * ts/alias-of-alias: t0014: introduce an alias testing suite alias: show the call history when an alias is looping alias: add support for aliases of an alias
2 parents 6d8f8eb + fef5f7f commit 506ee60

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

git.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,8 @@ static void execv_dashed_external(const char **argv)
675675
static int run_argv(int *argcp, const char ***argv)
676676
{
677677
int done_alias = 0;
678+
struct string_list cmd_list = STRING_LIST_INIT_NODUP;
679+
struct string_list_item *seen;
678680

679681
while (1) {
680682
/*
@@ -692,17 +694,37 @@ static int run_argv(int *argcp, const char ***argv)
692694
/* .. then try the external ones */
693695
execv_dashed_external(*argv);
694696

695-
/* It could be an alias -- this works around the insanity
697+
seen = unsorted_string_list_lookup(&cmd_list, *argv[0]);
698+
if (seen) {
699+
int i;
700+
struct strbuf sb = STRBUF_INIT;
701+
for (i = 0; i < cmd_list.nr; i++) {
702+
struct string_list_item *item = &cmd_list.items[i];
703+
704+
strbuf_addf(&sb, "\n %s", item->string);
705+
if (item == seen)
706+
strbuf_addstr(&sb, " <==");
707+
else if (i == cmd_list.nr - 1)
708+
strbuf_addstr(&sb, " ==>");
709+
}
710+
die(_("alias loop detected: expansion of '%s' does"
711+
" not terminate:%s"), cmd_list.items[0].string, sb.buf);
712+
}
713+
714+
string_list_append(&cmd_list, *argv[0]);
715+
716+
/*
717+
* It could be an alias -- this works around the insanity
696718
* of overriding "git log" with "git show" by having
697719
* alias.log = show
698720
*/
699-
if (done_alias)
700-
break;
701721
if (!handle_alias(argcp, argv))
702722
break;
703723
done_alias = 1;
704724
}
705725

726+
string_list_clear(&cmd_list, 0);
727+
706728
return done_alias;
707729
}
708730

t/t0014-alias.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/sh
2+
3+
test_description='git command aliasing'
4+
5+
. ./test-lib.sh
6+
7+
test_expect_success 'nested aliases - internal execution' '
8+
git config alias.nested-internal-1 nested-internal-2 &&
9+
git config alias.nested-internal-2 status &&
10+
git nested-internal-1 >output &&
11+
test_i18ngrep "^On branch " output
12+
'
13+
14+
test_expect_success 'nested aliases - mixed execution' '
15+
git config alias.nested-external-1 nested-external-2 &&
16+
git config alias.nested-external-2 "!git nested-external-3" &&
17+
git config alias.nested-external-3 status &&
18+
git nested-external-1 >output &&
19+
test_i18ngrep "^On branch " output
20+
'
21+
22+
test_expect_success 'looping aliases - internal execution' '
23+
git config alias.loop-internal-1 loop-internal-2 &&
24+
git config alias.loop-internal-2 loop-internal-3 &&
25+
git config alias.loop-internal-3 loop-internal-2 &&
26+
test_must_fail git loop-internal-1 2>output &&
27+
test_i18ngrep "^fatal: alias loop detected: expansion of" output
28+
'
29+
30+
# This test is disabled until external loops are fixed, because would block
31+
# the test suite for a full minute.
32+
#
33+
#test_expect_failure 'looping aliases - mixed execution' '
34+
# git config alias.loop-mixed-1 loop-mixed-2 &&
35+
# git config alias.loop-mixed-2 "!git loop-mixed-1" &&
36+
# test_must_fail git loop-mixed-1 2>output &&
37+
# test_i18ngrep "^fatal: alias loop detected: expansion of" output
38+
#'
39+
40+
test_done

0 commit comments

Comments
 (0)