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