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