Merge branch 'bb/remote-get-url'
authorJunio C Hamano <gitster@pobox.com>
Mon, 5 Oct 2015 19:30:25 +0000 (12:30 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 5 Oct 2015 19:30:25 +0000 (12:30 -0700)
"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

1  2 
builtin/remote.c
diff --combined builtin/remote.c
index 181668dedddef9bf79ab91d9740607cf31115a16,ec9ec981a30e24ec85d4c0f54165f59b220d5052..e4c3ea130c98b01f87bed617a6386b03158b42d5
@@@ -18,6 -18,7 +18,7 @@@ static const char * const builtin_remot
        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 +66,11 @@@ static const char * const builtin_remot
        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>"),
@@@ -581,6 -587,7 +587,6 @@@ static int migrate_file(struct remote *
  {
        struct strbuf buf = STRBUF_INIT;
        int i;
 -      const char *path = NULL;
  
        strbuf_addf(&buf, "remote.%s.url", remote->name);
        for (i = 0; i < remote->url_nr; i++)
                        return error(_("Could not append '%s' to '%s'"),
                                        remote->fetch_refspec[i], buf.buf);
        if (remote->origin == REMOTE_REMOTES)
 -              path = git_path("remotes/%s", remote->name);
 +              unlink_or_warn(git_path("remotes/%s", remote->name));
        else if (remote->origin == REMOTE_BRANCHES)
 -              path = git_path("branches/%s", remote->name);
 -      if (path)
 -              unlink_or_warn(path);
 +              unlink_or_warn(git_path("branches/%s", remote->name));
        return 0;
  }
  
@@@ -743,6 -752,26 +749,6 @@@ static int mv(int argc, const char **ar
        return 0;
  }
  
 -static int remove_branches(struct string_list *branches)
 -{
 -      struct strbuf err = STRBUF_INIT;
 -      int i, result = 0;
 -
 -      if (repack_without_refs(branches, &err))
 -              result |= error("%s", err.buf);
 -      strbuf_release(&err);
 -
 -      for (i = 0; i < branches->nr; i++) {
 -              struct string_list_item *item = branches->items + i;
 -              const char *refname = item->string;
 -
 -              if (delete_ref(refname, NULL, 0))
 -                      result |= error(_("Could not remove branch %s"), refname);
 -      }
 -
 -      return result;
 -}
 -
  static int rm(int argc, const char **argv)
  {
        struct option options[] = {
        strbuf_release(&buf);
  
        if (!result)
 -              result = remove_branches(&branches);
 +              result = delete_refs(&branches);
        string_list_clear(&branches, 0);
  
        if (skipped.nr) {
@@@ -1311,12 -1340,19 +1317,12 @@@ static int prune_remote(const char *rem
                string_list_append(&refs_to_prune, item->util);
        string_list_sort(&refs_to_prune);
  
 -      if (!dry_run) {
 -              struct strbuf err = STRBUF_INIT;
 -              if (repack_without_refs(&refs_to_prune, &err))
 -                      result |= error("%s", err.buf);
 -              strbuf_release(&err);
 -      }
 +      if (!dry_run)
 +              result |= delete_refs(&refs_to_prune);
  
        for_each_string_list_item(item, &states.stale) {
                const char *refname = item->util;
  
 -              if (!dry_run)
 -                      result |= delete_ref(refname, NULL, 0);
 -
                if (dry_run)
                        printf_ln(_(" * [would prune] %s"),
                               abbrev_ref(refname, "refs/remotes/"));
@@@ -1467,6 -1503,57 +1473,57 @@@ static int set_branches(int argc, cons
        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 -1663,8 +1633,8 @@@ int cmd_remote(int argc, const char **a
                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"))