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