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