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