Skip to content

Commit

Permalink
Merge branch 'bb/remote-get-url'
Browse files Browse the repository at this point in the history
"git remote" learned "get-url" subcommand to show the URL for a
given remote name used for fetching and pushing.

* bb/remote-get-url:
  remote: add get-url subcommand
  • Loading branch information
gitster committed Oct 5, 2015
2 parents ff2be26 + 96f78d3 commit e437cbd
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Documentation/git-remote.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ SYNOPSIS
'git remote remove' <name>
'git remote set-head' <name> (-a | --auto | -d | --delete | <branch>)
'git remote set-branches' [--add] <name> <branch>...
'git remote get-url' [--push] [--all] <name>
'git remote set-url' [--push] <name> <newurl> [<oldurl>]
'git remote set-url --add' [--push] <name> <newurl>
'git remote set-url --delete' [--push] <name> <url>
Expand Down Expand Up @@ -131,6 +132,15 @@ The named branches will be interpreted as if specified with the
With `--add`, instead of replacing the list of currently tracked
branches, adds to that list.

'get-url'::

Retrieves the URLs for a remote. Configurations for `insteadOf` and
`pushInsteadOf` are expanded here. By default, only the first URL is listed.
+
With '--push', push URLs are queried rather than fetch URLs.
+
With '--all', all URLs for the remote will be listed.

'set-url'::

Changes URLs for the remote. Sets first URL for remote <name> that matches
Expand Down
59 changes: 59 additions & 0 deletions builtin/remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ static const char * const builtin_remote_usage[] = {
N_("git remote prune [-n | --dry-run] <name>"),
N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"),
N_("git remote set-branches [--add] <name> <branch>..."),
N_("git remote get-url [--push] [--all] <name>"),
N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
N_("git remote set-url --add <name> <newurl>"),
N_("git remote set-url --delete <name> <url>"),
Expand Down Expand Up @@ -65,6 +66,11 @@ static const char * const builtin_remote_update_usage[] = {
NULL
};

static const char * const builtin_remote_geturl_usage[] = {
N_("git remote get-url [--push] [--all] <name>"),
NULL
};

static const char * const builtin_remote_seturl_usage[] = {
N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
N_("git remote set-url --add <name> <newurl>"),
Expand Down Expand Up @@ -1467,6 +1473,57 @@ static int set_branches(int argc, const char **argv)
return set_remote_branches(argv[0], argv + 1, add_mode);
}

static int get_url(int argc, const char **argv)
{
int i, push_mode = 0, all_mode = 0;
const char *remotename = NULL;
struct remote *remote;
const char **url;
int url_nr;
struct option options[] = {
OPT_BOOL('\0', "push", &push_mode,
N_("query push URLs rather than fetch URLs")),
OPT_BOOL('\0', "all", &all_mode,
N_("return all URLs")),
OPT_END()
};
argc = parse_options(argc, argv, NULL, options, builtin_remote_geturl_usage, 0);

if (argc != 1)
usage_with_options(builtin_remote_geturl_usage, options);

remotename = argv[0];

if (!remote_is_configured(remotename))
die(_("No such remote '%s'"), remotename);
remote = remote_get(remotename);

url_nr = 0;
if (push_mode) {
url = remote->pushurl;
url_nr = remote->pushurl_nr;
}
/* else fetch mode */

/* Use the fetch URL when no push URLs were found or requested. */
if (!url_nr) {
url = remote->url;
url_nr = remote->url_nr;
}

if (!url_nr)
die(_("no URLs configured for remote '%s'"), remotename);

if (all_mode) {
for (i = 0; i < url_nr; i++)
printf_ln("%s", url[i]);
} else {
printf_ln("%s", *url);
}

return 0;
}

static int set_url(int argc, const char **argv)
{
int i, push_mode = 0, add_mode = 0, delete_mode = 0;
Expand Down Expand Up @@ -1576,6 +1633,8 @@ int cmd_remote(int argc, const char **argv, const char *prefix)
result = set_head(argc, argv);
else if (!strcmp(argv[0], "set-branches"))
result = set_branches(argc, argv);
else if (!strcmp(argv[0], "get-url"))
result = get_url(argc, argv);
else if (!strcmp(argv[0], "set-url"))
result = set_url(argc, argv);
else if (!strcmp(argv[0], "show"))
Expand Down
37 changes: 37 additions & 0 deletions t/t5505-remote.sh
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,19 @@ test_expect_success 'new remote' '
cmp expect actual
'

get_url_test () {
cat >expect &&
git remote get-url "$@" >actual &&
test_cmp expect actual
}

test_expect_success 'get-url on new remote' '
echo foo | get_url_test someremote &&
echo foo | get_url_test --all someremote &&
echo foo | get_url_test --push someremote &&
echo foo | get_url_test --push --all someremote
'

test_expect_success 'remote set-url bar' '
git remote set-url someremote bar &&
echo bar >expect &&
Expand Down Expand Up @@ -961,6 +974,13 @@ test_expect_success 'remote set-url --push zot' '
cmp expect actual
'

test_expect_success 'get-url with different urls' '
echo baz | get_url_test someremote &&
echo baz | get_url_test --all someremote &&
echo zot | get_url_test --push someremote &&
echo zot | get_url_test --push --all someremote
'

test_expect_success 'remote set-url --push qux zot' '
git remote set-url --push someremote qux zot &&
echo qux >expect &&
Expand Down Expand Up @@ -995,6 +1015,14 @@ test_expect_success 'remote set-url --push --add aaa' '
cmp expect actual
'

test_expect_success 'get-url on multi push remote' '
echo foo | get_url_test --push someremote &&
get_url_test --push --all someremote <<-\EOF
foo
aaa
EOF
'

test_expect_success 'remote set-url --push bar aaa' '
git remote set-url --push someremote bar aaa &&
echo foo >expect &&
Expand Down Expand Up @@ -1039,6 +1067,14 @@ test_expect_success 'remote set-url --add bbb' '
cmp expect actual
'

test_expect_success 'get-url on multi fetch remote' '
echo baz | get_url_test someremote &&
get_url_test --all someremote <<-\EOF
baz
bbb
EOF
'

test_expect_success 'remote set-url --delete .*' '
test_must_fail git remote set-url --delete someremote .\* &&
echo "YYY" >expect &&
Expand Down Expand Up @@ -1108,6 +1144,7 @@ test_extra_arg rename origin newname
test_extra_arg remove origin
test_extra_arg set-head origin master
# set-branches takes any number of args
test_extra_arg get-url origin newurl
test_extra_arg set-url origin newurl oldurl
# show takes any number of args
# prune takes any number of args
Expand Down

0 comments on commit e437cbd

Please sign in to comment.