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