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