Allow programs to not depend on remotes having urls
For fetch and ls-remote, which use the first url of a remote, have transport_get() determine this by passing a remote and passing NULL for the url. For push, which uses every url of a remote, use each url in turn if there are any, and use NULL if there are none. This will allow the transport code to do something different if the location is not specified with a url. Also, have the message for a fetch say "foreign" if there is no url. Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Sverre Rabbelier <srabbelier@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Daniel Barkalow committed
Nov 18, 2009 at 02:42 UTC
fb0cc87ec0f1e9c13b27ea2c89127991d44ed3d3
4 files changed
+51
-29
builtin-fetch.c
+5
-2
@@ -309,7 +309,10 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
309
if (!fp)
310
return error("cannot open %s: %s\n", filename, strerror(errno));
311
312
- url = transport_anonymize_url(raw_url);
312
+ if (raw_url)
313
+ url = transport_anonymize_url(raw_url);
314
+ else
315
+ url = xstrdup("foreign");
316
for (rm = ref_map; rm; rm = rm->next) {
317
struct ref *ref = NULL;
318
@@ -704,7 +707,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
707
if (!remote)
708
die("Where do you want to fetch from today?");
709
707
- transport = transport_get(remote, remote->url[0]);
710
+ transport = transport_get(remote, NULL);
711
if (verbosity >= 2)
712
transport->verbose = 1;
713
if (verbosity < 0)
builtin-ls-remote.c
+1
-1
@@ -89,7 +89,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
89
remote = nongit ? NULL : remote_get(dest);
90
if (remote && !remote->url_nr)
91
die("remote %s has no configured URL", dest);
92
- transport = transport_get(remote, remote ? remote->url[0] : dest);
92
+ transport = transport_get(remote, NULL);
93
if (uploadpack != NULL)
94
transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
95
builtin-push.c
+42
-26
@@ -88,6 +88,36 @@ static void setup_default_push_refspecs(void)
88
}
89
}
90
91
+static int push_with_options(struct transport *transport, int flags)
92
+{
93
+ int err;
94
+ int nonfastforward;
95
+ if (receivepack)
96
+ transport_set_option(transport,
97
+ TRANS_OPT_RECEIVEPACK, receivepack);
98
+ if (thin)
99
+ transport_set_option(transport, TRANS_OPT_THIN, "yes");
100
+
101
+ if (flags & TRANSPORT_PUSH_VERBOSE)
102
+ fprintf(stderr, "Pushing to %s\n", transport->url);
103
+ err = transport_push(transport, refspec_nr, refspec, flags,
104
+ &nonfastforward);
105
+ err |= transport_disconnect(transport);
106
+
107
+ if (!err)
108
+ return 0;
109
+
110
+ error("failed to push some refs to '%s'", transport->url);
111
+
112
+ if (nonfastforward && advice_push_nonfastforward) {
113
+ printf("To prevent you from losing history, non-fast-forward updates were rejected\n"
114
+ "Merge the remote changes before pushing again. See the 'non-fast-forward'\n"
115
+ "section of 'git push --help' for details.\n");
116
+ }
117
+
118
+ return 1;
119
+}
120
+
121
static int do_push(const char *repo, int flags)
122
{
123
int i, errs;
@@ -136,33 +166,19 @@ static int do_push(const char *repo, int flags)
166
url = remote->url;
167
url_nr = remote->url_nr;
168
}
139
- for (i = 0; i < url_nr; i++) {
140
- struct transport *transport =
141
- transport_get(remote, url[i]);
142
- int err;
143
- int nonfastforward;
144
- if (receivepack)
145
- transport_set_option(transport,
146
- TRANS_OPT_RECEIVEPACK, receivepack);
147
- if (thin)
148
- transport_set_option(transport, TRANS_OPT_THIN, "yes");
149
-
150
- if (flags & TRANSPORT_PUSH_VERBOSE)
151
- fprintf(stderr, "Pushing to %s\n", url[i]);
152
- err = transport_push(transport, refspec_nr, refspec, flags,
153
- &nonfastforward);
154
- err |= transport_disconnect(transport);
155
-
156
- if (!err)
157
- continue;
158
-
159
- error("failed to push some refs to '%s'", url[i]);
160
- if (nonfastforward && advice_push_nonfastforward) {
161
- printf("To prevent you from losing history, non-fast-forward updates were rejected\n"
162
- "Merge the remote changes before pushing again. See the 'non-fast forward'\n"
163
- "section of 'git push --help' for details.\n");
169
+ if (url_nr) {
170
+ for (i = 0; i < url_nr; i++) {
171
+ struct transport *transport =
172
+ transport_get(remote, url[i]);
173
+ if (push_with_options(transport, flags))
174
+ errs++;
175
}
165
- errs++;
176
+ } else {
177
+ struct transport *transport =
178
+ transport_get(remote, NULL);
179
+
180
+ if (push_with_options(transport, flags))
181
+ errs++;
182
}
183
return !!errs;
184
}
transport.c
+3
@@ -813,6 +813,9 @@ struct transport *transport_get(struct remote *remote, const char *url)
813
struct transport *ret = xcalloc(1, sizeof(*ret));
814
815
ret->remote = remote;
816
+
817
+ if (!url && remote && remote->url)
818
+ url = remote->url[0];
819
ret->url = url;
820
821
if (!prefixcmp(url, "rsync:")) {