Merge branch 'maint'
[git/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"
f1863d0d 14#include "transport.h"
7dce19d3 15#include "submodule.h"
b888d61c 16
83201998 17static const char * const builtin_fetch_usage[] = {
e3163c75
TRC
18 "git fetch [<options>] [<repository> [<refspec>...]]",
19 "git fetch [<options>] <group>",
0adda936 20 "git fetch --multiple [<options>] [(<repository> | <group>)...]",
e3163c75 21 "git fetch --all [<options>]",
83201998
KH
22 NULL
23};
b888d61c 24
83201998
KH
25enum {
26 TAGS_UNSET = 0,
27 TAGS_DEFAULT = 1,
28 TAGS_SET = 2
29};
30
28a15401 31static int all, append, dry_run, force, keep, multiple, prune, update_head_ok, verbosity;
be254a0e 32static int progress, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
83201998 33static int tags = TAGS_DEFAULT;
4191c356 34static const char *depth;
83201998 35static const char *upload_pack;
2d324efa 36static struct strbuf default_rla = STRBUF_INIT;
e4022ed2 37static struct transport *transport;
7dce19d3 38static const char *submodule_prefix = "";
88a21979 39static const char *recurse_submodules_default;
e4022ed2 40
8f0700dd
JL
41static int option_parse_recurse_submodules(const struct option *opt,
42 const char *arg, int unset)
43{
44 if (unset) {
45 recurse_submodules = RECURSE_SUBMODULES_OFF;
46 } else {
47 if (arg)
48 recurse_submodules = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
49 else
50 recurse_submodules = RECURSE_SUBMODULES_ON;
51 }
52 return 0;
53}
e4022ed2 54
83201998 55static struct option builtin_fetch_options[] = {
7f87aff2 56 OPT__VERBOSITY(&verbosity),
9c4a036b
BG
57 OPT_BOOLEAN(0, "all", &all,
58 "fetch from all remotes"),
83201998
KH
59 OPT_BOOLEAN('a', "append", &append,
60 "append to .git/FETCH_HEAD instead of overwriting"),
23c6a803 61 OPT_STRING(0, "upload-pack", &upload_pack, "path",
83201998 62 "path to upload pack on remote end"),
76946b76 63 OPT__FORCE(&force, "force overwrite of local branch"),
16679e37
BG
64 OPT_BOOLEAN('m', "multiple", &multiple,
65 "fetch from multiple remotes"),
83201998
KH
66 OPT_SET_INT('t', "tags", &tags,
67 "fetch all tags and associated objects", TAGS_SET),
e7951290
JS
68 OPT_SET_INT('n', NULL, &tags,
69 "do not fetch all tags (--no-tags)", TAGS_UNSET),
f360d844 70 OPT_BOOLEAN('p', "prune", &prune,
8b3f3f84 71 "prune remote-tracking branches no longer on remote"),
8f0700dd 72 { OPTION_CALLBACK, 0, "recurse-submodules", NULL, "on-demand",
be254a0e 73 "control recursive fetching of submodules",
8f0700dd 74 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
28a15401
JS
75 OPT_BOOLEAN(0, "dry-run", &dry_run,
76 "dry run"),
83201998
KH
77 OPT_BOOLEAN('k', "keep", &keep, "keep downloaded pack"),
78 OPT_BOOLEAN('u', "update-head-ok", &update_head_ok,
79 "allow updating of HEAD ref"),
9839018e 80 OPT_BOOLEAN(0, "progress", &progress, "force progress reporting"),
23c6a803 81 OPT_STRING(0, "depth", &depth, "depth",
83201998 82 "deepen history of shallow clone"),
23c6a803 83 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, "dir",
7dce19d3 84 "prepend this to submodule path output", PARSE_OPT_HIDDEN },
88a21979
JL
85 { OPTION_STRING, 0, "recurse-submodules-default",
86 &recurse_submodules_default, NULL,
87 "default mode for recursion", PARSE_OPT_HIDDEN },
83201998
KH
88 OPT_END()
89};
90
e4022ed2
SP
91static void unlock_pack(void)
92{
93 if (transport)
94 transport_unlock_pack(transport);
95}
96
97static void unlock_pack_on_signal(int signo)
98{
99 unlock_pack();
4a16d072 100 sigchain_pop(signo);
e4022ed2
SP
101 raise(signo);
102}
b888d61c 103
85682c19 104static void add_merge_config(struct ref **head,
4577370e 105 const struct ref *remote_refs,
85682c19
SP
106 struct branch *branch,
107 struct ref ***tail)
b888d61c 108{
85682c19 109 int i;
b888d61c 110
85682c19
SP
111 for (i = 0; i < branch->merge_nr; i++) {
112 struct ref *rm, **old_tail = *tail;
113 struct refspec refspec;
114
115 for (rm = *head; rm; rm = rm->next) {
116 if (branch_merge_matches(branch, i, rm->name)) {
b888d61c 117 rm->merge = 1;
85682c19
SP
118 break;
119 }
b888d61c 120 }
85682c19
SP
121 if (rm)
122 continue;
123
9ad7c5ae 124 /*
8b3f3f84 125 * Not fetched to a remote-tracking branch? We need to fetch
85682c19 126 * it anyway to allow this branch's "branch.$name.merge"
05207a28 127 * to be honored by 'git pull', but we do not have to
9ad7c5ae
JH
128 * fail if branch.$name.merge is misconfigured to point
129 * at a nonexisting branch. If we were indeed called by
05207a28 130 * 'git pull', it will notice the misconfiguration because
9ad7c5ae
JH
131 * there is no entry in the resulting FETCH_HEAD marked
132 * for merging.
85682c19 133 */
8da61a2a 134 memset(&refspec, 0, sizeof(refspec));
85682c19 135 refspec.src = branch->merge[i]->src;
9ad7c5ae 136 get_fetch_map(remote_refs, &refspec, tail, 1);
85682c19
SP
137 for (rm = *old_tail; rm; rm = rm->next)
138 rm->merge = 1;
b888d61c
DB
139 }
140}
141
767f176a
SP
142static void find_non_local_tags(struct transport *transport,
143 struct ref **head,
144 struct ref ***tail);
145
b888d61c
DB
146static struct ref *get_ref_map(struct transport *transport,
147 struct refspec *refs, int ref_count, int tags,
148 int *autotags)
149{
150 int i;
151 struct ref *rm;
152 struct ref *ref_map = NULL;
153 struct ref **tail = &ref_map;
154
4577370e 155 const struct ref *remote_refs = transport_get_remote_refs(transport);
b888d61c 156
83201998 157 if (ref_count || tags == TAGS_SET) {
b888d61c 158 for (i = 0; i < ref_count; i++) {
9ad7c5ae 159 get_fetch_map(remote_refs, &refs[i], &tail, 0);
b888d61c
DB
160 if (refs[i].dst && refs[i].dst[0])
161 *autotags = 1;
162 }
163 /* Merge everything on the command line, but not --tags */
164 for (rm = ref_map; rm; rm = rm->next)
165 rm->merge = 1;
e0aaa29f
DB
166 if (tags == TAGS_SET)
167 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
b888d61c
DB
168 } else {
169 /* Use the defaults */
170 struct remote *remote = transport->remote;
85682c19
SP
171 struct branch *branch = branch_get(NULL);
172 int has_merge = branch_has_merge_config(branch);
3ee1757b
BC
173 if (remote &&
174 (remote->fetch_refspec_nr ||
f31dbdc7 175 /* Note: has_merge implies non-NULL branch->remote_name */
3ee1757b 176 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
b888d61c 177 for (i = 0; i < remote->fetch_refspec_nr; i++) {
9ad7c5ae 178 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
b888d61c
DB
179 if (remote->fetch[i].dst &&
180 remote->fetch[i].dst[0])
181 *autotags = 1;
85682c19 182 if (!i && !has_merge && ref_map &&
cfb8f898 183 !remote->fetch[0].pattern)
85682c19 184 ref_map->merge = 1;
b888d61c 185 }
da0204df
JS
186 /*
187 * if the remote we're fetching from is the same
188 * as given in branch.<name>.remote, we add the
189 * ref given in branch.<name>.merge, too.
f31dbdc7
BC
190 *
191 * Note: has_merge implies non-NULL branch->remote_name
da0204df 192 */
9ad7c5ae
JH
193 if (has_merge &&
194 !strcmp(branch->remote_name, remote->name))
85682c19 195 add_merge_config(&ref_map, remote_refs, branch, &tail);
b888d61c
DB
196 } else {
197 ref_map = get_remote_ref(remote_refs, "HEAD");
9ad7c5ae 198 if (!ref_map)
bd4a51fc 199 die(_("Couldn't find remote ref HEAD"));
b888d61c 200 ref_map->merge = 1;
5aaf7f2a 201 tail = &ref_map->next;
b888d61c
DB
202 }
203 }
767f176a
SP
204 if (tags == TAGS_DEFAULT && *autotags)
205 find_non_local_tags(transport, &ref_map, &tail);
2467a4fa 206 ref_remove_duplicates(ref_map);
b888d61c
DB
207
208 return ref_map;
209}
210
fa250759
JK
211#define STORE_REF_ERROR_OTHER 1
212#define STORE_REF_ERROR_DF_CONFLICT 2
213
b888d61c
DB
214static int s_update_ref(const char *action,
215 struct ref *ref,
216 int check_old)
217{
218 char msg[1024];
219 char *rla = getenv("GIT_REFLOG_ACTION");
220 static struct ref_lock *lock;
221
28a15401
JS
222 if (dry_run)
223 return 0;
b888d61c 224 if (!rla)
2d324efa 225 rla = default_rla.buf;
b888d61c
DB
226 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
227 lock = lock_any_ref_for_update(ref->name,
228 check_old ? ref->old_sha1 : NULL, 0);
229 if (!lock)
fa250759
JK
230 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
231 STORE_REF_ERROR_OTHER;
b888d61c 232 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
fa250759
JK
233 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
234 STORE_REF_ERROR_OTHER;
b888d61c
DB
235 return 0;
236}
237
9ef4272b 238#define REFCOL_WIDTH 10
165f3902 239
b888d61c 240static int update_local_ref(struct ref *ref,
165f3902 241 const char *remote,
165f3902 242 char *display)
b888d61c 243{
b888d61c
DB
244 struct commit *current = NULL, *updated;
245 enum object_type type;
246 struct branch *current_branch = branch_get(NULL);
4577e483 247 const char *pretty_ref = prettify_refname(ref->name);
b888d61c 248
165f3902 249 *display = 0;
b888d61c
DB
250 type = sha1_object_info(ref->new_sha1, NULL);
251 if (type < 0)
bd4a51fc 252 die(_("object %s not found"), sha1_to_hex(ref->new_sha1));
b888d61c 253
b888d61c 254 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
7f87aff2 255 if (verbosity > 0)
f1863d0d 256 sprintf(display, "= %-*s %-*s -> %s", TRANSPORT_SUMMARY_WIDTH,
502681cd 257 _("[up to date]"), REFCOL_WIDTH, remote,
9ef4272b 258 pretty_ref);
b888d61c
DB
259 return 0;
260 }
261
b3abdd9d
SP
262 if (current_branch &&
263 !strcmp(ref->name, current_branch->name) &&
b888d61c
DB
264 !(update_head_ok || is_bare_repository()) &&
265 !is_null_sha1(ref->old_sha1)) {
266 /*
267 * If this is the head, and it's not okay to update
268 * the head, and the old value of the head isn't empty...
269 */
502681cd
ÆAB
270 sprintf(display, _("! %-*s %-*s -> %s (can't fetch in current branch)"),
271 TRANSPORT_SUMMARY_WIDTH, _("[rejected]"), REFCOL_WIDTH, remote,
9ef4272b 272 pretty_ref);
b888d61c
DB
273 return 1;
274 }
275
276 if (!is_null_sha1(ref->old_sha1) &&
277 !prefixcmp(ref->name, "refs/tags/")) {
6315472e
JK
278 int r;
279 r = s_update_ref("updating tag", ref, 0);
280 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '-',
502681cd
ÆAB
281 TRANSPORT_SUMMARY_WIDTH, _("[tag update]"), REFCOL_WIDTH, remote,
282 pretty_ref, r ? _(" (unable to update local ref)") : "");
6315472e 283 return r;
b888d61c
DB
284 }
285
cfa5b2b7
SP
286 current = lookup_commit_reference_gently(ref->old_sha1, 1);
287 updated = lookup_commit_reference_gently(ref->new_sha1, 1);
b888d61c 288 if (!current || !updated) {
165f3902
NP
289 const char *msg;
290 const char *what;
6315472e 291 int r;
165f3902 292 if (!strncmp(ref->name, "refs/tags/", 10)) {
b888d61c 293 msg = "storing tag";
f7b3742a 294 what = _("[new tag]");
165f3902
NP
295 }
296 else {
b888d61c 297 msg = "storing head";
f7b3742a 298 what = _("[new branch]");
88a21979
JL
299 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
300 (recurse_submodules != RECURSE_SUBMODULES_ON))
301 check_for_new_submodule_commits(ref->new_sha1);
165f3902
NP
302 }
303
6315472e
JK
304 r = s_update_ref(msg, ref, 0);
305 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '*',
f1863d0d 306 TRANSPORT_SUMMARY_WIDTH, what, REFCOL_WIDTH, remote, pretty_ref,
f7b3742a 307 r ? _(" (unable to update local ref)") : "");
6315472e 308 return r;
b888d61c
DB
309 }
310
b888d61c 311 if (in_merge_bases(current, &updated, 1)) {
165f3902 312 char quickref[83];
6315472e 313 int r;
165f3902
NP
314 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
315 strcat(quickref, "..");
316 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
88a21979
JL
317 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
318 (recurse_submodules != RECURSE_SUBMODULES_ON))
319 check_for_new_submodule_commits(ref->new_sha1);
a75d7b54 320 r = s_update_ref("fast-forward", ref, 1);
6315472e 321 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : ' ',
f1863d0d 322 TRANSPORT_SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
f7b3742a 323 pretty_ref, r ? _(" (unable to update local ref)") : "");
6315472e 324 return r;
165f3902
NP
325 } else if (force || ref->force) {
326 char quickref[84];
6315472e 327 int r;
165f3902
NP
328 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
329 strcat(quickref, "...");
330 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
88a21979
JL
331 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
332 (recurse_submodules != RECURSE_SUBMODULES_ON))
333 check_for_new_submodule_commits(ref->new_sha1);
6315472e
JK
334 r = s_update_ref("forced-update", ref, 1);
335 sprintf(display, "%c %-*s %-*s -> %s (%s)", r ? '!' : '+',
f1863d0d 336 TRANSPORT_SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
6315472e 337 pretty_ref,
f7b3742a 338 r ? _("unable to update local ref") : _("forced update"));
6315472e 339 return r;
165f3902 340 } else {
321a153c
ÆAB
341 sprintf(display, "! %-*s %-*s -> %s %s",
342 TRANSPORT_SUMMARY_WIDTH, _("[rejected]"), REFCOL_WIDTH, remote,
343 pretty_ref, _("(non-fast-forward)"));
b888d61c
DB
344 return 1;
345 }
b888d61c
DB
346}
347
47abd85b 348static int store_updated_refs(const char *raw_url, const char *remote_name,
f3cb169b 349 struct ref *ref_map)
b888d61c
DB
350{
351 FILE *fp;
352 struct commit *commit;
efb98b44 353 int url_len, i, note_len, shown_url = 0, rc = 0;
b888d61c
DB
354 char note[1024];
355 const char *what, *kind;
356 struct ref *rm;
28a15401 357 char *url, *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
b888d61c 358
d6617c7c
AGR
359 fp = fopen(filename, "a");
360 if (!fp)
bd4a51fc 361 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
47abd85b 362
fb0cc87e
DB
363 if (raw_url)
364 url = transport_anonymize_url(raw_url);
365 else
366 url = xstrdup("foreign");
b888d61c
DB
367 for (rm = ref_map; rm; rm = rm->next) {
368 struct ref *ref = NULL;
369
370 if (rm->peer_ref) {
371 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
372 strcpy(ref->name, rm->peer_ref->name);
373 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
374 hashcpy(ref->new_sha1, rm->old_sha1);
1aad91f5 375 ref->force = rm->peer_ref->force;
b888d61c
DB
376 }
377
cfa5b2b7 378 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
b888d61c
DB
379 if (!commit)
380 rm->merge = 0;
381
382 if (!strcmp(rm->name, "HEAD")) {
383 kind = "";
384 what = "";
385 }
386 else if (!prefixcmp(rm->name, "refs/heads/")) {
387 kind = "branch";
388 what = rm->name + 11;
389 }
390 else if (!prefixcmp(rm->name, "refs/tags/")) {
391 kind = "tag";
392 what = rm->name + 10;
393 }
394 else if (!prefixcmp(rm->name, "refs/remotes/")) {
13931236 395 kind = "remote-tracking branch";
b888d61c
DB
396 what = rm->name + 13;
397 }
398 else {
399 kind = "";
400 what = rm->name;
401 }
402
403 url_len = strlen(url);
404 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
405 ;
406 url_len = i + 1;
407 if (4 < i && !strncmp(".git", url + i - 3, 4))
408 url_len = i - 3;
409
410 note_len = 0;
411 if (*what) {
412 if (*kind)
413 note_len += sprintf(note + note_len, "%s ",
414 kind);
415 note_len += sprintf(note + note_len, "'%s' of ", what);
416 }
95405ba6
AR
417 note[note_len] = '\0';
418 fprintf(fp, "%s\t%s\t%s",
b888d61c
DB
419 sha1_to_hex(commit ? commit->object.sha1 :
420 rm->old_sha1),
421 rm->merge ? "" : "not-for-merge",
422 note);
95405ba6
AR
423 for (i = 0; i < url_len; ++i)
424 if ('\n' == url[i])
425 fputs("\\n", fp);
426 else
427 fputc(url[i], fp);
428 fputc('\n', fp);
b888d61c 429
730b0200 430 if (ref) {
7f87aff2 431 rc |= update_local_ref(ref, what, note);
730b0200
AG
432 free(ref);
433 } else
f59774ad 434 sprintf(note, "* %-*s %-*s -> FETCH_HEAD",
f1863d0d 435 TRANSPORT_SUMMARY_WIDTH, *kind ? kind : "branch",
f59774ad 436 REFCOL_WIDTH, *what ? what : "HEAD");
f59774ad 437 if (*note) {
7f87aff2 438 if (verbosity >= 0 && !shown_url) {
502681cd 439 fprintf(stderr, _("From %.*s\n"),
f59774ad
JK
440 url_len, url);
441 shown_url = 1;
165f3902 442 }
7f87aff2
TA
443 if (verbosity >= 0)
444 fprintf(stderr, " %s\n", note);
165f3902 445 }
b888d61c 446 }
47abd85b 447 free(url);
b888d61c 448 fclose(fp);
fa250759 449 if (rc & STORE_REF_ERROR_DF_CONFLICT)
bd4a51fc 450 error(_("some local refs could not be updated; try running\n"
f3cb169b 451 " 'git remote prune %s' to remove any old, conflicting "
bd4a51fc 452 "branches"), remote_name);
efb98b44 453 return rc;
b888d61c
DB
454}
455
4191c356
SP
456/*
457 * We would want to bypass the object transfer altogether if
d9eb0205 458 * everything we are going to fetch already exists and is connected
4191c356
SP
459 * locally.
460 *
d9eb0205 461 * The refs we are going to fetch are in ref_map. If running
4191c356 462 *
d9eb0205 463 * $ git rev-list --objects --stdin --not --all
4191c356 464 *
d9eb0205 465 * (feeding all the refs in ref_map on its standard input)
4191c356
SP
466 * does not error out, that means everything reachable from the
467 * refs we are going to fetch exists and is connected to some of
468 * our existing refs.
469 */
470static int quickfetch(struct ref *ref_map)
471{
472 struct child_process revlist;
473 struct ref *ref;
d9eb0205
JH
474 int err;
475 const char *argv[] = {"rev-list",
476 "--quiet", "--objects", "--stdin", "--not", "--all", NULL};
4191c356
SP
477
478 /*
479 * If we are deepening a shallow clone we already have these
480 * objects reachable. Running rev-list here will return with
481 * a good (0) exit status and we'll bypass the fetch that we
482 * really need to perform. Claiming failure now will ensure
483 * we perform the network exchange to deepen our history.
484 */
485 if (depth)
486 return -1;
487
d9eb0205 488 if (!ref_map)
4191c356
SP
489 return 0;
490
4191c356 491 memset(&revlist, 0, sizeof(revlist));
d9eb0205 492 revlist.argv = argv;
4191c356 493 revlist.git_cmd = 1;
4191c356
SP
494 revlist.no_stdout = 1;
495 revlist.no_stderr = 1;
d9eb0205
JH
496 revlist.in = -1;
497
498 err = start_command(&revlist);
499 if (err) {
bd4a51fc 500 error(_("could not run rev-list"));
d9eb0205
JH
501 return err;
502 }
503
504 /*
505 * If rev-list --stdin encounters an unknown commit, it terminates,
506 * which will cause SIGPIPE in the write loop below.
507 */
508 sigchain_push(SIGPIPE, SIG_IGN);
509
510 for (ref = ref_map; ref; ref = ref->next) {
511 if (write_in_full(revlist.in, sha1_to_hex(ref->old_sha1), 40) < 0 ||
2b7ca830 512 write_str_in_full(revlist.in, "\n") < 0) {
d9eb0205 513 if (errno != EPIPE && errno != EINVAL)
bd4a51fc 514 error(_("failed write to rev-list: %s"), strerror(errno));
d9eb0205
JH
515 err = -1;
516 break;
517 }
518 }
519
520 if (close(revlist.in)) {
bd4a51fc 521 error(_("failed to close rev-list's stdin: %s"), strerror(errno));
d9eb0205
JH
522 err = -1;
523 }
524
525 sigchain_pop(SIGPIPE);
4191c356 526
d9eb0205 527 return finish_command(&revlist) || err;
4191c356
SP
528}
529
b888d61c
DB
530static int fetch_refs(struct transport *transport, struct ref *ref_map)
531{
4191c356
SP
532 int ret = quickfetch(ref_map);
533 if (ret)
534 ret = transport_fetch_refs(transport, ref_map);
b888d61c 535 if (!ret)
f3cb169b
JK
536 ret |= store_updated_refs(transport->url,
537 transport->remote->name,
538 ref_map);
1788c39c 539 transport_unlock_pack(transport);
b888d61c
DB
540 return ret;
541}
542
f360d844
JS
543static int prune_refs(struct transport *transport, struct ref *ref_map)
544{
545 int result = 0;
546 struct ref *ref, *stale_refs = get_stale_heads(transport->remote, ref_map);
547 const char *dangling_msg = dry_run
502681cd
ÆAB
548 ? _(" (%s will become dangling)\n")
549 : _(" (%s has become dangling)\n");
f360d844
JS
550
551 for (ref = stale_refs; ref; ref = ref->next) {
552 if (!dry_run)
553 result |= delete_ref(ref->name, NULL, 0);
554 if (verbosity >= 0) {
555 fprintf(stderr, " x %-*s %-*s -> %s\n",
502681cd
ÆAB
556 TRANSPORT_SUMMARY_WIDTH, _("[deleted]"),
557 REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
f360d844
JS
558 warn_dangling_symref(stderr, dangling_msg, ref->name);
559 }
560 }
561 free_refs(stale_refs);
562 return result;
563}
564
b888d61c
DB
565static int add_existing(const char *refname, const unsigned char *sha1,
566 int flag, void *cbdata)
567{
c455c87c 568 struct string_list *list = (struct string_list *)cbdata;
78a395d3 569 struct string_list_item *item = string_list_insert(list, refname);
b1a01e1c 570 item->util = (void *)sha1;
b888d61c
DB
571 return 0;
572}
573
cf7f929a
SP
574static int will_fetch(struct ref **head, const unsigned char *sha1)
575{
576 struct ref *rm = *head;
577 while (rm) {
578 if (!hashcmp(rm->old_sha1, sha1))
579 return 1;
580 rm = rm->next;
581 }
582 return 0;
583}
584
c50b2b47
SP
585static void find_non_local_tags(struct transport *transport,
586 struct ref **head,
587 struct ref ***tail)
b888d61c 588{
183113a5
TF
589 struct string_list existing_refs = STRING_LIST_INIT_NODUP;
590 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
4577370e 591 const struct ref *ref;
e984c54a 592 struct string_list_item *item = NULL;
b888d61c
DB
593
594 for_each_ref(add_existing, &existing_refs);
595 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
596 if (prefixcmp(ref->name, "refs/tags"))
597 continue;
598
e984c54a
JP
599 /*
600 * The peeled ref always follows the matching base
601 * ref, so if we see a peeled ref that we don't want
602 * to fetch then we can mark the ref entry in the list
603 * as one to ignore by setting util to NULL.
604 */
aac1d7b8 605 if (!suffixcmp(ref->name, "^{}")) {
e984c54a
JP
606 if (item && !has_sha1_file(ref->old_sha1) &&
607 !will_fetch(head, ref->old_sha1) &&
608 !has_sha1_file(item->util) &&
609 !will_fetch(head, item->util))
610 item->util = NULL;
611 item = NULL;
612 continue;
b888d61c
DB
613 }
614
e984c54a
JP
615 /*
616 * If item is non-NULL here, then we previously saw a
617 * ref not followed by a peeled reference, so we need
618 * to check if it is a lightweight tag that we want to
619 * fetch.
620 */
621 if (item && !has_sha1_file(item->util) &&
622 !will_fetch(head, item->util))
623 item->util = NULL;
b888d61c 624
e984c54a 625 item = NULL;
b888d61c 626
e984c54a
JP
627 /* skip duplicates and refs that we already have */
628 if (string_list_has_string(&remote_refs, ref->name) ||
629 string_list_has_string(&existing_refs, ref->name))
630 continue;
631
78a395d3 632 item = string_list_insert(&remote_refs, ref->name);
e984c54a 633 item->util = (void *)ref->old_sha1;
b888d61c 634 }
c455c87c 635 string_list_clear(&existing_refs, 0);
e984c54a
JP
636
637 /*
638 * We may have a final lightweight tag that needs to be
639 * checked to see if it needs fetching.
640 */
641 if (item && !has_sha1_file(item->util) &&
642 !will_fetch(head, item->util))
643 item->util = NULL;
644
645 /*
8a57c6e9
AR
646 * For all the tags in the remote_refs string list,
647 * add them to the list of refs to be fetched
e984c54a 648 */
8a57c6e9
AR
649 for_each_string_list_item(item, &remote_refs) {
650 /* Unless we have already decided to ignore this item... */
651 if (item->util)
652 {
653 struct ref *rm = alloc_ref(item->string);
654 rm->peer_ref = alloc_ref(item->string);
655 hashcpy(rm->old_sha1, item->util);
656 **tail = rm;
657 *tail = &rm->next;
658 }
659 }
e984c54a
JP
660
661 string_list_clear(&remote_refs, 0);
b888d61c
DB
662}
663
8ee5d731
JS
664static void check_not_current_branch(struct ref *ref_map)
665{
666 struct branch *current_branch = branch_get(NULL);
667
668 if (is_bare_repository() || !current_branch)
669 return;
670
671 for (; ref_map; ref_map = ref_map->next)
672 if (ref_map->peer_ref && !strcmp(current_branch->refname,
673 ref_map->peer_ref->name))
bd4a51fc
ÆAB
674 die(_("Refusing to fetch into current branch %s "
675 "of non-bare repository"), current_branch->refname);
8ee5d731
JS
676}
677
e6cc5104
JH
678static int truncate_fetch_head(void)
679{
680 char *filename = git_path("FETCH_HEAD");
681 FILE *fp = fopen(filename, "w");
682
683 if (!fp)
bd4a51fc 684 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
e6cc5104
JH
685 fclose(fp);
686 return 0;
687}
688
b888d61c
DB
689static int do_fetch(struct transport *transport,
690 struct refspec *refs, int ref_count)
691{
183113a5 692 struct string_list existing_refs = STRING_LIST_INIT_NODUP;
b1a01e1c 693 struct string_list_item *peer_item = NULL;
7f98428d 694 struct ref *ref_map;
b888d61c
DB
695 struct ref *rm;
696 int autotags = (transport->remote->fetch_tags == 1);
b1a01e1c
JP
697
698 for_each_ref(add_existing, &existing_refs);
699
ed368546
DJ
700 if (tags == TAGS_DEFAULT) {
701 if (transport->remote->fetch_tags == 2)
702 tags = TAGS_SET;
703 if (transport->remote->fetch_tags == -1)
704 tags = TAGS_UNSET;
705 }
b888d61c 706
824d5776 707 if (!transport->get_refs_list || !transport->fetch)
bd4a51fc 708 die(_("Don't know how to fetch from %s"), transport->url);
b888d61c
DB
709
710 /* if not appending, truncate FETCH_HEAD */
28a15401 711 if (!append && !dry_run) {
e6cc5104
JH
712 int errcode = truncate_fetch_head();
713 if (errcode)
714 return errcode;
d6617c7c 715 }
b888d61c
DB
716
717 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
8ee5d731
JS
718 if (!update_head_ok)
719 check_not_current_branch(ref_map);
b888d61c
DB
720
721 for (rm = ref_map; rm; rm = rm->next) {
b1a01e1c 722 if (rm->peer_ref) {
e8c8b713
JP
723 peer_item = string_list_lookup(&existing_refs,
724 rm->peer_ref->name);
b1a01e1c
JP
725 if (peer_item)
726 hashcpy(rm->peer_ref->old_sha1,
727 peer_item->util);
728 }
b888d61c
DB
729 }
730
41fa7d2e
SP
731 if (tags == TAGS_DEFAULT && autotags)
732 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
b888d61c
DB
733 if (fetch_refs(transport, ref_map)) {
734 free_refs(ref_map);
735 return 1;
736 }
f360d844
JS
737 if (prune)
738 prune_refs(transport, ref_map);
7f98428d 739 free_refs(ref_map);
b888d61c
DB
740
741 /* if neither --no-tags nor --tags was specified, do automated tag
742 * following ... */
83201998 743 if (tags == TAGS_DEFAULT && autotags) {
c50b2b47
SP
744 struct ref **tail = &ref_map;
745 ref_map = NULL;
746 find_non_local_tags(transport, &ref_map, &tail);
b888d61c 747 if (ref_map) {
41fa7d2e 748 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
b888d61c
DB
749 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
750 fetch_refs(transport, ref_map);
751 }
752 free_refs(ref_map);
753 }
754
b888d61c
DB
755 return 0;
756}
757
ab865e6e
SP
758static void set_option(const char *name, const char *value)
759{
760 int r = transport_set_option(transport, name, value);
761 if (r < 0)
bd4a51fc 762 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
ab865e6e
SP
763 name, value, transport->url);
764 if (r > 0)
bd4a51fc 765 warning(_("Option \"%s\" is ignored for %s\n"),
ab865e6e
SP
766 name, transport->url);
767}
768
9c4a036b
BG
769static int get_one_remote_for_fetch(struct remote *remote, void *priv)
770{
771 struct string_list *list = priv;
7cc91a2f 772 if (!remote->skip_default_update)
1d2f80fa 773 string_list_append(list, remote->name);
9c4a036b
BG
774 return 0;
775}
776
777struct remote_group_data {
778 const char *name;
779 struct string_list *list;
780};
781
782static int get_remote_group(const char *key, const char *value, void *priv)
783{
784 struct remote_group_data *g = priv;
785
786 if (!prefixcmp(key, "remotes.") &&
787 !strcmp(key + 8, g->name)) {
788 /* split list by white space */
789 int space = strcspn(value, " \t\n");
790 while (*value) {
791 if (space > 1) {
1d2f80fa
JP
792 string_list_append(g->list,
793 xstrndup(value, space));
9c4a036b
BG
794 }
795 value += space + (value[space] != '\0');
796 space = strcspn(value, " \t\n");
797 }
798 }
799
800 return 0;
801}
802
803static int add_remote_or_group(const char *name, struct string_list *list)
804{
805 int prev_nr = list->nr;
66dbfd55
GV
806 struct remote_group_data g;
807 g.name = name; g.list = list;
9c4a036b
BG
808
809 git_config(get_remote_group, &g);
810 if (list->nr == prev_nr) {
811 struct remote *remote;
812 if (!remote_is_configured(name))
813 return 0;
814 remote = remote_get(name);
1d2f80fa 815 string_list_append(list, remote->name);
9c4a036b
BG
816 }
817 return 1;
818}
819
7dce19d3 820static void add_options_to_argv(int *argc, const char **argv)
9c4a036b 821{
28a15401 822 if (dry_run)
7dce19d3 823 argv[(*argc)++] = "--dry-run";
f360d844 824 if (prune)
7dce19d3 825 argv[(*argc)++] = "--prune";
bba5322a 826 if (update_head_ok)
7dce19d3 827 argv[(*argc)++] = "--update-head-ok";
bba5322a 828 if (force)
7dce19d3 829 argv[(*argc)++] = "--force";
bba5322a 830 if (keep)
7dce19d3 831 argv[(*argc)++] = "--keep";
be254a0e 832 if (recurse_submodules == RECURSE_SUBMODULES_ON)
7dce19d3 833 argv[(*argc)++] = "--recurse-submodules";
8f0700dd
JL
834 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
835 argv[(*argc)++] = "--recurse-submodules=on-demand";
9c4a036b 836 if (verbosity >= 2)
7dce19d3 837 argv[(*argc)++] = "-v";
9c4a036b 838 if (verbosity >= 1)
7dce19d3 839 argv[(*argc)++] = "-v";
9c4a036b 840 else if (verbosity < 0)
7dce19d3
JL
841 argv[(*argc)++] = "-q";
842
843}
844
845static int fetch_multiple(struct string_list *list)
846{
847 int i, result = 0;
848 const char *argv[12] = { "fetch", "--append" };
849 int argc = 2;
850
851 add_options_to_argv(&argc, argv);
9c4a036b 852
e6cc5104
JH
853 if (!append && !dry_run) {
854 int errcode = truncate_fetch_head();
855 if (errcode)
856 return errcode;
857 }
858
9c4a036b
BG
859 for (i = 0; i < list->nr; i++) {
860 const char *name = list->items[i].string;
861 argv[argc] = name;
bba5322a 862 argv[argc + 1] = NULL;
9c4a036b 863 if (verbosity >= 0)
bd4a51fc 864 printf(_("Fetching %s\n"), name);
9c4a036b 865 if (run_command_v_opt(argv, RUN_GIT_CMD)) {
bd4a51fc 866 error(_("Could not fetch %s"), name);
9c4a036b
BG
867 result = 1;
868 }
869 }
870
871 return result;
872}
873
874static int fetch_one(struct remote *remote, int argc, const char **argv)
b888d61c 875{
2d324efa 876 int i;
b888d61c 877 static const char **refs = NULL;
d8ead159 878 struct refspec *refspec;
b888d61c 879 int ref_nr = 0;
7b7f39ea 880 int exit_code;
b888d61c 881
fa685bdf 882 if (!remote)
bd4a51fc
ÆAB
883 die(_("No remote repository specified. Please, specify either a URL or a\n"
884 "remote name from which new revisions should be fetched."));
fa685bdf 885
fb0cc87e 886 transport = transport_get(remote, NULL);
9839018e 887 transport_set_verbosity(transport, verbosity, progress);
b888d61c 888 if (upload_pack)
ab865e6e 889 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
b888d61c 890 if (keep)
ab865e6e
SP
891 set_option(TRANS_OPT_KEEP, "yes");
892 if (depth)
893 set_option(TRANS_OPT_DEPTH, depth);
b888d61c 894
9c4a036b 895 if (argc > 0) {
b888d61c 896 int j = 0;
83201998 897 refs = xcalloc(argc + 1, sizeof(const char *));
9c4a036b 898 for (i = 0; i < argc; i++) {
b888d61c
DB
899 if (!strcmp(argv[i], "tag")) {
900 char *ref;
901 i++;
f53423b0 902 if (i >= argc)
bd4a51fc 903 die(_("You need to specify a tag name."));
b888d61c
DB
904 ref = xmalloc(strlen(argv[i]) * 2 + 22);
905 strcpy(ref, "refs/tags/");
906 strcat(ref, argv[i]);
907 strcat(ref, ":refs/tags/");
908 strcat(ref, argv[i]);
909 refs[j++] = ref;
910 } else
911 refs[j++] = argv[i];
b888d61c
DB
912 }
913 refs[j] = NULL;
914 ref_nr = j;
b888d61c
DB
915 }
916
57b235a4 917 sigchain_push_common(unlock_pack_on_signal);
e4022ed2 918 atexit(unlock_pack);
d8ead159
JM
919 refspec = parse_fetch_refspec(ref_nr, refs);
920 exit_code = do_fetch(transport, refspec, ref_nr);
921 free(refspec);
7b7f39ea
AR
922 transport_disconnect(transport);
923 transport = NULL;
924 return exit_code;
b888d61c 925}
9c4a036b
BG
926
927int cmd_fetch(int argc, const char **argv, const char *prefix)
928{
929 int i;
183113a5 930 struct string_list list = STRING_LIST_INIT_NODUP;
9c4a036b
BG
931 struct remote *remote;
932 int result = 0;
933
bbc30f99
JK
934 packet_trace_identity("fetch");
935
9c4a036b
BG
936 /* Record the command line for the reflog */
937 strbuf_addstr(&default_rla, "fetch");
938 for (i = 1; i < argc; i++)
939 strbuf_addf(&default_rla, " %s", argv[i]);
940
941 argc = parse_options(argc, argv, prefix,
942 builtin_fetch_options, builtin_fetch_usage, 0);
943
944 if (all) {
945 if (argc == 1)
bd4a51fc 946 die(_("fetch --all does not take a repository argument"));
9c4a036b 947 else if (argc > 1)
bd4a51fc 948 die(_("fetch --all does not make sense with refspecs"));
9c4a036b
BG
949 (void) for_each_remote(get_one_remote_for_fetch, &list);
950 result = fetch_multiple(&list);
951 } else if (argc == 0) {
952 /* No arguments -- use default remote */
953 remote = remote_get(NULL);
954 result = fetch_one(remote, argc, argv);
16679e37
BG
955 } else if (multiple) {
956 /* All arguments are assumed to be remotes or groups */
957 for (i = 0; i < argc; i++)
958 if (!add_remote_or_group(argv[i], &list))
bd4a51fc 959 die(_("No such remote or remote group: %s"), argv[i]);
16679e37 960 result = fetch_multiple(&list);
9c4a036b
BG
961 } else {
962 /* Single remote or group */
963 (void) add_remote_or_group(argv[0], &list);
964 if (list.nr > 1) {
965 /* More than one remote */
966 if (argc > 1)
bd4a51fc 967 die(_("Fetching a group and specifying refspecs does not make sense"));
9c4a036b
BG
968 result = fetch_multiple(&list);
969 } else {
970 /* Zero or one remotes */
971 remote = remote_get(argv[0]);
972 result = fetch_one(remote, argc-1, argv+1);
973 }
974 }
975
be254a0e 976 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
7dce19d3
JL
977 const char *options[10];
978 int num_options = 0;
88a21979
JL
979 if (recurse_submodules_default) {
980 int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default);
981 set_config_fetch_recurse_submodules(arg);
982 }
7dce19d3
JL
983 gitmodules_config();
984 git_config(submodule_config, NULL);
985 add_options_to_argv(&num_options, options);
986 result = fetch_populated_submodules(num_options, options,
987 submodule_prefix,
8f0700dd 988 recurse_submodules,
7dce19d3
JL
989 verbosity < 0);
990 }
991
9c4a036b
BG
992 /* All names were strdup()ed or strndup()ed */
993 list.strdup_strings = 1;
994 string_list_clear(&list, 0);
995
996 return result;
997}