remote: make guess_remote_head() use exact HEAD lookup if it is available
[git/git.git] / builtin-remote.c
CommitLineData
211c8968
JS
1#include "cache.h"
2#include "parse-options.h"
3#include "transport.h"
4#include "remote.h"
c455c87c 5#include "string-list.h"
211c8968
JS
6#include "strbuf.h"
7#include "run-command.h"
8#include "refs.h"
9
10static const char * const builtin_remote_usage[] = {
357af14f
CR
11 "git remote [-v | --verbose]",
12 "git remote add [-t <branch>] [-m <master>] [-f] [--mirror] <name> <url>",
bf98421a 13 "git remote rename <old> <new>",
211c8968 14 "git remote rm <name>",
bc14fac8 15 "git remote set-head <name> [-a | -d | <branch>]",
357af14f
CR
16 "git remote show [-n] <name>",
17 "git remote prune [-n | --dry-run] <name>",
dbbd56f1 18 "git remote [-v | --verbose] update [group]",
211c8968
JS
19 NULL
20};
21
e61e0cc6
JS
22#define GET_REF_STATES (1<<0)
23#define GET_HEAD_NAMES (1<<1)
24
211c8968
JS
25static int verbose;
26
c4112bb6
JK
27static int show_all(void);
28
211c8968
JS
29static 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
211c8968
JS
37static int opt_parse_track(const struct option *opt, const char *arg, int not)
38{
c455c87c 39 struct string_list *list = opt->value;
211c8968 40 if (not)
c455c87c 41 string_list_clear(list, 0);
211c8968 42 else
c455c87c 43 string_list_append(arg, list);
211c8968
JS
44 return 0;
45}
46
47static int fetch_remote(const char *name)
48{
dbbd56f1
CR
49 const char *argv[] = { "fetch", name, NULL, NULL };
50 if (verbose) {
51 argv[1] = "-v";
52 argv[2] = name;
53 }
3000658f 54 printf("Updating %s\n", name);
211c8968
JS
55 if (run_command_v_opt(argv, RUN_GIT_CMD))
56 return error("Could not fetch %s", name);
57 return 0;
58}
59
60static int add(int argc, const char **argv)
61{
62 int fetch = 0, mirror = 0;
c455c87c 63 struct string_list track = { NULL, 0, 0 };
211c8968
JS
64 const char *master = NULL;
65 struct remote *remote;
f285a2d7 66 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
211c8968
JS
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
24b6177e
JF
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
211c8968
JS
97 strbuf_addf(&buf, "remote.%s.url", name);
98 if (git_config_set(buf.buf, url))
99 return 1;
100
24b6177e
JF
101 strbuf_reset(&buf);
102 strbuf_addf(&buf, "remote.%s.fetch", name);
103
211c8968 104 if (track.nr == 0)
c455c87c 105 string_list_append("*", &track);
211c8968 106 for (i = 0; i < track.nr; i++) {
c455c87c 107 struct string_list_item *item = track.items + i;
211c8968 108
211c8968 109 strbuf_reset(&buf2);
1ce89cc4 110 strbuf_addch(&buf2, '+');
211c8968
JS
111 if (mirror)
112 strbuf_addf(&buf2, "refs/%s:refs/%s",
c455c87c 113 item->string, item->string);
211c8968
JS
114 else
115 strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
c455c87c 116 item->string, name, item->string);
211c8968
JS
117 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
118 return 1;
119 }
120
84bb2dfd
PB
121 if (mirror) {
122 strbuf_reset(&buf);
123 strbuf_addf(&buf, "remote.%s.mirror", name);
bc699afc 124 if (git_config_set(buf.buf, "true"))
84bb2dfd
PB
125 return 1;
126 }
127
211c8968
JS
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);
c455c87c 144 string_list_clear(&track, 0);
211c8968
JS
145
146 return 0;
147}
148
149struct branch_info {
e0cc81e6 150 char *remote_name;
c455c87c 151 struct string_list merge;
211c8968
JS
152};
153
c455c87c 154static struct string_list branch_list;
211c8968 155
72972eb3
JH
156static 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
ef90d6d4 165static int config_read_branches(const char *key, const char *value, void *cb)
211c8968
JS
166{
167 if (!prefixcmp(key, "branch.")) {
168 char *name;
c455c87c 169 struct string_list_item *item;
211c8968
JS
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
c455c87c 183 item = string_list_insert(name, &branch_list);
211c8968
JS
184
185 if (!item->util)
186 item->util = xcalloc(sizeof(struct branch_info), 1);
187 info = item->util;
188 if (type == REMOTE) {
e0cc81e6 189 if (info->remote_name)
211c8968 190 warning("more than one branch.%s", key);
e0cc81e6 191 info->remote_name = xstrdup(value);
211c8968
JS
192 } else {
193 char *space = strchr(value, ' ');
72972eb3 194 value = abbrev_branch(value);
211c8968
JS
195 while (space) {
196 char *merge;
197 merge = xstrndup(value, space - value);
c455c87c 198 string_list_append(merge, &info->merge);
72972eb3 199 value = abbrev_branch(space + 1);
211c8968
JS
200 space = strchr(value, ' ');
201 }
c455c87c 202 string_list_append(xstrdup(value), &info->merge);
211c8968
JS
203 }
204 }
205 return 0;
206}
207
208static void read_branches(void)
209{
210 if (branch_list.nr)
211 return;
ef90d6d4 212 git_config(config_read_branches, NULL);
211c8968
JS
213}
214
215struct ref_states {
216 struct remote *remote;
e61e0cc6 217 struct string_list new, stale, tracked, heads;
211c8968
JS
218};
219
220static 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)) {
c455c87c 229 struct string_list_item *item;
72972eb3 230 const char *name = abbrev_branch(refspec.src);
740fdd27
JS
231 /* symbolic refs pointing nowhere were handled already */
232 if ((flags & REF_ISSYMREF) ||
3bd92563
JS
233 string_list_has_string(&states->tracked, name) ||
234 string_list_has_string(&states->new, name))
211c8968 235 return 0;
c455c87c 236 item = string_list_append(name, &states->stale);
211c8968
JS
237 item->util = xstrdup(refname);
238 }
239 return 0;
240}
241
e0cc81e6 242static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
211c8968
JS
243{
244 struct ref *fetch_map = NULL, **tail = &fetch_map;
e0cc81e6 245 struct ref *ref;
211c8968
JS
246 int i;
247
248 for (i = 0; i < states->remote->fetch_refspec_nr; i++)
e0cc81e6 249 if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1))
211c8968
JS
250 die("Could not get fetch map for refspec %s",
251 states->remote->fetch_refspec[i]);
252
c455c87c 253 states->new.strdup_strings = states->tracked.strdup_strings = 1;
211c8968 254 for (ref = fetch_map; ref; ref = ref->next) {
211c8968 255 unsigned char sha1[20];
211c8968 256 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
7b9a5e27
JS
257 string_list_append(abbrev_branch(ref->name), &states->new);
258 else
259 string_list_append(abbrev_branch(ref->name), &states->tracked);
211c8968
JS
260 }
261 free_refs(fetch_map);
262
3bd92563
JS
263 sort_string_list(&states->new);
264 sort_string_list(&states->tracked);
211c8968 265 for_each_ref(handle_one_branch, states);
c455c87c 266 sort_string_list(&states->stale);
211c8968
JS
267
268 return 0;
269}
270
e61e0cc6
JS
271static 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
7ad2458f
SP
293struct known_remote {
294 struct known_remote *next;
295 struct remote *remote;
296};
297
298struct known_remotes {
299 struct remote *to_delete;
300 struct known_remote *list;
301};
302
303static 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
211c8968 318struct branches_for_remote {
7ad2458f 319 struct remote *remote;
441adf0c 320 struct string_list *branches, *skipped;
7ad2458f 321 struct known_remotes *keep;
211c8968
JS
322};
323
324static 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;
7ad2458f 328 struct refspec refspec;
c455c87c 329 struct string_list_item *item;
7ad2458f 330 struct known_remote *kr;
211c8968 331
7ad2458f
SP
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 }
3b9dcff5 344
441adf0c
JS
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
7ad2458f
SP
355 /* make sure that symrefs are deleted */
356 if (flags & REF_ISSYMREF)
9db56f71 357 return unlink(git_path("%s", refname));
3b9dcff5 358
c455c87c 359 item = string_list_append(refname, branches->branches);
7ad2458f
SP
360 item->util = xmalloc(20);
361 hashcpy(item->util, sha1);
211c8968
JS
362
363 return 0;
364}
365
bf98421a
MV
366struct rename_info {
367 const char *old;
368 const char *new;
369 struct string_list *remote_branches;
370};
371
372static 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
1dd1239a
MV
395static 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
bf98421a
MV
427static 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
1dd1239a
MV
449 if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
450 return migrate_file(oldremote);
451
bf98421a
MV
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;
e0cc81e6 488 if (info->remote_name && !strcmp(info->remote_name, rename.old)) {
bf98421a
MV
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
c455c87c 551static int remove_branches(struct string_list *branches)
211c8968
JS
552{
553 int i, result = 0;
554 for (i = 0; i < branches->nr; i++) {
c455c87c
JS
555 struct string_list_item *item = branches->items + i;
556 const char *refname = item->string;
211c8968
JS
557 unsigned char *sha1 = item->util;
558
eca35a25 559 if (delete_ref(refname, sha1, 0))
211c8968
JS
560 result |= error("Could not remove branch %s", refname);
561 }
562 return result;
563}
564
565static int rm(int argc, const char **argv)
566{
567 struct option options[] = {
568 OPT_END()
569 };
570 struct remote *remote;
f285a2d7 571 struct strbuf buf = STRBUF_INIT;
7ad2458f 572 struct known_remotes known_remotes = { NULL, NULL };
c455c87c 573 struct string_list branches = { NULL, 0, 0, 1 };
441adf0c
JS
574 struct string_list skipped = { NULL, 0, 0, 1 };
575 struct branches_for_remote cb_data = {
576 NULL, &branches, &skipped, &known_remotes
577 };
e02f1762 578 int i, result;
211c8968
JS
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
7ad2458f
SP
587 known_remotes.to_delete = remote;
588 for_each_remote(add_known_remote, &known_remotes);
589
211c8968
JS
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++) {
c455c87c 596 struct string_list_item *item = branch_list.items + i;
211c8968 597 struct branch_info *info = item->util;
e0cc81e6 598 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
211c8968
JS
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",
c455c87c 603 item->string, *k);
211c8968
JS
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 */
7ad2458f 617 cb_data.remote = remote;
e02f1762 618 result = for_each_ref(add_branch_for_removal, &cb_data);
211c8968
JS
619 strbuf_release(&buf);
620
e02f1762
JS
621 if (!result)
622 result = remove_branches(&branches);
c455c87c 623 string_list_clear(&branches, 1);
211c8968 624
441adf0c
JS
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
e02f1762 637 return result;
211c8968
JS
638}
639
79bbc7fb
JS
640static void show_list(const char *title, struct string_list *list,
641 const char *extra_arg)
211c8968
JS
642{
643 int i;
644
645 if (!list->nr)
646 return;
647
79bbc7fb 648 printf(title, list->nr > 1 ? "es" : "", extra_arg);
211c8968 649 printf("\n");
20244ea2
JS
650 for (i = 0; i < list->nr; i++)
651 printf(" %s\n", list->items[i].string);
211c8968
JS
652}
653
88733235
JS
654static 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);
e61e0cc6 659 string_list_clear(&states->heads, 0);
88733235
JS
660}
661
cca7c97e
JS
662static 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
3bd92563
JS
668 if (flags & REF_ISSYMREF)
669 return 0;
670
cca7c97e
JS
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
67a7e2d0
OM
679static int get_remote_ref_states(const char *name,
680 struct ref_states *states,
681 int query)
682{
683 struct transport *transport;
e0cc81e6 684 const struct ref *remote_refs;
67a7e2d0
OM
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);
e0cc81e6 695 remote_refs = transport_get_remote_refs(transport);
67a7e2d0
OM
696 transport_disconnect(transport);
697
e61e0cc6
JS
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);
3bd92563 702 } else {
cca7c97e 703 for_each_ref(append_ref_to_tracked_list, states);
3bd92563
JS
704 sort_string_list(&states->tracked);
705 }
e7d5a97d
OM
706
707 return 0;
708}
709
67a7e2d0 710static int show(int argc, const char **argv)
211c8968 711{
e61e0cc6 712 int no_query = 0, result = 0, query_flag = 0;
211c8968
JS
713 struct option options[] = {
714 OPT_GROUP("show specific options"),
0ecfcb3b 715 OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
211c8968
JS
716 OPT_END()
717 };
718 struct ref_states states;
719
720 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
721
67a7e2d0
OM
722 if (argc < 1)
723 return show_all();
211c8968 724
e61e0cc6
JS
725 if (!no_query)
726 query_flag = (GET_REF_STATES | GET_HEAD_NAMES);
727
211c8968
JS
728 memset(&states, 0, sizeof(states));
729 for (; argc; argc--, argv++) {
0ecfcb3b 730 int i;
211c8968 731
e61e0cc6 732 get_remote_ref_states(*argv, &states, query_flag);
211c8968
JS
733
734 printf("* remote %s\n URL: %s\n", *argv,
735 states.remote->url_nr > 0 ?
736 states.remote->url[0] : "(no URL)");
e61e0cc6
JS
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 }
211c8968
JS
749
750 for (i = 0; i < branch_list.nr; i++) {
c455c87c 751 struct string_list_item *branch = branch_list.items + i;
211c8968
JS
752 struct branch_info *info = branch->util;
753 int j;
754
e0cc81e6 755 if (!info->merge.nr || strcmp(*argv, info->remote_name))
211c8968
JS
756 continue;
757 printf(" Remote branch%s merged with 'git pull' "
758 "while on branch %s\n ",
759 info->merge.nr > 1 ? "es" : "",
c455c87c 760 branch->string);
211c8968 761 for (j = 0; j < info->merge.nr; j++)
c455c87c 762 printf(" %s", info->merge.items[j].string);
211c8968
JS
763 printf("\n");
764 }
765
0ecfcb3b 766 if (!no_query) {
79bbc7fb
JS
767 show_list(" New remote branch%s (next fetch "
768 "will store in remotes/%s)",
769 &states.new, states.remote->name);
0ecfcb3b 770 show_list(" Stale tracking branch%s (use 'git remote "
79bbc7fb 771 "prune')", &states.stale, "");
0ecfcb3b 772 }
211c8968 773
79bbc7fb 774 show_list(" Tracked remote branch%s", &states.tracked, "");
e7d5a97d 775
211c8968 776 if (states.remote->push_refspec_nr) {
20244ea2 777 printf(" Local branch%s pushed with 'git push'\n",
211c8968
JS
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;
20244ea2
JS
782 printf(" %s%s%s%s\n",
783 spec->force ? "+" : "",
72972eb3
JH
784 abbrev_branch(spec->src),
785 spec->dst ? ":" : "",
786 spec->dst ? abbrev_branch(spec->dst) : "");
211c8968
JS
787 }
788 }
67a7e2d0 789
88733235 790 free_remote_ref_states(&states);
67a7e2d0
OM
791 }
792
793 return result;
794}
795
bc14fac8
JS
796static 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
67a7e2d0
OM
855static int prune(int argc, const char **argv)
856{
8d767927 857 int dry_run = 0, result = 0;
67a7e2d0
OM
858 struct option options[] = {
859 OPT_GROUP("prune specific options"),
8d767927 860 OPT__DRY_RUN(&dry_run),
67a7e2d0
OM
861 OPT_END()
862 };
863 struct ref_states states;
f8948e2f 864 const char *dangling_msg;
67a7e2d0
OM
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
f8948e2f
JH
871 dangling_msg = (dry_run
872 ? " %s will become dangling!\n"
873 : " %s has become dangling!\n");
874
67a7e2d0
OM
875 memset(&states, 0, sizeof(states));
876 for (; argc; argc--, argv++) {
877 int i;
878
e61e0cc6 879 get_remote_ref_states(*argv, &states, GET_REF_STATES);
8d767927 880
8b7d4e73
JH
881 if (states.stale.nr) {
882 printf("Pruning %s\n", *argv);
8d767927
OM
883 printf("URL: %s\n",
884 states.remote->url_nr
885 ? states.remote->url[0]
886 : "(no URL)");
8b7d4e73 887 }
67a7e2d0
OM
888
889 for (i = 0; i < states.stale.nr; i++) {
890 const char *refname = states.stale.items[i].util;
8d767927
OM
891
892 if (!dry_run)
eca35a25 893 result |= delete_ref(refname, NULL, 0);
8d767927
OM
894
895 printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
72972eb3 896 abbrev_ref(refname, "refs/remotes/"));
f8948e2f 897 warn_dangling_symref(dangling_msg, refname);
67a7e2d0
OM
898 }
899
88733235 900 free_remote_ref_states(&states);
211c8968
JS
901 }
902
903 return result;
904}
905
84521ed6 906static int get_one_remote_for_update(struct remote *remote, void *priv)
211c8968 907{
c455c87c 908 struct string_list *list = priv;
211c8968 909 if (!remote->skip_default_update)
83b76736 910 string_list_append(remote->name, list);
84521ed6
JS
911 return 0;
912}
913
914struct remote_group {
915 const char *name;
c455c87c 916 struct string_list *list;
84521ed6
JS
917} remote_group;
918
ef90d6d4 919static int get_remote_group(const char *key, const char *value, void *cb)
84521ed6
JS
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)
c455c87c 927 string_list_append(xstrndup(value, space),
84521ed6
JS
928 remote_group.list);
929 value += space + (value[space] != '\0');
930 space = strcspn(value, " \t\n");
931 }
932 }
933
211c8968
JS
934 return 0;
935}
936
937static int update(int argc, const char **argv)
938{
84521ed6 939 int i, result = 0;
c455c87c 940 struct string_list list = { NULL, 0, 0, 0 };
84521ed6 941 static const char *default_argv[] = { NULL, "default", NULL };
211c8968 942
84521ed6
JS
943 if (argc < 2) {
944 argc = 2;
945 argv = default_argv;
946 }
211c8968 947
84521ed6
JS
948 remote_group.list = &list;
949 for (i = 1; i < argc; i++) {
950 remote_group.name = argv[i];
ef90d6d4 951 result = git_config(get_remote_group, NULL);
84521ed6
JS
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++)
c455c87c 958 result |= fetch_remote(list.items[i].string);
84521ed6
JS
959
960 /* all names were strdup()ed or strndup()ed */
c455c87c
JS
961 list.strdup_strings = 1;
962 string_list_clear(&list, 0);
84521ed6
JS
963
964 return result;
211c8968
JS
965}
966
967static int get_one_entry(struct remote *remote, void *priv)
968{
c455c87c 969 struct string_list *list = priv;
211c8968 970
7d20e218
MG
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;
211c8968
JS
978
979 return 0;
980}
981
982static int show_all(void)
983{
c455c87c 984 struct string_list list = { NULL, 0, 0 };
211c8968
JS
985 int result = for_each_remote(get_one_entry, &list);
986
987 if (!result) {
988 int i;
989
c455c87c 990 sort_string_list(&list);
211c8968 991 for (i = 0; i < list.nr; i++) {
c455c87c 992 struct string_list_item *item = list.items + i;
7d20e218
MG
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 }
211c8968
JS
1001 }
1002 }
1003 return result;
1004}
1005
1006int 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);
bf98421a
MV
1021 else if (!strcmp(argv[0], "rename"))
1022 result = mv(argc, argv);
211c8968
JS
1023 else if (!strcmp(argv[0], "rm"))
1024 result = rm(argc, argv);
bc14fac8
JS
1025 else if (!strcmp(argv[0], "set-head"))
1026 result = set_head(argc, argv);
211c8968 1027 else if (!strcmp(argv[0], "show"))
67a7e2d0 1028 result = show(argc, argv);
211c8968 1029 else if (!strcmp(argv[0], "prune"))
67a7e2d0 1030 result = prune(argc, argv);
211c8968
JS
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}