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