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