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