]> Pileus Git - ~andy/git/blob - builtin-remote.c
remote: make guess_remote_head() use exact HEAD lookup if it is available
[~andy/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 [group]",
19         NULL
20 };
21
22 #define GET_REF_STATES (1<<0)
23 #define GET_HEAD_NAMES (1<<1)
24
25 static int verbose;
26
27 static int show_all(void);
28
29 static inline int postfixcmp(const char *string, const char *postfix)
30 {
31         int len1 = strlen(string), len2 = strlen(postfix);
32         if (len1 < len2)
33                 return 1;
34         return strcmp(string + len1 - len2, postfix);
35 }
36
37 static int opt_parse_track(const struct option *opt, const char *arg, int not)
38 {
39         struct string_list *list = opt->value;
40         if (not)
41                 string_list_clear(list, 0);
42         else
43                 string_list_append(arg, list);
44         return 0;
45 }
46
47 static int fetch_remote(const char *name)
48 {
49         const char *argv[] = { "fetch", name, NULL, NULL };
50         if (verbose) {
51                 argv[1] = "-v";
52                 argv[2] = name;
53         }
54         printf("Updating %s\n", name);
55         if (run_command_v_opt(argv, RUN_GIT_CMD))
56                 return error("Could not fetch %s", name);
57         return 0;
58 }
59
60 static int add(int argc, const char **argv)
61 {
62         int fetch = 0, mirror = 0;
63         struct string_list track = { NULL, 0, 0 };
64         const char *master = NULL;
65         struct remote *remote;
66         struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
67         const char *name, *url;
68         int i;
69
70         struct option options[] = {
71                 OPT_GROUP("add specific options"),
72                 OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"),
73                 OPT_CALLBACK('t', "track", &track, "branch",
74                         "branch(es) to track", opt_parse_track),
75                 OPT_STRING('m', "master", &master, "branch", "master branch"),
76                 OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"),
77                 OPT_END()
78         };
79
80         argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
81
82         if (argc < 2)
83                 usage_with_options(builtin_remote_usage, options);
84
85         name = argv[0];
86         url = argv[1];
87
88         remote = remote_get(name);
89         if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) ||
90                         remote->fetch_refspec_nr))
91                 die("remote %s already exists.", name);
92
93         strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
94         if (!valid_fetch_refspec(buf2.buf))
95                 die("'%s' is not a valid remote name", name);
96
97         strbuf_addf(&buf, "remote.%s.url", name);
98         if (git_config_set(buf.buf, url))
99                 return 1;
100
101         strbuf_reset(&buf);
102         strbuf_addf(&buf, "remote.%s.fetch", name);
103
104         if (track.nr == 0)
105                 string_list_append("*", &track);
106         for (i = 0; i < track.nr; i++) {
107                 struct string_list_item *item = track.items + i;
108
109                 strbuf_reset(&buf2);
110                 strbuf_addch(&buf2, '+');
111                 if (mirror)
112                         strbuf_addf(&buf2, "refs/%s:refs/%s",
113                                         item->string, item->string);
114                 else
115                         strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
116                                         item->string, name, item->string);
117                 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
118                         return 1;
119         }
120
121         if (mirror) {
122                 strbuf_reset(&buf);
123                 strbuf_addf(&buf, "remote.%s.mirror", name);
124                 if (git_config_set(buf.buf, "true"))
125                         return 1;
126         }
127
128         if (fetch && fetch_remote(name))
129                 return 1;
130
131         if (master) {
132                 strbuf_reset(&buf);
133                 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
134
135                 strbuf_reset(&buf2);
136                 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
137
138                 if (create_symref(buf.buf, buf2.buf, "remote add"))
139                         return error("Could not setup master '%s'", master);
140         }
141
142         strbuf_release(&buf);
143         strbuf_release(&buf2);
144         string_list_clear(&track, 0);
145
146         return 0;
147 }
148
149 struct branch_info {
150         char *remote_name;
151         struct string_list merge;
152 };
153
154 static struct string_list branch_list;
155
156 static const char *abbrev_ref(const char *name, const char *prefix)
157 {
158         const char *abbrev = skip_prefix(name, prefix);
159         if (abbrev)
160                 return abbrev;
161         return name;
162 }
163 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
164
165 static int config_read_branches(const char *key, const char *value, void *cb)
166 {
167         if (!prefixcmp(key, "branch.")) {
168                 char *name;
169                 struct string_list_item *item;
170                 struct branch_info *info;
171                 enum { REMOTE, MERGE } type;
172
173                 key += 7;
174                 if (!postfixcmp(key, ".remote")) {
175                         name = xstrndup(key, strlen(key) - 7);
176                         type = REMOTE;
177                 } else if (!postfixcmp(key, ".merge")) {
178                         name = xstrndup(key, strlen(key) - 6);
179                         type = MERGE;
180                 } else
181                         return 0;
182
183                 item = string_list_insert(name, &branch_list);
184
185                 if (!item->util)
186                         item->util = xcalloc(sizeof(struct branch_info), 1);
187                 info = item->util;
188                 if (type == REMOTE) {
189                         if (info->remote_name)
190                                 warning("more than one branch.%s", key);
191                         info->remote_name = xstrdup(value);
192                 } else {
193                         char *space = strchr(value, ' ');
194                         value = abbrev_branch(value);
195                         while (space) {
196                                 char *merge;
197                                 merge = xstrndup(value, space - value);
198                                 string_list_append(merge, &info->merge);
199                                 value = abbrev_branch(space + 1);
200                                 space = strchr(value, ' ');
201                         }
202                         string_list_append(xstrdup(value), &info->merge);
203                 }
204         }
205         return 0;
206 }
207
208 static void read_branches(void)
209 {
210         if (branch_list.nr)
211                 return;
212         git_config(config_read_branches, NULL);
213 }
214
215 struct ref_states {
216         struct remote *remote;
217         struct string_list new, stale, tracked, heads;
218 };
219
220 static int handle_one_branch(const char *refname,
221         const unsigned char *sha1, int flags, void *cb_data)
222 {
223         struct ref_states *states = cb_data;
224         struct refspec refspec;
225
226         memset(&refspec, 0, sizeof(refspec));
227         refspec.dst = (char *)refname;
228         if (!remote_find_tracking(states->remote, &refspec)) {
229                 struct string_list_item *item;
230                 const char *name = abbrev_branch(refspec.src);
231                 /* symbolic refs pointing nowhere were handled already */
232                 if ((flags & REF_ISSYMREF) ||
233                     string_list_has_string(&states->tracked, name) ||
234                     string_list_has_string(&states->new, name))
235                         return 0;
236                 item = string_list_append(name, &states->stale);
237                 item->util = xstrdup(refname);
238         }
239         return 0;
240 }
241
242 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
243 {
244         struct ref *fetch_map = NULL, **tail = &fetch_map;
245         struct ref *ref;
246         int i;
247
248         for (i = 0; i < states->remote->fetch_refspec_nr; i++)
249                 if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1))
250                         die("Could not get fetch map for refspec %s",
251                                 states->remote->fetch_refspec[i]);
252
253         states->new.strdup_strings = states->tracked.strdup_strings = 1;
254         for (ref = fetch_map; ref; ref = ref->next) {
255                 unsigned char sha1[20];
256                 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
257                         string_list_append(abbrev_branch(ref->name), &states->new);
258                 else
259                         string_list_append(abbrev_branch(ref->name), &states->tracked);
260         }
261         free_refs(fetch_map);
262
263         sort_string_list(&states->new);
264         sort_string_list(&states->tracked);
265         for_each_ref(handle_one_branch, states);
266         sort_string_list(&states->stale);
267
268         return 0;
269 }
270
271 static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
272 {
273         struct ref *ref, *matches;
274         struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
275         struct refspec refspec;
276
277         refspec.force = 0;
278         refspec.pattern = 1;
279         refspec.src = refspec.dst = "refs/heads/";
280         states->heads.strdup_strings = 1;
281         get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
282         matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
283                                     fetch_map, 1);
284         for(ref = matches; ref; ref = ref->next)
285                 string_list_append(abbrev_branch(ref->name), &states->heads);
286
287         free_refs(fetch_map);
288         free_refs(matches);
289
290         return 0;
291 }
292
293 struct known_remote {
294         struct known_remote *next;
295         struct remote *remote;
296 };
297
298 struct known_remotes {
299         struct remote *to_delete;
300         struct known_remote *list;
301 };
302
303 static int add_known_remote(struct remote *remote, void *cb_data)
304 {
305         struct known_remotes *all = cb_data;
306         struct known_remote *r;
307
308         if (!strcmp(all->to_delete->name, remote->name))
309                 return 0;
310
311         r = xmalloc(sizeof(*r));
312         r->remote = remote;
313         r->next = all->list;
314         all->list = r;
315         return 0;
316 }
317
318 struct branches_for_remote {
319         struct remote *remote;
320         struct string_list *branches, *skipped;
321         struct known_remotes *keep;
322 };
323
324 static int add_branch_for_removal(const char *refname,
325         const unsigned char *sha1, int flags, void *cb_data)
326 {
327         struct branches_for_remote *branches = cb_data;
328         struct refspec refspec;
329         struct string_list_item *item;
330         struct known_remote *kr;
331
332         memset(&refspec, 0, sizeof(refspec));
333         refspec.dst = (char *)refname;
334         if (remote_find_tracking(branches->remote, &refspec))
335                 return 0;
336
337         /* don't delete a branch if another remote also uses it */
338         for (kr = branches->keep->list; kr; kr = kr->next) {
339                 memset(&refspec, 0, sizeof(refspec));
340                 refspec.dst = (char *)refname;
341                 if (!remote_find_tracking(kr->remote, &refspec))
342                         return 0;
343         }
344
345         /* don't delete non-remote refs */
346         if (prefixcmp(refname, "refs/remotes")) {
347                 /* advise user how to delete local branches */
348                 if (!prefixcmp(refname, "refs/heads/"))
349                         string_list_append(abbrev_branch(refname),
350                                            branches->skipped);
351                 /* silently skip over other non-remote refs */
352                 return 0;
353         }
354
355         /* make sure that symrefs are deleted */
356         if (flags & REF_ISSYMREF)
357                 return unlink(git_path("%s", refname));
358
359         item = string_list_append(refname, branches->branches);
360         item->util = xmalloc(20);
361         hashcpy(item->util, sha1);
362
363         return 0;
364 }
365
366 struct rename_info {
367         const char *old;
368         const char *new;
369         struct string_list *remote_branches;
370 };
371
372 static int read_remote_branches(const char *refname,
373         const unsigned char *sha1, int flags, void *cb_data)
374 {
375         struct rename_info *rename = cb_data;
376         struct strbuf buf = STRBUF_INIT;
377         struct string_list_item *item;
378         int flag;
379         unsigned char orig_sha1[20];
380         const char *symref;
381
382         strbuf_addf(&buf, "refs/remotes/%s", rename->old);
383         if(!prefixcmp(refname, buf.buf)) {
384                 item = string_list_append(xstrdup(refname), rename->remote_branches);
385                 symref = resolve_ref(refname, orig_sha1, 1, &flag);
386                 if (flag & REF_ISSYMREF)
387                         item->util = xstrdup(symref);
388                 else
389                         item->util = NULL;
390         }
391
392         return 0;
393 }
394
395 static int migrate_file(struct remote *remote)
396 {
397         struct strbuf buf = STRBUF_INIT;
398         int i;
399         char *path = NULL;
400
401         strbuf_addf(&buf, "remote.%s.url", remote->name);
402         for (i = 0; i < remote->url_nr; i++)
403                 if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0))
404                         return error("Could not append '%s' to '%s'",
405                                         remote->url[i], buf.buf);
406         strbuf_reset(&buf);
407         strbuf_addf(&buf, "remote.%s.push", remote->name);
408         for (i = 0; i < remote->push_refspec_nr; i++)
409                 if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0))
410                         return error("Could not append '%s' to '%s'",
411                                         remote->push_refspec[i], buf.buf);
412         strbuf_reset(&buf);
413         strbuf_addf(&buf, "remote.%s.fetch", remote->name);
414         for (i = 0; i < remote->fetch_refspec_nr; i++)
415                 if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0))
416                         return error("Could not append '%s' to '%s'",
417                                         remote->fetch_refspec[i], buf.buf);
418         if (remote->origin == REMOTE_REMOTES)
419                 path = git_path("remotes/%s", remote->name);
420         else if (remote->origin == REMOTE_BRANCHES)
421                 path = git_path("branches/%s", remote->name);
422         if (path && unlink(path))
423                 warning("failed to remove '%s'", path);
424         return 0;
425 }
426
427 static int mv(int argc, const char **argv)
428 {
429         struct option options[] = {
430                 OPT_END()
431         };
432         struct remote *oldremote, *newremote;
433         struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT;
434         struct string_list remote_branches = { NULL, 0, 0, 0 };
435         struct rename_info rename;
436         int i;
437
438         if (argc != 3)
439                 usage_with_options(builtin_remote_usage, options);
440
441         rename.old = argv[1];
442         rename.new = argv[2];
443         rename.remote_branches = &remote_branches;
444
445         oldremote = remote_get(rename.old);
446         if (!oldremote)
447                 die("No such remote: %s", rename.old);
448
449         if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
450                 return migrate_file(oldremote);
451
452         newremote = remote_get(rename.new);
453         if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr))
454                 die("remote %s already exists.", rename.new);
455
456         strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new);
457         if (!valid_fetch_refspec(buf.buf))
458                 die("'%s' is not a valid remote name", rename.new);
459
460         strbuf_reset(&buf);
461         strbuf_addf(&buf, "remote.%s", rename.old);
462         strbuf_addf(&buf2, "remote.%s", rename.new);
463         if (git_config_rename_section(buf.buf, buf2.buf) < 1)
464                 return error("Could not rename config section '%s' to '%s'",
465                                 buf.buf, buf2.buf);
466
467         strbuf_reset(&buf);
468         strbuf_addf(&buf, "remote.%s.fetch", rename.new);
469         if (git_config_set_multivar(buf.buf, NULL, NULL, 1))
470                 return error("Could not remove config section '%s'", buf.buf);
471         for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
472                 char *ptr;
473
474                 strbuf_reset(&buf2);
475                 strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
476                 ptr = strstr(buf2.buf, rename.old);
477                 if (ptr)
478                         strbuf_splice(&buf2, ptr-buf2.buf, strlen(rename.old),
479                                         rename.new, strlen(rename.new));
480                 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
481                         return error("Could not append '%s'", buf.buf);
482         }
483
484         read_branches();
485         for (i = 0; i < branch_list.nr; i++) {
486                 struct string_list_item *item = branch_list.items + i;
487                 struct branch_info *info = item->util;
488                 if (info->remote_name && !strcmp(info->remote_name, rename.old)) {
489                         strbuf_reset(&buf);
490                         strbuf_addf(&buf, "branch.%s.remote", item->string);
491                         if (git_config_set(buf.buf, rename.new)) {
492                                 return error("Could not set '%s'", buf.buf);
493                         }
494                 }
495         }
496
497         /*
498          * First remove symrefs, then rename the rest, finally create
499          * the new symrefs.
500          */
501         for_each_ref(read_remote_branches, &rename);
502         for (i = 0; i < remote_branches.nr; i++) {
503                 struct string_list_item *item = remote_branches.items + i;
504                 int flag = 0;
505                 unsigned char sha1[20];
506                 const char *symref;
507
508                 symref = resolve_ref(item->string, sha1, 1, &flag);
509                 if (!(flag & REF_ISSYMREF))
510                         continue;
511                 if (delete_ref(item->string, NULL, REF_NODEREF))
512                         die("deleting '%s' failed", item->string);
513         }
514         for (i = 0; i < remote_branches.nr; i++) {
515                 struct string_list_item *item = remote_branches.items + i;
516
517                 if (item->util)
518                         continue;
519                 strbuf_reset(&buf);
520                 strbuf_addstr(&buf, item->string);
521                 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
522                                 rename.new, strlen(rename.new));
523                 strbuf_reset(&buf2);
524                 strbuf_addf(&buf2, "remote: renamed %s to %s",
525                                 item->string, buf.buf);
526                 if (rename_ref(item->string, buf.buf, buf2.buf))
527                         die("renaming '%s' failed", item->string);
528         }
529         for (i = 0; i < remote_branches.nr; i++) {
530                 struct string_list_item *item = remote_branches.items + i;
531
532                 if (!item->util)
533                         continue;
534                 strbuf_reset(&buf);
535                 strbuf_addstr(&buf, item->string);
536                 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
537                                 rename.new, strlen(rename.new));
538                 strbuf_reset(&buf2);
539                 strbuf_addstr(&buf2, item->util);
540                 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old),
541                                 rename.new, strlen(rename.new));
542                 strbuf_reset(&buf3);
543                 strbuf_addf(&buf3, "remote: renamed %s to %s",
544                                 item->string, buf.buf);
545                 if (create_symref(buf.buf, buf2.buf, buf3.buf))
546                         die("creating '%s' failed", buf.buf);
547         }
548         return 0;
549 }
550
551 static int remove_branches(struct string_list *branches)
552 {
553         int i, result = 0;
554         for (i = 0; i < branches->nr; i++) {
555                 struct string_list_item *item = branches->items + i;
556                 const char *refname = item->string;
557                 unsigned char *sha1 = item->util;
558
559                 if (delete_ref(refname, sha1, 0))
560                         result |= error("Could not remove branch %s", refname);
561         }
562         return result;
563 }
564
565 static int rm(int argc, const char **argv)
566 {
567         struct option options[] = {
568                 OPT_END()
569         };
570         struct remote *remote;
571         struct strbuf buf = STRBUF_INIT;
572         struct known_remotes known_remotes = { NULL, NULL };
573         struct string_list branches = { NULL, 0, 0, 1 };
574         struct string_list skipped = { NULL, 0, 0, 1 };
575         struct branches_for_remote cb_data = {
576                 NULL, &branches, &skipped, &known_remotes
577         };
578         int i, result;
579
580         if (argc != 2)
581                 usage_with_options(builtin_remote_usage, options);
582
583         remote = remote_get(argv[1]);
584         if (!remote)
585                 die("No such remote: %s", argv[1]);
586
587         known_remotes.to_delete = remote;
588         for_each_remote(add_known_remote, &known_remotes);
589
590         strbuf_addf(&buf, "remote.%s", remote->name);
591         if (git_config_rename_section(buf.buf, NULL) < 1)
592                 return error("Could not remove config section '%s'", buf.buf);
593
594         read_branches();
595         for (i = 0; i < branch_list.nr; i++) {
596                 struct string_list_item *item = branch_list.items + i;
597                 struct branch_info *info = item->util;
598                 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
599                         const char *keys[] = { "remote", "merge", NULL }, **k;
600                         for (k = keys; *k; k++) {
601                                 strbuf_reset(&buf);
602                                 strbuf_addf(&buf, "branch.%s.%s",
603                                                 item->string, *k);
604                                 if (git_config_set(buf.buf, NULL)) {
605                                         strbuf_release(&buf);
606                                         return -1;
607                                 }
608                         }
609                 }
610         }
611
612         /*
613          * We cannot just pass a function to for_each_ref() which deletes
614          * the branches one by one, since for_each_ref() relies on cached
615          * refs, which are invalidated when deleting a branch.
616          */
617         cb_data.remote = remote;
618         result = for_each_ref(add_branch_for_removal, &cb_data);
619         strbuf_release(&buf);
620
621         if (!result)
622                 result = remove_branches(&branches);
623         string_list_clear(&branches, 1);
624
625         if (skipped.nr) {
626                 fprintf(stderr, skipped.nr == 1 ?
627                         "Note: A non-remote branch was not removed; "
628                         "to delete it, use:\n" :
629                         "Note: Non-remote branches were not removed; "
630                         "to delete them, use:\n");
631                 for (i = 0; i < skipped.nr; i++)
632                         fprintf(stderr, "  git branch -d %s\n",
633                                 skipped.items[i].string);
634         }
635         string_list_clear(&skipped, 0);
636
637         return result;
638 }
639
640 static void show_list(const char *title, struct string_list *list,
641                       const char *extra_arg)
642 {
643         int i;
644
645         if (!list->nr)
646                 return;
647
648         printf(title, list->nr > 1 ? "es" : "", extra_arg);
649         printf("\n");
650         for (i = 0; i < list->nr; i++)
651                 printf("    %s\n", list->items[i].string);
652 }
653
654 static void free_remote_ref_states(struct ref_states *states)
655 {
656         string_list_clear(&states->new, 0);
657         string_list_clear(&states->stale, 0);
658         string_list_clear(&states->tracked, 0);
659         string_list_clear(&states->heads, 0);
660 }
661
662 static int append_ref_to_tracked_list(const char *refname,
663         const unsigned char *sha1, int flags, void *cb_data)
664 {
665         struct ref_states *states = cb_data;
666         struct refspec refspec;
667
668         if (flags & REF_ISSYMREF)
669                 return 0;
670
671         memset(&refspec, 0, sizeof(refspec));
672         refspec.dst = (char *)refname;
673         if (!remote_find_tracking(states->remote, &refspec))
674                 string_list_append(abbrev_branch(refspec.src), &states->tracked);
675
676         return 0;
677 }
678
679 static int get_remote_ref_states(const char *name,
680                                  struct ref_states *states,
681                                  int query)
682 {
683         struct transport *transport;
684         const struct ref *remote_refs;
685
686         states->remote = remote_get(name);
687         if (!states->remote)
688                 return error("No such remote: %s", name);
689
690         read_branches();
691
692         if (query) {
693                 transport = transport_get(NULL, states->remote->url_nr > 0 ?
694                         states->remote->url[0] : NULL);
695                 remote_refs = transport_get_remote_refs(transport);
696                 transport_disconnect(transport);
697
698                 if (query & GET_REF_STATES)
699                         get_ref_states(remote_refs, states);
700                 if (query & GET_HEAD_NAMES)
701                         get_head_names(remote_refs, states);
702         } else {
703                 for_each_ref(append_ref_to_tracked_list, states);
704                 sort_string_list(&states->tracked);
705         }
706
707         return 0;
708 }
709
710 static int show(int argc, const char **argv)
711 {
712         int no_query = 0, result = 0, query_flag = 0;
713         struct option options[] = {
714                 OPT_GROUP("show specific options"),
715                 OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
716                 OPT_END()
717         };
718         struct ref_states states;
719
720         argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
721
722         if (argc < 1)
723                 return show_all();
724
725         if (!no_query)
726                 query_flag = (GET_REF_STATES | GET_HEAD_NAMES);
727
728         memset(&states, 0, sizeof(states));
729         for (; argc; argc--, argv++) {
730                 int i;
731
732                 get_remote_ref_states(*argv, &states, query_flag);
733
734                 printf("* remote %s\n  URL: %s\n", *argv,
735                         states.remote->url_nr > 0 ?
736                                 states.remote->url[0] : "(no URL)");
737                 if (no_query)
738                         printf("  HEAD branch: (not queried)\n");
739                 else if (!states.heads.nr)
740                         printf("  HEAD branch: (unknown)\n");
741                 else if (states.heads.nr == 1)
742                         printf("  HEAD branch: %s\n", states.heads.items[0].string);
743                 else {
744                         printf("  HEAD branch (remote HEAD is ambiguous,"
745                                " may be one of the following):\n");
746                         for (i = 0; i < states.heads.nr; i++)
747                                 printf("    %s\n", states.heads.items[i].string);
748                 }
749
750                 for (i = 0; i < branch_list.nr; i++) {
751                         struct string_list_item *branch = branch_list.items + i;
752                         struct branch_info *info = branch->util;
753                         int j;
754
755                         if (!info->merge.nr || strcmp(*argv, info->remote_name))
756                                 continue;
757                         printf("  Remote branch%s merged with 'git pull' "
758                                 "while on branch %s\n   ",
759                                 info->merge.nr > 1 ? "es" : "",
760                                 branch->string);
761                         for (j = 0; j < info->merge.nr; j++)
762                                 printf(" %s", info->merge.items[j].string);
763                         printf("\n");
764                 }
765
766                 if (!no_query) {
767                         show_list("  New remote branch%s (next fetch "
768                                 "will store in remotes/%s)",
769                                 &states.new, states.remote->name);
770                         show_list("  Stale tracking branch%s (use 'git remote "
771                                 "prune')", &states.stale, "");
772                 }
773
774                 show_list("  Tracked remote branch%s", &states.tracked, "");
775
776                 if (states.remote->push_refspec_nr) {
777                         printf("  Local branch%s pushed with 'git push'\n",
778                                 states.remote->push_refspec_nr > 1 ?
779                                         "es" : "");
780                         for (i = 0; i < states.remote->push_refspec_nr; i++) {
781                                 struct refspec *spec = states.remote->push + i;
782                                 printf("    %s%s%s%s\n",
783                                        spec->force ? "+" : "",
784                                        abbrev_branch(spec->src),
785                                        spec->dst ? ":" : "",
786                                        spec->dst ? abbrev_branch(spec->dst) : "");
787                         }
788                 }
789
790                 free_remote_ref_states(&states);
791         }
792
793         return result;
794 }
795
796 static int set_head(int argc, const char **argv)
797 {
798         int i, opt_a = 0, opt_d = 0, result = 0;
799         struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
800         char *head_name = NULL;
801
802         struct option options[] = {
803                 OPT_GROUP("set-head specific options"),
804                 OPT_BOOLEAN('a', "auto", &opt_a,
805                             "set refs/remotes/<name>/HEAD according to remote"),
806                 OPT_BOOLEAN('d', "delete", &opt_d,
807                             "delete refs/remotes/<name>/HEAD"),
808                 OPT_END()
809         };
810         argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
811         if (argc)
812                 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
813
814         if (!opt_a && !opt_d && argc == 2) {
815                 head_name = xstrdup(argv[1]);
816         } else if (opt_a && !opt_d && argc == 1) {
817                 struct ref_states states;
818                 memset(&states, 0, sizeof(states));
819                 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
820                 if (!states.heads.nr)
821                         result |= error("Cannot determine remote HEAD");
822                 else if (states.heads.nr > 1) {
823                         result |= error("Multiple remote HEAD branches. "
824                                         "Please choose one explicitly with:");
825                         for (i = 0; i < states.heads.nr; i++)
826                                 fprintf(stderr, "  git remote set-head %s %s\n",
827                                         argv[0], states.heads.items[i].string);
828                 } else
829                         head_name = xstrdup(states.heads.items[0].string);
830                 free_remote_ref_states(&states);
831         } else if (opt_d && !opt_a && argc == 1) {
832                 if (delete_ref(buf.buf, NULL, REF_NODEREF))
833                         result |= error("Could not delete %s", buf.buf);
834         } else
835                 usage_with_options(builtin_remote_usage, options);
836
837         if (head_name) {
838                 unsigned char sha1[20];
839                 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
840                 /* make sure it's valid */
841                 if (!resolve_ref(buf2.buf, sha1, 1, NULL))
842                         result |= error("Not a valid ref: %s", buf2.buf);
843                 else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
844                         result |= error("Could not setup %s", buf.buf);
845                 if (opt_a)
846                         printf("%s/HEAD set to %s\n", argv[0], head_name);
847                 free(head_name);
848         }
849
850         strbuf_release(&buf);
851         strbuf_release(&buf2);
852         return result;
853 }
854
855 static int prune(int argc, const char **argv)
856 {
857         int dry_run = 0, result = 0;
858         struct option options[] = {
859                 OPT_GROUP("prune specific options"),
860                 OPT__DRY_RUN(&dry_run),
861                 OPT_END()
862         };
863         struct ref_states states;
864         const char *dangling_msg;
865
866         argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
867
868         if (argc < 1)
869                 usage_with_options(builtin_remote_usage, options);
870
871         dangling_msg = (dry_run
872                         ? " %s will become dangling!\n"
873                         : " %s has become dangling!\n");
874
875         memset(&states, 0, sizeof(states));
876         for (; argc; argc--, argv++) {
877                 int i;
878
879                 get_remote_ref_states(*argv, &states, GET_REF_STATES);
880
881                 if (states.stale.nr) {
882                         printf("Pruning %s\n", *argv);
883                         printf("URL: %s\n",
884                                states.remote->url_nr
885                                ? states.remote->url[0]
886                                : "(no URL)");
887                 }
888
889                 for (i = 0; i < states.stale.nr; i++) {
890                         const char *refname = states.stale.items[i].util;
891
892                         if (!dry_run)
893                                 result |= delete_ref(refname, NULL, 0);
894
895                         printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
896                                abbrev_ref(refname, "refs/remotes/"));
897                         warn_dangling_symref(dangling_msg, refname);
898                 }
899
900                 free_remote_ref_states(&states);
901         }
902
903         return result;
904 }
905
906 static int get_one_remote_for_update(struct remote *remote, void *priv)
907 {
908         struct string_list *list = priv;
909         if (!remote->skip_default_update)
910                 string_list_append(remote->name, list);
911         return 0;
912 }
913
914 struct remote_group {
915         const char *name;
916         struct string_list *list;
917 } remote_group;
918
919 static int get_remote_group(const char *key, const char *value, void *cb)
920 {
921         if (!prefixcmp(key, "remotes.") &&
922                         !strcmp(key + 8, remote_group.name)) {
923                 /* split list by white space */
924                 int space = strcspn(value, " \t\n");
925                 while (*value) {
926                         if (space > 1)
927                                 string_list_append(xstrndup(value, space),
928                                                 remote_group.list);
929                         value += space + (value[space] != '\0');
930                         space = strcspn(value, " \t\n");
931                 }
932         }
933
934         return 0;
935 }
936
937 static int update(int argc, const char **argv)
938 {
939         int i, result = 0;
940         struct string_list list = { NULL, 0, 0, 0 };
941         static const char *default_argv[] = { NULL, "default", NULL };
942
943         if (argc < 2) {
944                 argc = 2;
945                 argv = default_argv;
946         }
947
948         remote_group.list = &list;
949         for (i = 1; i < argc; i++) {
950                 remote_group.name = argv[i];
951                 result = git_config(get_remote_group, NULL);
952         }
953
954         if (!result && !list.nr  && argc == 2 && !strcmp(argv[1], "default"))
955                 result = for_each_remote(get_one_remote_for_update, &list);
956
957         for (i = 0; i < list.nr; i++)
958                 result |= fetch_remote(list.items[i].string);
959
960         /* all names were strdup()ed or strndup()ed */
961         list.strdup_strings = 1;
962         string_list_clear(&list, 0);
963
964         return result;
965 }
966
967 static int get_one_entry(struct remote *remote, void *priv)
968 {
969         struct string_list *list = priv;
970
971         if (remote->url_nr > 0) {
972                 int i;
973
974                 for (i = 0; i < remote->url_nr; i++)
975                         string_list_append(remote->name, list)->util = (void *)remote->url[i];
976         } else
977                 string_list_append(remote->name, list)->util = NULL;
978
979         return 0;
980 }
981
982 static int show_all(void)
983 {
984         struct string_list list = { NULL, 0, 0 };
985         int result = for_each_remote(get_one_entry, &list);
986
987         if (!result) {
988                 int i;
989
990                 sort_string_list(&list);
991                 for (i = 0; i < list.nr; i++) {
992                         struct string_list_item *item = list.items + i;
993                         if (verbose)
994                                 printf("%s\t%s\n", item->string,
995                                         item->util ? (const char *)item->util : "");
996                         else {
997                                 if (i && !strcmp((item - 1)->string, item->string))
998                                         continue;
999                                 printf("%s\n", item->string);
1000                         }
1001                 }
1002         }
1003         return result;
1004 }
1005
1006 int cmd_remote(int argc, const char **argv, const char *prefix)
1007 {
1008         struct option options[] = {
1009                 OPT__VERBOSE(&verbose),
1010                 OPT_END()
1011         };
1012         int result;
1013
1014         argc = parse_options(argc, argv, options, builtin_remote_usage,
1015                 PARSE_OPT_STOP_AT_NON_OPTION);
1016
1017         if (argc < 1)
1018                 result = show_all();
1019         else if (!strcmp(argv[0], "add"))
1020                 result = add(argc, argv);
1021         else if (!strcmp(argv[0], "rename"))
1022                 result = mv(argc, argv);
1023         else if (!strcmp(argv[0], "rm"))
1024                 result = rm(argc, argv);
1025         else if (!strcmp(argv[0], "set-head"))
1026                 result = set_head(argc, argv);
1027         else if (!strcmp(argv[0], "show"))
1028                 result = show(argc, argv);
1029         else if (!strcmp(argv[0], "prune"))
1030                 result = prune(argc, argv);
1031         else if (!strcmp(argv[0], "update"))
1032                 result = update(argc, argv);
1033         else {
1034                 error("Unknown subcommand: %s", argv[0]);
1035                 usage_with_options(builtin_remote_usage, options);
1036         }
1037
1038         return result ? 1 : 0;
1039 }