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