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