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