at commit f0eb23a8

1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "commit-graph.h"
4 #include "config.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "hashmap.h"
8 #include "hex.h"
9 #include "lockfile.h"
10 #include "loose.h"
11 #include "midx.h"
12 #include "object-file-convert.h"
13 #include "object-file.h"
14 #include "object-name.h"
15 #include "odb.h"
16 #include "odb/source-inmemory.h"
17 #include "path.h"
18 #include "promisor-remote.h"
19 #include "quote.h"
20 #include "replace-object.h"
21 #include "run-command.h"
22 #include "setup.h"
23 #include "strbuf.h"
24 #include "strvec.h"
25 #include "submodule.h"
26 #include "tmp-objdir.h"
27 #include "trace2.h"
28 #include "write-or-die.h"
29
30 /*
31 * NEEDSWORK: we're using "core.ignoreCase" to deduplicate alternates that
32 * _may_ be the same. This requires quite a bit of boilerplate for dubious
33 * benefit:
34 *
35 * - Duplicating alternates should really only lead to regressed performance.
36 *
37 * - We don't properly resolve symlinks or mointpoints, so we may still end
38 * up duplicating alternates.
39 *
40 * - The value may be lying, in which case we might deduplicate alternates
41 * that are in fact not mapping to the same directory.
42 *
43 * We should investigate whether we can remove this whole mechanism outright.
44 */
45 static int odb_source_paths_cmp(struct object_database *o,
46 const char *a, const char *b)
47 {
48 if (o->source_paths_icase < 0) {
49 int icase = 0;
50 repo_config_get_bool(o->repo, "core.ignorecase", &icase);
51 o->source_paths_icase = icase;
52 }
53
54 return o->source_paths_icase ? strcasecmp(a, b) : strcmp(a, b);
55 }
56
57 static int odb_source_by_path_cmp(const void *cb_data,
58 const struct hashmap_entry *entry,
59 const struct hashmap_entry *entry_or_key,
60 const void *keydata)
61 {
62 struct object_database *o = (struct object_database *)cb_data;
63 const struct odb_source *source = container_of(entry, const struct odb_source, by_path_entry);
64 const char *path = keydata;
65
66 if (!path)
67 path = container_of(entry_or_key, const struct odb_source, by_path_entry)->path;
68
69 return odb_source_paths_cmp(o, source->path, path);
70 }
71
72 int odb_mkstemp(struct object_database *odb,
73 struct strbuf *temp_filename, const char *pattern)
74 {
75 int fd;
76 /*
77 * we let the umask do its job, don't try to be more
78 * restrictive except to remove write permission.
79 */
80 int mode = 0444;
81 repo_git_path_replace(odb->repo, temp_filename, "objects/%s", pattern);
82 fd = git_mkstemp_mode(temp_filename->buf, mode);
83 if (0 <= fd)
84 return fd;
85
86 /* slow path */
87 /* some mkstemp implementations erase temp_filename on failure */
88 repo_git_path_replace(odb->repo, temp_filename, "objects/%s", pattern);
89 safe_create_leading_directories(odb->repo, temp_filename->buf);
90 return xmkstemp_mode(temp_filename->buf, mode);
91 }
92
93 /*
94 * Return non-zero iff the path is usable as an alternate object database.
95 */
96 static bool odb_is_source_usable(struct object_database *o, const char *path)
97 {
98 struct strbuf normalized_objdir = STRBUF_INIT;
99 struct hashmap_entry key;
100 bool usable = false;
101
102 strbuf_realpath(&normalized_objdir, o->sources->path, 1);
103
104 /* Detect cases where alternate disappeared */
105 if (!is_directory(path)) {
106 error(_("object directory %s does not exist; "
107 "check .git/objects/info/alternates"),
108 path);
109 goto out;
110 }
111
112 /*
113 * Prevent the common mistake of listing the same
114 * thing twice, or object directory itself.
115 */
116 if (!hashmap_get_size(&o->source_by_path)) {
117 assert(!o->sources->next);
118 hashmap_entry_init(&o->sources->by_path_entry,
119 strihash(o->sources->path));
120 hashmap_add(&o->source_by_path, &o->sources->by_path_entry);
121 }
122
123 if (!odb_source_paths_cmp(o, path, normalized_objdir.buf))
124 goto out;
125
126 hashmap_entry_init(&key, strihash(path));
127 if (hashmap_get(&o->source_by_path, &key, path))
128 goto out;
129
130 usable = true;
131
132 out:
133 strbuf_release(&normalized_objdir);
134 return usable;
135 }
136
137 void parse_alternates(const char *string,
138 int sep,
139 const char *relative_base,
140 struct strvec *out)
141 {
142 struct strbuf pathbuf = STRBUF_INIT;
143 struct strbuf buf = STRBUF_INIT;
144
145 if (!string || !*string)
146 return;
147
148 while (*string) {
149 const char *end;
150
151 strbuf_reset(&buf);
152 strbuf_reset(&pathbuf);
153
154 if (*string == '#') {
155 /* comment; consume up to next separator */
156 end = strchrnul(string, sep);
157 } else if (*string == '"' && !unquote_c_style(&buf, string, &end)) {
158 /*
159 * quoted path; unquote_c_style has copied the
160 * data for us and set "end". Broken quoting (e.g.,
161 * an entry that doesn't end with a quote) falls
162 * back to the unquoted case below.
163 */
164 } else {
165 /* normal, unquoted path */
166 end = strchrnul(string, sep);
167 strbuf_add(&buf, string, end - string);
168 }
169
170 if (*end)
171 end++;
172 string = end;
173
174 if (!buf.len)
175 continue;
176
177 if (!is_absolute_path(buf.buf) && relative_base) {
178 strbuf_realpath(&pathbuf, relative_base, 1);
179 strbuf_addch(&pathbuf, '/');
180 }
181 strbuf_addbuf(&pathbuf, &buf);
182
183 strbuf_reset(&buf);
184 if (!strbuf_realpath(&buf, pathbuf.buf, 0)) {
185 error(_("unable to normalize alternate object path: %s"),
186 pathbuf.buf);
187 continue;
188 }
189
190 /*
191 * The trailing slash after the directory name is given by
192 * this function at the end. Remove duplicates.
193 */
194 while (buf.len && buf.buf[buf.len - 1] == '/')
195 strbuf_setlen(&buf, buf.len - 1);
196
197 strvec_push(out, buf.buf);
198 }
199
200 strbuf_release(&pathbuf);
201 strbuf_release(&buf);
202 }
203
204 static struct odb_source *odb_add_alternate_recursively(struct object_database *odb,
205 const char *source,
206 int depth)
207 {
208 struct odb_source *alternate = NULL;
209 struct strvec sources = STRVEC_INIT;
210
211 if (!odb_is_source_usable(odb, source))
212 goto error;
213
214 alternate = odb_source_new(odb, source, false);
215
216 /* add the alternate entry */
217 *odb->sources_tail = alternate;
218 odb->sources_tail = &(alternate->next);
219
220 hashmap_entry_init(&alternate->by_path_entry, strihash(alternate->path));
221 if (hashmap_get(&odb->source_by_path, &alternate->by_path_entry,
222 alternate->path))
223 BUG("source must not yet exist");
224 hashmap_add(&odb->source_by_path, &alternate->by_path_entry);
225
226 /* recursively add alternates */
227 odb_source_read_alternates(alternate, &sources);
228 if (sources.nr && depth + 1 > 5) {
229 error(_("%s: ignoring alternate object stores, nesting too deep"),
230 source);
231 } else {
232 for (size_t i = 0; i < sources.nr; i++)
233 odb_add_alternate_recursively(odb, sources.v[i], depth + 1);
234 }
235
236 error:
237 strvec_clear(&sources);
238 return alternate;
239 }
240
241 void odb_add_to_alternates_file(struct object_database *odb,
242 const char *dir)
243 {
244 int ret = odb_source_write_alternate(odb->sources, dir);
245 if (ret < 0)
246 die(NULL);
247 odb_add_alternate_recursively(odb, dir, 0);
248 }
249
250 struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
251 const char *dir, int will_destroy,
252 struct odb_source **prev_source)
253 {
254 struct odb_source *source;
255
256 /*
257 * Make a new primary odb and link the old primary ODB in as an
258 * alternate
259 */
260 source = odb_source_new(odb, dir, false);
261
262 /*
263 * Disable ref updates while a temporary odb is active, since
264 * the objects in the database may roll back.
265 */
266 odb->repo->disable_ref_updates = true;
267 source->will_destroy = will_destroy;
268 source->next = odb->sources;
269 odb->sources = source;
270
271 if (prev_source)
272 *prev_source = source->next;
273
274 return source;
275 }
276
277 void odb_restore_primary_source(struct object_database *odb,
278 struct odb_source *restore_source,
279 const char *old_path)
280 {
281 struct odb_source *cur_source = odb->sources;
282
283 if (strcmp(old_path, cur_source->path))
284 BUG("expected %s as primary object store; found %s",
285 old_path, cur_source->path);
286
287 if (cur_source->next != restore_source)
288 BUG("we expect the old primary object store to be the first alternate");
289
290 odb->repo->disable_ref_updates = false;
291 odb->sources = restore_source;
292 odb_source_free(cur_source);
293 }
294
295 char *compute_alternate_path(const char *path, struct strbuf *err)
296 {
297 char *ref_git = NULL;
298 const char *repo;
299 int seen_error = 0;
300
301 ref_git = real_pathdup(path, 0);
302 if (!ref_git) {
303 seen_error = 1;
304 strbuf_addf(err, _("path '%s' does not exist"), path);
305 goto out;
306 }
307
308 repo = read_gitfile(ref_git);
309 if (!repo)
310 repo = read_gitfile(mkpath("%s/.git", ref_git));
311 if (repo) {
312 free(ref_git);
313 ref_git = xstrdup(repo);
314 }
315
316 if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) {
317 char *ref_git_git = mkpathdup("%s/.git", ref_git);
318 free(ref_git);
319 ref_git = ref_git_git;
320 } else if (!is_directory(mkpath("%s/objects", ref_git))) {
321 struct strbuf sb = STRBUF_INIT;
322 seen_error = 1;
323 if (get_common_dir(&sb, ref_git)) {
324 strbuf_addf(err,
325 _("reference repository '%s' as a linked "
326 "checkout is not supported yet."),
327 path);
328 goto out;
329 }
330
331 strbuf_addf(err, _("reference repository '%s' is not a "
332 "local repository."), path);
333 goto out;
334 }
335
336 if (!access(mkpath("%s/shallow", ref_git), F_OK)) {
337 strbuf_addf(err, _("reference repository '%s' is shallow"),
338 path);
339 seen_error = 1;
340 goto out;
341 }
342
343 if (!access(mkpath("%s/info/grafts", ref_git), F_OK)) {
344 strbuf_addf(err,
345 _("reference repository '%s' is grafted"),
346 path);
347 seen_error = 1;
348 goto out;
349 }
350
351 out:
352 if (seen_error) {
353 FREE_AND_NULL(ref_git);
354 }
355
356 return ref_git;
357 }
358
359 struct odb_source *odb_find_source(struct object_database *odb, const char *obj_dir)
360 {
361 struct odb_source *source;
362 char *obj_dir_real = real_pathdup(obj_dir, 1);
363 struct strbuf odb_path_real = STRBUF_INIT;
364
365 for (source = odb->sources; source; source = source->next) {
366 strbuf_realpath(&odb_path_real, source->path, 1);
367 if (!strcmp(obj_dir_real, odb_path_real.buf))
368 break;
369 }
370
371 free(obj_dir_real);
372 strbuf_release(&odb_path_real);
373
374 return source;
375 }
376
377 struct odb_source *odb_find_source_or_die(struct object_database *odb, const char *obj_dir)
378 {
379 struct odb_source *source = odb_find_source(odb, obj_dir);
380 if (!source)
381 die(_("could not find object directory matching %s"), obj_dir);
382 return source;
383 }
384
385 static void fill_alternate_refs_command(struct repository *repo,
386 struct child_process *cmd,
387 const char *repo_path)
388 {
389 const char *value;
390
391 if (!repo_config_get_value(repo, "core.alternateRefsCommand", &value)) {
392 cmd->use_shell = 1;
393
394 strvec_push(&cmd->args, value);
395 strvec_push(&cmd->args, repo_path);
396 } else {
397 cmd->git_cmd = 1;
398
399 strvec_pushf(&cmd->args, "--git-dir=%s", repo_path);
400 strvec_push(&cmd->args, "for-each-ref");
401 strvec_push(&cmd->args, "--format=%(objectname)");
402
403 if (!repo_config_get_value(repo, "core.alternateRefsPrefixes", &value)) {
404 strvec_push(&cmd->args, "--");
405 strvec_split(&cmd->args, value);
406 }
407 }
408
409 strvec_pushv(&cmd->env, (const char **)local_repo_env);
410 cmd->out = -1;
411 }
412
413 static void read_alternate_refs(struct repository *repo,
414 const char *path,
415 odb_for_each_alternate_ref_fn *cb,
416 void *payload)
417 {
418 struct child_process cmd = CHILD_PROCESS_INIT;
419 struct strbuf line = STRBUF_INIT;
420 FILE *fh;
421
422 fill_alternate_refs_command(repo, &cmd, path);
423
424 if (start_command(&cmd))
425 return;
426
427 fh = xfdopen(cmd.out, "r");
428 while (strbuf_getline_lf(&line, fh) != EOF) {
429 struct object_id oid;
430 const char *p;
431
432 if (parse_oid_hex_algop(line.buf, &oid, &p, repo->hash_algo) || *p) {
433 warning(_("invalid line while parsing alternate refs: %s"),
434 line.buf);
435 break;
436 }
437
438 cb(&oid, payload);
439 }
440
441 fclose(fh);
442 finish_command(&cmd);
443 strbuf_release(&line);
444 }
445
446 struct alternate_refs_data {
447 odb_for_each_alternate_ref_fn *fn;
448 void *payload;
449 };
450
451 static int refs_from_alternate_cb(struct odb_source *alternate,
452 void *payload)
453 {
454 struct strbuf path = STRBUF_INIT;
455 size_t base_len;
456 struct alternate_refs_data *cb = payload;
457
458 if (!strbuf_realpath(&path, alternate->path, 0))
459 goto out;
460 if (!strbuf_strip_suffix(&path, "/objects"))
461 goto out;
462 base_len = path.len;
463
464 /* Is this a git repository with refs? */
465 strbuf_addstr(&path, "/refs");
466 if (!is_directory(path.buf))
467 goto out;
468 strbuf_setlen(&path, base_len);
469
470 read_alternate_refs(alternate->odb->repo, path.buf, cb->fn, cb->payload);
471
472 out:
473 strbuf_release(&path);
474 return 0;
475 }
476
477 void odb_for_each_alternate_ref(struct object_database *odb,
478 odb_for_each_alternate_ref_fn cb, void *payload)
479 {
480 struct alternate_refs_data data;
481 data.fn = cb;
482 data.payload = payload;
483 odb_for_each_alternate(odb, refs_from_alternate_cb, &data);
484 }
485
486 int odb_for_each_alternate(struct object_database *odb,
487 odb_for_each_alternate_fn cb, void *payload)
488 {
489 struct odb_source *alternate;
490 int r = 0;
491
492 for (alternate = odb->sources->next; alternate; alternate = alternate->next) {
493 r = cb(alternate, payload);
494 if (r)
495 break;
496 }
497 return r;
498 }
499
500 static void odb_prepare_alternates(struct object_database *odb,
501 const char *alternate_db)
502 {
503 struct strvec sources = STRVEC_INIT;
504
505 parse_alternates(alternate_db, PATH_SEP, NULL, &sources);
506 odb_source_read_alternates(odb->sources, &sources);
507
508 for (size_t i = 0; i < sources.nr; i++)
509 odb_add_alternate_recursively(odb, sources.v[i], 0);
510
511 strvec_clear(&sources);
512 }
513
514 int odb_has_alternates(struct object_database *odb)
515 {
516 return !!odb->sources->next;
517 }
518
519 int obj_read_use_lock = 0;
520 pthread_mutex_t obj_read_mutex;
521
522 void enable_obj_read_lock(void)
523 {
524 if (obj_read_use_lock)
525 return;
526
527 obj_read_use_lock = 1;
528 init_recursive_mutex(&obj_read_mutex);
529 }
530
531 void disable_obj_read_lock(void)
532 {
533 if (!obj_read_use_lock)
534 return;
535
536 obj_read_use_lock = 0;
537 pthread_mutex_destroy(&obj_read_mutex);
538 }
539
540 static enum odb_read_status do_oid_object_info_extended(struct object_database *odb,
541 const struct object_id *oid,
542 struct object_info *oi, unsigned flags)
543 {
544 struct strbuf corrupt_err = STRBUF_INIT;
545 const struct object_id *real = oid;
546 enum odb_read_status ret;
547 int already_retried = 0;
548 bool corrupt = false;
549
550 if (flags & OBJECT_INFO_LOOKUP_REPLACE)
551 real = lookup_replace_object(odb->repo, oid);
552
553 if (is_null_oid(real))
554 return -1;
555
556 if (!odb_source_read_object_info(odb->inmemory_objects, oid, oi, flags, NULL))
557 return 0;
558
559 while (1) {
560 struct odb_source *source;
561
562 for (source = odb->sources; source; source = source->next) {
563 ret = odb_source_read_object_info(source, real, oi, flags,
564 corrupt_err.len ? NULL : &corrupt_err);
565 if (!ret)
566 goto out;
567 if (ret != ODB_READ_NOT_FOUND)
568 corrupt = true;
569 }
570
571 /*
572 * When the object hasn't been found we try a second read and
573 * tell the sources so. This may cause them to invalidate
574 * caches or reload on-disk state.
575 */
576 if (!(flags & OBJECT_INFO_QUICK)) {
577 for (source = odb->sources; source; source = source->next) {
578 ret = odb_source_read_object_info(source, real, oi,
579 flags | OBJECT_INFO_SECOND_READ,
580 corrupt_err.len ? NULL : &corrupt_err);
581 if (!ret)
582 goto out;
583 if (ret != ODB_READ_NOT_FOUND)
584 corrupt = true;
585 }
586 }
587
588 /* Check if it is a missing object */
589 if (odb->repo->fetch_if_missing && repo_has_promisor_remote(odb->repo) &&
590 !already_retried &&
591 !(flags & OBJECT_INFO_SKIP_FETCH_OBJECT)) {
592 promisor_remote_get_direct(odb->repo, real, 1);
593 already_retried = 1;
594 continue;
595 }
596
597 if (flags & OBJECT_INFO_DIE_IF_CORRUPT) {
598 if ((flags & OBJECT_INFO_LOOKUP_REPLACE) && !oideq(real, oid))
599 die(_("replacement %s not found for %s"),
600 oid_to_hex(real), oid_to_hex(oid));
601 if (corrupt) {
602 if (corrupt_err.len)
603 die("%s", corrupt_err.buf);
604 die(_("object %s is corrupt"), oid_to_hex(real));
605 }
606 }
607
608 ret = corrupt ? ODB_READ_ERROR : ODB_READ_NOT_FOUND;
609 goto out;
610 }
611
612 out:
613 strbuf_release(&corrupt_err);
614 return ret;
615 }
616
617 static int oid_object_info_convert(struct repository *r,
618 const struct object_id *input_oid,
619 struct object_info *input_oi, unsigned flags)
620 {
621 const struct git_hash_algo *input_algo = &hash_algos[input_oid->algo];
622 int do_die = flags & OBJECT_INFO_DIE_IF_CORRUPT;
623 enum object_type type;
624 struct object_id oid, delta_base_oid;
625 struct object_info new_oi, *oi;
626 size_t size;
627 void *content;
628 int ret;
629
630 if (repo_oid_to_algop(r, input_oid, r->hash_algo, &oid)) {
631 if (do_die)
632 die(_("missing mapping of %s to %s"),
633 oid_to_hex(input_oid), r->hash_algo->name);
634 return -1;
635 }
636
637 /* Is new_oi needed? */
638 oi = input_oi;
639 if (input_oi && (input_oi->delta_base_oid || input_oi->sizep ||
640 input_oi->contentp)) {
641 new_oi = *input_oi;
642 /* Does delta_base_oid need to be converted? */
643 if (input_oi->delta_base_oid)
644 new_oi.delta_base_oid = &delta_base_oid;
645 /* Will the attributes differ when converted? */
646 if (input_oi->sizep || input_oi->contentp) {
647 new_oi.contentp = &content;
648 new_oi.sizep = &size;
649 new_oi.typep = &type;
650 }
651 oi = &new_oi;
652 }
653
654 ret = odb_read_object_info_extended(r->objects, &oid, oi, flags);
655 if (ret)
656 return -1;
657 if (oi == input_oi)
658 return ret;
659
660 if (new_oi.contentp) {
661 struct strbuf outbuf = STRBUF_INIT;
662
663 if (type != OBJ_BLOB) {
664 ret = convert_object_file(r, &outbuf,
665 r->hash_algo, input_algo,
666 content, size, type, !do_die);
667 free(content);
668 if (ret == -1)
669 return -1;
670 size = outbuf.len;
671 content = strbuf_detach(&outbuf, NULL);
672 }
673 if (input_oi->sizep)
674 *input_oi->sizep = size;
675 if (input_oi->contentp)
676 *input_oi->contentp = content;
677 else
678 free(content);
679 if (input_oi->typep)
680 *input_oi->typep = type;
681 }
682 if (new_oi.delta_base_oid == &delta_base_oid) {
683 if (repo_oid_to_algop(r, &delta_base_oid, input_algo,
684 input_oi->delta_base_oid)) {
685 if (do_die)
686 die(_("missing mapping of %s to %s"),
687 oid_to_hex(&delta_base_oid),
688 input_algo->name);
689 return -1;
690 }
691 }
692 if (input_oi->source_infop)
693 *input_oi->source_infop = *new_oi.source_infop;
694 return ret;
695 }
696
697 enum odb_read_status odb_read_object_info_extended(struct object_database *odb,
698 const struct object_id *oid,
699 struct object_info *oi,
700 enum object_info_flags flags)
701 {
702 enum odb_read_status ret;
703
704 if (oid->algo && (hash_algo_by_ptr(odb->repo->hash_algo) != oid->algo))
705 return oid_object_info_convert(odb->repo, oid, oi, flags);
706
707 obj_read_lock();
708 ret = do_oid_object_info_extended(odb, oid, oi, flags);
709 obj_read_unlock();
710 return ret;
711 }
712
713
714 /* returns enum object_type or negative */
715 int odb_read_object_info(struct object_database *odb,
716 const struct object_id *oid,
717 size_t *sizep)
718 {
719 enum object_type type;
720 struct object_info oi = OBJECT_INFO_INIT;
721
722 oi.typep = &type;
723 oi.sizep = sizep;
724 if (odb_read_object_info_extended(odb, oid, &oi,
725 OBJECT_INFO_LOOKUP_REPLACE) < 0)
726 return -1;
727 return type;
728 }
729
730 int odb_pretend_object(struct object_database *odb,
731 void *buf, size_t len, enum object_type type,
732 struct object_id *oid)
733 {
734 hash_object_file(odb->repo->hash_algo, buf, len, type, oid);
735 if (odb_has_object(odb, oid, 0))
736 return 0;
737
738 return odb_source_write_object(odb->inmemory_objects,
739 buf, len, type, oid, NULL, NULL, 0);
740 }
741
742 void *odb_read_object(struct object_database *odb,
743 const struct object_id *oid,
744 enum object_type *type,
745 size_t *size)
746 {
747 struct object_info oi = OBJECT_INFO_INIT;
748 unsigned flags = OBJECT_INFO_DIE_IF_CORRUPT | OBJECT_INFO_LOOKUP_REPLACE;
749 void *data;
750
751 oi.typep = type;
752 oi.sizep = size;
753 oi.contentp = &data;
754 if (odb_read_object_info_extended(odb, oid, &oi, flags))
755 return NULL;
756
757 return data;
758 }
759
760 void *odb_read_object_peeled(struct object_database *odb,
761 const struct object_id *oid,
762 enum object_type required_type,
763 size_t *size,
764 struct object_id *actual_oid_return)
765 {
766 enum object_type type;
767 void *buffer;
768 size_t isize;
769 struct object_id actual_oid;
770
771 oidcpy(&actual_oid, oid);
772 while (1) {
773 int ref_length = -1;
774 const char *ref_type = NULL;
775
776 buffer = odb_read_object(odb, &actual_oid, &type, &isize);
777 if (!buffer)
778 return NULL;
779 if (type == required_type) {
780 *size = isize;
781 if (actual_oid_return)
782 oidcpy(actual_oid_return, &actual_oid);
783 return buffer;
784 }
785 /* Handle references */
786 else if (type == OBJ_COMMIT)
787 ref_type = "tree ";
788 else if (type == OBJ_TAG)
789 ref_type = "object ";
790 else {
791 free(buffer);
792 return NULL;
793 }
794 ref_length = strlen(ref_type);
795
796 if (ref_length + odb->repo->hash_algo->hexsz > isize ||
797 memcmp(buffer, ref_type, ref_length) ||
798 get_oid_hex_algop((char *) buffer + ref_length, &actual_oid,
799 odb->repo->hash_algo)) {
800 free(buffer);
801 return NULL;
802 }
803 free(buffer);
804 /* Now we have the ID of the referred-to object in
805 * actual_oid. Check again. */
806 }
807 }
808
809 int odb_has_object(struct object_database *odb, const struct object_id *oid,
810 enum odb_has_object_flags flags)
811 {
812 unsigned object_info_flags = 0;
813
814 if (!startup_info->have_repository)
815 return 0;
816 if (!(flags & ODB_HAS_OBJECT_RECHECK_PACKED))
817 object_info_flags |= OBJECT_INFO_QUICK;
818 if (!(flags & ODB_HAS_OBJECT_FETCH_PROMISOR))
819 object_info_flags |= OBJECT_INFO_SKIP_FETCH_OBJECT;
820
821 return odb_read_object_info_extended(odb, oid, NULL, object_info_flags) >= 0;
822 }
823
824 int odb_freshen_object(struct object_database *odb,
825 const struct object_id *oid)
826 {
827 struct odb_source *source;
828 for (source = odb->sources; source; source = source->next)
829 if (odb_source_freshen_object(source, oid, NULL))
830 return 1;
831 return 0;
832 }
833
834 int odb_for_each_object_ext(struct object_database *odb,
835 const struct object_info *request,
836 odb_for_each_object_cb cb,
837 void *cb_data,
838 const struct odb_for_each_object_options *opts)
839 {
840 int ret;
841
842 for (struct odb_source *source = odb->sources; source; source = source->next) {
843 if (opts->flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY && !source->local)
844 continue;
845
846 ret = odb_source_for_each_object(source, request, cb, cb_data, opts);
847 if (ret)
848 return ret;
849 }
850
851 return 0;
852 }
853
854 int odb_for_each_object(struct object_database *odb,
855 const struct object_info *request,
856 odb_for_each_object_cb cb,
857 void *cb_data,
858 enum odb_for_each_object_flags flags)
859 {
860 struct odb_for_each_object_options opts = {
861 .flags = flags,
862 };
863 return odb_for_each_object_ext(odb, request, cb, cb_data, &opts);
864 }
865
866 int odb_count_objects(struct object_database *odb,
867 enum odb_count_objects_flags flags,
868 unsigned long *out)
869 {
870 struct odb_source *source;
871 unsigned long count = 0;
872 int ret;
873
874 if (odb->object_count_valid && odb->object_count_flags == flags) {
875 *out = odb->object_count;
876 return 0;
877 }
878
879 for (source = odb->sources; source; source = source->next) {
880 unsigned long c;
881
882 ret = odb_source_count_objects(source, flags, &c);
883 if (ret < 0)
884 goto out;
885
886 count += c;
887 }
888
889 odb->object_count = count;
890 odb->object_count_valid = 1;
891 odb->object_count_flags = flags;
892
893 *out = count;
894 ret = 0;
895
896 out:
897 return ret;
898 }
899
900 /*
901 * Return the slot of the most-significant bit set in "val". There are various
902 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
903 * probably not a big deal here.
904 */
905 static unsigned msb(unsigned long val)
906 {
907 unsigned r = 0;
908 while (val >>= 1)
909 r++;
910 return r;
911 }
912
913 int odb_find_abbrev_len(struct object_database *odb,
914 const struct object_id *oid,
915 int min_length,
916 unsigned *out)
917 {
918 const struct git_hash_algo *algo =
919 oid->algo ? &hash_algos[oid->algo] : odb->repo->hash_algo;
920 const unsigned hexsz = algo->hexsz;
921 unsigned len;
922 int ret;
923
924 if (min_length < 0) {
925 unsigned long count;
926
927 if (odb_count_objects(odb, ODB_COUNT_OBJECTS_APPROXIMATE, &count) < 0)
928 count = 0;
929
930 /*
931 * Add one because the MSB only tells us the highest bit set,
932 * not including the value of all the _other_ bits (so "15"
933 * is only one off of 2^4, but the MSB is the 3rd bit.
934 */
935 len = msb(count) + 1;
936 /*
937 * We now know we have on the order of 2^len objects, which
938 * expects a collision at 2^(len/2). But we also care about hex
939 * chars, not bits, and there are 4 bits per hex. So all
940 * together we need to divide by 2 and round up.
941 */
942 len = DIV_ROUND_UP(len, 2);
943 /*
944 * For very small repos, we stick with our regular fallback.
945 */
946 if (len < FALLBACK_DEFAULT_ABBREV)
947 len = FALLBACK_DEFAULT_ABBREV;
948 } else {
949 len = min_length;
950 }
951
952 if (len >= hexsz || !len) {
953 *out = hexsz;
954 ret = 0;
955 goto out;
956 }
957
958 for (struct odb_source *source = odb->sources; source; source = source->next) {
959 ret = odb_source_find_abbrev_len(source, oid, len, &len);
960 if (ret)
961 goto out;
962 }
963
964 ret = 0;
965 *out = len;
966
967 out:
968 return ret;
969 }
970
971 void odb_assert_oid_type(struct object_database *odb,
972 const struct object_id *oid, enum object_type expect)
973 {
974 enum object_type type = odb_read_object_info(odb, oid, NULL);
975 if (type < 0)
976 die(_("%s is not a valid object"), oid_to_hex(oid));
977 if (type != expect)
978 die(_("%s is not a valid '%s' object"), oid_to_hex(oid),
979 type_name(expect));
980 }
981
982 int odb_write_object_ext(struct object_database *odb,
983 const void *buf, unsigned long len,
984 enum object_type type,
985 struct object_id *oid,
986 const struct object_id *compat_oid_in,
987 enum odb_write_object_flags flags)
988 {
989 const struct git_hash_algo *compat = odb->repo->compat_hash_algo;
990 struct object_id compat_oid, *compat_oid_p = NULL;
991
992 hash_object_file(odb->repo->hash_algo, buf, len, type, oid);
993
994 /*
995 * We can skip the write in case we already have the object available.
996 * In that case, we only freshen its mtime.
997 */
998 if (odb_freshen_object(odb, oid))
999 return 0;
1000
1001 if (compat) {
1002 const struct git_hash_algo *algo = odb->repo->hash_algo;
1003
1004 if (compat_oid_in) {
1005 oidcpy(&compat_oid, compat_oid_in);
1006 } else if (type == OBJ_BLOB) {
1007 hash_object_file(compat, buf, len, type, &compat_oid);
1008 } else {
1009 struct strbuf converted = STRBUF_INIT;
1010 convert_object_file(odb->repo, &converted, algo, compat,
1011 buf, len, type, 0);
1012 hash_object_file(compat, converted.buf, converted.len,
1013 type, &compat_oid);
1014 strbuf_release(&converted);
1015 }
1016
1017 compat_oid_p = &compat_oid;
1018 }
1019
1020 return odb_source_write_object(odb->sources, buf, len, type,
1021 oid, compat_oid_p, NULL, flags);
1022 }
1023
1024 int odb_write_object_stream(struct object_database *odb,
1025 struct odb_stream *stream,
1026 struct object_id *oid)
1027 {
1028 return odb_source_write_object_stream(odb->sources, stream, oid);
1029 }
1030
1031 int odb_optimize(struct object_database *odb,
1032 const struct odb_optimize_options *opts)
1033 {
1034 return odb_source_optimize(odb->sources, opts);
1035 }
1036
1037 bool odb_optimize_required(struct object_database *odb,
1038 const struct odb_optimize_options *opts)
1039 {
1040 return odb_source_optimize_required(odb->sources, opts);
1041 }
1042
1043 void odb_generate_pack_options_release(struct odb_generate_pack_options *opts)
1044 {
1045 oid_array_clear(&opts->wants);
1046 oid_array_clear(&opts->haves);
1047 oid_array_clear(&opts->shallows);
1048 }
1049
1050 int odb_generate_pack(struct object_database *odb,
1051 struct odb_pack_generator **out,
1052 const struct odb_generate_pack_options *opts)
1053 {
1054 if (!odb->sources->generate_pack)
1055 return error(_("primary object source does not support generating packfiles"));
1056 return odb_source_generate_pack(odb->sources, out, opts);
1057 }
1058
1059 int odb_pack_generator_finish(struct odb_pack_generator *generator)
1060 {
1061 return generator->finish(generator);
1062 }
1063
1064 struct object_database *odb_new(struct repository *repo,
1065 enum odb_new_flags flags)
1066 {
1067 char *primary_source = NULL, *secondary_sources = NULL;
1068 struct object_database *o;
1069
1070 CALLOC_ARRAY(o, 1);
1071 o->repo = repo;
1072 pthread_mutex_init(&o->replace_mutex, NULL);
1073 hashmap_init(&o->source_by_path, odb_source_by_path_cmp, o, 0);
1074 o->source_paths_icase = -1;
1075
1076 if (flags & ODB_NEW_HONOR_ENV) {
1077 primary_source = xstrdup_or_null(getenv(DB_ENVIRONMENT));
1078 secondary_sources = xstrdup_or_null(getenv(ALTERNATE_DB_ENVIRONMENT));
1079 }
1080 if (!primary_source)
1081 primary_source = xstrfmt("%s/objects", repo->commondir);
1082
1083 o->sources = odb_source_new(o, primary_source, true);
1084 o->sources_tail = &o->sources->next;
1085 o->inmemory_objects = &odb_source_inmemory_new(o)->base;
1086
1087 odb_prepare_alternates(o, secondary_sources);
1088
1089 free(secondary_sources);
1090 free(primary_source);
1091 return o;
1092 }
1093
1094 void odb_close(struct object_database *o)
1095 {
1096 struct odb_source *source;
1097 for (source = o->sources; source; source = source->next)
1098 odb_source_close(source);
1099 close_commit_graph(o);
1100 }
1101
1102 static void odb_free_sources(struct object_database *o)
1103 {
1104 while (o->sources) {
1105 struct odb_source *next;
1106
1107 next = o->sources->next;
1108 odb_source_free(o->sources);
1109 o->sources = next;
1110 }
1111
1112 odb_source_free(o->inmemory_objects);
1113 o->inmemory_objects = NULL;
1114
1115 hashmap_clear(&o->source_by_path);
1116 }
1117
1118 void odb_free(struct object_database *o)
1119 {
1120 if (!o)
1121 return;
1122
1123 oidmap_clear(&o->replace_map, 1);
1124 pthread_mutex_destroy(&o->replace_mutex);
1125
1126 odb_close(o);
1127 odb_free_sources(o);
1128
1129 free(o);
1130 }
1131
1132 void odb_prepare(struct object_database *o, enum odb_prepare_flags flags)
1133 {
1134 struct odb_source *source;
1135
1136 obj_read_lock();
1137
1138 /*
1139 * Reprepare alt odbs, in case the alternates file was modified
1140 * during the course of this process. This only _adds_ odbs to
1141 * the linked list, so existing odbs will continue to exist for
1142 * the lifetime of the process. Consequently, we don't have to
1143 * reprocess GIT_ALTERNATE_OBJECT_DIRECTORIES here.
1144 */
1145 if (flags & ODB_PREPARE_FLUSH_CACHES) {
1146 odb_prepare_alternates(o, NULL);
1147 o->object_count_valid = 0;
1148 }
1149
1150 for (source = o->sources; source; source = source->next)
1151 odb_source_prepare(source, flags);
1152
1153 obj_read_unlock();
1154 }
1155
1156 void odb_reprepare(struct object_database *o)
1157 {
1158 odb_prepare(o, ODB_PREPARE_FLUSH_CACHES);
1159 }