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