Add for_each_remote() function, and extend remote_find_tracking()
The function for_each_remote() does exactly what the name suggests. The function remote_find_tracking() was extended to be able to search remote refs for a given local ref. The caller sets either src or dst (but not both) in the refspec parameter, and remote_find_tracking() will fill in the other and return 0. Both changes are required for the next step: simplification of git-branch's --track functionality. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Jul 10, 2007 at 18:48 UTC
b42f69273bf5ae2d5bb0c7479bfa9827e7b351ac
3 files changed
+51
-18
remote.c
+45
-15
@@ -279,6 +279,25 @@ struct remote *remote_get(const char *name)
279
return ret;
280
}
281
282
+int for_each_remote(each_remote_fn fn, void *priv)
283
+{
284
+ int i, result = 0;
285
+ read_config();
286
+ for (i = 0; i < allocated_remotes && !result; i++) {
287
+ struct remote *r = remotes[i];
288
+ if (!r)
289
+ continue;
290
+ if (!r->fetch)
291
+ r->fetch = parse_ref_spec(r->fetch_refspec_nr,
292
+ r->fetch_refspec);
293
+ if (!r->push)
294
+ r->push = parse_ref_spec(r->push_refspec_nr,
295
+ r->push_refspec);
296
+ result = fn(r, priv);
297
+ }
298
+ return result;
299
+}
300
+
301
int remote_has_uri(struct remote *remote, const char *uri)
302
{
303
int i;
@@ -291,32 +310,43 @@ int remote_has_uri(struct remote *remote, const char *uri)
310
311
int remote_find_tracking(struct remote *remote, struct refspec *refspec)
312
{
313
+ int find_src = refspec->src == NULL;
314
+ char *needle, **result;
315
int i;
316
+
317
+ if (find_src) {
318
+ if (refspec->dst == NULL)
319
+ return error("find_tracking: need either src or dst");
320
+ needle = refspec->dst;
321
+ result = &refspec->src;
322
+ } else {
323
+ needle = refspec->src;
324
+ result = &refspec->dst;
325
+ }
326
+
327
for (i = 0; i < remote->fetch_refspec_nr; i++) {
328
struct refspec *fetch = &remote->fetch[i];
329
+ const char *key = find_src ? fetch->dst : fetch->src;
330
+ const char *value = find_src ? fetch->src : fetch->dst;
331
if (!fetch->dst)
332
continue;
333
if (fetch->pattern) {
300
- if (!prefixcmp(refspec->src, fetch->src)) {
301
- refspec->dst =
302
- xmalloc(strlen(fetch->dst) +
303
- strlen(refspec->src) -
304
- strlen(fetch->src) + 1);
305
- strcpy(refspec->dst, fetch->dst);
306
- strcpy(refspec->dst + strlen(fetch->dst),
307
- refspec->src + strlen(fetch->src));
308
- refspec->force = fetch->force;
309
- return 0;
310
- }
311
- } else {
312
- if (!strcmp(refspec->src, fetch->src)) {
313
- refspec->dst = xstrdup(fetch->dst);
334
+ if (!prefixcmp(needle, key)) {
335
+ *result = xmalloc(strlen(value) +
336
+ strlen(needle) -
337
+ strlen(key) + 1);
338
+ strcpy(*result, value);
339
+ strcpy(*result + strlen(value),
340
+ needle + strlen(key));
341
refspec->force = fetch->force;
342
return 0;
343
}
344
+ } else if (!strcmp(needle, key)) {
345
+ *result = xstrdup(value);
346
+ refspec->force = fetch->force;
347
+ return 0;
348
}
349
}
319
- refspec->dst = NULL;
350
return -1;
351
}
352
remote.h
+4
-1
@@ -20,13 +20,16 @@ struct remote {
20
21
struct remote *remote_get(const char *name);
22
23
+typedef int each_remote_fn(struct remote *remote, void *priv);
24
+int for_each_remote(each_remote_fn fn, void *priv);
25
+
26
int remote_has_uri(struct remote *remote, const char *uri);
27
28
struct refspec {
29
unsigned force : 1;
30
unsigned pattern : 1;
31
29
- const char *src;
32
+ char *src;
33
char *dst;
34
};
35
send-pack.c
+2
-2
@@ -305,8 +305,8 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha
305
if (remote) {
306
struct refspec rs;
307
rs.src = ref->name;
308
- remote_find_tracking(remote, &rs);
309
- if (rs.dst) {
308
+ rs.dst = NULL;
309
+ if (!remote_find_tracking(remote, &rs)) {
310
struct ref_lock *lock;
311
fprintf(stderr, " Also local %s\n", rs.dst);
312
if (will_delete_ref) {