1/*
2 * "git fetch"
3 */
4#include "cache.h"
5#include "refs.h"
6#include "commit.h"
7#include "builtin.h"
8#include "string-list.h"
9#include "remote.h"
10#include "transport.h"
11#include "run-command.h"
12#include "parse-options.h"
13#include "sigchain.h"
14#include "submodule-config.h"
15#include "submodule.h"
16#include "connected.h"
17#include "argv-array.h"
18
19static const char * const builtin_fetch_usage[] = {
20 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
21 N_("git fetch [<options>] <group>"),
22 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
23 N_("git fetch --all [<options>]"),
24 NULL
25};
26
27enum {
28 TAGS_UNSET = 0,
29 TAGS_DEFAULT = 1,
30 TAGS_SET = 2
31};
32
33static int fetch_prune_config = -1; /* unspecified */
34static int prune = -1; /* unspecified */
35#define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
36
37static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity;
38static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
39static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen;
40static int max_children = 1;
41static const char *depth;
42static const char *deepen_since;
43static const char *upload_pack;
44static struct strbuf default_rla = STRBUF_INIT;
45static struct transport *gtransport;
46static struct transport *gsecondary;
47static const char *submodule_prefix = "";
48static const char *recurse_submodules_default;
49static int shown_url = 0;
50static int refmap_alloc, refmap_nr;
51static const char **refmap_array;
52
53static int option_parse_recurse_submodules(const struct option *opt,
54 const char *arg, int unset)
55{
56 if (unset) {
57 recurse_submodules = RECURSE_SUBMODULES_OFF;
58 } else {
59 if (arg)
60 recurse_submodules = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
61 else
62 recurse_submodules = RECURSE_SUBMODULES_ON;
63 }
64 return 0;
65}
66
67static int git_fetch_config(const char *k, const char *v, void *cb)
68{
69 if (!strcmp(k, "fetch.prune")) {
70 fetch_prune_config = git_config_bool(k, v);
71 return 0;
72 }
73 return git_default_config(k, v, cb);
74}
75
76static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
77{
78 ALLOC_GROW(refmap_array, refmap_nr + 1, refmap_alloc);
79
80 /*
81 * "git fetch --refmap='' origin foo"
82 * can be used to tell the command not to store anywhere
83 */
84 if (*arg)
85 refmap_array[refmap_nr++] = arg;
86 return 0;
87}
88
89static struct option builtin_fetch_options[] = {
90 OPT__VERBOSITY(&verbosity),
91 OPT_BOOL(0, "all", &all,
92 N_("fetch from all remotes")),
93 OPT_BOOL('a', "append", &append,
94 N_("append to .git/FETCH_HEAD instead of overwriting")),
95 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
96 N_("path to upload pack on remote end")),
97 OPT__FORCE(&force, N_("force overwrite of local branch")),
98 OPT_BOOL('m', "multiple", &multiple,
99 N_("fetch from multiple remotes")),
100 OPT_SET_INT('t', "tags", &tags,
101 N_("fetch all tags and associated objects"), TAGS_SET),
102 OPT_SET_INT('n', NULL, &tags,
103 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
104 OPT_INTEGER('j', "jobs", &max_children,
105 N_("number of submodules fetched in parallel")),
106 OPT_BOOL('p', "prune", &prune,
107 N_("prune remote-tracking branches no longer on remote")),
108 { OPTION_CALLBACK, 0, "recurse-submodules", NULL, N_("on-demand"),
109 N_("control recursive fetching of submodules"),
110 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
111 OPT_BOOL(0, "dry-run", &dry_run,
112 N_("dry run")),
113 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
114 OPT_BOOL('u', "update-head-ok", &update_head_ok,
115 N_("allow updating of HEAD ref")),
116 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
117 OPT_STRING(0, "depth", &depth, N_("depth"),
118 N_("deepen history of shallow clone")),
119 OPT_STRING(0, "shallow-since", &deepen_since, N_("time"),
120 N_("deepen history of shallow repository based on time")),
121 { OPTION_SET_INT, 0, "unshallow", &unshallow, NULL,
122 N_("convert to a complete repository"),
123 PARSE_OPT_NONEG | PARSE_OPT_NOARG, NULL, 1 },
124 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
125 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
126 { OPTION_STRING, 0, "recurse-submodules-default",
127 &recurse_submodules_default, NULL,
128 N_("default mode for recursion"), PARSE_OPT_HIDDEN },
129 OPT_BOOL(0, "update-shallow", &update_shallow,
130 N_("accept refs that update .git/shallow")),
131 { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"),
132 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg },
133 OPT_END()
134};
135
136static void unlock_pack(void)
137{
138 if (gtransport)
139 transport_unlock_pack(gtransport);
140 if (gsecondary)
141 transport_unlock_pack(gsecondary);
142}
143
144static void unlock_pack_on_signal(int signo)
145{
146 unlock_pack();
147 sigchain_pop(signo);
148 raise(signo);
149}
150
151static void add_merge_config(struct ref **head,
152 const struct ref *remote_refs,
153 struct branch *branch,
154 struct ref ***tail)
155{
156 int i;
157
158 for (i = 0; i < branch->merge_nr; i++) {
159 struct ref *rm, **old_tail = *tail;
160 struct refspec refspec;
161
162 for (rm = *head; rm; rm = rm->next) {
163 if (branch_merge_matches(branch, i, rm->name)) {
164 rm->fetch_head_status = FETCH_HEAD_MERGE;
165 break;
166 }
167 }
168 if (rm)
169 continue;
170
171 /*
172 * Not fetched to a remote-tracking branch? We need to fetch
173 * it anyway to allow this branch's "branch.$name.merge"
174 * to be honored by 'git pull', but we do not have to
175 * fail if branch.$name.merge is misconfigured to point
176 * at a nonexisting branch. If we were indeed called by
177 * 'git pull', it will notice the misconfiguration because
178 * there is no entry in the resulting FETCH_HEAD marked
179 * for merging.
180 */
181 memset(&refspec, 0, sizeof(refspec));
182 refspec.src = branch->merge[i]->src;
183 get_fetch_map(remote_refs, &refspec, tail, 1);
184 for (rm = *old_tail; rm; rm = rm->next)
185 rm->fetch_head_status = FETCH_HEAD_MERGE;
186 }
187}
188
189static int add_existing(const char *refname, const struct object_id *oid,
190 int flag, void *cbdata)
191{
192 struct string_list *list = (struct string_list *)cbdata;
193 struct string_list_item *item = string_list_insert(list, refname);
194 struct object_id *old_oid = xmalloc(sizeof(*old_oid));
195
196 oidcpy(old_oid, oid);
197 item->util = old_oid;
198 return 0;
199}
200
201static int will_fetch(struct ref **head, const unsigned char *sha1)
202{
203 struct ref *rm = *head;
204 while (rm) {
205 if (!hashcmp(rm->old_oid.hash, sha1))
206 return 1;
207 rm = rm->next;
208 }
209 return 0;
210}
211
212static void find_non_local_tags(struct transport *transport,
213 struct ref **head,
214 struct ref ***tail)
215{
216 struct string_list existing_refs = STRING_LIST_INIT_DUP;
217 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
218 const struct ref *ref;
219 struct string_list_item *item = NULL;
220
221 for_each_ref(add_existing, &existing_refs);
222 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
223 if (!starts_with(ref->name, "refs/tags/"))
224 continue;
225
226 /*
227 * The peeled ref always follows the matching base
228 * ref, so if we see a peeled ref that we don't want
229 * to fetch then we can mark the ref entry in the list
230 * as one to ignore by setting util to NULL.
231 */
232 if (ends_with(ref->name, "^{}")) {
233 if (item && !has_object_file(&ref->old_oid) &&
234 !will_fetch(head, ref->old_oid.hash) &&
235 !has_sha1_file(item->util) &&
236 !will_fetch(head, item->util))
237 item->util = NULL;
238 item = NULL;
239 continue;
240 }
241
242 /*
243 * If item is non-NULL here, then we previously saw a
244 * ref not followed by a peeled reference, so we need
245 * to check if it is a lightweight tag that we want to
246 * fetch.
247 */
248 if (item && !has_sha1_file(item->util) &&
249 !will_fetch(head, item->util))
250 item->util = NULL;
251
252 item = NULL;
253
254 /* skip duplicates and refs that we already have */
255 if (string_list_has_string(&remote_refs, ref->name) ||
256 string_list_has_string(&existing_refs, ref->name))
257 continue;
258
259 item = string_list_insert(&remote_refs, ref->name);
260 item->util = (void *)&ref->old_oid;
261 }
262 string_list_clear(&existing_refs, 1);
263
264 /*
265 * We may have a final lightweight tag that needs to be
266 * checked to see if it needs fetching.
267 */
268 if (item && !has_sha1_file(item->util) &&
269 !will_fetch(head, item->util))
270 item->util = NULL;
271
272 /*
273 * For all the tags in the remote_refs string list,
274 * add them to the list of refs to be fetched
275 */
276 for_each_string_list_item(item, &remote_refs) {
277 /* Unless we have already decided to ignore this item... */
278 if (item->util)
279 {
280 struct ref *rm = alloc_ref(item->string);
281 rm->peer_ref = alloc_ref(item->string);
282 oidcpy(&rm->old_oid, item->util);
283 **tail = rm;
284 *tail = &rm->next;
285 }
286 }
287
288 string_list_clear(&remote_refs, 0);
289}
290
291static struct ref *get_ref_map(struct transport *transport,
292 struct refspec *refspecs, int refspec_count,
293 int tags, int *autotags)
294{
295 int i;
296 struct ref *rm;
297 struct ref *ref_map = NULL;
298 struct ref **tail = &ref_map;
299
300 /* opportunistically-updated references: */
301 struct ref *orefs = NULL, **oref_tail = &orefs;
302
303 const struct ref *remote_refs = transport_get_remote_refs(transport);
304
305 if (refspec_count) {
306 struct refspec *fetch_refspec;
307 int fetch_refspec_nr;
308
309 for (i = 0; i < refspec_count; i++) {
310 get_fetch_map(remote_refs, &refspecs[i], &tail, 0);
311 if (refspecs[i].dst && refspecs[i].dst[0])
312 *autotags = 1;
313 }
314 /* Merge everything on the command line (but not --tags) */
315 for (rm = ref_map; rm; rm = rm->next)
316 rm->fetch_head_status = FETCH_HEAD_MERGE;
317
318 /*
319 * For any refs that we happen to be fetching via
320 * command-line arguments, the destination ref might
321 * have been missing or have been different than the
322 * remote-tracking ref that would be derived from the
323 * configured refspec. In these cases, we want to
324 * take the opportunity to update their configured
325 * remote-tracking reference. However, we do not want
326 * to mention these entries in FETCH_HEAD at all, as
327 * they would simply be duplicates of existing
328 * entries, so we set them FETCH_HEAD_IGNORE below.
329 *
330 * We compute these entries now, based only on the
331 * refspecs specified on the command line. But we add
332 * them to the list following the refspecs resulting
333 * from the tags option so that one of the latter,
334 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
335 * by ref_remove_duplicates() in favor of one of these
336 * opportunistic entries with FETCH_HEAD_IGNORE.
337 */
338 if (refmap_array) {
339 fetch_refspec = parse_fetch_refspec(refmap_nr, refmap_array);
340 fetch_refspec_nr = refmap_nr;
341 } else {
342 fetch_refspec = transport->remote->fetch;
343 fetch_refspec_nr = transport->remote->fetch_refspec_nr;
344 }
345
346 for (i = 0; i < fetch_refspec_nr; i++)
347 get_fetch_map(ref_map, &fetch_refspec[i], &oref_tail, 1);
348
349 if (tags == TAGS_SET)
350 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
351 } else if (refmap_array) {
352 die("--refmap option is only meaningful with command-line refspec(s).");
353 } else {
354 /* Use the defaults */
355 struct remote *remote = transport->remote;
356 struct branch *branch = branch_get(NULL);
357 int has_merge = branch_has_merge_config(branch);
358 if (remote &&
359 (remote->fetch_refspec_nr ||
360 /* Note: has_merge implies non-NULL branch->remote_name */
361 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
362 for (i = 0; i < remote->fetch_refspec_nr; i++) {
363 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
364 if (remote->fetch[i].dst &&
365 remote->fetch[i].dst[0])
366 *autotags = 1;
367 if (!i && !has_merge && ref_map &&
368 !remote->fetch[0].pattern)
369 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
370 }
371 /*
372 * if the remote we're fetching from is the same
373 * as given in branch.<name>.remote, we add the
374 * ref given in branch.<name>.merge, too.
375 *
376 * Note: has_merge implies non-NULL branch->remote_name
377 */
378 if (has_merge &&
379 !strcmp(branch->remote_name, remote->name))
380 add_merge_config(&ref_map, remote_refs, branch, &tail);
381 } else {
382 ref_map = get_remote_ref(remote_refs, "HEAD");
383 if (!ref_map)
384 die(_("Couldn't find remote ref HEAD"));
385 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
386 tail = &ref_map->next;
387 }
388 }
389
390 if (tags == TAGS_SET)
391 /* also fetch all tags */
392 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
393 else if (tags == TAGS_DEFAULT && *autotags)
394 find_non_local_tags(transport, &ref_map, &tail);
395
396 /* Now append any refs to be updated opportunistically: */
397 *tail = orefs;
398 for (rm = orefs; rm; rm = rm->next) {
399 rm->fetch_head_status = FETCH_HEAD_IGNORE;
400 tail = &rm->next;
401 }
402
403 return ref_remove_duplicates(ref_map);
404}
405
406#define STORE_REF_ERROR_OTHER 1
407#define STORE_REF_ERROR_DF_CONFLICT 2
408
409static int s_update_ref(const char *action,
410 struct ref *ref,
411 int check_old)
412{
413 char msg[1024];
414 char *rla = getenv("GIT_REFLOG_ACTION");
415 struct ref_transaction *transaction;
416 struct strbuf err = STRBUF_INIT;
417 int ret, df_conflict = 0;
418
419 if (dry_run)
420 return 0;
421 if (!rla)
422 rla = default_rla.buf;
423 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
424
425 transaction = ref_transaction_begin(&err);
426 if (!transaction ||
427 ref_transaction_update(transaction, ref->name,
428 ref->new_oid.hash,
429 check_old ? ref->old_oid.hash : NULL,
430 0, msg, &err))
431 goto fail;
432
433 ret = ref_transaction_commit(transaction, &err);
434 if (ret) {
435 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
436 goto fail;
437 }
438
439 ref_transaction_free(transaction);
440 strbuf_release(&err);
441 return 0;
442fail:
443 ref_transaction_free(transaction);
444 error("%s", err.buf);
445 strbuf_release(&err);
446 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
447 : STORE_REF_ERROR_OTHER;
448}
449
450#define REFCOL_WIDTH 10
451
452static int update_local_ref(struct ref *ref,
453 const char *remote,
454 const struct ref *remote_ref,
455 struct strbuf *display)
456{
457 struct commit *current = NULL, *updated;
458 enum object_type type;
459 struct branch *current_branch = branch_get(NULL);
460 const char *pretty_ref = prettify_refname(ref->name);
461
462 type = sha1_object_info(ref->new_oid.hash, NULL);
463 if (type < 0)
464 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
465
466 if (!oidcmp(&ref->old_oid, &ref->new_oid)) {
467 if (verbosity > 0)
468 strbuf_addf(display, "= %-*s %-*s -> %s",
469 TRANSPORT_SUMMARY(_("[up to date]")),
470 REFCOL_WIDTH, remote, pretty_ref);
471 return 0;
472 }
473
474 if (current_branch &&
475 !strcmp(ref->name, current_branch->name) &&
476 !(update_head_ok || is_bare_repository()) &&
477 !is_null_oid(&ref->old_oid)) {
478 /*
479 * If this is the head, and it's not okay to update
480 * the head, and the old value of the head isn't empty...
481 */
482 strbuf_addf(display,
483 _("! %-*s %-*s -> %s (can't fetch in current branch)"),
484 TRANSPORT_SUMMARY(_("[rejected]")),
485 REFCOL_WIDTH, remote, pretty_ref);
486 return 1;
487 }
488
489 if (!is_null_oid(&ref->old_oid) &&
490 starts_with(ref->name, "refs/tags/")) {
491 int r;
492 r = s_update_ref("updating tag", ref, 0);
493 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
494 r ? '!' : '-',
495 TRANSPORT_SUMMARY(_("[tag update]")),
496 REFCOL_WIDTH, remote, pretty_ref,
497 r ? _(" (unable to update local ref)") : "");
498 return r;
499 }
500
501 current = lookup_commit_reference_gently(ref->old_oid.hash, 1);
502 updated = lookup_commit_reference_gently(ref->new_oid.hash, 1);
503 if (!current || !updated) {
504 const char *msg;
505 const char *what;
506 int r;
507 /*
508 * Nicely describe the new ref we're fetching.
509 * Base this on the remote's ref name, as it's
510 * more likely to follow a standard layout.
511 */
512 const char *name = remote_ref ? remote_ref->name : "";
513 if (starts_with(name, "refs/tags/")) {
514 msg = "storing tag";
515 what = _("[new tag]");
516 } else if (starts_with(name, "refs/heads/")) {
517 msg = "storing head";
518 what = _("[new branch]");
519 } else {
520 msg = "storing ref";
521 what = _("[new ref]");
522 }
523
524 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
525 (recurse_submodules != RECURSE_SUBMODULES_ON))
526 check_for_new_submodule_commits(ref->new_oid.hash);
527 r = s_update_ref(msg, ref, 0);
528 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
529 r ? '!' : '*',
530 TRANSPORT_SUMMARY(what),
531 REFCOL_WIDTH, remote, pretty_ref,
532 r ? _(" (unable to update local ref)") : "");
533 return r;
534 }
535
536 if (in_merge_bases(current, updated)) {
537 struct strbuf quickref = STRBUF_INIT;
538 int r;
539 strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
540 strbuf_addstr(&quickref, "..");
541 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
542 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
543 (recurse_submodules != RECURSE_SUBMODULES_ON))
544 check_for_new_submodule_commits(ref->new_oid.hash);
545 r = s_update_ref("fast-forward", ref, 1);
546 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
547 r ? '!' : ' ',
548 TRANSPORT_SUMMARY_WIDTH, quickref.buf,
549 REFCOL_WIDTH, remote, pretty_ref,
550 r ? _(" (unable to update local ref)") : "");
551 strbuf_release(&quickref);
552 return r;
553 } else if (force || ref->force) {
554 struct strbuf quickref = STRBUF_INIT;
555 int r;
556 strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
557 strbuf_addstr(&quickref, "...");
558 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
559 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
560 (recurse_submodules != RECURSE_SUBMODULES_ON))
561 check_for_new_submodule_commits(ref->new_oid.hash);
562 r = s_update_ref("forced-update", ref, 1);
563 strbuf_addf(display, "%c %-*s %-*s -> %s (%s)",
564 r ? '!' : '+',
565 TRANSPORT_SUMMARY_WIDTH, quickref.buf,
566 REFCOL_WIDTH, remote, pretty_ref,
567 r ? _("unable to update local ref") : _("forced update"));
568 strbuf_release(&quickref);
569 return r;
570 } else {
571 strbuf_addf(display, "! %-*s %-*s -> %s %s",
572 TRANSPORT_SUMMARY(_("[rejected]")),
573 REFCOL_WIDTH, remote, pretty_ref,
574 _("(non-fast-forward)"));
575 return 1;
576 }
577}
578
579static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
580{
581 struct ref **rm = cb_data;
582 struct ref *ref = *rm;
583
584 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
585 ref = ref->next;
586 if (!ref)
587 return -1; /* end of the list */
588 *rm = ref->next;
589 hashcpy(sha1, ref->old_oid.hash);
590 return 0;
591}
592
593static int store_updated_refs(const char *raw_url, const char *remote_name,
594 struct ref *ref_map)
595{
596 FILE *fp;
597 struct commit *commit;
598 int url_len, i, rc = 0;
599 struct strbuf note = STRBUF_INIT;
600 const char *what, *kind;
601 struct ref *rm;
602 char *url;
603 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head();
604 int want_status;
605
606 fp = fopen(filename, "a");
607 if (!fp)
608 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
609
610 if (raw_url)
611 url = transport_anonymize_url(raw_url);
612 else
613 url = xstrdup("foreign");
614
615 rm = ref_map;
616 if (check_everything_connected(iterate_ref_map, 0, &rm)) {
617 rc = error(_("%s did not send all necessary objects\n"), url);
618 goto abort;
619 }
620
621 /*
622 * We do a pass for each fetch_head_status type in their enum order, so
623 * merged entries are written before not-for-merge. That lets readers
624 * use FETCH_HEAD as a refname to refer to the ref to be merged.
625 */
626 for (want_status = FETCH_HEAD_MERGE;
627 want_status <= FETCH_HEAD_IGNORE;
628 want_status++) {
629 for (rm = ref_map; rm; rm = rm->next) {
630 struct ref *ref = NULL;
631 const char *merge_status_marker = "";
632
633 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
634 if (want_status == FETCH_HEAD_MERGE)
635 warning(_("reject %s because shallow roots are not allowed to be updated"),
636 rm->peer_ref ? rm->peer_ref->name : rm->name);
637 continue;
638 }
639
640 commit = lookup_commit_reference_gently(rm->old_oid.hash, 1);
641 if (!commit)
642 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
643
644 if (rm->fetch_head_status != want_status)
645 continue;
646
647 if (rm->peer_ref) {
648 ref = alloc_ref(rm->peer_ref->name);
649 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
650 oidcpy(&ref->new_oid, &rm->old_oid);
651 ref->force = rm->peer_ref->force;
652 }
653
654
655 if (!strcmp(rm->name, "HEAD")) {
656 kind = "";
657 what = "";
658 }
659 else if (starts_with(rm->name, "refs/heads/")) {
660 kind = "branch";
661 what = rm->name + 11;
662 }
663 else if (starts_with(rm->name, "refs/tags/")) {
664 kind = "tag";
665 what = rm->name + 10;
666 }
667 else if (starts_with(rm->name, "refs/remotes/")) {
668 kind = "remote-tracking branch";
669 what = rm->name + 13;
670 }
671 else {
672 kind = "";
673 what = rm->name;
674 }
675
676 url_len = strlen(url);
677 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
678 ;
679 url_len = i + 1;
680 if (4 < i && !strncmp(".git", url + i - 3, 4))
681 url_len = i - 3;
682
683 strbuf_reset(¬e);
684 if (*what) {
685 if (*kind)
686 strbuf_addf(¬e, "%s ", kind);
687 strbuf_addf(¬e, "'%s' of ", what);
688 }
689 switch (rm->fetch_head_status) {
690 case FETCH_HEAD_NOT_FOR_MERGE:
691 merge_status_marker = "not-for-merge";
692 /* fall-through */
693 case FETCH_HEAD_MERGE:
694 fprintf(fp, "%s\t%s\t%s",
695 oid_to_hex(&rm->old_oid),
696 merge_status_marker,
697 note.buf);
698 for (i = 0; i < url_len; ++i)
699 if ('\n' == url[i])
700 fputs("\\n", fp);
701 else
702 fputc(url[i], fp);
703 fputc('\n', fp);
704 break;
705 default:
706 /* do not write anything to FETCH_HEAD */
707 break;
708 }
709
710 strbuf_reset(¬e);
711 if (ref) {
712 rc |= update_local_ref(ref, what, rm, ¬e);
713 free(ref);
714 } else
715 strbuf_addf(¬e, "* %-*s %-*s -> FETCH_HEAD",
716 TRANSPORT_SUMMARY_WIDTH,
717 *kind ? kind : "branch",
718 REFCOL_WIDTH,
719 *what ? what : "HEAD");
720 if (note.len) {
721 if (verbosity >= 0 && !shown_url) {
722 fprintf(stderr, _("From %.*s\n"),
723 url_len, url);
724 shown_url = 1;
725 }
726 if (verbosity >= 0)
727 fprintf(stderr, " %s\n", note.buf);
728 }
729 }
730 }
731
732 if (rc & STORE_REF_ERROR_DF_CONFLICT)
733 error(_("some local refs could not be updated; try running\n"
734 " 'git remote prune %s' to remove any old, conflicting "
735 "branches"), remote_name);
736
737 abort:
738 strbuf_release(¬e);
739 free(url);
740 fclose(fp);
741 return rc;
742}
743
744/*
745 * We would want to bypass the object transfer altogether if
746 * everything we are going to fetch already exists and is connected
747 * locally.
748 */
749static int quickfetch(struct ref *ref_map)
750{
751 struct ref *rm = ref_map;
752
753 /*
754 * If we are deepening a shallow clone we already have these
755 * objects reachable. Running rev-list here will return with
756 * a good (0) exit status and we'll bypass the fetch that we
757 * really need to perform. Claiming failure now will ensure
758 * we perform the network exchange to deepen our history.
759 */
760 if (deepen)
761 return -1;
762 return check_everything_connected(iterate_ref_map, 1, &rm);
763}
764
765static int fetch_refs(struct transport *transport, struct ref *ref_map)
766{
767 int ret = quickfetch(ref_map);
768 if (ret)
769 ret = transport_fetch_refs(transport, ref_map);
770 if (!ret)
771 ret |= store_updated_refs(transport->url,
772 transport->remote->name,
773 ref_map);
774 transport_unlock_pack(transport);
775 return ret;
776}
777
778static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
779 const char *raw_url)
780{
781 int url_len, i, result = 0;
782 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
783 char *url;
784 const char *dangling_msg = dry_run
785 ? _(" (%s will become dangling)")
786 : _(" (%s has become dangling)");
787
788 if (raw_url)
789 url = transport_anonymize_url(raw_url);
790 else
791 url = xstrdup("foreign");
792
793 url_len = strlen(url);
794 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
795 ;
796
797 url_len = i + 1;
798 if (4 < i && !strncmp(".git", url + i - 3, 4))
799 url_len = i - 3;
800
801 if (!dry_run) {
802 struct string_list refnames = STRING_LIST_INIT_NODUP;
803
804 for (ref = stale_refs; ref; ref = ref->next)
805 string_list_append(&refnames, ref->name);
806
807 result = delete_refs(&refnames);
808 string_list_clear(&refnames, 0);
809 }
810
811 if (verbosity >= 0) {
812 for (ref = stale_refs; ref; ref = ref->next) {
813 if (!shown_url) {
814 fprintf(stderr, _("From %.*s\n"), url_len, url);
815 shown_url = 1;
816 }
817 fprintf(stderr, " x %-*s %-*s -> %s\n",
818 TRANSPORT_SUMMARY(_("[deleted]")),
819 REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
820 warn_dangling_symref(stderr, dangling_msg, ref->name);
821 }
822 }
823
824 free(url);
825 free_refs(stale_refs);
826 return result;
827}
828
829static void check_not_current_branch(struct ref *ref_map)
830{
831 struct branch *current_branch = branch_get(NULL);
832
833 if (is_bare_repository() || !current_branch)
834 return;
835
836 for (; ref_map; ref_map = ref_map->next)
837 if (ref_map->peer_ref && !strcmp(current_branch->refname,
838 ref_map->peer_ref->name))
839 die(_("Refusing to fetch into current branch %s "
840 "of non-bare repository"), current_branch->refname);
841}
842
843static int truncate_fetch_head(void)
844{
845 const char *filename = git_path_fetch_head();
846 FILE *fp = fopen_for_writing(filename);
847
848 if (!fp)
849 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
850 fclose(fp);
851 return 0;
852}
853
854static void set_option(struct transport *transport, const char *name, const char *value)
855{
856 int r = transport_set_option(transport, name, value);
857 if (r < 0)
858 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
859 name, value, transport->url);
860 if (r > 0)
861 warning(_("Option \"%s\" is ignored for %s\n"),
862 name, transport->url);
863}
864
865static struct transport *prepare_transport(struct remote *remote, int deepen)
866{
867 struct transport *transport;
868 transport = transport_get(remote, NULL);
869 transport_set_verbosity(transport, verbosity, progress);
870 if (upload_pack)
871 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
872 if (keep)
873 set_option(transport, TRANS_OPT_KEEP, "yes");
874 if (depth)
875 set_option(transport, TRANS_OPT_DEPTH, depth);
876 if (deepen && deepen_since)
877 set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since);
878 if (update_shallow)
879 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
880 return transport;
881}
882
883static void backfill_tags(struct transport *transport, struct ref *ref_map)
884{
885 int cannot_reuse;
886
887 /*
888 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
889 * when remote helper is used (setting it to an empty string
890 * is not unsetting). We could extend the remote helper
891 * protocol for that, but for now, just force a new connection
892 * without deepen-since.
893 */
894 cannot_reuse = transport->cannot_reuse || deepen_since;
895 if (cannot_reuse) {
896 gsecondary = prepare_transport(transport->remote, 0);
897 transport = gsecondary;
898 }
899
900 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
901 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
902 fetch_refs(transport, ref_map);
903
904 if (gsecondary) {
905 transport_disconnect(gsecondary);
906 gsecondary = NULL;
907 }
908}
909
910static int do_fetch(struct transport *transport,
911 struct refspec *refs, int ref_count)
912{
913 struct string_list existing_refs = STRING_LIST_INIT_DUP;
914 struct ref *ref_map;
915 struct ref *rm;
916 int autotags = (transport->remote->fetch_tags == 1);
917 int retcode = 0;
918
919 for_each_ref(add_existing, &existing_refs);
920
921 if (tags == TAGS_DEFAULT) {
922 if (transport->remote->fetch_tags == 2)
923 tags = TAGS_SET;
924 if (transport->remote->fetch_tags == -1)
925 tags = TAGS_UNSET;
926 }
927
928 if (!transport->get_refs_list || !transport->fetch)
929 die(_("Don't know how to fetch from %s"), transport->url);
930
931 /* if not appending, truncate FETCH_HEAD */
932 if (!append && !dry_run) {
933 retcode = truncate_fetch_head();
934 if (retcode)
935 goto cleanup;
936 }
937
938 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
939 if (!update_head_ok)
940 check_not_current_branch(ref_map);
941
942 for (rm = ref_map; rm; rm = rm->next) {
943 if (rm->peer_ref) {
944 struct string_list_item *peer_item =
945 string_list_lookup(&existing_refs,
946 rm->peer_ref->name);
947 if (peer_item) {
948 struct object_id *old_oid = peer_item->util;
949 oidcpy(&rm->peer_ref->old_oid, old_oid);
950 }
951 }
952 }
953
954 if (tags == TAGS_DEFAULT && autotags)
955 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
956 if (prune) {
957 /*
958 * We only prune based on refspecs specified
959 * explicitly (via command line or configuration); we
960 * don't care whether --tags was specified.
961 */
962 if (ref_count) {
963 prune_refs(refs, ref_count, ref_map, transport->url);
964 } else {
965 prune_refs(transport->remote->fetch,
966 transport->remote->fetch_refspec_nr,
967 ref_map,
968 transport->url);
969 }
970 }
971 if (fetch_refs(transport, ref_map)) {
972 free_refs(ref_map);
973 retcode = 1;
974 goto cleanup;
975 }
976 free_refs(ref_map);
977
978 /* if neither --no-tags nor --tags was specified, do automated tag
979 * following ... */
980 if (tags == TAGS_DEFAULT && autotags) {
981 struct ref **tail = &ref_map;
982 ref_map = NULL;
983 find_non_local_tags(transport, &ref_map, &tail);
984 if (ref_map)
985 backfill_tags(transport, ref_map);
986 free_refs(ref_map);
987 }
988
989 cleanup:
990 string_list_clear(&existing_refs, 1);
991 return retcode;
992}
993
994static int get_one_remote_for_fetch(struct remote *remote, void *priv)
995{
996 struct string_list *list = priv;
997 if (!remote->skip_default_update)
998 string_list_append(list, remote->name);
999 return 0;
1000}
1001
1002struct remote_group_data {
1003 const char *name;
1004 struct string_list *list;
1005};
1006
1007static int get_remote_group(const char *key, const char *value, void *priv)
1008{
1009 struct remote_group_data *g = priv;
1010
1011 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
1012 /* split list by white space */
1013 while (*value) {
1014 size_t wordlen = strcspn(value, " \t\n");
1015
1016 if (wordlen >= 1)
1017 string_list_append(g->list,
1018 xstrndup(value, wordlen));
1019 value += wordlen + (value[wordlen] != '\0');
1020 }
1021 }
1022
1023 return 0;
1024}
1025
1026static int add_remote_or_group(const char *name, struct string_list *list)
1027{
1028 int prev_nr = list->nr;
1029 struct remote_group_data g;
1030 g.name = name; g.list = list;
1031
1032 git_config(get_remote_group, &g);
1033 if (list->nr == prev_nr) {
1034 struct remote *remote;
1035 if (!remote_is_configured(name))
1036 return 0;
1037 remote = remote_get(name);
1038 string_list_append(list, remote->name);
1039 }
1040 return 1;
1041}
1042
1043static void add_options_to_argv(struct argv_array *argv)
1044{
1045 if (dry_run)
1046 argv_array_push(argv, "--dry-run");
1047 if (prune != -1)
1048 argv_array_push(argv, prune ? "--prune" : "--no-prune");
1049 if (update_head_ok)
1050 argv_array_push(argv, "--update-head-ok");
1051 if (force)
1052 argv_array_push(argv, "--force");
1053 if (keep)
1054 argv_array_push(argv, "--keep");
1055 if (recurse_submodules == RECURSE_SUBMODULES_ON)
1056 argv_array_push(argv, "--recurse-submodules");
1057 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
1058 argv_array_push(argv, "--recurse-submodules=on-demand");
1059 if (tags == TAGS_SET)
1060 argv_array_push(argv, "--tags");
1061 else if (tags == TAGS_UNSET)
1062 argv_array_push(argv, "--no-tags");
1063 if (verbosity >= 2)
1064 argv_array_push(argv, "-v");
1065 if (verbosity >= 1)
1066 argv_array_push(argv, "-v");
1067 else if (verbosity < 0)
1068 argv_array_push(argv, "-q");
1069
1070}
1071
1072static int fetch_multiple(struct string_list *list)
1073{
1074 int i, result = 0;
1075 struct argv_array argv = ARGV_ARRAY_INIT;
1076
1077 if (!append && !dry_run) {
1078 int errcode = truncate_fetch_head();
1079 if (errcode)
1080 return errcode;
1081 }
1082
1083 argv_array_pushl(&argv, "fetch", "--append", NULL);
1084 add_options_to_argv(&argv);
1085
1086 for (i = 0; i < list->nr; i++) {
1087 const char *name = list->items[i].string;
1088 argv_array_push(&argv, name);
1089 if (verbosity >= 0)
1090 printf(_("Fetching %s\n"), name);
1091 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
1092 error(_("Could not fetch %s"), name);
1093 result = 1;
1094 }
1095 argv_array_pop(&argv);
1096 }
1097
1098 argv_array_clear(&argv);
1099 return result;
1100}
1101
1102static int fetch_one(struct remote *remote, int argc, const char **argv)
1103{
1104 static const char **refs = NULL;
1105 struct refspec *refspec;
1106 int ref_nr = 0;
1107 int exit_code;
1108
1109 if (!remote)
1110 die(_("No remote repository specified. Please, specify either a URL or a\n"
1111 "remote name from which new revisions should be fetched."));
1112
1113 gtransport = prepare_transport(remote, 1);
1114
1115 if (prune < 0) {
1116 /* no command line request */
1117 if (0 <= gtransport->remote->prune)
1118 prune = gtransport->remote->prune;
1119 else if (0 <= fetch_prune_config)
1120 prune = fetch_prune_config;
1121 else
1122 prune = PRUNE_BY_DEFAULT;
1123 }
1124
1125 if (argc > 0) {
1126 int j = 0;
1127 int i;
1128 refs = xcalloc(argc + 1, sizeof(const char *));
1129 for (i = 0; i < argc; i++) {
1130 if (!strcmp(argv[i], "tag")) {
1131 i++;
1132 if (i >= argc)
1133 die(_("You need to specify a tag name."));
1134 refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1135 argv[i], argv[i]);
1136 } else
1137 refs[j++] = argv[i];
1138 }
1139 refs[j] = NULL;
1140 ref_nr = j;
1141 }
1142
1143 sigchain_push_common(unlock_pack_on_signal);
1144 atexit(unlock_pack);
1145 refspec = parse_fetch_refspec(ref_nr, refs);
1146 exit_code = do_fetch(gtransport, refspec, ref_nr);
1147 free_refspec(ref_nr, refspec);
1148 transport_disconnect(gtransport);
1149 gtransport = NULL;
1150 return exit_code;
1151}
1152
1153int cmd_fetch(int argc, const char **argv, const char *prefix)
1154{
1155 int i;
1156 struct string_list list = STRING_LIST_INIT_NODUP;
1157 struct remote *remote;
1158 int result = 0;
1159 struct argv_array argv_gc_auto = ARGV_ARRAY_INIT;
1160
1161 packet_trace_identity("fetch");
1162
1163 /* Record the command line for the reflog */
1164 strbuf_addstr(&default_rla, "fetch");
1165 for (i = 1; i < argc; i++)
1166 strbuf_addf(&default_rla, " %s", argv[i]);
1167
1168 git_config(git_fetch_config, NULL);
1169
1170 argc = parse_options(argc, argv, prefix,
1171 builtin_fetch_options, builtin_fetch_usage, 0);
1172
1173 if (unshallow) {
1174 if (depth)
1175 die(_("--depth and --unshallow cannot be used together"));
1176 else if (!is_repository_shallow())
1177 die(_("--unshallow on a complete repository does not make sense"));
1178 else
1179 depth = xstrfmt("%d", INFINITE_DEPTH);
1180 }
1181
1182 /* no need to be strict, transport_set_option() will validate it again */
1183 if (depth && atoi(depth) < 1)
1184 die(_("depth %s is not a positive number"), depth);
1185 if (depth || deepen_since)
1186 deepen = 1;
1187
1188 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
1189 if (recurse_submodules_default) {
1190 int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default);
1191 set_config_fetch_recurse_submodules(arg);
1192 }
1193 gitmodules_config();
1194 git_config(submodule_config, NULL);
1195 }
1196
1197 if (all) {
1198 if (argc == 1)
1199 die(_("fetch --all does not take a repository argument"));
1200 else if (argc > 1)
1201 die(_("fetch --all does not make sense with refspecs"));
1202 (void) for_each_remote(get_one_remote_for_fetch, &list);
1203 result = fetch_multiple(&list);
1204 } else if (argc == 0) {
1205 /* No arguments -- use default remote */
1206 remote = remote_get(NULL);
1207 result = fetch_one(remote, argc, argv);
1208 } else if (multiple) {
1209 /* All arguments are assumed to be remotes or groups */
1210 for (i = 0; i < argc; i++)
1211 if (!add_remote_or_group(argv[i], &list))
1212 die(_("No such remote or remote group: %s"), argv[i]);
1213 result = fetch_multiple(&list);
1214 } else {
1215 /* Single remote or group */
1216 (void) add_remote_or_group(argv[0], &list);
1217 if (list.nr > 1) {
1218 /* More than one remote */
1219 if (argc > 1)
1220 die(_("Fetching a group and specifying refspecs does not make sense"));
1221 result = fetch_multiple(&list);
1222 } else {
1223 /* Zero or one remotes */
1224 remote = remote_get(argv[0]);
1225 result = fetch_one(remote, argc-1, argv+1);
1226 }
1227 }
1228
1229 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
1230 struct argv_array options = ARGV_ARRAY_INIT;
1231
1232 add_options_to_argv(&options);
1233 result = fetch_populated_submodules(&options,
1234 submodule_prefix,
1235 recurse_submodules,
1236 verbosity < 0,
1237 max_children);
1238 argv_array_clear(&options);
1239 }
1240
1241 /* All names were strdup()ed or strndup()ed */
1242 list.strdup_strings = 1;
1243 string_list_clear(&list, 0);
1244
1245 close_all_packs();
1246
1247 argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
1248 if (verbosity < 0)
1249 argv_array_push(&argv_gc_auto, "--quiet");
1250 run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
1251 argv_array_clear(&argv_gc_auto);
1252
1253 return result;
1254}