remote: add get-url subcommand

Expanding `insteadOf` is a part of ls-remote --url and there is no way to expand `pushInsteadOf` as well. Add a get-url subcommand to be able to query both as well as a way to get all configured urls. Signed-off-by: Ben Boeckel <mathstuf@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ben Boeckel committed Sep 15, 2015 at 21:53 UTC 96f78d39989d1fcf393d7bc42357467dd8cf0f15
3 files changed +106
Documentation/git-remote.txt
+10
@@ -15,6 +15,7 @@ SYNOPSIS
15 'git remote remove' <name>
16 'git remote set-head' <name> (-a | --auto | -d | --delete | <branch>)
17 'git remote set-branches' [--add] <name> <branch>...
18 +'git remote get-url' [--push] [--all] <name>
19 'git remote set-url' [--push] <name> <newurl> [<oldurl>]
20 'git remote set-url --add' [--push] <name> <newurl>
21 'git remote set-url --delete' [--push] <name> <url>
@@ -131,6 +132,15 @@ The named branches will be interpreted as if specified with the
132 With `--add`, instead of replacing the list of currently tracked
133 branches, adds to that list.
134
135 +'get-url'::
136 +
137 +Retrieves the URLs for a remote. Configurations for `insteadOf` and
138 +`pushInsteadOf` are expanded here. By default, only the first URL is listed.
139 ++
140 +With '--push', push URLs are queried rather than fetch URLs.
141 ++
142 +With '--all', all URLs for the remote will be listed.
143 +
144 'set-url'::
145
146 Changes URLs for the remote. Sets first URL for remote <name> that matches
builtin/remote.c
+59
@@ -18,6 +18,7 @@ static const char * const builtin_remote_usage[] = {
18 N_("git remote prune [-n | --dry-run] <name>"),
19 N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"),
20 N_("git remote set-branches [--add] <name> <branch>..."),
21 + N_("git remote get-url [--push] [--all] <name>"),
22 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
23 N_("git remote set-url --add <name> <newurl>"),
24 N_("git remote set-url --delete <name> <url>"),
@@ -65,6 +66,11 @@ static const char * const builtin_remote_update_usage[] = {
66 NULL
67 };
68
69 +static const char * const builtin_remote_geturl_usage[] = {
70 + N_("git remote get-url [--push] [--all] <name>"),
71 + NULL
72 +};
73 +
74 static const char * const builtin_remote_seturl_usage[] = {
75 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
76 N_("git remote set-url --add <name> <newurl>"),
@@ -1497,6 +1503,57 @@ static int set_branches(int argc, const char **argv)
1503 return set_remote_branches(argv[0], argv + 1, add_mode);
1504 }
1505
1506 +static int get_url(int argc, const char **argv)
1507 +{
1508 + int i, push_mode = 0, all_mode = 0;
1509 + const char *remotename = NULL;
1510 + struct remote *remote;
1511 + const char **url;
1512 + int url_nr;
1513 + struct option options[] = {
1514 + OPT_BOOL('\0', "push", &push_mode,
1515 + N_("query push URLs rather than fetch URLs")),
1516 + OPT_BOOL('\0', "all", &all_mode,
1517 + N_("return all URLs")),
1518 + OPT_END()
1519 + };
1520 + argc = parse_options(argc, argv, NULL, options, builtin_remote_geturl_usage, 0);
1521 +
1522 + if (argc != 1)
1523 + usage_with_options(builtin_remote_geturl_usage, options);
1524 +
1525 + remotename = argv[0];
1526 +
1527 + if (!remote_is_configured(remotename))
1528 + die(_("No such remote '%s'"), remotename);
1529 + remote = remote_get(remotename);
1530 +
1531 + url_nr = 0;
1532 + if (push_mode) {
1533 + url = remote->pushurl;
1534 + url_nr = remote->pushurl_nr;
1535 + }
1536 + /* else fetch mode */
1537 +
1538 + /* Use the fetch URL when no push URLs were found or requested. */
1539 + if (!url_nr) {
1540 + url = remote->url;
1541 + url_nr = remote->url_nr;
1542 + }
1543 +
1544 + if (!url_nr)
1545 + die(_("no URLs configured for remote '%s'"), remotename);
1546 +
1547 + if (all_mode) {
1548 + for (i = 0; i < url_nr; i++)
1549 + printf_ln("%s", url[i]);
1550 + } else {
1551 + printf_ln("%s", *url);
1552 + }
1553 +
1554 + return 0;
1555 +}
1556 +
1557 static int set_url(int argc, const char **argv)
1558 {
1559 int i, push_mode = 0, add_mode = 0, delete_mode = 0;
@@ -1606,6 +1663,8 @@ int cmd_remote(int argc, const char **argv, const char *prefix)
1663 result = set_head(argc, argv);
1664 else if (!strcmp(argv[0], "set-branches"))
1665 result = set_branches(argc, argv);
1666 + else if (!strcmp(argv[0], "get-url"))
1667 + result = get_url(argc, argv);
1668 else if (!strcmp(argv[0], "set-url"))
1669 result = set_url(argc, argv);
1670 else if (!strcmp(argv[0], "show"))
t/t5505-remote.sh
+37
@@ -919,6 +919,19 @@ test_expect_success 'new remote' '
919 cmp expect actual
920 '
921
922 +get_url_test () {
923 + cat >expect &&
924 + git remote get-url "$@" >actual &&
925 + test_cmp expect actual
926 +}
927 +
928 +test_expect_success 'get-url on new remote' '
929 + echo foo | get_url_test someremote &&
930 + echo foo | get_url_test --all someremote &&
931 + echo foo | get_url_test --push someremote &&
932 + echo foo | get_url_test --push --all someremote
933 +'
934 +
935 test_expect_success 'remote set-url bar' '
936 git remote set-url someremote bar &&
937 echo bar >expect &&
@@ -961,6 +974,13 @@ test_expect_success 'remote set-url --push zot' '
974 cmp expect actual
975 '
976
977 +test_expect_success 'get-url with different urls' '
978 + echo baz | get_url_test someremote &&
979 + echo baz | get_url_test --all someremote &&
980 + echo zot | get_url_test --push someremote &&
981 + echo zot | get_url_test --push --all someremote
982 +'
983 +
984 test_expect_success 'remote set-url --push qux zot' '
985 git remote set-url --push someremote qux zot &&
986 echo qux >expect &&
@@ -995,6 +1015,14 @@ test_expect_success 'remote set-url --push --add aaa' '
1015 cmp expect actual
1016 '
1017
1018 +test_expect_success 'get-url on multi push remote' '
1019 + echo foo | get_url_test --push someremote &&
1020 + get_url_test --push --all someremote <<-\EOF
1021 + foo
1022 + aaa
1023 + EOF
1024 +'
1025 +
1026 test_expect_success 'remote set-url --push bar aaa' '
1027 git remote set-url --push someremote bar aaa &&
1028 echo foo >expect &&
@@ -1039,6 +1067,14 @@ test_expect_success 'remote set-url --add bbb' '
1067 cmp expect actual
1068 '
1069
1070 +test_expect_success 'get-url on multi fetch remote' '
1071 + echo baz | get_url_test someremote &&
1072 + get_url_test --all someremote <<-\EOF
1073 + baz
1074 + bbb
1075 + EOF
1076 +'
1077 +
1078 test_expect_success 'remote set-url --delete .*' '
1079 test_must_fail git remote set-url --delete someremote .\* &&
1080 echo "YYY" >expect &&
@@ -1108,6 +1144,7 @@ test_extra_arg rename origin newname
1144 test_extra_arg remove origin
1145 test_extra_arg set-head origin master
1146 # set-branches takes any number of args
1147 +test_extra_arg get-url origin newurl
1148 test_extra_arg set-url origin newurl oldurl
1149 # show takes any number of args
1150 # prune takes any number of args