33b740cd105d03fca9d63b5a68c2ebec3977114f
1/*
2 * "git fetch"
3 */
4#include "cache.h"
5#include "refs.h"
6#include "commit.h"
7#include "builtin.h"
8#include "path-list.h"
9#include "remote.h"
10#include "transport.h"
11
12static const char fetch_usage[] = "git-fetch [-a | --append] [--upload-pack <upload-pack>] [-f | --force] [--no-tags] [-t | --tags] [-k | --keep] [-u | --update-head-ok] [--depth <depth>] [-v | --verbose] [<repository> <refspec>...]";
13
14static int append, force, tags, no_tags, update_head_ok, verbose, quiet;
15
16static int unpacklimit;
17
18static char *default_rla = NULL;
19
20static void find_merge_config(struct ref *ref_map, struct remote *remote)
21{
22 struct ref *rm = ref_map;
23 struct branch *branch = branch_get(NULL);
24
25 for (rm = ref_map; rm; rm = rm->next) {
26 if (!branch_has_merge_config(branch)) {
27 if (remote && remote->fetch &&
28 !strcmp(remote->fetch[0].src, rm->name))
29 rm->merge = 1;
30 } else {
31 if (branch_merges(branch, rm->name))
32 rm->merge = 1;
33 }
34 }
35}
36
37static struct ref *get_ref_map(struct transport *transport,
38 struct refspec *refs, int ref_count, int tags,
39 int *autotags)
40{
41 int i;
42 struct ref *rm;
43 struct ref *ref_map = NULL;
44 struct ref **tail = &ref_map;
45
46 struct ref *remote_refs = transport_get_remote_refs(transport);
47
48 if (ref_count || tags) {
49 for (i = 0; i < ref_count; i++) {
50 get_fetch_map(remote_refs, &refs[i], &tail);
51 if (refs[i].dst && refs[i].dst[0])
52 *autotags = 1;
53 }
54 /* Merge everything on the command line, but not --tags */
55 for (rm = ref_map; rm; rm = rm->next)
56 rm->merge = 1;
57 if (tags) {
58 struct refspec refspec;
59 refspec.src = "refs/tags/";
60 refspec.dst = "refs/tags/";
61 refspec.pattern = 1;
62 refspec.force = 0;
63 get_fetch_map(remote_refs, &refspec, &tail);
64 }
65 } else {
66 /* Use the defaults */
67 struct remote *remote = transport->remote;
68 if (remote->fetch_refspec_nr) {
69 for (i = 0; i < remote->fetch_refspec_nr; i++) {
70 get_fetch_map(remote_refs, &remote->fetch[i], &tail);
71 if (remote->fetch[i].dst &&
72 remote->fetch[i].dst[0])
73 *autotags = 1;
74 }
75 find_merge_config(ref_map, remote);
76 } else {
77 ref_map = get_remote_ref(remote_refs, "HEAD");
78
79 ref_map->merge = 1;
80 }
81 }
82
83 return ref_map;
84}
85
86static void show_new(enum object_type type, unsigned char *sha1_new)
87{
88 fprintf(stderr, " %s: %s\n", typename(type),
89 find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
90}
91
92static int s_update_ref(const char *action,
93 struct ref *ref,
94 int check_old)
95{
96 char msg[1024];
97 char *rla = getenv("GIT_REFLOG_ACTION");
98 static struct ref_lock *lock;
99
100 if (!rla)
101 rla = default_rla;
102 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
103 lock = lock_any_ref_for_update(ref->name,
104 check_old ? ref->old_sha1 : NULL, 0);
105 if (!lock)
106 return 1;
107 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
108 return 1;
109 return 0;
110}
111
112static int update_local_ref(struct ref *ref,
113 const char *note,
114 int verbose)
115{
116 char oldh[41], newh[41];
117 struct commit *current = NULL, *updated;
118 enum object_type type;
119 struct branch *current_branch = branch_get(NULL);
120
121 type = sha1_object_info(ref->new_sha1, NULL);
122 if (type < 0)
123 die("object %s not found", sha1_to_hex(ref->new_sha1));
124
125 if (!*ref->name) {
126 /* Not storing */
127 if (verbose) {
128 fprintf(stderr, "* fetched %s\n", note);
129 show_new(type, ref->new_sha1);
130 }
131 return 0;
132 }
133
134 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
135 if (verbose) {
136 fprintf(stderr, "* %s: same as %s\n",
137 ref->name, note);
138 show_new(type, ref->new_sha1);
139 }
140 return 0;
141 }
142
143 if (!strcmp(ref->name, current_branch->name) &&
144 !(update_head_ok || is_bare_repository()) &&
145 !is_null_sha1(ref->old_sha1)) {
146 /*
147 * If this is the head, and it's not okay to update
148 * the head, and the old value of the head isn't empty...
149 */
150 fprintf(stderr,
151 " * %s: Cannot fetch into the current branch.\n",
152 ref->name);
153 return 1;
154 }
155
156 if (!is_null_sha1(ref->old_sha1) &&
157 !prefixcmp(ref->name, "refs/tags/")) {
158 fprintf(stderr, "* %s: updating with %s\n",
159 ref->name, note);
160 show_new(type, ref->new_sha1);
161 return s_update_ref("updating tag", ref, 0);
162 }
163
164 current = lookup_commit_reference(ref->old_sha1);
165 updated = lookup_commit_reference(ref->new_sha1);
166 if (!current || !updated) {
167 char *msg;
168 if (!strncmp(ref->name, "refs/tags/", 10))
169 msg = "storing tag";
170 else
171 msg = "storing head";
172 fprintf(stderr, "* %s: storing %s\n",
173 ref->name, note);
174 show_new(type, ref->new_sha1);
175 return s_update_ref(msg, ref, 0);
176 }
177
178 strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
179 strcpy(newh, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
180
181 if (in_merge_bases(current, &updated, 1)) {
182 fprintf(stderr, "* %s: fast forward to %s\n",
183 ref->name, note);
184 fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
185 return s_update_ref("fast forward", ref, 1);
186 }
187 if (!force && !ref->force) {
188 fprintf(stderr,
189 "* %s: not updating to non-fast forward %s\n",
190 ref->name, note);
191 fprintf(stderr,
192 " old...new: %s...%s\n", oldh, newh);
193 return 1;
194 }
195 fprintf(stderr,
196 "* %s: forcing update to non-fast forward %s\n",
197 ref->name, note);
198 fprintf(stderr, " old...new: %s...%s\n", oldh, newh);
199 return s_update_ref("forced-update", ref, 1);
200}
201
202static void store_updated_refs(const char *url, struct ref *ref_map)
203{
204 FILE *fp;
205 struct commit *commit;
206 int url_len, i, note_len;
207 char note[1024];
208 const char *what, *kind;
209 struct ref *rm;
210
211 fp = fopen(git_path("FETCH_HEAD"), "a");
212 for (rm = ref_map; rm; rm = rm->next) {
213 struct ref *ref = NULL;
214
215 if (rm->peer_ref) {
216 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
217 strcpy(ref->name, rm->peer_ref->name);
218 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
219 hashcpy(ref->new_sha1, rm->old_sha1);
220 ref->force = rm->peer_ref->force;
221 }
222
223 commit = lookup_commit_reference(rm->old_sha1);
224 if (!commit)
225 rm->merge = 0;
226
227 if (!strcmp(rm->name, "HEAD")) {
228 kind = "";
229 what = "";
230 }
231 else if (!prefixcmp(rm->name, "refs/heads/")) {
232 kind = "branch";
233 what = rm->name + 11;
234 }
235 else if (!prefixcmp(rm->name, "refs/tags/")) {
236 kind = "tag";
237 what = rm->name + 10;
238 }
239 else if (!prefixcmp(rm->name, "refs/remotes/")) {
240 kind = "remote branch";
241 what = rm->name + 13;
242 }
243 else {
244 kind = "";
245 what = rm->name;
246 }
247
248 url_len = strlen(url);
249 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
250 ;
251 url_len = i + 1;
252 if (4 < i && !strncmp(".git", url + i - 3, 4))
253 url_len = i - 3;
254
255 note_len = 0;
256 if (*what) {
257 if (*kind)
258 note_len += sprintf(note + note_len, "%s ",
259 kind);
260 note_len += sprintf(note + note_len, "'%s' of ", what);
261 }
262 note_len += sprintf(note + note_len, "%.*s", url_len, url);
263 fprintf(fp, "%s\t%s\t%s\n",
264 sha1_to_hex(commit ? commit->object.sha1 :
265 rm->old_sha1),
266 rm->merge ? "" : "not-for-merge",
267 note);
268
269 if (ref)
270 update_local_ref(ref, note, verbose);
271 }
272 fclose(fp);
273}
274
275static int fetch_refs(struct transport *transport, struct ref *ref_map)
276{
277 int ret = transport_fetch_refs(transport, ref_map);
278 if (!ret)
279 store_updated_refs(transport->url, ref_map);
280 return ret;
281}
282
283static int add_existing(const char *refname, const unsigned char *sha1,
284 int flag, void *cbdata)
285{
286 struct path_list *list = (struct path_list *)cbdata;
287 path_list_insert(refname, list);
288 return 0;
289}
290
291static struct ref *find_non_local_tags(struct transport *transport,
292 struct ref *fetch_map)
293{
294 static struct path_list existing_refs = { NULL, 0, 0, 0 };
295 struct path_list new_refs = { NULL, 0, 0, 1 };
296 char *ref_name;
297 int ref_name_len;
298 unsigned char *ref_sha1;
299 struct ref *tag_ref;
300 struct ref *rm = NULL;
301 struct ref *ref_map = NULL;
302 struct ref **tail = &ref_map;
303 struct ref *ref;
304
305 for_each_ref(add_existing, &existing_refs);
306 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
307 if (prefixcmp(ref->name, "refs/tags"))
308 continue;
309
310 ref_name = xstrdup(ref->name);
311 ref_name_len = strlen(ref_name);
312 ref_sha1 = ref->old_sha1;
313
314 if (!strcmp(ref_name + ref_name_len - 3, "^{}")) {
315 ref_name[ref_name_len - 3] = 0;
316 tag_ref = transport_get_remote_refs(transport);
317 while (tag_ref) {
318 if (!strcmp(tag_ref->name, ref_name)) {
319 ref_sha1 = tag_ref->old_sha1;
320 break;
321 }
322 tag_ref = tag_ref->next;
323 }
324 }
325
326 if (!path_list_has_path(&existing_refs, ref_name) &&
327 !path_list_has_path(&new_refs, ref_name) &&
328 lookup_object(ref->old_sha1)) {
329 fprintf(stderr, "Auto-following %s\n",
330 ref_name);
331
332 path_list_insert(ref_name, &new_refs);
333
334 rm = alloc_ref(strlen(ref_name) + 1);
335 strcpy(rm->name, ref_name);
336 rm->peer_ref = alloc_ref(strlen(ref_name) + 1);
337 strcpy(rm->peer_ref->name, ref_name);
338 hashcpy(rm->old_sha1, ref_sha1);
339
340 *tail = rm;
341 tail = &rm->next;
342 }
343 free(ref_name);
344 }
345
346 return ref_map;
347}
348
349static int do_fetch(struct transport *transport,
350 struct refspec *refs, int ref_count)
351{
352 struct ref *ref_map, *fetch_map;
353 struct ref *rm;
354 int autotags = (transport->remote->fetch_tags == 1);
355 if (transport->remote->fetch_tags == 2 && !no_tags)
356 tags = 1;
357 if (transport->remote->fetch_tags == -1)
358 no_tags = 1;
359
360 if (!transport->ops || !transport->ops->get_refs_list ||
361 !(transport->ops->fetch_refs || transport->ops->fetch_objs))
362 die("Don't know how to fetch from %s", transport->url);
363
364 /* if not appending, truncate FETCH_HEAD */
365 if (!append)
366 fclose(fopen(git_path("FETCH_HEAD"), "w"));
367
368 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
369
370 for (rm = ref_map; rm; rm = rm->next) {
371 if (rm->peer_ref)
372 read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
373 }
374
375 if (fetch_refs(transport, ref_map)) {
376 free_refs(ref_map);
377 return 1;
378 }
379
380 fetch_map = ref_map;
381
382 /* if neither --no-tags nor --tags was specified, do automated tag
383 * following ... */
384 if (!(tags || no_tags) && autotags) {
385 ref_map = find_non_local_tags(transport, fetch_map);
386 if (ref_map) {
387 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
388 fetch_refs(transport, ref_map);
389 }
390 free_refs(ref_map);
391 }
392
393 free_refs(fetch_map);
394
395 return 0;
396}
397
398static int fetch_config(const char *var, const char *value)
399{
400 if (strcmp(var, "fetch.unpacklimit") == 0) {
401 unpacklimit = git_config_int(var, value);
402 return 0;
403 }
404
405 if (strcmp(var, "transfer.unpacklimit") == 0) {
406 unpacklimit = git_config_int(var, value);
407 return 0;
408 }
409
410 return git_default_config(var, value);
411}
412
413int cmd_fetch(int argc, const char **argv, const char *prefix)
414{
415 struct remote *remote;
416 struct transport *transport;
417 int i, j, rla_offset;
418 static const char **refs = NULL;
419 int ref_nr = 0;
420 int cmd_len = 0;
421 const char *depth = NULL, *upload_pack = NULL;
422 int keep = 0;
423
424 git_config(fetch_config);
425
426 for (i = 1; i < argc; i++) {
427 const char *arg = argv[i];
428 cmd_len += strlen(arg);
429
430 if (arg[0] != '-')
431 break;
432 if (!strcmp(arg, "--append") || !strcmp(arg, "-a")) {
433 append = 1;
434 continue;
435 }
436 if (!prefixcmp(arg, "--upload-pack=")) {
437 upload_pack = arg + 14;
438 continue;
439 }
440 if (!strcmp(arg, "--upload-pack")) {
441 i++;
442 if (i == argc)
443 usage(fetch_usage);
444 upload_pack = argv[i];
445 continue;
446 }
447 if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
448 force = 1;
449 continue;
450 }
451 if (!strcmp(arg, "--no-tags")) {
452 no_tags = 1;
453 continue;
454 }
455 if (!strcmp(arg, "--tags") || !strcmp(arg, "-t")) {
456 tags = 1;
457 continue;
458 }
459 if (!strcmp(arg, "--keep") || !strcmp(arg, "-k")) {
460 keep = 1;
461 continue;
462 }
463 if (!strcmp(arg, "--update-head-ok") || !strcmp(arg, "-u")) {
464 update_head_ok = 1;
465 continue;
466 }
467 if (!prefixcmp(arg, "--depth=")) {
468 depth = arg + 8;
469 continue;
470 }
471 if (!strcmp(arg, "--depth")) {
472 i++;
473 if (i == argc)
474 usage(fetch_usage);
475 depth = argv[i];
476 continue;
477 }
478 if (!strcmp(arg, "--quiet")) {
479 quiet = 1;
480 continue;
481 }
482 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
483 verbose++;
484 continue;
485 }
486 usage(fetch_usage);
487 }
488
489 for (j = i; j < argc; j++)
490 cmd_len += strlen(argv[j]);
491
492 default_rla = xmalloc(cmd_len + 5 + argc + 1);
493 sprintf(default_rla, "fetch");
494 rla_offset = strlen(default_rla);
495 for (j = 1; j < argc; j++) {
496 sprintf(default_rla + rla_offset, " %s", argv[j]);
497 rla_offset += strlen(argv[j]) + 1;
498 }
499
500 if (i == argc)
501 remote = remote_get(NULL);
502 else
503 remote = remote_get(argv[i++]);
504
505 transport = transport_get(remote, remote->uri[0], 1);
506 if (verbose >= 2)
507 transport->verbose = 1;
508 if (quiet)
509 transport->verbose = 0;
510 if (upload_pack)
511 transport_set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
512 if (keep)
513 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
514 transport_set_option(transport, TRANS_OPT_DEPTH, depth);
515
516 if (!transport->url)
517 die("Where do you want to fetch from today?");
518
519 if (i < argc) {
520 int j = 0;
521 refs = xcalloc(argc - i + 1, sizeof(const char *));
522 while (i < argc) {
523 if (!strcmp(argv[i], "tag")) {
524 char *ref;
525 i++;
526 ref = xmalloc(strlen(argv[i]) * 2 + 22);
527 strcpy(ref, "refs/tags/");
528 strcat(ref, argv[i]);
529 strcat(ref, ":refs/tags/");
530 strcat(ref, argv[i]);
531 refs[j++] = ref;
532 } else
533 refs[j++] = argv[i];
534 i++;
535 }
536 refs[j] = NULL;
537 ref_nr = j;
538 for (j = 0; refs[j]; j++)
539 printf("ref: %s\n", refs[j]);
540 }
541
542 return do_fetch(transport, parse_ref_spec(ref_nr, refs), ref_nr);
543}