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