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