Git fork

remote: simplify url/pushurl selection

When we want to know the push urls for a remote, there is some simple
logic:

- if the user configured any remote.*.pushurl keys, then those make
the complete set of push urls

- otherwise we push to all urls in remote.*.url

Many spots implement this with a level of indirection, assigning to a
local url/url_nr pair. But since both arrays are now strvecs, we can
just use a pointer to select the appropriate strvec, shortening the code
a bit.

Even though this is now a one-liner, since it is application logic that
is present in so many places, it's worth abstracting a helper function.
In fact, we already have such a function, but it's local to
builtin/push.c. So we'll just make it available everywhere via remote.h.

There are two spots to pay special attention to here:

1. in builtin/remote.c's get_url(), we are selecting first based on
push_mode and then falling back to "url" when we're in push_mode
but no pushurl is defined. The updated code makes that much more
clear, compared to the original which had an "else" fall-through.

2. likewise in that file's set_url(), we _only_ respect push_mode,
sine the point is that we are adding to pushurl in that case
(whether it is empty or not). And thus it does not use our helper
function.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Jeff King and committed by
Junio C Hamano
b68118d2 8e804415

+31 -65
+5 -16
builtin/push.c
··· 141 141 free_refs(local_refs); 142 142 } 143 143 144 - static int push_url_of_remote(struct remote *remote, const char ***url_p) 145 - { 146 - if (remote->pushurl.nr) { 147 - *url_p = remote->pushurl.v; 148 - return remote->pushurl.nr; 149 - } 150 - *url_p = remote->url.v; 151 - return remote->url.nr; 152 - } 153 - 154 144 static NORETURN void die_push_simple(struct branch *branch, 155 145 struct remote *remote) 156 146 { ··· 434 424 struct remote *remote) 435 425 { 436 426 int i, errs; 437 - const char **url; 438 - int url_nr; 427 + struct strvec *url; 439 428 struct refspec *push_refspec = &rs; 440 429 441 430 if (push_options->nr) ··· 448 437 setup_default_push_refspecs(&flags, remote); 449 438 } 450 439 errs = 0; 451 - url_nr = push_url_of_remote(remote, &url); 452 - if (url_nr) { 453 - for (i = 0; i < url_nr; i++) { 440 + url = push_url_of_remote(remote); 441 + if (url->nr) { 442 + for (i = 0; i < url->nr; i++) { 454 443 struct transport *transport = 455 - transport_get(remote, url[i]); 444 + transport_get(remote, url->v[i]); 456 445 if (flags & TRANSPORT_PUSH_OPTIONS) 457 446 transport->push_options = push_options; 458 447 if (push_with_options(transport, push_refspec, flags))
+20 -49
builtin/remote.c
··· 1213 1213 { 1214 1214 struct string_list *list = priv; 1215 1215 struct strbuf remote_info_buf = STRBUF_INIT; 1216 - const char **url; 1217 - int i, url_nr; 1216 + struct strvec *url; 1217 + int i; 1218 1218 1219 1219 if (remote->url.nr > 0) { 1220 1220 struct strbuf promisor_config = STRBUF_INIT; ··· 1230 1230 strbuf_detach(&remote_info_buf, NULL); 1231 1231 } else 1232 1232 string_list_append(list, remote->name)->util = NULL; 1233 - if (remote->pushurl.nr) { 1234 - url = remote->pushurl.v; 1235 - url_nr = remote->pushurl.nr; 1236 - } else { 1237 - url = remote->url.v; 1238 - url_nr = remote->url.nr; 1239 - } 1240 - for (i = 0; i < url_nr; i++) 1233 + url = push_url_of_remote(remote); 1234 + for (i = 0; i < url->nr; i++) 1241 1235 { 1242 - strbuf_addf(&remote_info_buf, "%s (push)", url[i]); 1236 + strbuf_addf(&remote_info_buf, "%s (push)", url->v[i]); 1243 1237 string_list_append(list, remote->name)->util = 1244 1238 strbuf_detach(&remote_info_buf, NULL); 1245 1239 } ··· 1295 1289 1296 1290 for (; argc; argc--, argv++) { 1297 1291 int i; 1298 - const char **url; 1299 - int url_nr; 1292 + struct strvec *url; 1300 1293 1301 1294 get_remote_ref_states(*argv, &info.states, query_flag); 1302 1295 1303 1296 printf_ln(_("* remote %s"), *argv); 1304 1297 printf_ln(_(" Fetch URL: %s"), info.states.remote->url.nr > 0 ? 1305 1298 info.states.remote->url.v[0] : _("(no URL)")); 1306 - if (info.states.remote->pushurl.nr) { 1307 - url = info.states.remote->pushurl.v; 1308 - url_nr = info.states.remote->pushurl.nr; 1309 - } else { 1310 - url = info.states.remote->url.v; 1311 - url_nr = info.states.remote->url.nr; 1312 - } 1313 - for (i = 0; i < url_nr; i++) 1299 + url = push_url_of_remote(info.states.remote); 1300 + for (i = 0; i < url->nr; i++) 1314 1301 /* 1315 1302 * TRANSLATORS: the colon ':' should align 1316 1303 * with the one in " Fetch URL: %s" 1317 1304 * translation. 1318 1305 */ 1319 - printf_ln(_(" Push URL: %s"), url[i]); 1306 + printf_ln(_(" Push URL: %s"), url->v[i]); 1320 1307 if (!i) 1321 1308 printf_ln(_(" Push URL: %s"), _("(no URL)")); 1322 1309 if (no_query) ··· 1622 1609 int i, push_mode = 0, all_mode = 0; 1623 1610 const char *remotename = NULL; 1624 1611 struct remote *remote; 1625 - const char **url; 1626 - int url_nr; 1612 + struct strvec *url; 1627 1613 struct option options[] = { 1628 1614 OPT_BOOL('\0', "push", &push_mode, 1629 1615 N_("query push URLs rather than fetch URLs")), ··· 1645 1631 exit(2); 1646 1632 } 1647 1633 1648 - url_nr = 0; 1649 - if (push_mode) { 1650 - url = remote->pushurl.v; 1651 - url_nr = remote->pushurl.nr; 1652 - } 1653 - /* else fetch mode */ 1654 - 1655 - /* Use the fetch URL when no push URLs were found or requested. */ 1656 - if (!url_nr) { 1657 - url = remote->url.v; 1658 - url_nr = remote->url.nr; 1659 - } 1660 - 1661 - if (!url_nr) 1634 + url = push_mode ? push_url_of_remote(remote) : &remote->url; 1635 + if (!url->nr) 1662 1636 die(_("no URLs configured for remote '%s'"), remotename); 1663 1637 1664 1638 if (all_mode) { 1665 - for (i = 0; i < url_nr; i++) 1666 - printf_ln("%s", url[i]); 1639 + for (i = 0; i < url->nr; i++) 1640 + printf_ln("%s", url->v[i]); 1667 1641 } else { 1668 - printf_ln("%s", *url); 1642 + printf_ln("%s", url->v[0]); 1669 1643 } 1670 1644 1671 1645 return 0; ··· 1680 1654 const char *oldurl = NULL; 1681 1655 struct remote *remote; 1682 1656 regex_t old_regex; 1683 - const char **urlset; 1684 - int urlset_nr; 1657 + struct strvec *urlset; 1685 1658 struct strbuf name_buf = STRBUF_INIT; 1686 1659 struct option options[] = { 1687 1660 OPT_BOOL('\0', "push", &push_mode, ··· 1718 1691 1719 1692 if (push_mode) { 1720 1693 strbuf_addf(&name_buf, "remote.%s.pushurl", remotename); 1721 - urlset = remote->pushurl.v; 1722 - urlset_nr = remote->pushurl.nr; 1694 + urlset = &remote->pushurl; 1723 1695 } else { 1724 1696 strbuf_addf(&name_buf, "remote.%s.url", remotename); 1725 - urlset = remote->url.v; 1726 - urlset_nr = remote->url.nr; 1697 + urlset = &remote->url; 1727 1698 } 1728 1699 1729 1700 /* Special cases that add new entry. */ ··· 1740 1711 if (regcomp(&old_regex, oldurl, REG_EXTENDED)) 1741 1712 die(_("Invalid old URL pattern: %s"), oldurl); 1742 1713 1743 - for (i = 0; i < urlset_nr; i++) 1744 - if (!regexec(&old_regex, urlset[i], 0, NULL, 0)) 1714 + for (i = 0; i < urlset->nr; i++) 1715 + if (!regexec(&old_regex, urlset->v[i], 0, NULL, 0)) 1745 1716 matches++; 1746 1717 else 1747 1718 negative_matches++;
+5
remote.c
··· 827 827 return 0; 828 828 } 829 829 830 + struct strvec *push_url_of_remote(struct remote *remote) 831 + { 832 + return remote->pushurl.nr ? &remote->pushurl : &remote->url; 833 + } 834 + 830 835 static int match_name_with_pattern(const char *key, const char *name, 831 836 const char *value, char **result) 832 837 {
+1
remote.h
··· 123 123 int for_each_remote(each_remote_fn fn, void *priv); 124 124 125 125 int remote_has_url(struct remote *remote, const char *url); 126 + struct strvec *push_url_of_remote(struct remote *remote); 126 127 127 128 struct ref_push_report { 128 129 const char *ref_name;