at commit a51b77aa

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