]> git.ipfire.org Git - thirdparty/git.git/blob - builtin-remote.c
git-compat-util.h: avoid using c99 flex array feature with Sun compiler 5.8
[thirdparty/git.git] / builtin-remote.c
1 #include "cache.h"
2 #include "parse-options.h"
3 #include "transport.h"
4 #include "remote.h"
5 #include "string-list.h"
6 #include "strbuf.h"
7 #include "run-command.h"
8 #include "refs.h"
9
10 static const char * const builtin_remote_usage[] = {
11 "git remote [-v | --verbose]",
12 "git remote add [-t <branch>] [-m <master>] [-f] [--mirror] <name> <url>",
13 "git remote rename <old> <new>",
14 "git remote rm <name>",
15 "git remote set-head <name> [-a | -d | <branch>]",
16 "git remote show [-n] <name>",
17 "git remote prune [-n | --dry-run] <name>",
18 "git remote [-v | --verbose] update [-p | --prune] [group]",
19 NULL
20 };
21
22 #define GET_REF_STATES (1<<0)
23 #define GET_HEAD_NAMES (1<<1)
24 #define GET_PUSH_REF_STATES (1<<2)
25
26 static int verbose;
27
28 static int show_all(void);
29 static int prune_remote(const char *remote, int dry_run);
30
31 static inline int postfixcmp(const char *string, const char *postfix)
32 {
33 int len1 = strlen(string), len2 = strlen(postfix);
34 if (len1 < len2)
35 return 1;
36 return strcmp(string + len1 - len2, postfix);
37 }
38
39 static int opt_parse_track(const struct option *opt, const char *arg, int not)
40 {
41 struct string_list *list = opt->value;
42 if (not)
43 string_list_clear(list, 0);
44 else
45 string_list_append(arg, list);
46 return 0;
47 }
48
49 static int fetch_remote(const char *name)
50 {
51 const char *argv[] = { "fetch", name, NULL, NULL };
52 if (verbose) {
53 argv[1] = "-v";
54 argv[2] = name;
55 }
56 printf("Updating %s\n", name);
57 if (run_command_v_opt(argv, RUN_GIT_CMD))
58 return error("Could not fetch %s", name);
59 return 0;
60 }
61
62 static int add(int argc, const char **argv)
63 {
64 int fetch = 0, mirror = 0;
65 struct string_list track = { NULL, 0, 0 };
66 const char *master = NULL;
67 struct remote *remote;
68 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
69 const char *name, *url;
70 int i;
71
72 struct option options[] = {
73 OPT_GROUP("add specific options"),
74 OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"),
75 OPT_CALLBACK('t', "track", &track, "branch",
76 "branch(es) to track", opt_parse_track),
77 OPT_STRING('m', "master", &master, "branch", "master branch"),
78 OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"),
79 OPT_END()
80 };
81
82 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
83
84 if (argc < 2)
85 usage_with_options(builtin_remote_usage, options);
86
87 name = argv[0];
88 url = argv[1];
89
90 remote = remote_get(name);
91 if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) ||
92 remote->fetch_refspec_nr))
93 die("remote %s already exists.", name);
94
95 strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
96 if (!valid_fetch_refspec(buf2.buf))
97 die("'%s' is not a valid remote name", name);
98
99 strbuf_addf(&buf, "remote.%s.url", name);
100 if (git_config_set(buf.buf, url))
101 return 1;
102
103 strbuf_reset(&buf);
104 strbuf_addf(&buf, "remote.%s.fetch", name);
105
106 if (track.nr == 0)
107 string_list_append("*", &track);
108 for (i = 0; i < track.nr; i++) {
109 struct string_list_item *item = track.items + i;
110
111 strbuf_reset(&buf2);
112 strbuf_addch(&buf2, '+');
113 if (mirror)
114 strbuf_addf(&buf2, "refs/%s:refs/%s",
115 item->string, item->string);
116 else
117 strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
118 item->string, name, item->string);
119 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
120 return 1;
121 }
122
123 if (mirror) {
124 strbuf_reset(&buf);
125 strbuf_addf(&buf, "remote.%s.mirror", name);
126 if (git_config_set(buf.buf, "true"))
127 return 1;
128 }
129
130 if (fetch && fetch_remote(name))
131 return 1;
132
133 if (master) {
134 strbuf_reset(&buf);
135 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
136
137 strbuf_reset(&buf2);
138 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
139
140 if (create_symref(buf.buf, buf2.buf, "remote add"))
141 return error("Could not setup master '%s'", master);
142 }
143
144 strbuf_release(&buf);
145 strbuf_release(&buf2);
146 string_list_clear(&track, 0);
147
148 return 0;
149 }
150
151 struct branch_info {
152 char *remote_name;
153 struct string_list merge;
154 int rebase;
155 };
156
157 static struct string_list branch_list;
158
159 static const char *abbrev_ref(const char *name, const char *prefix)
160 {
161 const char *abbrev = skip_prefix(name, prefix);
162 if (abbrev)
163 return abbrev;
164 return name;
165 }
166 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
167
168 static int config_read_branches(const char *key, const char *value, void *cb)
169 {
170 if (!prefixcmp(key, "branch.")) {
171 const char *orig_key = key;
172 char *name;
173 struct string_list_item *item;
174 struct branch_info *info;
175 enum { REMOTE, MERGE, REBASE } type;
176
177 key += 7;
178 if (!postfixcmp(key, ".remote")) {
179 name = xstrndup(key, strlen(key) - 7);
180 type = REMOTE;
181 } else if (!postfixcmp(key, ".merge")) {
182 name = xstrndup(key, strlen(key) - 6);
183 type = MERGE;
184 } else if (!postfixcmp(key, ".rebase")) {
185 name = xstrndup(key, strlen(key) - 7);
186 type = REBASE;
187 } else
188 return 0;
189
190 item = string_list_insert(name, &branch_list);
191
192 if (!item->util)
193 item->util = xcalloc(sizeof(struct branch_info), 1);
194 info = item->util;
195 if (type == REMOTE) {
196 if (info->remote_name)
197 warning("more than one %s", orig_key);
198 info->remote_name = xstrdup(value);
199 } else if (type == MERGE) {
200 char *space = strchr(value, ' ');
201 value = abbrev_branch(value);
202 while (space) {
203 char *merge;
204 merge = xstrndup(value, space - value);
205 string_list_append(merge, &info->merge);
206 value = abbrev_branch(space + 1);
207 space = strchr(value, ' ');
208 }
209 string_list_append(xstrdup(value), &info->merge);
210 } else
211 info->rebase = git_config_bool(orig_key, value);
212 }
213 return 0;
214 }
215
216 static void read_branches(void)
217 {
218 if (branch_list.nr)
219 return;
220 git_config(config_read_branches, NULL);
221 }
222
223 struct ref_states {
224 struct remote *remote;
225 struct string_list new, stale, tracked, heads, push;
226 int queried;
227 };
228
229 static int handle_one_branch(const char *refname,
230 const unsigned char *sha1, int flags, void *cb_data)
231 {
232 struct ref_states *states = cb_data;
233 struct refspec refspec;
234
235 memset(&refspec, 0, sizeof(refspec));
236 refspec.dst = (char *)refname;
237 if (!remote_find_tracking(states->remote, &refspec)) {
238 struct string_list_item *item;
239 const char *name = abbrev_branch(refspec.src);
240 /* symbolic refs pointing nowhere were handled already */
241 if ((flags & REF_ISSYMREF) ||
242 string_list_has_string(&states->tracked, name) ||
243 string_list_has_string(&states->new, name))
244 return 0;
245 item = string_list_append(name, &states->stale);
246 item->util = xstrdup(refname);
247 }
248 return 0;
249 }
250
251 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
252 {
253 struct ref *fetch_map = NULL, **tail = &fetch_map;
254 struct ref *ref;
255 int i;
256
257 for (i = 0; i < states->remote->fetch_refspec_nr; i++)
258 if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1))
259 die("Could not get fetch map for refspec %s",
260 states->remote->fetch_refspec[i]);
261
262 states->new.strdup_strings = states->tracked.strdup_strings = 1;
263 for (ref = fetch_map; ref; ref = ref->next) {
264 unsigned char sha1[20];
265 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
266 string_list_append(abbrev_branch(ref->name), &states->new);
267 else
268 string_list_append(abbrev_branch(ref->name), &states->tracked);
269 }
270 free_refs(fetch_map);
271
272 sort_string_list(&states->new);
273 sort_string_list(&states->tracked);
274 for_each_ref(handle_one_branch, states);
275 sort_string_list(&states->stale);
276
277 return 0;
278 }
279
280 struct push_info {
281 char *dest;
282 int forced;
283 enum {
284 PUSH_STATUS_CREATE = 0,
285 PUSH_STATUS_DELETE,
286 PUSH_STATUS_UPTODATE,
287 PUSH_STATUS_FASTFORWARD,
288 PUSH_STATUS_OUTOFDATE,
289 PUSH_STATUS_NOTQUERIED,
290 } status;
291 };
292
293 static int get_push_ref_states(const struct ref *remote_refs,
294 struct ref_states *states)
295 {
296 struct remote *remote = states->remote;
297 struct ref *ref, *local_refs, *push_map, **push_tail;
298 if (remote->mirror)
299 return 0;
300
301 local_refs = get_local_heads();
302 ref = push_map = copy_ref_list(remote_refs);
303 while (ref->next)
304 ref = ref->next;
305 push_tail = &ref->next;
306
307 match_refs(local_refs, push_map, &push_tail, remote->push_refspec_nr,
308 remote->push_refspec, MATCH_REFS_NONE);
309
310 states->push.strdup_strings = 1;
311 for (ref = push_map; ref; ref = ref->next) {
312 struct string_list_item *item;
313 struct push_info *info;
314
315 if (!ref->peer_ref)
316 continue;
317 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
318
319 item = string_list_append(abbrev_branch(ref->peer_ref->name),
320 &states->push);
321 item->util = xcalloc(sizeof(struct push_info), 1);
322 info = item->util;
323 info->forced = ref->force;
324 info->dest = xstrdup(abbrev_branch(ref->name));
325
326 if (is_null_sha1(ref->new_sha1)) {
327 info->status = PUSH_STATUS_DELETE;
328 } else if (!hashcmp(ref->old_sha1, ref->new_sha1))
329 info->status = PUSH_STATUS_UPTODATE;
330 else if (is_null_sha1(ref->old_sha1))
331 info->status = PUSH_STATUS_CREATE;
332 else if (has_sha1_file(ref->old_sha1) &&
333 ref_newer(ref->new_sha1, ref->old_sha1))
334 info->status = PUSH_STATUS_FASTFORWARD;
335 else
336 info->status = PUSH_STATUS_OUTOFDATE;
337 }
338 free_refs(local_refs);
339 free_refs(push_map);
340 return 0;
341 }
342
343 static int get_push_ref_states_noquery(struct ref_states *states)
344 {
345 int i;
346 struct remote *remote = states->remote;
347 struct string_list_item *item;
348 struct push_info *info;
349
350 if (remote->mirror)
351 return 0;
352
353 states->push.strdup_strings = 1;
354 if (!remote->push_refspec_nr) {
355 item = string_list_append("(matching)", &states->push);
356 info = item->util = xcalloc(sizeof(struct push_info), 1);
357 info->status = PUSH_STATUS_NOTQUERIED;
358 info->dest = xstrdup(item->string);
359 }
360 for (i = 0; i < remote->push_refspec_nr; i++) {
361 struct refspec *spec = remote->push + i;
362 if (spec->matching)
363 item = string_list_append("(matching)", &states->push);
364 else if (strlen(spec->src))
365 item = string_list_append(spec->src, &states->push);
366 else
367 item = string_list_append("(delete)", &states->push);
368
369 info = item->util = xcalloc(sizeof(struct push_info), 1);
370 info->forced = spec->force;
371 info->status = PUSH_STATUS_NOTQUERIED;
372 info->dest = xstrdup(spec->dst ? spec->dst : item->string);
373 }
374 return 0;
375 }
376
377 static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
378 {
379 struct ref *ref, *matches;
380 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
381 struct refspec refspec;
382
383 refspec.force = 0;
384 refspec.pattern = 1;
385 refspec.src = refspec.dst = "refs/heads/*";
386 states->heads.strdup_strings = 1;
387 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
388 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
389 fetch_map, 1);
390 for(ref = matches; ref; ref = ref->next)
391 string_list_append(abbrev_branch(ref->name), &states->heads);
392
393 free_refs(fetch_map);
394 free_refs(matches);
395
396 return 0;
397 }
398
399 struct known_remote {
400 struct known_remote *next;
401 struct remote *remote;
402 };
403
404 struct known_remotes {
405 struct remote *to_delete;
406 struct known_remote *list;
407 };
408
409 static int add_known_remote(struct remote *remote, void *cb_data)
410 {
411 struct known_remotes *all = cb_data;
412 struct known_remote *r;
413
414 if (!strcmp(all->to_delete->name, remote->name))
415 return 0;
416
417 r = xmalloc(sizeof(*r));
418 r->remote = remote;
419 r->next = all->list;
420 all->list = r;
421 return 0;
422 }
423
424 struct branches_for_remote {
425 struct remote *remote;
426 struct string_list *branches, *skipped;
427 struct known_remotes *keep;
428 };
429
430 static int add_branch_for_removal(const char *refname,
431 const unsigned char *sha1, int flags, void *cb_data)
432 {
433 struct branches_for_remote *branches = cb_data;
434 struct refspec refspec;
435 struct string_list_item *item;
436 struct known_remote *kr;
437
438 memset(&refspec, 0, sizeof(refspec));
439 refspec.dst = (char *)refname;
440 if (remote_find_tracking(branches->remote, &refspec))
441 return 0;
442
443 /* don't delete a branch if another remote also uses it */
444 for (kr = branches->keep->list; kr; kr = kr->next) {
445 memset(&refspec, 0, sizeof(refspec));
446 refspec.dst = (char *)refname;
447 if (!remote_find_tracking(kr->remote, &refspec))
448 return 0;
449 }
450
451 /* don't delete non-remote refs */
452 if (prefixcmp(refname, "refs/remotes")) {
453 /* advise user how to delete local branches */
454 if (!prefixcmp(refname, "refs/heads/"))
455 string_list_append(abbrev_branch(refname),
456 branches->skipped);
457 /* silently skip over other non-remote refs */
458 return 0;
459 }
460
461 /* make sure that symrefs are deleted */
462 if (flags & REF_ISSYMREF)
463 return unlink(git_path("%s", refname));
464
465 item = string_list_append(refname, branches->branches);
466 item->util = xmalloc(20);
467 hashcpy(item->util, sha1);
468
469 return 0;
470 }
471
472 struct rename_info {
473 const char *old;
474 const char *new;
475 struct string_list *remote_branches;
476 };
477
478 static int read_remote_branches(const char *refname,
479 const unsigned char *sha1, int flags, void *cb_data)
480 {
481 struct rename_info *rename = cb_data;
482 struct strbuf buf = STRBUF_INIT;
483 struct string_list_item *item;
484 int flag;
485 unsigned char orig_sha1[20];
486 const char *symref;
487
488 strbuf_addf(&buf, "refs/remotes/%s", rename->old);
489 if(!prefixcmp(refname, buf.buf)) {
490 item = string_list_append(xstrdup(refname), rename->remote_branches);
491 symref = resolve_ref(refname, orig_sha1, 1, &flag);
492 if (flag & REF_ISSYMREF)
493 item->util = xstrdup(symref);
494 else
495 item->util = NULL;
496 }
497
498 return 0;
499 }
500
501 static int migrate_file(struct remote *remote)
502 {
503 struct strbuf buf = STRBUF_INIT;
504 int i;
505 char *path = NULL;
506
507 strbuf_addf(&buf, "remote.%s.url", remote->name);
508 for (i = 0; i < remote->url_nr; i++)
509 if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0))
510 return error("Could not append '%s' to '%s'",
511 remote->url[i], buf.buf);
512 strbuf_reset(&buf);
513 strbuf_addf(&buf, "remote.%s.push", remote->name);
514 for (i = 0; i < remote->push_refspec_nr; i++)
515 if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0))
516 return error("Could not append '%s' to '%s'",
517 remote->push_refspec[i], buf.buf);
518 strbuf_reset(&buf);
519 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
520 for (i = 0; i < remote->fetch_refspec_nr; i++)
521 if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0))
522 return error("Could not append '%s' to '%s'",
523 remote->fetch_refspec[i], buf.buf);
524 if (remote->origin == REMOTE_REMOTES)
525 path = git_path("remotes/%s", remote->name);
526 else if (remote->origin == REMOTE_BRANCHES)
527 path = git_path("branches/%s", remote->name);
528 if (path)
529 unlink_or_warn(path);
530 return 0;
531 }
532
533 static int mv(int argc, const char **argv)
534 {
535 struct option options[] = {
536 OPT_END()
537 };
538 struct remote *oldremote, *newremote;
539 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT;
540 struct string_list remote_branches = { NULL, 0, 0, 0 };
541 struct rename_info rename;
542 int i;
543
544 if (argc != 3)
545 usage_with_options(builtin_remote_usage, options);
546
547 rename.old = argv[1];
548 rename.new = argv[2];
549 rename.remote_branches = &remote_branches;
550
551 oldremote = remote_get(rename.old);
552 if (!oldremote)
553 die("No such remote: %s", rename.old);
554
555 if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
556 return migrate_file(oldremote);
557
558 newremote = remote_get(rename.new);
559 if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr))
560 die("remote %s already exists.", rename.new);
561
562 strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new);
563 if (!valid_fetch_refspec(buf.buf))
564 die("'%s' is not a valid remote name", rename.new);
565
566 strbuf_reset(&buf);
567 strbuf_addf(&buf, "remote.%s", rename.old);
568 strbuf_addf(&buf2, "remote.%s", rename.new);
569 if (git_config_rename_section(buf.buf, buf2.buf) < 1)
570 return error("Could not rename config section '%s' to '%s'",
571 buf.buf, buf2.buf);
572
573 strbuf_reset(&buf);
574 strbuf_addf(&buf, "remote.%s.fetch", rename.new);
575 if (git_config_set_multivar(buf.buf, NULL, NULL, 1))
576 return error("Could not remove config section '%s'", buf.buf);
577 for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
578 char *ptr;
579
580 strbuf_reset(&buf2);
581 strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
582 ptr = strstr(buf2.buf, rename.old);
583 if (ptr)
584 strbuf_splice(&buf2, ptr-buf2.buf, strlen(rename.old),
585 rename.new, strlen(rename.new));
586 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
587 return error("Could not append '%s'", buf.buf);
588 }
589
590 read_branches();
591 for (i = 0; i < branch_list.nr; i++) {
592 struct string_list_item *item = branch_list.items + i;
593 struct branch_info *info = item->util;
594 if (info->remote_name && !strcmp(info->remote_name, rename.old)) {
595 strbuf_reset(&buf);
596 strbuf_addf(&buf, "branch.%s.remote", item->string);
597 if (git_config_set(buf.buf, rename.new)) {
598 return error("Could not set '%s'", buf.buf);
599 }
600 }
601 }
602
603 /*
604 * First remove symrefs, then rename the rest, finally create
605 * the new symrefs.
606 */
607 for_each_ref(read_remote_branches, &rename);
608 for (i = 0; i < remote_branches.nr; i++) {
609 struct string_list_item *item = remote_branches.items + i;
610 int flag = 0;
611 unsigned char sha1[20];
612
613 resolve_ref(item->string, sha1, 1, &flag);
614 if (!(flag & REF_ISSYMREF))
615 continue;
616 if (delete_ref(item->string, NULL, REF_NODEREF))
617 die("deleting '%s' failed", item->string);
618 }
619 for (i = 0; i < remote_branches.nr; i++) {
620 struct string_list_item *item = remote_branches.items + i;
621
622 if (item->util)
623 continue;
624 strbuf_reset(&buf);
625 strbuf_addstr(&buf, item->string);
626 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
627 rename.new, strlen(rename.new));
628 strbuf_reset(&buf2);
629 strbuf_addf(&buf2, "remote: renamed %s to %s",
630 item->string, buf.buf);
631 if (rename_ref(item->string, buf.buf, buf2.buf))
632 die("renaming '%s' failed", item->string);
633 }
634 for (i = 0; i < remote_branches.nr; i++) {
635 struct string_list_item *item = remote_branches.items + i;
636
637 if (!item->util)
638 continue;
639 strbuf_reset(&buf);
640 strbuf_addstr(&buf, item->string);
641 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
642 rename.new, strlen(rename.new));
643 strbuf_reset(&buf2);
644 strbuf_addstr(&buf2, item->util);
645 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old),
646 rename.new, strlen(rename.new));
647 strbuf_reset(&buf3);
648 strbuf_addf(&buf3, "remote: renamed %s to %s",
649 item->string, buf.buf);
650 if (create_symref(buf.buf, buf2.buf, buf3.buf))
651 die("creating '%s' failed", buf.buf);
652 }
653 return 0;
654 }
655
656 static int remove_branches(struct string_list *branches)
657 {
658 int i, result = 0;
659 for (i = 0; i < branches->nr; i++) {
660 struct string_list_item *item = branches->items + i;
661 const char *refname = item->string;
662 unsigned char *sha1 = item->util;
663
664 if (delete_ref(refname, sha1, 0))
665 result |= error("Could not remove branch %s", refname);
666 }
667 return result;
668 }
669
670 static int rm(int argc, const char **argv)
671 {
672 struct option options[] = {
673 OPT_END()
674 };
675 struct remote *remote;
676 struct strbuf buf = STRBUF_INIT;
677 struct known_remotes known_remotes = { NULL, NULL };
678 struct string_list branches = { NULL, 0, 0, 1 };
679 struct string_list skipped = { NULL, 0, 0, 1 };
680 struct branches_for_remote cb_data = {
681 NULL, &branches, &skipped, &known_remotes
682 };
683 int i, result;
684
685 if (argc != 2)
686 usage_with_options(builtin_remote_usage, options);
687
688 remote = remote_get(argv[1]);
689 if (!remote)
690 die("No such remote: %s", argv[1]);
691
692 known_remotes.to_delete = remote;
693 for_each_remote(add_known_remote, &known_remotes);
694
695 strbuf_addf(&buf, "remote.%s", remote->name);
696 if (git_config_rename_section(buf.buf, NULL) < 1)
697 return error("Could not remove config section '%s'", buf.buf);
698
699 read_branches();
700 for (i = 0; i < branch_list.nr; i++) {
701 struct string_list_item *item = branch_list.items + i;
702 struct branch_info *info = item->util;
703 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
704 const char *keys[] = { "remote", "merge", NULL }, **k;
705 for (k = keys; *k; k++) {
706 strbuf_reset(&buf);
707 strbuf_addf(&buf, "branch.%s.%s",
708 item->string, *k);
709 if (git_config_set(buf.buf, NULL)) {
710 strbuf_release(&buf);
711 return -1;
712 }
713 }
714 }
715 }
716
717 /*
718 * We cannot just pass a function to for_each_ref() which deletes
719 * the branches one by one, since for_each_ref() relies on cached
720 * refs, which are invalidated when deleting a branch.
721 */
722 cb_data.remote = remote;
723 result = for_each_ref(add_branch_for_removal, &cb_data);
724 strbuf_release(&buf);
725
726 if (!result)
727 result = remove_branches(&branches);
728 string_list_clear(&branches, 1);
729
730 if (skipped.nr) {
731 fprintf(stderr, skipped.nr == 1 ?
732 "Note: A non-remote branch was not removed; "
733 "to delete it, use:\n" :
734 "Note: Non-remote branches were not removed; "
735 "to delete them, use:\n");
736 for (i = 0; i < skipped.nr; i++)
737 fprintf(stderr, " git branch -d %s\n",
738 skipped.items[i].string);
739 }
740 string_list_clear(&skipped, 0);
741
742 return result;
743 }
744
745 void clear_push_info(void *util, const char *string)
746 {
747 struct push_info *info = util;
748 free(info->dest);
749 free(info);
750 }
751
752 static void free_remote_ref_states(struct ref_states *states)
753 {
754 string_list_clear(&states->new, 0);
755 string_list_clear(&states->stale, 0);
756 string_list_clear(&states->tracked, 0);
757 string_list_clear(&states->heads, 0);
758 string_list_clear_func(&states->push, clear_push_info);
759 }
760
761 static int append_ref_to_tracked_list(const char *refname,
762 const unsigned char *sha1, int flags, void *cb_data)
763 {
764 struct ref_states *states = cb_data;
765 struct refspec refspec;
766
767 if (flags & REF_ISSYMREF)
768 return 0;
769
770 memset(&refspec, 0, sizeof(refspec));
771 refspec.dst = (char *)refname;
772 if (!remote_find_tracking(states->remote, &refspec))
773 string_list_append(abbrev_branch(refspec.src), &states->tracked);
774
775 return 0;
776 }
777
778 static int get_remote_ref_states(const char *name,
779 struct ref_states *states,
780 int query)
781 {
782 struct transport *transport;
783 const struct ref *remote_refs;
784
785 states->remote = remote_get(name);
786 if (!states->remote)
787 return error("No such remote: %s", name);
788
789 read_branches();
790
791 if (query) {
792 transport = transport_get(NULL, states->remote->url_nr > 0 ?
793 states->remote->url[0] : NULL);
794 remote_refs = transport_get_remote_refs(transport);
795 transport_disconnect(transport);
796
797 states->queried = 1;
798 if (query & GET_REF_STATES)
799 get_ref_states(remote_refs, states);
800 if (query & GET_HEAD_NAMES)
801 get_head_names(remote_refs, states);
802 if (query & GET_PUSH_REF_STATES)
803 get_push_ref_states(remote_refs, states);
804 } else {
805 for_each_ref(append_ref_to_tracked_list, states);
806 sort_string_list(&states->tracked);
807 get_push_ref_states_noquery(states);
808 }
809
810 return 0;
811 }
812
813 struct show_info {
814 struct string_list *list;
815 struct ref_states *states;
816 int width, width2;
817 int any_rebase;
818 };
819
820 int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
821 {
822 struct show_info *info = cb_data;
823 int n = strlen(item->string);
824 if (n > info->width)
825 info->width = n;
826 string_list_insert(item->string, info->list);
827 return 0;
828 }
829
830 int show_remote_info_item(struct string_list_item *item, void *cb_data)
831 {
832 struct show_info *info = cb_data;
833 struct ref_states *states = info->states;
834 const char *name = item->string;
835
836 if (states->queried) {
837 const char *fmt = "%s";
838 const char *arg = "";
839 if (string_list_has_string(&states->new, name)) {
840 fmt = " new (next fetch will store in remotes/%s)";
841 arg = states->remote->name;
842 } else if (string_list_has_string(&states->tracked, name))
843 arg = " tracked";
844 else if (string_list_has_string(&states->stale, name))
845 arg = " stale (use 'git remote prune' to remove)";
846 else
847 arg = " ???";
848 printf(" %-*s", info->width, name);
849 printf(fmt, arg);
850 printf("\n");
851 } else
852 printf(" %s\n", name);
853
854 return 0;
855 }
856
857 int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
858 {
859 struct show_info *show_info = cb_data;
860 struct ref_states *states = show_info->states;
861 struct branch_info *branch_info = branch_item->util;
862 struct string_list_item *item;
863 int n;
864
865 if (!branch_info->merge.nr || !branch_info->remote_name ||
866 strcmp(states->remote->name, branch_info->remote_name))
867 return 0;
868 if ((n = strlen(branch_item->string)) > show_info->width)
869 show_info->width = n;
870 if (branch_info->rebase)
871 show_info->any_rebase = 1;
872
873 item = string_list_insert(branch_item->string, show_info->list);
874 item->util = branch_info;
875
876 return 0;
877 }
878
879 int show_local_info_item(struct string_list_item *item, void *cb_data)
880 {
881 struct show_info *show_info = cb_data;
882 struct branch_info *branch_info = item->util;
883 struct string_list *merge = &branch_info->merge;
884 const char *also;
885 int i;
886
887 if (branch_info->rebase && branch_info->merge.nr > 1) {
888 error("invalid branch.%s.merge; cannot rebase onto > 1 branch",
889 item->string);
890 return 0;
891 }
892
893 printf(" %-*s ", show_info->width, item->string);
894 if (branch_info->rebase) {
895 printf("rebases onto remote %s\n", merge->items[0].string);
896 return 0;
897 } else if (show_info->any_rebase) {
898 printf(" merges with remote %s\n", merge->items[0].string);
899 also = " and with remote";
900 } else {
901 printf("merges with remote %s\n", merge->items[0].string);
902 also = " and with remote";
903 }
904 for (i = 1; i < merge->nr; i++)
905 printf(" %-*s %s %s\n", show_info->width, "", also,
906 merge->items[i].string);
907
908 return 0;
909 }
910
911 int add_push_to_show_info(struct string_list_item *push_item, void *cb_data)
912 {
913 struct show_info *show_info = cb_data;
914 struct push_info *push_info = push_item->util;
915 struct string_list_item *item;
916 int n;
917 if ((n = strlen(push_item->string)) > show_info->width)
918 show_info->width = n;
919 if ((n = strlen(push_info->dest)) > show_info->width2)
920 show_info->width2 = n;
921 item = string_list_append(push_item->string, show_info->list);
922 item->util = push_item->util;
923 return 0;
924 }
925
926 /*
927 * Sorting comparison for a string list that has push_info
928 * structs in its util field
929 */
930 static int cmp_string_with_push(const void *va, const void *vb)
931 {
932 const struct string_list_item *a = va;
933 const struct string_list_item *b = vb;
934 const struct push_info *a_push = a->util;
935 const struct push_info *b_push = b->util;
936 int cmp = strcmp(a->string, b->string);
937 return cmp ? cmp : strcmp(a_push->dest, b_push->dest);
938 }
939
940 int show_push_info_item(struct string_list_item *item, void *cb_data)
941 {
942 struct show_info *show_info = cb_data;
943 struct push_info *push_info = item->util;
944 char *src = item->string, *status = NULL;
945
946 switch (push_info->status) {
947 case PUSH_STATUS_CREATE:
948 status = "create";
949 break;
950 case PUSH_STATUS_DELETE:
951 status = "delete";
952 src = "(none)";
953 break;
954 case PUSH_STATUS_UPTODATE:
955 status = "up to date";
956 break;
957 case PUSH_STATUS_FASTFORWARD:
958 status = "fast forwardable";
959 break;
960 case PUSH_STATUS_OUTOFDATE:
961 status = "local out of date";
962 break;
963 case PUSH_STATUS_NOTQUERIED:
964 break;
965 }
966 if (status)
967 printf(" %-*s %s to %-*s (%s)\n", show_info->width, src,
968 push_info->forced ? "forces" : "pushes",
969 show_info->width2, push_info->dest, status);
970 else
971 printf(" %-*s %s to %s\n", show_info->width, src,
972 push_info->forced ? "forces" : "pushes",
973 push_info->dest);
974 return 0;
975 }
976
977 static int show(int argc, const char **argv)
978 {
979 int no_query = 0, result = 0, query_flag = 0;
980 struct option options[] = {
981 OPT_GROUP("show specific options"),
982 OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
983 OPT_END()
984 };
985 struct ref_states states;
986 struct string_list info_list = { NULL, 0, 0, 0 };
987 struct show_info info;
988
989 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
990
991 if (argc < 1)
992 return show_all();
993
994 if (!no_query)
995 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
996
997 memset(&states, 0, sizeof(states));
998 memset(&info, 0, sizeof(info));
999 info.states = &states;
1000 info.list = &info_list;
1001 for (; argc; argc--, argv++) {
1002 int i;
1003
1004 get_remote_ref_states(*argv, &states, query_flag);
1005
1006 printf("* remote %s\n URL: %s\n", *argv,
1007 states.remote->url_nr > 0 ?
1008 states.remote->url[0] : "(no URL)");
1009 if (no_query)
1010 printf(" HEAD branch: (not queried)\n");
1011 else if (!states.heads.nr)
1012 printf(" HEAD branch: (unknown)\n");
1013 else if (states.heads.nr == 1)
1014 printf(" HEAD branch: %s\n", states.heads.items[0].string);
1015 else {
1016 printf(" HEAD branch (remote HEAD is ambiguous,"
1017 " may be one of the following):\n");
1018 for (i = 0; i < states.heads.nr; i++)
1019 printf(" %s\n", states.heads.items[i].string);
1020 }
1021
1022 /* remote branch info */
1023 info.width = 0;
1024 for_each_string_list(add_remote_to_show_info, &states.new, &info);
1025 for_each_string_list(add_remote_to_show_info, &states.tracked, &info);
1026 for_each_string_list(add_remote_to_show_info, &states.stale, &info);
1027 if (info.list->nr)
1028 printf(" Remote branch%s:%s\n",
1029 info.list->nr > 1 ? "es" : "",
1030 no_query ? " (status not queried)" : "");
1031 for_each_string_list(show_remote_info_item, info.list, &info);
1032 string_list_clear(info.list, 0);
1033
1034 /* git pull info */
1035 info.width = 0;
1036 info.any_rebase = 0;
1037 for_each_string_list(add_local_to_show_info, &branch_list, &info);
1038 if (info.list->nr)
1039 printf(" Local branch%s configured for 'git pull':\n",
1040 info.list->nr > 1 ? "es" : "");
1041 for_each_string_list(show_local_info_item, info.list, &info);
1042 string_list_clear(info.list, 0);
1043
1044 /* git push info */
1045 if (states.remote->mirror)
1046 printf(" Local refs will be mirrored by 'git push'\n");
1047
1048 info.width = info.width2 = 0;
1049 for_each_string_list(add_push_to_show_info, &states.push, &info);
1050 qsort(info.list->items, info.list->nr,
1051 sizeof(*info.list->items), cmp_string_with_push);
1052 if (info.list->nr)
1053 printf(" Local ref%s configured for 'git push'%s:\n",
1054 info.list->nr > 1 ? "s" : "",
1055 no_query ? " (status not queried)" : "");
1056 for_each_string_list(show_push_info_item, info.list, &info);
1057 string_list_clear(info.list, 0);
1058
1059 free_remote_ref_states(&states);
1060 }
1061
1062 return result;
1063 }
1064
1065 static int set_head(int argc, const char **argv)
1066 {
1067 int i, opt_a = 0, opt_d = 0, result = 0;
1068 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1069 char *head_name = NULL;
1070
1071 struct option options[] = {
1072 OPT_GROUP("set-head specific options"),
1073 OPT_BOOLEAN('a', "auto", &opt_a,
1074 "set refs/remotes/<name>/HEAD according to remote"),
1075 OPT_BOOLEAN('d', "delete", &opt_d,
1076 "delete refs/remotes/<name>/HEAD"),
1077 OPT_END()
1078 };
1079 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
1080 if (argc)
1081 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1082
1083 if (!opt_a && !opt_d && argc == 2) {
1084 head_name = xstrdup(argv[1]);
1085 } else if (opt_a && !opt_d && argc == 1) {
1086 struct ref_states states;
1087 memset(&states, 0, sizeof(states));
1088 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1089 if (!states.heads.nr)
1090 result |= error("Cannot determine remote HEAD");
1091 else if (states.heads.nr > 1) {
1092 result |= error("Multiple remote HEAD branches. "
1093 "Please choose one explicitly with:");
1094 for (i = 0; i < states.heads.nr; i++)
1095 fprintf(stderr, " git remote set-head %s %s\n",
1096 argv[0], states.heads.items[i].string);
1097 } else
1098 head_name = xstrdup(states.heads.items[0].string);
1099 free_remote_ref_states(&states);
1100 } else if (opt_d && !opt_a && argc == 1) {
1101 if (delete_ref(buf.buf, NULL, REF_NODEREF))
1102 result |= error("Could not delete %s", buf.buf);
1103 } else
1104 usage_with_options(builtin_remote_usage, options);
1105
1106 if (head_name) {
1107 unsigned char sha1[20];
1108 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1109 /* make sure it's valid */
1110 if (!resolve_ref(buf2.buf, sha1, 1, NULL))
1111 result |= error("Not a valid ref: %s", buf2.buf);
1112 else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
1113 result |= error("Could not setup %s", buf.buf);
1114 if (opt_a)
1115 printf("%s/HEAD set to %s\n", argv[0], head_name);
1116 free(head_name);
1117 }
1118
1119 strbuf_release(&buf);
1120 strbuf_release(&buf2);
1121 return result;
1122 }
1123
1124 static int prune(int argc, const char **argv)
1125 {
1126 int dry_run = 0, result = 0;
1127 struct option options[] = {
1128 OPT_GROUP("prune specific options"),
1129 OPT__DRY_RUN(&dry_run),
1130 OPT_END()
1131 };
1132
1133 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
1134
1135 if (argc < 1)
1136 usage_with_options(builtin_remote_usage, options);
1137
1138 for (; argc; argc--, argv++)
1139 result |= prune_remote(*argv, dry_run);
1140
1141 return result;
1142 }
1143
1144 static int prune_remote(const char *remote, int dry_run)
1145 {
1146 int result = 0, i;
1147 struct ref_states states;
1148 const char *dangling_msg = dry_run
1149 ? " %s will become dangling!\n"
1150 : " %s has become dangling!\n";
1151
1152 memset(&states, 0, sizeof(states));
1153 get_remote_ref_states(remote, &states, GET_REF_STATES);
1154
1155 if (states.stale.nr) {
1156 printf("Pruning %s\n", remote);
1157 printf("URL: %s\n",
1158 states.remote->url_nr
1159 ? states.remote->url[0]
1160 : "(no URL)");
1161 }
1162
1163 for (i = 0; i < states.stale.nr; i++) {
1164 const char *refname = states.stale.items[i].util;
1165
1166 if (!dry_run)
1167 result |= delete_ref(refname, NULL, 0);
1168
1169 printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
1170 abbrev_ref(refname, "refs/remotes/"));
1171 warn_dangling_symref(dangling_msg, refname);
1172 }
1173
1174 free_remote_ref_states(&states);
1175 return result;
1176 }
1177
1178 static int get_one_remote_for_update(struct remote *remote, void *priv)
1179 {
1180 struct string_list *list = priv;
1181 if (!remote->skip_default_update)
1182 string_list_append(remote->name, list);
1183 return 0;
1184 }
1185
1186 struct remote_group {
1187 const char *name;
1188 struct string_list *list;
1189 } remote_group;
1190
1191 static int get_remote_group(const char *key, const char *value, void *num_hits)
1192 {
1193 if (!prefixcmp(key, "remotes.") &&
1194 !strcmp(key + 8, remote_group.name)) {
1195 /* split list by white space */
1196 int space = strcspn(value, " \t\n");
1197 while (*value) {
1198 if (space > 1) {
1199 string_list_append(xstrndup(value, space),
1200 remote_group.list);
1201 ++*((int *)num_hits);
1202 }
1203 value += space + (value[space] != '\0');
1204 space = strcspn(value, " \t\n");
1205 }
1206 }
1207
1208 return 0;
1209 }
1210
1211 static int update(int argc, const char **argv)
1212 {
1213 int i, result = 0, prune = 0;
1214 struct string_list list = { NULL, 0, 0, 0 };
1215 static const char *default_argv[] = { NULL, "default", NULL };
1216 struct option options[] = {
1217 OPT_GROUP("update specific options"),
1218 OPT_BOOLEAN('p', "prune", &prune,
1219 "prune remotes after fetching"),
1220 OPT_END()
1221 };
1222
1223 argc = parse_options(argc, argv, options, builtin_remote_usage,
1224 PARSE_OPT_KEEP_ARGV0);
1225 if (argc < 2) {
1226 argc = 2;
1227 argv = default_argv;
1228 }
1229
1230 remote_group.list = &list;
1231 for (i = 1; i < argc; i++) {
1232 int groups_found = 0;
1233 remote_group.name = argv[i];
1234 result = git_config(get_remote_group, &groups_found);
1235 if (!groups_found && (i != 1 || strcmp(argv[1], "default"))) {
1236 struct remote *remote;
1237 if (!remote_is_configured(argv[i]))
1238 die("No such remote or remote group: %s",
1239 argv[i]);
1240 remote = remote_get(argv[i]);
1241 string_list_append(remote->name, remote_group.list);
1242 }
1243 }
1244
1245 if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default"))
1246 result = for_each_remote(get_one_remote_for_update, &list);
1247
1248 for (i = 0; i < list.nr; i++) {
1249 int err = fetch_remote(list.items[i].string);
1250 result |= err;
1251 if (!err && prune)
1252 result |= prune_remote(list.items[i].string, 0);
1253 }
1254
1255 /* all names were strdup()ed or strndup()ed */
1256 list.strdup_strings = 1;
1257 string_list_clear(&list, 0);
1258
1259 return result;
1260 }
1261
1262 static int get_one_entry(struct remote *remote, void *priv)
1263 {
1264 struct string_list *list = priv;
1265
1266 if (remote->url_nr > 0) {
1267 int i;
1268
1269 for (i = 0; i < remote->url_nr; i++)
1270 string_list_append(remote->name, list)->util = (void *)remote->url[i];
1271 } else
1272 string_list_append(remote->name, list)->util = NULL;
1273
1274 return 0;
1275 }
1276
1277 static int show_all(void)
1278 {
1279 struct string_list list = { NULL, 0, 0 };
1280 int result = for_each_remote(get_one_entry, &list);
1281
1282 if (!result) {
1283 int i;
1284
1285 sort_string_list(&list);
1286 for (i = 0; i < list.nr; i++) {
1287 struct string_list_item *item = list.items + i;
1288 if (verbose)
1289 printf("%s\t%s\n", item->string,
1290 item->util ? (const char *)item->util : "");
1291 else {
1292 if (i && !strcmp((item - 1)->string, item->string))
1293 continue;
1294 printf("%s\n", item->string);
1295 }
1296 }
1297 }
1298 return result;
1299 }
1300
1301 int cmd_remote(int argc, const char **argv, const char *prefix)
1302 {
1303 struct option options[] = {
1304 OPT__VERBOSE(&verbose),
1305 OPT_END()
1306 };
1307 int result;
1308
1309 argc = parse_options(argc, argv, options, builtin_remote_usage,
1310 PARSE_OPT_STOP_AT_NON_OPTION);
1311
1312 if (argc < 1)
1313 result = show_all();
1314 else if (!strcmp(argv[0], "add"))
1315 result = add(argc, argv);
1316 else if (!strcmp(argv[0], "rename"))
1317 result = mv(argc, argv);
1318 else if (!strcmp(argv[0], "rm"))
1319 result = rm(argc, argv);
1320 else if (!strcmp(argv[0], "set-head"))
1321 result = set_head(argc, argv);
1322 else if (!strcmp(argv[0], "show"))
1323 result = show(argc, argv);
1324 else if (!strcmp(argv[0], "prune"))
1325 result = prune(argc, argv);
1326 else if (!strcmp(argv[0], "update"))
1327 result = update(argc, argv);
1328 else {
1329 error("Unknown subcommand: %s", argv[0]);
1330 usage_with_options(builtin_remote_usage, options);
1331 }
1332
1333 return result ? 1 : 0;
1334 }