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