Merge branch 'jc/add-u-A-default-to-top'
[gitweb.git] / builtin / remote.c
index 181668dedddef9bf79ab91d9740607cf31115a16..e4c3ea130c98b01f87bed617a6386b03158b42d5 100644 (file)
@@ -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>"),
@@ -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>"),
@@ -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;
@@ -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"))