]>
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" |
027771fc | 14 | #include "submodule-config.h" |
7dce19d3 | 15 | #include "submodule.h" |
f96400cb | 16 | #include "connected.h" |
85556d4e | 17 | #include "argv-array.h" |
6bc91f23 | 18 | #include "utf8.h" |
b888d61c | 19 | |
83201998 | 20 | static const char * const builtin_fetch_usage[] = { |
719acedb NTND |
21 | N_("git fetch [<options>] [<repository> [<refspec>...]]"), |
22 | N_("git fetch [<options>] <group>"), | |
23 | N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"), | |
24 | N_("git fetch --all [<options>]"), | |
83201998 KH |
25 | NULL |
26 | }; | |
b888d61c | 27 | |
83201998 KH |
28 | enum { |
29 | TAGS_UNSET = 0, | |
30 | TAGS_DEFAULT = 1, | |
31 | TAGS_SET = 2 | |
32 | }; | |
33 | ||
737c5a9c MS |
34 | static int fetch_prune_config = -1; /* unspecified */ |
35 | static int prune = -1; /* unspecified */ | |
36 | #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */ | |
37 | ||
cccf74e2 | 38 | static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity, deepen_relative; |
01fdc21f | 39 | static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT; |
508ea882 | 40 | static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen; |
a028a193 | 41 | static int max_children = -1; |
c915f11e | 42 | static enum transport_family family; |
4191c356 | 43 | static const char *depth; |
508ea882 | 44 | static const char *deepen_since; |
83201998 | 45 | static const char *upload_pack; |
a45a2600 | 46 | static struct string_list deepen_not = STRING_LIST_INIT_NODUP; |
2d324efa | 47 | static struct strbuf default_rla = STRBUF_INIT; |
af234459 | 48 | static struct transport *gtransport; |
b26ed430 | 49 | static struct transport *gsecondary; |
7dce19d3 | 50 | static const char *submodule_prefix = ""; |
88a21979 | 51 | static const char *recurse_submodules_default; |
4b3b33a7 | 52 | static int shown_url = 0; |
c5558f80 JH |
53 | static int refmap_alloc, refmap_nr; |
54 | static const char **refmap_array; | |
e4022ed2 | 55 | |
8f0700dd JL |
56 | static int option_parse_recurse_submodules(const struct option *opt, |
57 | const char *arg, int unset) | |
58 | { | |
59 | if (unset) { | |
60 | recurse_submodules = RECURSE_SUBMODULES_OFF; | |
61 | } else { | |
62 | if (arg) | |
63 | recurse_submodules = parse_fetch_recurse_submodules_arg(opt->long_name, arg); | |
64 | else | |
65 | recurse_submodules = RECURSE_SUBMODULES_ON; | |
66 | } | |
67 | return 0; | |
68 | } | |
e4022ed2 | 69 | |
737c5a9c MS |
70 | static int git_fetch_config(const char *k, const char *v, void *cb) |
71 | { | |
72 | if (!strcmp(k, "fetch.prune")) { | |
73 | fetch_prune_config = git_config_bool(k, v); | |
74 | return 0; | |
75 | } | |
58f4203e SB |
76 | |
77 | if (!strcmp(k, "submodule.recurse")) { | |
78 | int r = git_config_bool(k, v) ? | |
79 | RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF; | |
80 | recurse_submodules = r; | |
81 | } | |
82 | ||
72549dfd | 83 | return git_default_config(k, v, cb); |
737c5a9c MS |
84 | } |
85 | ||
c5558f80 JH |
86 | static int parse_refmap_arg(const struct option *opt, const char *arg, int unset) |
87 | { | |
88 | ALLOC_GROW(refmap_array, refmap_nr + 1, refmap_alloc); | |
89 | ||
90 | /* | |
91 | * "git fetch --refmap='' origin foo" | |
92 | * can be used to tell the command not to store anywhere | |
93 | */ | |
94 | if (*arg) | |
95 | refmap_array[refmap_nr++] = arg; | |
96 | return 0; | |
97 | } | |
98 | ||
83201998 | 99 | static struct option builtin_fetch_options[] = { |
7f87aff2 | 100 | OPT__VERBOSITY(&verbosity), |
d5d09d47 SB |
101 | OPT_BOOL(0, "all", &all, |
102 | N_("fetch from all remotes")), | |
103 | OPT_BOOL('a', "append", &append, | |
104 | N_("append to .git/FETCH_HEAD instead of overwriting")), | |
719acedb NTND |
105 | OPT_STRING(0, "upload-pack", &upload_pack, N_("path"), |
106 | N_("path to upload pack on remote end")), | |
107 | OPT__FORCE(&force, N_("force overwrite of local branch")), | |
d5d09d47 SB |
108 | OPT_BOOL('m', "multiple", &multiple, |
109 | N_("fetch from multiple remotes")), | |
83201998 | 110 | OPT_SET_INT('t', "tags", &tags, |
719acedb | 111 | N_("fetch all tags and associated objects"), TAGS_SET), |
e7951290 | 112 | OPT_SET_INT('n', NULL, &tags, |
719acedb | 113 | N_("do not fetch all tags (--no-tags)"), TAGS_UNSET), |
62104ba1 SB |
114 | OPT_INTEGER('j', "jobs", &max_children, |
115 | N_("number of submodules fetched in parallel")), | |
d5d09d47 SB |
116 | OPT_BOOL('p', "prune", &prune, |
117 | N_("prune remote-tracking branches no longer on remote")), | |
719acedb NTND |
118 | { OPTION_CALLBACK, 0, "recurse-submodules", NULL, N_("on-demand"), |
119 | N_("control recursive fetching of submodules"), | |
8f0700dd | 120 | PARSE_OPT_OPTARG, option_parse_recurse_submodules }, |
d5d09d47 SB |
121 | OPT_BOOL(0, "dry-run", &dry_run, |
122 | N_("dry run")), | |
123 | OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")), | |
124 | OPT_BOOL('u', "update-head-ok", &update_head_ok, | |
719acedb NTND |
125 | N_("allow updating of HEAD ref")), |
126 | OPT_BOOL(0, "progress", &progress, N_("force progress reporting")), | |
127 | OPT_STRING(0, "depth", &depth, N_("depth"), | |
128 | N_("deepen history of shallow clone")), | |
508ea882 NTND |
129 | OPT_STRING(0, "shallow-since", &deepen_since, N_("time"), |
130 | N_("deepen history of shallow repository based on time")), | |
a45a2600 | 131 | OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"), |
6d873865 | 132 | N_("deepen history of shallow clone, excluding rev")), |
cccf74e2 NTND |
133 | OPT_INTEGER(0, "deepen", &deepen_relative, |
134 | N_("deepen history of shallow clone")), | |
4dcb167f NTND |
135 | { OPTION_SET_INT, 0, "unshallow", &unshallow, NULL, |
136 | N_("convert to a complete repository"), | |
137 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, NULL, 1 }, | |
719acedb NTND |
138 | { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"), |
139 | N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN }, | |
88a21979 JL |
140 | { OPTION_STRING, 0, "recurse-submodules-default", |
141 | &recurse_submodules_default, NULL, | |
719acedb | 142 | N_("default mode for recursion"), PARSE_OPT_HIDDEN }, |
48d25cae NTND |
143 | OPT_BOOL(0, "update-shallow", &update_shallow, |
144 | N_("accept refs that update .git/shallow")), | |
c5558f80 JH |
145 | { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"), |
146 | N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg }, | |
c915f11e EW |
147 | OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"), |
148 | TRANSPORT_FAMILY_IPV4), | |
149 | OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"), | |
150 | TRANSPORT_FAMILY_IPV6), | |
83201998 KH |
151 | OPT_END() |
152 | }; | |
153 | ||
e4022ed2 SP |
154 | static void unlock_pack(void) |
155 | { | |
af234459 JH |
156 | if (gtransport) |
157 | transport_unlock_pack(gtransport); | |
b26ed430 JH |
158 | if (gsecondary) |
159 | transport_unlock_pack(gsecondary); | |
e4022ed2 SP |
160 | } |
161 | ||
162 | static void unlock_pack_on_signal(int signo) | |
163 | { | |
164 | unlock_pack(); | |
4a16d072 | 165 | sigchain_pop(signo); |
e4022ed2 SP |
166 | raise(signo); |
167 | } | |
b888d61c | 168 | |
85682c19 | 169 | static void add_merge_config(struct ref **head, |
4577370e | 170 | const struct ref *remote_refs, |
85682c19 SP |
171 | struct branch *branch, |
172 | struct ref ***tail) | |
b888d61c | 173 | { |
85682c19 | 174 | int i; |
b888d61c | 175 | |
85682c19 SP |
176 | for (i = 0; i < branch->merge_nr; i++) { |
177 | struct ref *rm, **old_tail = *tail; | |
178 | struct refspec refspec; | |
179 | ||
180 | for (rm = *head; rm; rm = rm->next) { | |
181 | if (branch_merge_matches(branch, i, rm->name)) { | |
900f2814 | 182 | rm->fetch_head_status = FETCH_HEAD_MERGE; |
85682c19 SP |
183 | break; |
184 | } | |
b888d61c | 185 | } |
85682c19 SP |
186 | if (rm) |
187 | continue; | |
188 | ||
9ad7c5ae | 189 | /* |
8b3f3f84 | 190 | * Not fetched to a remote-tracking branch? We need to fetch |
85682c19 | 191 | * it anyway to allow this branch's "branch.$name.merge" |
05207a28 | 192 | * to be honored by 'git pull', but we do not have to |
9ad7c5ae JH |
193 | * fail if branch.$name.merge is misconfigured to point |
194 | * at a nonexisting branch. If we were indeed called by | |
05207a28 | 195 | * 'git pull', it will notice the misconfiguration because |
9ad7c5ae JH |
196 | * there is no entry in the resulting FETCH_HEAD marked |
197 | * for merging. | |
85682c19 | 198 | */ |
8da61a2a | 199 | memset(&refspec, 0, sizeof(refspec)); |
85682c19 | 200 | refspec.src = branch->merge[i]->src; |
9ad7c5ae | 201 | get_fetch_map(remote_refs, &refspec, tail, 1); |
85682c19 | 202 | for (rm = *old_tail; rm; rm = rm->next) |
900f2814 | 203 | rm->fetch_head_status = FETCH_HEAD_MERGE; |
b888d61c DB |
204 | } |
205 | } | |
206 | ||
0e0b7de4 | 207 | static int add_existing(const char *refname, const struct object_id *oid, |
a0fbb5a3 MH |
208 | int flag, void *cbdata) |
209 | { | |
210 | struct string_list *list = (struct string_list *)cbdata; | |
211 | struct string_list_item *item = string_list_insert(list, refname); | |
0e0b7de4 MH |
212 | struct object_id *old_oid = xmalloc(sizeof(*old_oid)); |
213 | ||
214 | oidcpy(old_oid, oid); | |
215 | item->util = old_oid; | |
a0fbb5a3 MH |
216 | return 0; |
217 | } | |
218 | ||
219 | static int will_fetch(struct ref **head, const unsigned char *sha1) | |
220 | { | |
221 | struct ref *rm = *head; | |
222 | while (rm) { | |
f4e54d02 | 223 | if (!hashcmp(rm->old_oid.hash, sha1)) |
a0fbb5a3 MH |
224 | return 1; |
225 | rm = rm->next; | |
226 | } | |
227 | return 0; | |
228 | } | |
229 | ||
767f176a SP |
230 | static void find_non_local_tags(struct transport *transport, |
231 | struct ref **head, | |
a0fbb5a3 MH |
232 | struct ref ***tail) |
233 | { | |
234 | struct string_list existing_refs = STRING_LIST_INIT_DUP; | |
235 | struct string_list remote_refs = STRING_LIST_INIT_NODUP; | |
236 | const struct ref *ref; | |
237 | struct string_list_item *item = NULL; | |
238 | ||
0e0b7de4 | 239 | for_each_ref(add_existing, &existing_refs); |
a0fbb5a3 | 240 | for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) { |
ad704485 | 241 | if (!starts_with(ref->name, "refs/tags/")) |
a0fbb5a3 MH |
242 | continue; |
243 | ||
244 | /* | |
245 | * The peeled ref always follows the matching base | |
246 | * ref, so if we see a peeled ref that we don't want | |
247 | * to fetch then we can mark the ref entry in the list | |
248 | * as one to ignore by setting util to NULL. | |
249 | */ | |
ad704485 | 250 | if (ends_with(ref->name, "^{}")) { |
5827a035 JK |
251 | if (item && |
252 | !has_object_file_with_flags(&ref->old_oid, HAS_SHA1_QUICK) && | |
f4e54d02 | 253 | !will_fetch(head, ref->old_oid.hash) && |
5827a035 | 254 | !has_sha1_file_with_flags(item->util, HAS_SHA1_QUICK) && |
a0fbb5a3 MH |
255 | !will_fetch(head, item->util)) |
256 | item->util = NULL; | |
257 | item = NULL; | |
258 | continue; | |
259 | } | |
260 | ||
261 | /* | |
262 | * If item is non-NULL here, then we previously saw a | |
263 | * ref not followed by a peeled reference, so we need | |
264 | * to check if it is a lightweight tag that we want to | |
265 | * fetch. | |
266 | */ | |
5827a035 JK |
267 | if (item && |
268 | !has_sha1_file_with_flags(item->util, HAS_SHA1_QUICK) && | |
a0fbb5a3 MH |
269 | !will_fetch(head, item->util)) |
270 | item->util = NULL; | |
271 | ||
272 | item = NULL; | |
273 | ||
274 | /* skip duplicates and refs that we already have */ | |
275 | if (string_list_has_string(&remote_refs, ref->name) || | |
276 | string_list_has_string(&existing_refs, ref->name)) | |
277 | continue; | |
278 | ||
279 | item = string_list_insert(&remote_refs, ref->name); | |
f4e54d02 | 280 | item->util = (void *)&ref->old_oid; |
a0fbb5a3 MH |
281 | } |
282 | string_list_clear(&existing_refs, 1); | |
283 | ||
284 | /* | |
285 | * We may have a final lightweight tag that needs to be | |
286 | * checked to see if it needs fetching. | |
287 | */ | |
5827a035 JK |
288 | if (item && |
289 | !has_sha1_file_with_flags(item->util, HAS_SHA1_QUICK) && | |
a0fbb5a3 MH |
290 | !will_fetch(head, item->util)) |
291 | item->util = NULL; | |
292 | ||
293 | /* | |
294 | * For all the tags in the remote_refs string list, | |
295 | * add them to the list of refs to be fetched | |
296 | */ | |
297 | for_each_string_list_item(item, &remote_refs) { | |
298 | /* Unless we have already decided to ignore this item... */ | |
299 | if (item->util) | |
300 | { | |
301 | struct ref *rm = alloc_ref(item->string); | |
302 | rm->peer_ref = alloc_ref(item->string); | |
f4e54d02 | 303 | oidcpy(&rm->old_oid, item->util); |
a0fbb5a3 MH |
304 | **tail = rm; |
305 | *tail = &rm->next; | |
306 | } | |
307 | } | |
308 | ||
309 | string_list_clear(&remote_refs, 0); | |
310 | } | |
767f176a | 311 | |
b888d61c | 312 | static struct ref *get_ref_map(struct transport *transport, |
f137a45e MH |
313 | struct refspec *refspecs, int refspec_count, |
314 | int tags, int *autotags) | |
b888d61c DB |
315 | { |
316 | int i; | |
317 | struct ref *rm; | |
318 | struct ref *ref_map = NULL; | |
319 | struct ref **tail = &ref_map; | |
320 | ||
c5a84e92 MH |
321 | /* opportunistically-updated references: */ |
322 | struct ref *orefs = NULL, **oref_tail = &orefs; | |
b888d61c | 323 | |
c5a84e92 | 324 | const struct ref *remote_refs = transport_get_remote_refs(transport); |
f2690487 | 325 | |
c5a84e92 | 326 | if (refspec_count) { |
c5558f80 JH |
327 | struct refspec *fetch_refspec; |
328 | int fetch_refspec_nr; | |
329 | ||
f137a45e MH |
330 | for (i = 0; i < refspec_count; i++) { |
331 | get_fetch_map(remote_refs, &refspecs[i], &tail, 0); | |
332 | if (refspecs[i].dst && refspecs[i].dst[0]) | |
b888d61c DB |
333 | *autotags = 1; |
334 | } | |
0281c930 | 335 | /* Merge everything on the command line (but not --tags) */ |
b888d61c | 336 | for (rm = ref_map; rm; rm = rm->next) |
900f2814 | 337 | rm->fetch_head_status = FETCH_HEAD_MERGE; |
f2690487 JK |
338 | |
339 | /* | |
0281c930 MH |
340 | * For any refs that we happen to be fetching via |
341 | * command-line arguments, the destination ref might | |
342 | * have been missing or have been different than the | |
343 | * remote-tracking ref that would be derived from the | |
344 | * configured refspec. In these cases, we want to | |
345 | * take the opportunity to update their configured | |
346 | * remote-tracking reference. However, we do not want | |
347 | * to mention these entries in FETCH_HEAD at all, as | |
348 | * they would simply be duplicates of existing | |
349 | * entries, so we set them FETCH_HEAD_IGNORE below. | |
350 | * | |
351 | * We compute these entries now, based only on the | |
352 | * refspecs specified on the command line. But we add | |
353 | * them to the list following the refspecs resulting | |
354 | * from the tags option so that one of the latter, | |
355 | * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed | |
356 | * by ref_remove_duplicates() in favor of one of these | |
357 | * opportunistic entries with FETCH_HEAD_IGNORE. | |
f2690487 | 358 | */ |
c5558f80 JH |
359 | if (refmap_array) { |
360 | fetch_refspec = parse_fetch_refspec(refmap_nr, refmap_array); | |
361 | fetch_refspec_nr = refmap_nr; | |
362 | } else { | |
363 | fetch_refspec = transport->remote->fetch; | |
364 | fetch_refspec_nr = transport->remote->fetch_refspec_nr; | |
365 | } | |
366 | ||
367 | for (i = 0; i < fetch_refspec_nr; i++) | |
368 | get_fetch_map(ref_map, &fetch_refspec[i], &oref_tail, 1); | |
c5558f80 JH |
369 | } else if (refmap_array) { |
370 | die("--refmap option is only meaningful with command-line refspec(s)."); | |
b888d61c DB |
371 | } else { |
372 | /* Use the defaults */ | |
373 | struct remote *remote = transport->remote; | |
85682c19 SP |
374 | struct branch *branch = branch_get(NULL); |
375 | int has_merge = branch_has_merge_config(branch); | |
3ee1757b BC |
376 | if (remote && |
377 | (remote->fetch_refspec_nr || | |
f31dbdc7 | 378 | /* Note: has_merge implies non-NULL branch->remote_name */ |
3ee1757b | 379 | (has_merge && !strcmp(branch->remote_name, remote->name)))) { |
b888d61c | 380 | for (i = 0; i < remote->fetch_refspec_nr; i++) { |
9ad7c5ae | 381 | get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0); |
b888d61c DB |
382 | if (remote->fetch[i].dst && |
383 | remote->fetch[i].dst[0]) | |
384 | *autotags = 1; | |
85682c19 | 385 | if (!i && !has_merge && ref_map && |
cfb8f898 | 386 | !remote->fetch[0].pattern) |
900f2814 | 387 | ref_map->fetch_head_status = FETCH_HEAD_MERGE; |
b888d61c | 388 | } |
da0204df JS |
389 | /* |
390 | * if the remote we're fetching from is the same | |
391 | * as given in branch.<name>.remote, we add the | |
392 | * ref given in branch.<name>.merge, too. | |
f31dbdc7 BC |
393 | * |
394 | * Note: has_merge implies non-NULL branch->remote_name | |
da0204df | 395 | */ |
9ad7c5ae JH |
396 | if (has_merge && |
397 | !strcmp(branch->remote_name, remote->name)) | |
85682c19 | 398 | add_merge_config(&ref_map, remote_refs, branch, &tail); |
b888d61c DB |
399 | } else { |
400 | ref_map = get_remote_ref(remote_refs, "HEAD"); | |
9ad7c5ae | 401 | if (!ref_map) |
bd4a51fc | 402 | die(_("Couldn't find remote ref HEAD")); |
900f2814 | 403 | ref_map->fetch_head_status = FETCH_HEAD_MERGE; |
5aaf7f2a | 404 | tail = &ref_map->next; |
b888d61c DB |
405 | } |
406 | } | |
0281c930 | 407 | |
c5a84e92 MH |
408 | if (tags == TAGS_SET) |
409 | /* also fetch all tags */ | |
410 | get_fetch_map(remote_refs, tag_refspec, &tail, 0); | |
411 | else if (tags == TAGS_DEFAULT && *autotags) | |
767f176a | 412 | find_non_local_tags(transport, &ref_map, &tail); |
0281c930 | 413 | |
c5a84e92 MH |
414 | /* Now append any refs to be updated opportunistically: */ |
415 | *tail = orefs; | |
416 | for (rm = orefs; rm; rm = rm->next) { | |
417 | rm->fetch_head_status = FETCH_HEAD_IGNORE; | |
418 | tail = &rm->next; | |
419 | } | |
420 | ||
b9afe665 | 421 | return ref_remove_duplicates(ref_map); |
b888d61c DB |
422 | } |
423 | ||
fa250759 JK |
424 | #define STORE_REF_ERROR_OTHER 1 |
425 | #define STORE_REF_ERROR_DF_CONFLICT 2 | |
426 | ||
b888d61c DB |
427 | static int s_update_ref(const char *action, |
428 | struct ref *ref, | |
429 | int check_old) | |
430 | { | |
1412f762 | 431 | char *msg; |
b888d61c | 432 | char *rla = getenv("GIT_REFLOG_ACTION"); |
cd94f765 RS |
433 | struct ref_transaction *transaction; |
434 | struct strbuf err = STRBUF_INIT; | |
435 | int ret, df_conflict = 0; | |
b888d61c | 436 | |
28a15401 JS |
437 | if (dry_run) |
438 | return 0; | |
b888d61c | 439 | if (!rla) |
2d324efa | 440 | rla = default_rla.buf; |
1412f762 | 441 | msg = xstrfmt("%s: %s", rla, action); |
cd94f765 RS |
442 | |
443 | transaction = ref_transaction_begin(&err); | |
444 | if (!transaction || | |
1d147bdf | 445 | ref_transaction_update(transaction, ref->name, |
f4e54d02 | 446 | ref->new_oid.hash, |
447 | check_old ? ref->old_oid.hash : NULL, | |
1d147bdf | 448 | 0, msg, &err)) |
cd94f765 RS |
449 | goto fail; |
450 | ||
451 | ret = ref_transaction_commit(transaction, &err); | |
452 | if (ret) { | |
453 | df_conflict = (ret == TRANSACTION_NAME_CONFLICT); | |
454 | goto fail; | |
455 | } | |
456 | ||
457 | ref_transaction_free(transaction); | |
458 | strbuf_release(&err); | |
1412f762 | 459 | free(msg); |
b888d61c | 460 | return 0; |
cd94f765 RS |
461 | fail: |
462 | ref_transaction_free(transaction); | |
463 | error("%s", err.buf); | |
464 | strbuf_release(&err); | |
1412f762 | 465 | free(msg); |
cd94f765 RS |
466 | return df_conflict ? STORE_REF_ERROR_DF_CONFLICT |
467 | : STORE_REF_ERROR_OTHER; | |
b888d61c DB |
468 | } |
469 | ||
6bc91f23 | 470 | static int refcol_width = 10; |
bc437d10 | 471 | static int compact_format; |
6bc91f23 NTND |
472 | |
473 | static void adjust_refcol_width(const struct ref *ref) | |
474 | { | |
475 | int max, rlen, llen, len; | |
476 | ||
477 | /* uptodate lines are only shown on high verbosity level */ | |
478 | if (!verbosity && !oidcmp(&ref->peer_ref->old_oid, &ref->old_oid)) | |
479 | return; | |
480 | ||
481 | max = term_columns(); | |
482 | rlen = utf8_strwidth(prettify_refname(ref->name)); | |
bc437d10 | 483 | |
6bc91f23 NTND |
484 | llen = utf8_strwidth(prettify_refname(ref->peer_ref->name)); |
485 | ||
486 | /* | |
487 | * rough estimation to see if the output line is too long and | |
488 | * should not be counted (we can't do precise calculation | |
489 | * anyway because we don't know if the error explanation part | |
490 | * will be printed in update_local_ref) | |
491 | */ | |
bc437d10 NTND |
492 | if (compact_format) { |
493 | llen = 0; | |
494 | max = max * 2 / 3; | |
495 | } | |
6bc91f23 NTND |
496 | len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen; |
497 | if (len >= max) | |
498 | return; | |
499 | ||
bc437d10 NTND |
500 | /* |
501 | * Not precise calculation for compact mode because '*' can | |
502 | * appear on the left hand side of '->' and shrink the column | |
503 | * back. | |
504 | */ | |
6bc91f23 NTND |
505 | if (refcol_width < rlen) |
506 | refcol_width = rlen; | |
507 | } | |
508 | ||
509 | static void prepare_format_display(struct ref *ref_map) | |
510 | { | |
511 | struct ref *rm; | |
bc437d10 NTND |
512 | const char *format = "full"; |
513 | ||
514 | git_config_get_string_const("fetch.output", &format); | |
515 | if (!strcasecmp(format, "full")) | |
516 | compact_format = 0; | |
517 | else if (!strcasecmp(format, "compact")) | |
518 | compact_format = 1; | |
519 | else | |
520 | die(_("configuration fetch.output contains invalid value %s"), | |
521 | format); | |
6bc91f23 NTND |
522 | |
523 | for (rm = ref_map; rm; rm = rm->next) { | |
524 | if (rm->status == REF_STATUS_REJECT_SHALLOW || | |
525 | !rm->peer_ref || | |
526 | !strcmp(rm->name, "HEAD")) | |
527 | continue; | |
528 | ||
529 | adjust_refcol_width(rm); | |
530 | } | |
531 | } | |
165f3902 | 532 | |
bc437d10 NTND |
533 | static void print_remote_to_local(struct strbuf *display, |
534 | const char *remote, const char *local) | |
535 | { | |
536 | strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local); | |
537 | } | |
538 | ||
539 | static int find_and_replace(struct strbuf *haystack, | |
540 | const char *needle, | |
541 | const char *placeholder) | |
542 | { | |
543 | const char *p = strstr(haystack->buf, needle); | |
544 | int plen, nlen; | |
545 | ||
546 | if (!p) | |
547 | return 0; | |
548 | ||
549 | if (p > haystack->buf && p[-1] != '/') | |
550 | return 0; | |
551 | ||
552 | plen = strlen(p); | |
553 | nlen = strlen(needle); | |
554 | if (plen > nlen && p[nlen] != '/') | |
555 | return 0; | |
556 | ||
557 | strbuf_splice(haystack, p - haystack->buf, nlen, | |
558 | placeholder, strlen(placeholder)); | |
559 | return 1; | |
560 | } | |
561 | ||
562 | static void print_compact(struct strbuf *display, | |
563 | const char *remote, const char *local) | |
564 | { | |
565 | struct strbuf r = STRBUF_INIT; | |
566 | struct strbuf l = STRBUF_INIT; | |
567 | ||
568 | if (!strcmp(remote, local)) { | |
569 | strbuf_addf(display, "%-*s -> *", refcol_width, remote); | |
570 | return; | |
571 | } | |
572 | ||
573 | strbuf_addstr(&r, remote); | |
574 | strbuf_addstr(&l, local); | |
575 | ||
576 | if (!find_and_replace(&r, local, "*")) | |
577 | find_and_replace(&l, remote, "*"); | |
578 | print_remote_to_local(display, r.buf, l.buf); | |
579 | ||
580 | strbuf_release(&r); | |
581 | strbuf_release(&l); | |
582 | } | |
583 | ||
d0b39a03 NTND |
584 | static void format_display(struct strbuf *display, char code, |
585 | const char *summary, const char *error, | |
901f3d40 JH |
586 | const char *remote, const char *local, |
587 | int summary_width) | |
d0b39a03 | 588 | { |
901f3d40 JH |
589 | int width = (summary_width + strlen(summary) - gettext_width(summary)); |
590 | ||
591 | strbuf_addf(display, "%c %-*s ", code, width, summary); | |
bc437d10 NTND |
592 | if (!compact_format) |
593 | print_remote_to_local(display, remote, local); | |
594 | else | |
595 | print_compact(display, remote, local); | |
d0b39a03 NTND |
596 | if (error) |
597 | strbuf_addf(display, " (%s)", error); | |
598 | } | |
165f3902 | 599 | |
b888d61c | 600 | static int update_local_ref(struct ref *ref, |
165f3902 | 601 | const char *remote, |
6da618d5 | 602 | const struct ref *remote_ref, |
901f3d40 JH |
603 | struct strbuf *display, |
604 | int summary_width) | |
b888d61c | 605 | { |
b888d61c DB |
606 | struct commit *current = NULL, *updated; |
607 | enum object_type type; | |
608 | struct branch *current_branch = branch_get(NULL); | |
4577e483 | 609 | const char *pretty_ref = prettify_refname(ref->name); |
b888d61c | 610 | |
f4e54d02 | 611 | type = sha1_object_info(ref->new_oid.hash, NULL); |
b888d61c | 612 | if (type < 0) |
f4e54d02 | 613 | die(_("object %s not found"), oid_to_hex(&ref->new_oid)); |
b888d61c | 614 | |
f4e54d02 | 615 | if (!oidcmp(&ref->old_oid, &ref->new_oid)) { |
7f87aff2 | 616 | if (verbosity > 0) |
d0b39a03 | 617 | format_display(display, '=', _("[up to date]"), NULL, |
901f3d40 | 618 | remote, pretty_ref, summary_width); |
b888d61c DB |
619 | return 0; |
620 | } | |
621 | ||
b3abdd9d SP |
622 | if (current_branch && |
623 | !strcmp(ref->name, current_branch->name) && | |
b888d61c | 624 | !(update_head_ok || is_bare_repository()) && |
f4e54d02 | 625 | !is_null_oid(&ref->old_oid)) { |
b888d61c DB |
626 | /* |
627 | * If this is the head, and it's not okay to update | |
628 | * the head, and the old value of the head isn't empty... | |
629 | */ | |
d0b39a03 NTND |
630 | format_display(display, '!', _("[rejected]"), |
631 | _("can't fetch in current branch"), | |
901f3d40 | 632 | remote, pretty_ref, summary_width); |
b888d61c DB |
633 | return 1; |
634 | } | |
635 | ||
f4e54d02 | 636 | if (!is_null_oid(&ref->old_oid) && |
59556548 | 637 | starts_with(ref->name, "refs/tags/")) { |
6315472e JK |
638 | int r; |
639 | r = s_update_ref("updating tag", ref, 0); | |
2cb040ba | 640 | format_display(display, r ? '!' : 't', _("[tag update]"), |
d0b39a03 | 641 | r ? _("unable to update local ref") : NULL, |
901f3d40 | 642 | remote, pretty_ref, summary_width); |
6315472e | 643 | return r; |
b888d61c DB |
644 | } |
645 | ||
bc83266a | 646 | current = lookup_commit_reference_gently(&ref->old_oid, 1); |
647 | updated = lookup_commit_reference_gently(&ref->new_oid, 1); | |
b888d61c | 648 | if (!current || !updated) { |
165f3902 NP |
649 | const char *msg; |
650 | const char *what; | |
6315472e | 651 | int r; |
0997adaa MB |
652 | /* |
653 | * Nicely describe the new ref we're fetching. | |
654 | * Base this on the remote's ref name, as it's | |
655 | * more likely to follow a standard layout. | |
656 | */ | |
657 | const char *name = remote_ref ? remote_ref->name : ""; | |
59556548 | 658 | if (starts_with(name, "refs/tags/")) { |
b888d61c | 659 | msg = "storing tag"; |
f7b3742a | 660 | what = _("[new tag]"); |
59556548 | 661 | } else if (starts_with(name, "refs/heads/")) { |
b888d61c | 662 | msg = "storing head"; |
f7b3742a | 663 | what = _("[new branch]"); |
0997adaa MB |
664 | } else { |
665 | msg = "storing ref"; | |
666 | what = _("[new ref]"); | |
165f3902 NP |
667 | } |
668 | ||
a6801adc JL |
669 | if ((recurse_submodules != RECURSE_SUBMODULES_OFF) && |
670 | (recurse_submodules != RECURSE_SUBMODULES_ON)) | |
2eb80bcd | 671 | check_for_new_submodule_commits(&ref->new_oid); |
6315472e | 672 | r = s_update_ref(msg, ref, 0); |
d0b39a03 NTND |
673 | format_display(display, r ? '!' : '*', what, |
674 | r ? _("unable to update local ref") : NULL, | |
901f3d40 | 675 | remote, pretty_ref, summary_width); |
6315472e | 676 | return r; |
b888d61c DB |
677 | } |
678 | ||
a20efee9 | 679 | if (in_merge_bases(current, updated)) { |
bd22d4ff | 680 | struct strbuf quickref = STRBUF_INIT; |
6315472e | 681 | int r; |
ed1c9977 | 682 | strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV); |
bd22d4ff | 683 | strbuf_addstr(&quickref, ".."); |
f4e54d02 | 684 | strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV); |
88a21979 JL |
685 | if ((recurse_submodules != RECURSE_SUBMODULES_OFF) && |
686 | (recurse_submodules != RECURSE_SUBMODULES_ON)) | |
2eb80bcd | 687 | check_for_new_submodule_commits(&ref->new_oid); |
a75d7b54 | 688 | r = s_update_ref("fast-forward", ref, 1); |
d0b39a03 NTND |
689 | format_display(display, r ? '!' : ' ', quickref.buf, |
690 | r ? _("unable to update local ref") : NULL, | |
901f3d40 | 691 | remote, pretty_ref, summary_width); |
bd22d4ff | 692 | strbuf_release(&quickref); |
6315472e | 693 | return r; |
165f3902 | 694 | } else if (force || ref->force) { |
bd22d4ff | 695 | struct strbuf quickref = STRBUF_INIT; |
6315472e | 696 | int r; |
ed1c9977 | 697 | strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV); |
bd22d4ff | 698 | strbuf_addstr(&quickref, "..."); |
f4e54d02 | 699 | strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV); |
88a21979 JL |
700 | if ((recurse_submodules != RECURSE_SUBMODULES_OFF) && |
701 | (recurse_submodules != RECURSE_SUBMODULES_ON)) | |
2eb80bcd | 702 | check_for_new_submodule_commits(&ref->new_oid); |
6315472e | 703 | r = s_update_ref("forced-update", ref, 1); |
d0b39a03 NTND |
704 | format_display(display, r ? '!' : '+', quickref.buf, |
705 | r ? _("unable to update local ref") : _("forced update"), | |
901f3d40 | 706 | remote, pretty_ref, summary_width); |
bd22d4ff | 707 | strbuf_release(&quickref); |
6315472e | 708 | return r; |
165f3902 | 709 | } else { |
d0b39a03 | 710 | format_display(display, '!', _("[rejected]"), _("non-fast-forward"), |
901f3d40 | 711 | remote, pretty_ref, summary_width); |
b888d61c DB |
712 | return 1; |
713 | } | |
b888d61c DB |
714 | } |
715 | ||
f0e278b1 | 716 | static int iterate_ref_map(void *cb_data, unsigned char sha1[20]) |
6b67e0dc | 717 | { |
f0e278b1 JH |
718 | struct ref **rm = cb_data; |
719 | struct ref *ref = *rm; | |
6b67e0dc | 720 | |
4820a33b NTND |
721 | while (ref && ref->status == REF_STATUS_REJECT_SHALLOW) |
722 | ref = ref->next; | |
f0e278b1 JH |
723 | if (!ref) |
724 | return -1; /* end of the list */ | |
725 | *rm = ref->next; | |
f4e54d02 | 726 | hashcpy(sha1, ref->old_oid.hash); |
f0e278b1 | 727 | return 0; |
6b67e0dc JH |
728 | } |
729 | ||
47abd85b | 730 | static int store_updated_refs(const char *raw_url, const char *remote_name, |
f3cb169b | 731 | struct ref *ref_map) |
b888d61c DB |
732 | { |
733 | FILE *fp; | |
734 | struct commit *commit; | |
4b3b33a7 | 735 | int url_len, i, rc = 0; |
5914f2d0 | 736 | struct strbuf note = STRBUF_INIT; |
b888d61c DB |
737 | const char *what, *kind; |
738 | struct ref *rm; | |
dcf69262 | 739 | char *url; |
f932729c | 740 | const char *filename = dry_run ? "/dev/null" : git_path_fetch_head(); |
900f2814 | 741 | int want_status; |
11fd66de | 742 | int summary_width = transport_summary_width(ref_map); |
b888d61c | 743 | |
d6617c7c AGR |
744 | fp = fopen(filename, "a"); |
745 | if (!fp) | |
6da31d7f | 746 | return error_errno(_("cannot open %s"), filename); |
47abd85b | 747 | |
fb0cc87e DB |
748 | if (raw_url) |
749 | url = transport_anonymize_url(raw_url); | |
750 | else | |
751 | url = xstrdup("foreign"); | |
6b67e0dc | 752 | |
f0e278b1 | 753 | rm = ref_map; |
7043c707 | 754 | if (check_connected(iterate_ref_map, &rm, NULL)) { |
9516a598 TRC |
755 | rc = error(_("%s did not send all necessary objects\n"), url); |
756 | goto abort; | |
757 | } | |
6b67e0dc | 758 | |
6bc91f23 NTND |
759 | prepare_format_display(ref_map); |
760 | ||
96890f4c | 761 | /* |
900f2814 JK |
762 | * We do a pass for each fetch_head_status type in their enum order, so |
763 | * merged entries are written before not-for-merge. That lets readers | |
764 | * use FETCH_HEAD as a refname to refer to the ref to be merged. | |
96890f4c | 765 | */ |
900f2814 JK |
766 | for (want_status = FETCH_HEAD_MERGE; |
767 | want_status <= FETCH_HEAD_IGNORE; | |
768 | want_status++) { | |
96890f4c JH |
769 | for (rm = ref_map; rm; rm = rm->next) { |
770 | struct ref *ref = NULL; | |
900f2814 | 771 | const char *merge_status_marker = ""; |
96890f4c | 772 | |
4820a33b NTND |
773 | if (rm->status == REF_STATUS_REJECT_SHALLOW) { |
774 | if (want_status == FETCH_HEAD_MERGE) | |
775 | warning(_("reject %s because shallow roots are not allowed to be updated"), | |
776 | rm->peer_ref ? rm->peer_ref->name : rm->name); | |
777 | continue; | |
778 | } | |
779 | ||
bc83266a | 780 | commit = lookup_commit_reference_gently(&rm->old_oid, |
781 | 1); | |
96890f4c | 782 | if (!commit) |
900f2814 | 783 | rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE; |
96890f4c | 784 | |
900f2814 | 785 | if (rm->fetch_head_status != want_status) |
96890f4c JH |
786 | continue; |
787 | ||
788 | if (rm->peer_ref) { | |
6f687c21 | 789 | ref = alloc_ref(rm->peer_ref->name); |
f4e54d02 | 790 | oidcpy(&ref->old_oid, &rm->peer_ref->old_oid); |
791 | oidcpy(&ref->new_oid, &rm->old_oid); | |
96890f4c JH |
792 | ref->force = rm->peer_ref->force; |
793 | } | |
b888d61c | 794 | |
b888d61c | 795 | |
96890f4c JH |
796 | if (!strcmp(rm->name, "HEAD")) { |
797 | kind = ""; | |
798 | what = ""; | |
799 | } | |
59556548 | 800 | else if (starts_with(rm->name, "refs/heads/")) { |
96890f4c JH |
801 | kind = "branch"; |
802 | what = rm->name + 11; | |
803 | } | |
59556548 | 804 | else if (starts_with(rm->name, "refs/tags/")) { |
96890f4c JH |
805 | kind = "tag"; |
806 | what = rm->name + 10; | |
807 | } | |
59556548 | 808 | else if (starts_with(rm->name, "refs/remotes/")) { |
96890f4c JH |
809 | kind = "remote-tracking branch"; |
810 | what = rm->name + 13; | |
811 | } | |
812 | else { | |
813 | kind = ""; | |
814 | what = rm->name; | |
815 | } | |
b888d61c | 816 | |
96890f4c JH |
817 | url_len = strlen(url); |
818 | for (i = url_len - 1; url[i] == '/' && 0 <= i; i--) | |
819 | ; | |
820 | url_len = i + 1; | |
821 | if (4 < i && !strncmp(".git", url + i - 3, 4)) | |
822 | url_len = i - 3; | |
823 | ||
824 | strbuf_reset(¬e); | |
825 | if (*what) { | |
826 | if (*kind) | |
827 | strbuf_addf(¬e, "%s ", kind); | |
828 | strbuf_addf(¬e, "'%s' of ", what); | |
829 | } | |
900f2814 JK |
830 | switch (rm->fetch_head_status) { |
831 | case FETCH_HEAD_NOT_FOR_MERGE: | |
832 | merge_status_marker = "not-for-merge"; | |
833 | /* fall-through */ | |
834 | case FETCH_HEAD_MERGE: | |
835 | fprintf(fp, "%s\t%s\t%s", | |
f4e54d02 | 836 | oid_to_hex(&rm->old_oid), |
900f2814 JK |
837 | merge_status_marker, |
838 | note.buf); | |
839 | for (i = 0; i < url_len; ++i) | |
840 | if ('\n' == url[i]) | |
841 | fputs("\\n", fp); | |
842 | else | |
843 | fputc(url[i], fp); | |
844 | fputc('\n', fp); | |
845 | break; | |
846 | default: | |
847 | /* do not write anything to FETCH_HEAD */ | |
848 | break; | |
849 | } | |
96890f4c JH |
850 | |
851 | strbuf_reset(¬e); | |
852 | if (ref) { | |
901f3d40 JH |
853 | rc |= update_local_ref(ref, what, rm, ¬e, |
854 | summary_width); | |
96890f4c JH |
855 | free(ref); |
856 | } else | |
d0b39a03 NTND |
857 | format_display(¬e, '*', |
858 | *kind ? kind : "branch", NULL, | |
859 | *what ? what : "HEAD", | |
901f3d40 | 860 | "FETCH_HEAD", summary_width); |
96890f4c JH |
861 | if (note.len) { |
862 | if (verbosity >= 0 && !shown_url) { | |
863 | fprintf(stderr, _("From %.*s\n"), | |
864 | url_len, url); | |
865 | shown_url = 1; | |
866 | } | |
867 | if (verbosity >= 0) | |
868 | fprintf(stderr, " %s\n", note.buf); | |
165f3902 NP |
869 | } |
870 | } | |
b888d61c | 871 | } |
9516a598 | 872 | |
fa250759 | 873 | if (rc & STORE_REF_ERROR_DF_CONFLICT) |
bd4a51fc | 874 | error(_("some local refs could not be updated; try running\n" |
f3cb169b | 875 | " 'git remote prune %s' to remove any old, conflicting " |
bd4a51fc | 876 | "branches"), remote_name); |
9516a598 TRC |
877 | |
878 | abort: | |
5914f2d0 | 879 | strbuf_release(¬e); |
9516a598 TRC |
880 | free(url); |
881 | fclose(fp); | |
efb98b44 | 882 | return rc; |
b888d61c DB |
883 | } |
884 | ||
4191c356 SP |
885 | /* |
886 | * We would want to bypass the object transfer altogether if | |
d9eb0205 | 887 | * everything we are going to fetch already exists and is connected |
4191c356 | 888 | * locally. |
4191c356 SP |
889 | */ |
890 | static int quickfetch(struct ref *ref_map) | |
891 | { | |
f0e278b1 | 892 | struct ref *rm = ref_map; |
7043c707 | 893 | struct check_connected_options opt = CHECK_CONNECTED_INIT; |
f0e278b1 | 894 | |
4191c356 SP |
895 | /* |
896 | * If we are deepening a shallow clone we already have these | |
897 | * objects reachable. Running rev-list here will return with | |
898 | * a good (0) exit status and we'll bypass the fetch that we | |
899 | * really need to perform. Claiming failure now will ensure | |
900 | * we perform the network exchange to deepen our history. | |
901 | */ | |
508ea882 | 902 | if (deepen) |
4191c356 | 903 | return -1; |
7043c707 JK |
904 | opt.quiet = 1; |
905 | return check_connected(iterate_ref_map, &rm, &opt); | |
4191c356 SP |
906 | } |
907 | ||
b888d61c DB |
908 | static int fetch_refs(struct transport *transport, struct ref *ref_map) |
909 | { | |
4191c356 SP |
910 | int ret = quickfetch(ref_map); |
911 | if (ret) | |
912 | ret = transport_fetch_refs(transport, ref_map); | |
b888d61c | 913 | if (!ret) |
f3cb169b JK |
914 | ret |= store_updated_refs(transport->url, |
915 | transport->remote->name, | |
916 | ref_map); | |
1788c39c | 917 | transport_unlock_pack(transport); |
b888d61c DB |
918 | return ret; |
919 | } | |
920 | ||
4b3b33a7 TM |
921 | static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map, |
922 | const char *raw_url) | |
f360d844 | 923 | { |
4b3b33a7 | 924 | int url_len, i, result = 0; |
ed43de6e | 925 | struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map); |
4b3b33a7 | 926 | char *url; |
11fd66de | 927 | int summary_width = transport_summary_width(stale_refs); |
f360d844 | 928 | const char *dangling_msg = dry_run |
18986d53 NTND |
929 | ? _(" (%s will become dangling)") |
930 | : _(" (%s has become dangling)"); | |
f360d844 | 931 | |
4b3b33a7 TM |
932 | if (raw_url) |
933 | url = transport_anonymize_url(raw_url); | |
934 | else | |
935 | url = xstrdup("foreign"); | |
936 | ||
937 | url_len = strlen(url); | |
938 | for (i = url_len - 1; url[i] == '/' && 0 <= i; i--) | |
939 | ; | |
940 | ||
941 | url_len = i + 1; | |
942 | if (4 < i && !strncmp(".git", url + i - 3, 4)) | |
943 | url_len = i - 3; | |
944 | ||
a087b432 MH |
945 | if (!dry_run) { |
946 | struct string_list refnames = STRING_LIST_INIT_NODUP; | |
947 | ||
948 | for (ref = stale_refs; ref; ref = ref->next) | |
949 | string_list_append(&refnames, ref->name); | |
950 | ||
64da4199 | 951 | result = delete_refs("fetch: prune", &refnames, 0); |
a087b432 MH |
952 | string_list_clear(&refnames, 0); |
953 | } | |
954 | ||
955 | if (verbosity >= 0) { | |
956 | for (ref = stale_refs; ref; ref = ref->next) { | |
d0b39a03 | 957 | struct strbuf sb = STRBUF_INIT; |
a087b432 MH |
958 | if (!shown_url) { |
959 | fprintf(stderr, _("From %.*s\n"), url_len, url); | |
960 | shown_url = 1; | |
961 | } | |
2cb040ba | 962 | format_display(&sb, '-', _("[deleted]"), NULL, |
901f3d40 JH |
963 | _("(none)"), prettify_refname(ref->name), |
964 | summary_width); | |
d0b39a03 NTND |
965 | fprintf(stderr, " %s\n",sb.buf); |
966 | strbuf_release(&sb); | |
f360d844 JS |
967 | warn_dangling_symref(stderr, dangling_msg, ref->name); |
968 | } | |
969 | } | |
a087b432 | 970 | |
4b3b33a7 | 971 | free(url); |
f360d844 JS |
972 | free_refs(stale_refs); |
973 | return result; | |
974 | } | |
975 | ||
8ee5d731 JS |
976 | static void check_not_current_branch(struct ref *ref_map) |
977 | { | |
978 | struct branch *current_branch = branch_get(NULL); | |
979 | ||
980 | if (is_bare_repository() || !current_branch) | |
981 | return; | |
982 | ||
983 | for (; ref_map; ref_map = ref_map->next) | |
984 | if (ref_map->peer_ref && !strcmp(current_branch->refname, | |
985 | ref_map->peer_ref->name)) | |
bd4a51fc ÆAB |
986 | die(_("Refusing to fetch into current branch %s " |
987 | "of non-bare repository"), current_branch->refname); | |
8ee5d731 JS |
988 | } |
989 | ||
e6cc5104 JH |
990 | static int truncate_fetch_head(void) |
991 | { | |
f932729c | 992 | const char *filename = git_path_fetch_head(); |
ea56518d | 993 | FILE *fp = fopen_for_writing(filename); |
e6cc5104 JH |
994 | |
995 | if (!fp) | |
6da31d7f | 996 | return error_errno(_("cannot open %s"), filename); |
e6cc5104 JH |
997 | fclose(fp); |
998 | return 0; | |
999 | } | |
1000 | ||
db5723c6 JH |
1001 | static void set_option(struct transport *transport, const char *name, const char *value) |
1002 | { | |
1003 | int r = transport_set_option(transport, name, value); | |
1004 | if (r < 0) | |
1005 | die(_("Option \"%s\" value \"%s\" is not valid for %s"), | |
1006 | name, value, transport->url); | |
1007 | if (r > 0) | |
1008 | warning(_("Option \"%s\" is ignored for %s\n"), | |
1009 | name, transport->url); | |
1010 | } | |
1011 | ||
508ea882 | 1012 | static struct transport *prepare_transport(struct remote *remote, int deepen) |
db5723c6 JH |
1013 | { |
1014 | struct transport *transport; | |
1015 | transport = transport_get(remote, NULL); | |
1016 | transport_set_verbosity(transport, verbosity, progress); | |
c915f11e | 1017 | transport->family = family; |
db5723c6 JH |
1018 | if (upload_pack) |
1019 | set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack); | |
1020 | if (keep) | |
1021 | set_option(transport, TRANS_OPT_KEEP, "yes"); | |
1022 | if (depth) | |
1023 | set_option(transport, TRANS_OPT_DEPTH, depth); | |
508ea882 NTND |
1024 | if (deepen && deepen_since) |
1025 | set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since); | |
a45a2600 NTND |
1026 | if (deepen && deepen_not.nr) |
1027 | set_option(transport, TRANS_OPT_DEEPEN_NOT, | |
1028 | (const char *)&deepen_not); | |
cccf74e2 NTND |
1029 | if (deepen_relative) |
1030 | set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes"); | |
48d25cae NTND |
1031 | if (update_shallow) |
1032 | set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes"); | |
db5723c6 JH |
1033 | return transport; |
1034 | } | |
1035 | ||
069d5032 JH |
1036 | static void backfill_tags(struct transport *transport, struct ref *ref_map) |
1037 | { | |
508ea882 NTND |
1038 | int cannot_reuse; |
1039 | ||
1040 | /* | |
1041 | * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it | |
1042 | * when remote helper is used (setting it to an empty string | |
1043 | * is not unsetting). We could extend the remote helper | |
1044 | * protocol for that, but for now, just force a new connection | |
a45a2600 | 1045 | * without deepen-since. Similar story for deepen-not. |
508ea882 | 1046 | */ |
a45a2600 NTND |
1047 | cannot_reuse = transport->cannot_reuse || |
1048 | deepen_since || deepen_not.nr; | |
508ea882 NTND |
1049 | if (cannot_reuse) { |
1050 | gsecondary = prepare_transport(transport->remote, 0); | |
b26ed430 JH |
1051 | transport = gsecondary; |
1052 | } | |
1053 | ||
069d5032 JH |
1054 | transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL); |
1055 | transport_set_option(transport, TRANS_OPT_DEPTH, "0"); | |
cccf74e2 | 1056 | transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL); |
069d5032 | 1057 | fetch_refs(transport, ref_map); |
b26ed430 JH |
1058 | |
1059 | if (gsecondary) { | |
1060 | transport_disconnect(gsecondary); | |
1061 | gsecondary = NULL; | |
1062 | } | |
069d5032 JH |
1063 | } |
1064 | ||
b888d61c DB |
1065 | static int do_fetch(struct transport *transport, |
1066 | struct refspec *refs, int ref_count) | |
1067 | { | |
b87dbcc8 | 1068 | struct string_list existing_refs = STRING_LIST_INIT_DUP; |
7f98428d | 1069 | struct ref *ref_map; |
b888d61c DB |
1070 | struct ref *rm; |
1071 | int autotags = (transport->remote->fetch_tags == 1); | |
5b87d8d3 | 1072 | int retcode = 0; |
b1a01e1c | 1073 | |
0e0b7de4 | 1074 | for_each_ref(add_existing, &existing_refs); |
b1a01e1c | 1075 | |
ed368546 DJ |
1076 | if (tags == TAGS_DEFAULT) { |
1077 | if (transport->remote->fetch_tags == 2) | |
1078 | tags = TAGS_SET; | |
1079 | if (transport->remote->fetch_tags == -1) | |
1080 | tags = TAGS_UNSET; | |
1081 | } | |
b888d61c | 1082 | |
824d5776 | 1083 | if (!transport->get_refs_list || !transport->fetch) |
bd4a51fc | 1084 | die(_("Don't know how to fetch from %s"), transport->url); |
b888d61c DB |
1085 | |
1086 | /* if not appending, truncate FETCH_HEAD */ | |
28a15401 | 1087 | if (!append && !dry_run) { |
5b87d8d3 MH |
1088 | retcode = truncate_fetch_head(); |
1089 | if (retcode) | |
1090 | goto cleanup; | |
d6617c7c | 1091 | } |
b888d61c DB |
1092 | |
1093 | ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags); | |
8ee5d731 JS |
1094 | if (!update_head_ok) |
1095 | check_not_current_branch(ref_map); | |
b888d61c DB |
1096 | |
1097 | for (rm = ref_map; rm; rm = rm->next) { | |
b1a01e1c | 1098 | if (rm->peer_ref) { |
6f64a16f MH |
1099 | struct string_list_item *peer_item = |
1100 | string_list_lookup(&existing_refs, | |
1101 | rm->peer_ref->name); | |
0e0b7de4 MH |
1102 | if (peer_item) { |
1103 | struct object_id *old_oid = peer_item->util; | |
f4e54d02 | 1104 | oidcpy(&rm->peer_ref->old_oid, old_oid); |
0e0b7de4 | 1105 | } |
b1a01e1c | 1106 | } |
b888d61c DB |
1107 | } |
1108 | ||
41fa7d2e SP |
1109 | if (tags == TAGS_DEFAULT && autotags) |
1110 | transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1"); | |
ed43de6e | 1111 | if (prune) { |
0838bf47 MH |
1112 | /* |
1113 | * We only prune based on refspecs specified | |
1114 | * explicitly (via command line or configuration); we | |
1115 | * don't care whether --tags was specified. | |
1116 | */ | |
c5a84e92 | 1117 | if (ref_count) { |
4b3b33a7 | 1118 | prune_refs(refs, ref_count, ref_map, transport->url); |
e8c1e6c7 | 1119 | } else { |
0838bf47 MH |
1120 | prune_refs(transport->remote->fetch, |
1121 | transport->remote->fetch_refspec_nr, | |
4b3b33a7 TM |
1122 | ref_map, |
1123 | transport->url); | |
e8c1e6c7 | 1124 | } |
ed43de6e | 1125 | } |
10a6cc88 TM |
1126 | if (fetch_refs(transport, ref_map)) { |
1127 | free_refs(ref_map); | |
1128 | retcode = 1; | |
1129 | goto cleanup; | |
1130 | } | |
7f98428d | 1131 | free_refs(ref_map); |
b888d61c DB |
1132 | |
1133 | /* if neither --no-tags nor --tags was specified, do automated tag | |
1134 | * following ... */ | |
83201998 | 1135 | if (tags == TAGS_DEFAULT && autotags) { |
c50b2b47 SP |
1136 | struct ref **tail = &ref_map; |
1137 | ref_map = NULL; | |
1138 | find_non_local_tags(transport, &ref_map, &tail); | |
069d5032 JH |
1139 | if (ref_map) |
1140 | backfill_tags(transport, ref_map); | |
b888d61c DB |
1141 | free_refs(ref_map); |
1142 | } | |
1143 | ||
5b87d8d3 | 1144 | cleanup: |
f83918ed | 1145 | string_list_clear(&existing_refs, 1); |
5b87d8d3 | 1146 | return retcode; |
b888d61c DB |
1147 | } |
1148 | ||
9c4a036b BG |
1149 | static int get_one_remote_for_fetch(struct remote *remote, void *priv) |
1150 | { | |
1151 | struct string_list *list = priv; | |
7cc91a2f | 1152 | if (!remote->skip_default_update) |
1d2f80fa | 1153 | string_list_append(list, remote->name); |
9c4a036b BG |
1154 | return 0; |
1155 | } | |
1156 | ||
1157 | struct remote_group_data { | |
1158 | const char *name; | |
1159 | struct string_list *list; | |
1160 | }; | |
1161 | ||
1162 | static int get_remote_group(const char *key, const char *value, void *priv) | |
1163 | { | |
1164 | struct remote_group_data *g = priv; | |
1165 | ||
bc598c32 | 1166 | if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) { |
9c4a036b | 1167 | /* split list by white space */ |
9c4a036b | 1168 | while (*value) { |
5f65499f MH |
1169 | size_t wordlen = strcspn(value, " \t\n"); |
1170 | ||
e286542d | 1171 | if (wordlen >= 1) |
b7410f61 | 1172 | string_list_append_nodup(g->list, |
e286542d MH |
1173 | xstrndup(value, wordlen)); |
1174 | value += wordlen + (value[wordlen] != '\0'); | |
9c4a036b BG |
1175 | } |
1176 | } | |
1177 | ||
1178 | return 0; | |
1179 | } | |
1180 | ||
1181 | static int add_remote_or_group(const char *name, struct string_list *list) | |
1182 | { | |
1183 | int prev_nr = list->nr; | |
66dbfd55 GV |
1184 | struct remote_group_data g; |
1185 | g.name = name; g.list = list; | |
9c4a036b BG |
1186 | |
1187 | git_config(get_remote_group, &g); | |
1188 | if (list->nr == prev_nr) { | |
674468b3 | 1189 | struct remote *remote = remote_get(name); |
e459b073 | 1190 | if (!remote_is_configured(remote, 0)) |
9c4a036b | 1191 | return 0; |
1d2f80fa | 1192 | string_list_append(list, remote->name); |
9c4a036b BG |
1193 | } |
1194 | return 1; | |
1195 | } | |
1196 | ||
85556d4e | 1197 | static void add_options_to_argv(struct argv_array *argv) |
9c4a036b | 1198 | { |
28a15401 | 1199 | if (dry_run) |
85556d4e | 1200 | argv_array_push(argv, "--dry-run"); |
90765fa3 MH |
1201 | if (prune != -1) |
1202 | argv_array_push(argv, prune ? "--prune" : "--no-prune"); | |
bba5322a | 1203 | if (update_head_ok) |
85556d4e | 1204 | argv_array_push(argv, "--update-head-ok"); |
bba5322a | 1205 | if (force) |
85556d4e | 1206 | argv_array_push(argv, "--force"); |
bba5322a | 1207 | if (keep) |
85556d4e | 1208 | argv_array_push(argv, "--keep"); |
be254a0e | 1209 | if (recurse_submodules == RECURSE_SUBMODULES_ON) |
85556d4e | 1210 | argv_array_push(argv, "--recurse-submodules"); |
8f0700dd | 1211 | else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) |
85556d4e | 1212 | argv_array_push(argv, "--recurse-submodules=on-demand"); |
85566460 DJ |
1213 | if (tags == TAGS_SET) |
1214 | argv_array_push(argv, "--tags"); | |
1215 | else if (tags == TAGS_UNSET) | |
1216 | argv_array_push(argv, "--no-tags"); | |
9c4a036b | 1217 | if (verbosity >= 2) |
85556d4e | 1218 | argv_array_push(argv, "-v"); |
9c4a036b | 1219 | if (verbosity >= 1) |
85556d4e | 1220 | argv_array_push(argv, "-v"); |
9c4a036b | 1221 | else if (verbosity < 0) |
85556d4e | 1222 | argv_array_push(argv, "-q"); |
7dce19d3 JL |
1223 | |
1224 | } | |
1225 | ||
1226 | static int fetch_multiple(struct string_list *list) | |
1227 | { | |
1228 | int i, result = 0; | |
85556d4e | 1229 | struct argv_array argv = ARGV_ARRAY_INIT; |
9c4a036b | 1230 | |
e6cc5104 JH |
1231 | if (!append && !dry_run) { |
1232 | int errcode = truncate_fetch_head(); | |
1233 | if (errcode) | |
1234 | return errcode; | |
1235 | } | |
1236 | ||
85556d4e JK |
1237 | argv_array_pushl(&argv, "fetch", "--append", NULL); |
1238 | add_options_to_argv(&argv); | |
1239 | ||
9c4a036b BG |
1240 | for (i = 0; i < list->nr; i++) { |
1241 | const char *name = list->items[i].string; | |
85556d4e | 1242 | argv_array_push(&argv, name); |
9c4a036b | 1243 | if (verbosity >= 0) |
bd4a51fc | 1244 | printf(_("Fetching %s\n"), name); |
85556d4e | 1245 | if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) { |
bd4a51fc | 1246 | error(_("Could not fetch %s"), name); |
9c4a036b BG |
1247 | result = 1; |
1248 | } | |
85556d4e | 1249 | argv_array_pop(&argv); |
9c4a036b BG |
1250 | } |
1251 | ||
85556d4e | 1252 | argv_array_clear(&argv); |
9c4a036b BG |
1253 | return result; |
1254 | } | |
1255 | ||
1256 | static int fetch_one(struct remote *remote, int argc, const char **argv) | |
b888d61c | 1257 | { |
b888d61c | 1258 | static const char **refs = NULL; |
d8ead159 | 1259 | struct refspec *refspec; |
b888d61c | 1260 | int ref_nr = 0; |
7b7f39ea | 1261 | int exit_code; |
b888d61c | 1262 | |
fa685bdf | 1263 | if (!remote) |
bd4a51fc ÆAB |
1264 | die(_("No remote repository specified. Please, specify either a URL or a\n" |
1265 | "remote name from which new revisions should be fetched.")); | |
fa685bdf | 1266 | |
508ea882 | 1267 | gtransport = prepare_transport(remote, 1); |
737c5a9c MS |
1268 | |
1269 | if (prune < 0) { | |
1270 | /* no command line request */ | |
20419de9 JH |
1271 | if (0 <= gtransport->remote->prune) |
1272 | prune = gtransport->remote->prune; | |
737c5a9c MS |
1273 | else if (0 <= fetch_prune_config) |
1274 | prune = fetch_prune_config; | |
1275 | else | |
1276 | prune = PRUNE_BY_DEFAULT; | |
1277 | } | |
1278 | ||
9c4a036b | 1279 | if (argc > 0) { |
b888d61c | 1280 | int j = 0; |
bf7e645c | 1281 | int i; |
50a6c8ef | 1282 | refs = xcalloc(st_add(argc, 1), sizeof(const char *)); |
9c4a036b | 1283 | for (i = 0; i < argc; i++) { |
b888d61c | 1284 | if (!strcmp(argv[i], "tag")) { |
b888d61c | 1285 | i++; |
f53423b0 | 1286 | if (i >= argc) |
bd4a51fc | 1287 | die(_("You need to specify a tag name.")); |
b2724c87 JK |
1288 | refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s", |
1289 | argv[i], argv[i]); | |
b888d61c DB |
1290 | } else |
1291 | refs[j++] = argv[i]; | |
b888d61c DB |
1292 | } |
1293 | refs[j] = NULL; | |
1294 | ref_nr = j; | |
b888d61c DB |
1295 | } |
1296 | ||
57b235a4 | 1297 | sigchain_push_common(unlock_pack_on_signal); |
e4022ed2 | 1298 | atexit(unlock_pack); |
d8ead159 | 1299 | refspec = parse_fetch_refspec(ref_nr, refs); |
af234459 | 1300 | exit_code = do_fetch(gtransport, refspec, ref_nr); |
5caf1973 | 1301 | free_refspec(ref_nr, refspec); |
af234459 JH |
1302 | transport_disconnect(gtransport); |
1303 | gtransport = NULL; | |
7b7f39ea | 1304 | return exit_code; |
b888d61c | 1305 | } |
9c4a036b BG |
1306 | |
1307 | int cmd_fetch(int argc, const char **argv, const char *prefix) | |
1308 | { | |
1309 | int i; | |
b7410f61 | 1310 | struct string_list list = STRING_LIST_INIT_DUP; |
9c4a036b BG |
1311 | struct remote *remote; |
1312 | int result = 0; | |
1991006c | 1313 | struct argv_array argv_gc_auto = ARGV_ARRAY_INIT; |
9c4a036b | 1314 | |
bbc30f99 JK |
1315 | packet_trace_identity("fetch"); |
1316 | ||
9c4a036b BG |
1317 | /* Record the command line for the reflog */ |
1318 | strbuf_addstr(&default_rla, "fetch"); | |
1319 | for (i = 1; i < argc; i++) | |
1320 | strbuf_addf(&default_rla, " %s", argv[i]); | |
1321 | ||
737c5a9c MS |
1322 | git_config(git_fetch_config, NULL); |
1323 | ||
9c4a036b BG |
1324 | argc = parse_options(argc, argv, prefix, |
1325 | builtin_fetch_options, builtin_fetch_usage, 0); | |
1326 | ||
cccf74e2 NTND |
1327 | if (deepen_relative) { |
1328 | if (deepen_relative < 0) | |
1329 | die(_("Negative depth in --deepen is not supported")); | |
1330 | if (depth) | |
1331 | die(_("--deepen and --depth are mutually exclusive")); | |
1332 | depth = xstrfmt("%d", deepen_relative); | |
1333 | } | |
4dcb167f NTND |
1334 | if (unshallow) { |
1335 | if (depth) | |
1336 | die(_("--depth and --unshallow cannot be used together")); | |
1337 | else if (!is_repository_shallow()) | |
1338 | die(_("--unshallow on a complete repository does not make sense")); | |
2805bb59 JK |
1339 | else |
1340 | depth = xstrfmt("%d", INFINITE_DEPTH); | |
4dcb167f NTND |
1341 | } |
1342 | ||
5594bcad NTND |
1343 | /* no need to be strict, transport_set_option() will validate it again */ |
1344 | if (depth && atoi(depth) < 1) | |
1345 | die(_("depth %s is not a positive number"), depth); | |
a45a2600 | 1346 | if (depth || deepen_since || deepen_not.nr) |
508ea882 | 1347 | deepen = 1; |
5594bcad | 1348 | |
18322bad JL |
1349 | if (recurse_submodules != RECURSE_SUBMODULES_OFF) { |
1350 | if (recurse_submodules_default) { | |
1351 | int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default); | |
1352 | set_config_fetch_recurse_submodules(arg); | |
1353 | } | |
1354 | gitmodules_config(); | |
1355 | git_config(submodule_config, NULL); | |
1356 | } | |
1357 | ||
9c4a036b BG |
1358 | if (all) { |
1359 | if (argc == 1) | |
bd4a51fc | 1360 | die(_("fetch --all does not take a repository argument")); |
9c4a036b | 1361 | else if (argc > 1) |
bd4a51fc | 1362 | die(_("fetch --all does not make sense with refspecs")); |
9c4a036b BG |
1363 | (void) for_each_remote(get_one_remote_for_fetch, &list); |
1364 | result = fetch_multiple(&list); | |
1365 | } else if (argc == 0) { | |
1366 | /* No arguments -- use default remote */ | |
1367 | remote = remote_get(NULL); | |
1368 | result = fetch_one(remote, argc, argv); | |
16679e37 BG |
1369 | } else if (multiple) { |
1370 | /* All arguments are assumed to be remotes or groups */ | |
1371 | for (i = 0; i < argc; i++) | |
1372 | if (!add_remote_or_group(argv[i], &list)) | |
bd4a51fc | 1373 | die(_("No such remote or remote group: %s"), argv[i]); |
16679e37 | 1374 | result = fetch_multiple(&list); |
9c4a036b BG |
1375 | } else { |
1376 | /* Single remote or group */ | |
1377 | (void) add_remote_or_group(argv[0], &list); | |
1378 | if (list.nr > 1) { | |
1379 | /* More than one remote */ | |
1380 | if (argc > 1) | |
bd4a51fc | 1381 | die(_("Fetching a group and specifying refspecs does not make sense")); |
9c4a036b BG |
1382 | result = fetch_multiple(&list); |
1383 | } else { | |
1384 | /* Zero or one remotes */ | |
1385 | remote = remote_get(argv[0]); | |
1386 | result = fetch_one(remote, argc-1, argv+1); | |
1387 | } | |
1388 | } | |
1389 | ||
be254a0e | 1390 | if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) { |
85556d4e JK |
1391 | struct argv_array options = ARGV_ARRAY_INIT; |
1392 | ||
1393 | add_options_to_argv(&options); | |
50d89ad6 | 1394 | result = fetch_populated_submodules(&options, |
7dce19d3 | 1395 | submodule_prefix, |
8f0700dd | 1396 | recurse_submodules, |
62104ba1 SB |
1397 | verbosity < 0, |
1398 | max_children); | |
85556d4e | 1399 | argv_array_clear(&options); |
7dce19d3 JL |
1400 | } |
1401 | ||
9c4a036b BG |
1402 | string_list_clear(&list, 0); |
1403 | ||
0898c962 JS |
1404 | close_all_packs(); |
1405 | ||
1991006c | 1406 | argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL); |
6fceed3b NTND |
1407 | if (verbosity < 0) |
1408 | argv_array_push(&argv_gc_auto, "--quiet"); | |
1991006c NTND |
1409 | run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD); |
1410 | argv_array_clear(&argv_gc_auto); | |
131b8fcb | 1411 | |
9c4a036b BG |
1412 | return result; |
1413 | } |