at commit 508ec983

1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "commit-graph.h"
4 #include "config.h"
5 #include "dir.h"
6 #include "environment.h"
7 #include "gettext.h"
8 #include "hex.h"
9 #include "khash.h"
10 #include "lockfile.h"
11 #include "loose.h"
12 #include "midx.h"
13 #include "object-file-convert.h"
14 #include "object-file.h"
15 #include "object-name.h"
16 #include "odb.h"
17 #include "odb/source-inmemory.h"
18 #include "packfile.h"
19 #include "path.h"
20 #include "promisor-remote.h"
21 #include "quote.h"
22 #include "replace-object.h"
23 #include "run-command.h"
24 #include "setup.h"
25 #include "strbuf.h"
26 #include "strvec.h"
27 #include "submodule.h"
28 #include "tmp-objdir.h"
29 #include "trace2.h"
30 #include "write-or-die.h"
31
32 KHASH_INIT(odb_path_map, const char * /* key: odb_path */,
33 struct odb_source *, 1, fspathhash, fspatheq)
34
35 int odb_mkstemp(struct object_database *odb,
36 struct strbuf *temp_filename, const char *pattern)
37 {
38 int fd;
39 /*
40 * we let the umask do its job, don't try to be more
41 * restrictive except to remove write permission.
42 */
43 int mode = 0444;
44 repo_git_path_replace(odb->repo, temp_filename, "objects/%s", pattern);
45 fd = git_mkstemp_mode(temp_filename->buf, mode);
46 if (0 <= fd)
47 return fd;
48
49 /* slow path */
50 /* some mkstemp implementations erase temp_filename on failure */
51 repo_git_path_replace(odb->repo, temp_filename, "objects/%s", pattern);
52 safe_create_leading_directories(odb->repo, temp_filename->buf);
53 return xmkstemp_mode(temp_filename->buf, mode);
54 }
55
56 /*
57 * Return non-zero iff the path is usable as an alternate object database.
58 */
59 static bool odb_is_source_usable(struct object_database *o, const char *path)
60 {
61 int r;
62 struct strbuf normalized_objdir = STRBUF_INIT;
63 bool usable = false;
64
65 strbuf_realpath(&normalized_objdir, o->sources->path, 1);
66
67 /* Detect cases where alternate disappeared */
68 if (!is_directory(path)) {
69 error(_("object directory %s does not exist; "
70 "check .git/objects/info/alternates"),
71 path);
72 goto out;
73 }
74
75 /*
76 * Prevent the common mistake of listing the same
77 * thing twice, or object directory itself.
78 */
79 if (!o->source_by_path) {
80 khiter_t p;
81
82 o->source_by_path = kh_init_odb_path_map();
83 assert(!o->sources->next);
84 p = kh_put_odb_path_map(o->source_by_path, o->sources->path, &r);
85 assert(r == 1); /* never used */
86 kh_value(o->source_by_path, p) = o->sources;
87 }
88
89 if (fspatheq(path, normalized_objdir.buf))
90 goto out;
91
92 if (kh_get_odb_path_map(o->source_by_path, path) < kh_end(o->source_by_path))
93 goto out;
94
95 usable = true;
96
97 out:
98 strbuf_release(&normalized_objdir);
99 return usable;
100 }
101
102 void parse_alternates(const char *string,
103 int sep,
104 const char *relative_base,
105 struct strvec *out)
106 {
107 struct strbuf pathbuf = STRBUF_INIT;
108 struct strbuf buf = STRBUF_INIT;
109
110 if (!string || !*string)
111 return;
112
113 while (*string) {
114 const char *end;
115
116 strbuf_reset(&buf);
117 strbuf_reset(&pathbuf);
118
119 if (*string == '#') {
120 /* comment; consume up to next separator */
121 end = strchrnul(string, sep);
122 } else if (*string == '"' && !unquote_c_style(&buf, string, &end)) {
123 /*
124 * quoted path; unquote_c_style has copied the
125 * data for us and set "end". Broken quoting (e.g.,
126 * an entry that doesn't end with a quote) falls
127 * back to the unquoted case below.
128 */
129 } else {
130 /* normal, unquoted path */
131 end = strchrnul(string, sep);
132 strbuf_add(&buf, string, end - string);
133 }
134
135 if (*end)
136 end++;
137 string = end;
138
139 if (!buf.len)
140 continue;
141
142 if (!is_absolute_path(buf.buf) && relative_base) {
143 strbuf_realpath(&pathbuf, relative_base, 1);
144 strbuf_addch(&pathbuf, '/');
145 }
146 strbuf_addbuf(&pathbuf, &buf);
147
148 strbuf_reset(&buf);
149 if (!strbuf_realpath(&buf, pathbuf.buf, 0)) {
150 error(_("unable to normalize alternate object path: %s"),
151 pathbuf.buf);
152 continue;
153 }
154
155 /*
156 * The trailing slash after the directory name is given by
157 * this function at the end. Remove duplicates.
158 */
159 while (buf.len && buf.buf[buf.len - 1] == '/')
160 strbuf_setlen(&buf, buf.len - 1);
161
162 strvec_push(out, buf.buf);
163 }
164
165 strbuf_release(&pathbuf);
166 strbuf_release(&buf);
167 }
168
169 static struct odb_source *odb_add_alternate_recursively(struct object_database *odb,
170 const char *source,
171 int depth)
172 {
173 struct odb_source *alternate = NULL;
174 struct strvec sources = STRVEC_INIT;
175 khiter_t pos;
176 int ret;
177
178 if (!odb_is_source_usable(odb, source))
179 goto error;
180
181 alternate = odb_source_new(odb, source, false);
182
183 /* add the alternate entry */
184 *odb->sources_tail = alternate;
185 odb->sources_tail = &(alternate->next);
186
187 pos = kh_put_odb_path_map(odb->source_by_path, alternate->path, &ret);
188 if (!ret)
189 BUG("source must not yet exist");
190 kh_value(odb->source_by_path, pos) = alternate;
191
192 /* recursively add alternates */
193 odb_source_read_alternates(alternate, &sources);
194 if (sources.nr && depth + 1 > 5) {
195 error(_("%s: ignoring alternate object stores, nesting too deep"),
196 source);
197 } else {
198 for (size_t i = 0; i < sources.nr; i++)
199 odb_add_alternate_recursively(odb, sources.v[i], depth + 1);
200 }
201
202 error:
203 strvec_clear(&sources);
204 return alternate;
205 }
206
207 void odb_add_to_alternates_file(struct object_database *odb,
208 const char *dir)
209 {
210 int ret = odb_source_write_alternate(odb->sources, dir);
211 if (ret < 0)
212 die(NULL);
213 if (odb->loaded_alternates)
214 odb_add_alternate_recursively(odb, dir, 0);
215 }
216
217 struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,
218 const char *dir)
219 {
220 /*
221 * Make sure alternates are initialized, or else our entry may be
222 * overwritten when they are.
223 */
224 odb_prepare_alternates(odb);
225 return odb_add_alternate_recursively(odb, dir, 0);
226 }
227
228 struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
229 const char *dir, int will_destroy)
230 {
231 struct odb_source *source;
232
233 /*
234 * Make sure alternates are initialized, or else our entry may be
235 * overwritten when they are.
236 */
237 odb_prepare_alternates(odb);
238
239 /*
240 * Make a new primary odb and link the old primary ODB in as an
241 * alternate
242 */
243 source = odb_source_new(odb, dir, false);
244
245 /*
246 * Disable ref updates while a temporary odb is active, since
247 * the objects in the database may roll back.
248 */
249 odb->repo->disable_ref_updates = true;
250 source->will_destroy = will_destroy;
251 source->next = odb->sources;
252 odb->sources = source;
253 return source->next;
254 }
255
256 void odb_restore_primary_source(struct object_database *odb,
257 struct odb_source *restore_source,
258 const char *old_path)
259 {
260 struct odb_source *cur_source = odb->sources;
261
262 if (strcmp(old_path, cur_source->path))
263 BUG("expected %s as primary object store; found %s",
264 old_path, cur_source->path);
265
266 if (cur_source->next != restore_source)
267 BUG("we expect the old primary object store to be the first alternate");
268
269 odb->repo->disable_ref_updates = false;
270 odb->sources = restore_source;
271 odb_source_free(cur_source);
272 }
273
274 char *compute_alternate_path(const char *path, struct strbuf *err)
275 {
276 char *ref_git = NULL;
277 const char *repo;
278 int seen_error = 0;
279
280 ref_git = real_pathdup(path, 0);
281 if (!ref_git) {
282 seen_error = 1;
283 strbuf_addf(err, _("path '%s' does not exist"), path);
284 goto out;
285 }
286
287 repo = read_gitfile(ref_git);
288 if (!repo)
289 repo = read_gitfile(mkpath("%s/.git", ref_git));
290 if (repo) {
291 free(ref_git);
292 ref_git = xstrdup(repo);
293 }
294
295 if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) {
296 char *ref_git_git = mkpathdup("%s/.git", ref_git);
297 free(ref_git);
298 ref_git = ref_git_git;
299 } else if (!is_directory(mkpath("%s/objects", ref_git))) {
300 struct strbuf sb = STRBUF_INIT;
301 seen_error = 1;
302 if (get_common_dir(&sb, ref_git)) {
303 strbuf_addf(err,
304 _("reference repository '%s' as a linked "
305 "checkout is not supported yet."),
306 path);
307 goto out;
308 }
309
310 strbuf_addf(err, _("reference repository '%s' is not a "
311 "local repository."), path);
312 goto out;
313 }
314
315 if (!access(mkpath("%s/shallow", ref_git), F_OK)) {
316 strbuf_addf(err, _("reference repository '%s' is shallow"),
317 path);
318 seen_error = 1;
319 goto out;
320 }
321
322 if (!access(mkpath("%s/info/grafts", ref_git), F_OK)) {
323 strbuf_addf(err,
324 _("reference repository '%s' is grafted"),
325 path);
326 seen_error = 1;
327 goto out;
328 }
329
330 out:
331 if (seen_error) {
332 FREE_AND_NULL(ref_git);
333 }
334
335 return ref_git;
336 }
337
338 struct odb_source *odb_find_source(struct object_database *odb, const char *obj_dir)
339 {
340 struct odb_source *source;
341 char *obj_dir_real = real_pathdup(obj_dir, 1);
342 struct strbuf odb_path_real = STRBUF_INIT;
343
344 odb_prepare_alternates(odb);
345 for (source = odb->sources; source; source = source->next) {
346 strbuf_realpath(&odb_path_real, source->path, 1);
347 if (!strcmp(obj_dir_real, odb_path_real.buf))
348 break;
349 }
350
351 free(obj_dir_real);
352 strbuf_release(&odb_path_real);
353
354 return source;
355 }
356
357 struct odb_source *odb_find_source_or_die(struct object_database *odb, const char *obj_dir)
358 {
359 struct odb_source *source = odb_find_source(odb, obj_dir);
360 if (!source)
361 die(_("could not find object directory matching %s"), obj_dir);
362 return source;
363 }
364
365 void odb_add_submodule_source_by_path(struct object_database *odb,
366 const char *path)
367 {
368 string_list_insert(&odb->submodule_source_paths, path);
369 }
370
371 static void fill_alternate_refs_command(struct repository *repo,
372 struct child_process *cmd,
373 const char *repo_path)
374 {
375 const char *value;
376
377 if (!repo_config_get_value(repo, "core.alternateRefsCommand", &value)) {
378 cmd->use_shell = 1;
379
380 strvec_push(&cmd->args, value);
381 strvec_push(&cmd->args, repo_path);
382 } else {
383 cmd->git_cmd = 1;
384
385 strvec_pushf(&cmd->args, "--git-dir=%s", repo_path);
386 strvec_push(&cmd->args, "for-each-ref");
387 strvec_push(&cmd->args, "--format=%(objectname)");
388
389 if (!repo_config_get_value(repo, "core.alternateRefsPrefixes", &value)) {
390 strvec_push(&cmd->args, "--");
391 strvec_split(&cmd->args, value);
392 }
393 }
394
395 strvec_pushv(&cmd->env, (const char **)local_repo_env);
396 cmd->out = -1;
397 }
398
399 static void read_alternate_refs(struct repository *repo,
400 const char *path,
401 odb_for_each_alternate_ref_fn *cb,
402 void *payload)
403 {
404 struct child_process cmd = CHILD_PROCESS_INIT;
405 struct strbuf line = STRBUF_INIT;
406 FILE *fh;
407
408 fill_alternate_refs_command(repo, &cmd, path);
409
410 if (start_command(&cmd))
411 return;
412
413 fh = xfdopen(cmd.out, "r");
414 while (strbuf_getline_lf(&line, fh) != EOF) {
415 struct object_id oid;
416 const char *p;
417
418 if (parse_oid_hex_algop(line.buf, &oid, &p, repo->hash_algo) || *p) {
419 warning(_("invalid line while parsing alternate refs: %s"),
420 line.buf);
421 break;
422 }
423
424 cb(&oid, payload);
425 }
426
427 fclose(fh);
428 finish_command(&cmd);
429 strbuf_release(&line);
430 }
431
432 struct alternate_refs_data {
433 odb_for_each_alternate_ref_fn *fn;
434 void *payload;
435 };
436
437 static int refs_from_alternate_cb(struct odb_source *alternate,
438 void *payload)
439 {
440 struct strbuf path = STRBUF_INIT;
441 size_t base_len;
442 struct alternate_refs_data *cb = payload;
443
444 if (!strbuf_realpath(&path, alternate->path, 0))
445 goto out;
446 if (!strbuf_strip_suffix(&path, "/objects"))
447 goto out;
448 base_len = path.len;
449
450 /* Is this a git repository with refs? */
451 strbuf_addstr(&path, "/refs");
452 if (!is_directory(path.buf))
453 goto out;
454 strbuf_setlen(&path, base_len);
455
456 read_alternate_refs(alternate->odb->repo, path.buf, cb->fn, cb->payload);
457
458 out:
459 strbuf_release(&path);
460 return 0;
461 }
462
463 void odb_for_each_alternate_ref(struct object_database *odb,
464 odb_for_each_alternate_ref_fn cb, void *payload)
465 {
466 struct alternate_refs_data data;
467 data.fn = cb;
468 data.payload = payload;
469 odb_for_each_alternate(odb, refs_from_alternate_cb, &data);
470 }
471
472 int odb_for_each_alternate(struct object_database *odb,
473 odb_for_each_alternate_fn cb, void *payload)
474 {
475 struct odb_source *alternate;
476 int r = 0;
477
478 odb_prepare_alternates(odb);
479 for (alternate = odb->sources->next; alternate; alternate = alternate->next) {
480 r = cb(alternate, payload);
481 if (r)
482 break;
483 }
484 return r;
485 }
486
487 void odb_prepare_alternates(struct object_database *odb)
488 {
489 struct strvec sources = STRVEC_INIT;
490
491 if (odb->loaded_alternates)
492 return;
493
494 parse_alternates(odb->alternate_db, PATH_SEP, NULL, &sources);
495 odb_source_read_alternates(odb->sources, &sources);
496 for (size_t i = 0; i < sources.nr; i++)
497 odb_add_alternate_recursively(odb, sources.v[i], 0);
498
499 odb->loaded_alternates = 1;
500
501 strvec_clear(&sources);
502 }
503
504 int odb_has_alternates(struct object_database *odb)
505 {
506 odb_prepare_alternates(odb);
507 return !!odb->sources->next;
508 }
509
510 int obj_read_use_lock = 0;
511 pthread_mutex_t obj_read_mutex;
512
513 void enable_obj_read_lock(void)
514 {
515 if (obj_read_use_lock)
516 return;
517
518 obj_read_use_lock = 1;
519 init_recursive_mutex(&obj_read_mutex);
520 }
521
522 void disable_obj_read_lock(void)
523 {
524 if (!obj_read_use_lock)
525 return;
526
527 obj_read_use_lock = 0;
528 pthread_mutex_destroy(&obj_read_mutex);
529 }
530
531 static int register_all_submodule_sources(struct object_database *odb)
532 {
533 int ret = odb->submodule_source_paths.nr;
534
535 for (size_t i = 0; i < odb->submodule_source_paths.nr; i++)
536 odb_add_to_alternates_memory(odb,
537 odb->submodule_source_paths.items[i].string);
538 if (ret) {
539 string_list_clear(&odb->submodule_source_paths, 0);
540 trace2_data_intmax("submodule", odb->repo,
541 "register_all_submodule_sources/registered", ret);
542 if (git_env_bool("GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB", 0))
543 BUG("register_all_submodule_sources() called");
544 }
545 return ret;
546 }
547
548 static int do_oid_object_info_extended(struct object_database *odb,
549 const struct object_id *oid,
550 struct object_info *oi, unsigned flags)
551 {
552 const struct object_id *real = oid;
553 int already_retried = 0;
554
555 if (flags & OBJECT_INFO_LOOKUP_REPLACE)
556 real = lookup_replace_object(odb->repo, oid);
557
558 if (is_null_oid(real))
559 return -1;
560
561 if (!odb_source_read_object_info(odb->inmemory_objects, oid, oi, flags))
562 return 0;
563
564 odb_prepare_alternates(odb);
565
566 while (1) {
567 struct odb_source *source;
568
569 for (source = odb->sources; source; source = source->next)
570 if (!odb_source_read_object_info(source, real, oi, flags))
571 return 0;
572
573 /*
574 * When the object hasn't been found we try a second read and
575 * tell the sources so. This may cause them to invalidate
576 * caches or reload on-disk state.
577 */
578 if (!(flags & OBJECT_INFO_QUICK)) {
579 for (source = odb->sources; source; source = source->next)
580 if (!odb_source_read_object_info(source, real, oi,
581 flags | OBJECT_INFO_SECOND_READ))
582 return 0;
583 }
584
585 /*
586 * This might be an attempt at accessing a submodule object as
587 * if it were in main object store (having called
588 * `odb_add_submodule_source_by_path()` on that submodule's
589 * ODB). If any such ODBs exist, register them and try again.
590 */
591 if (register_all_submodule_sources(odb))
592 /* We added some alternates; retry */
593 continue;
594
595 /* Check if it is a missing object */
596 if (odb->repo->fetch_if_missing && repo_has_promisor_remote(odb->repo) &&
597 !already_retried &&
598 !(flags & OBJECT_INFO_SKIP_FETCH_OBJECT)) {
599 promisor_remote_get_direct(odb->repo, real, 1);
600 already_retried = 1;
601 continue;
602 }
603
604 if (flags & OBJECT_INFO_DIE_IF_CORRUPT) {
605 const struct packed_git *p;
606 if ((flags & OBJECT_INFO_LOOKUP_REPLACE) && !oideq(real, oid))
607 die(_("replacement %s not found for %s"),
608 oid_to_hex(real), oid_to_hex(oid));
609 if ((p = has_packed_and_bad(odb->repo, real)))
610 die(_("packed object %s (stored in %s) is corrupt"),
611 oid_to_hex(real), p->pack_name);
612 }
613 return -1;
614 }
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 int 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 int 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 odb_prepare_alternates(odb);
829 for (source = odb->sources; source; source = source->next)
830 if (odb_source_freshen_object(source, oid, NULL))
831 return 1;
832 return 0;
833 }
834
835 int odb_for_each_object_ext(struct object_database *odb,
836 const struct object_info *request,
837 odb_for_each_object_cb cb,
838 void *cb_data,
839 const struct odb_for_each_object_options *opts)
840 {
841 int ret;
842
843 odb_prepare_alternates(odb);
844 for (struct odb_source *source = odb->sources; source; source = source->next) {
845 if (opts->flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY && !source->local)
846 continue;
847
848 ret = odb_source_for_each_object(source, request, cb, cb_data, opts);
849 if (ret)
850 return ret;
851 }
852
853 return 0;
854 }
855
856 int odb_for_each_object(struct object_database *odb,
857 const struct object_info *request,
858 odb_for_each_object_cb cb,
859 void *cb_data,
860 enum odb_for_each_object_flags flags)
861 {
862 struct odb_for_each_object_options opts = {
863 .flags = flags,
864 };
865 return odb_for_each_object_ext(odb, request, cb, cb_data, &opts);
866 }
867
868 int odb_count_objects(struct object_database *odb,
869 enum odb_count_objects_flags flags,
870 unsigned long *out)
871 {
872 struct odb_source *source;
873 unsigned long count = 0;
874 int ret;
875
876 if (odb->object_count_valid && odb->object_count_flags == flags) {
877 *out = odb->object_count;
878 return 0;
879 }
880
881 odb_prepare_alternates(odb);
882 for (source = odb->sources; source; source = source->next) {
883 unsigned long c;
884
885 ret = odb_source_count_objects(source, flags, &c);
886 if (ret < 0)
887 goto out;
888
889 count += c;
890 }
891
892 odb->object_count = count;
893 odb->object_count_valid = 1;
894 odb->object_count_flags = flags;
895
896 *out = count;
897 ret = 0;
898
899 out:
900 return ret;
901 }
902
903 /*
904 * Return the slot of the most-significant bit set in "val". There are various
905 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
906 * probably not a big deal here.
907 */
908 static unsigned msb(unsigned long val)
909 {
910 unsigned r = 0;
911 while (val >>= 1)
912 r++;
913 return r;
914 }
915
916 int odb_find_abbrev_len(struct object_database *odb,
917 const struct object_id *oid,
918 int min_length,
919 unsigned *out)
920 {
921 const struct git_hash_algo *algo =
922 oid->algo ? &hash_algos[oid->algo] : odb->repo->hash_algo;
923 const unsigned hexsz = algo->hexsz;
924 unsigned len;
925 int ret;
926
927 if (min_length < 0) {
928 unsigned long count;
929
930 if (odb_count_objects(odb, ODB_COUNT_OBJECTS_APPROXIMATE, &count) < 0)
931 count = 0;
932
933 /*
934 * Add one because the MSB only tells us the highest bit set,
935 * not including the value of all the _other_ bits (so "15"
936 * is only one off of 2^4, but the MSB is the 3rd bit.
937 */
938 len = msb(count) + 1;
939 /*
940 * We now know we have on the order of 2^len objects, which
941 * expects a collision at 2^(len/2). But we also care about hex
942 * chars, not bits, and there are 4 bits per hex. So all
943 * together we need to divide by 2 and round up.
944 */
945 len = DIV_ROUND_UP(len, 2);
946 /*
947 * For very small repos, we stick with our regular fallback.
948 */
949 if (len < FALLBACK_DEFAULT_ABBREV)
950 len = FALLBACK_DEFAULT_ABBREV;
951 } else {
952 len = min_length;
953 }
954
955 if (len >= hexsz || !len) {
956 *out = hexsz;
957 ret = 0;
958 goto out;
959 }
960
961 odb_prepare_alternates(odb);
962 for (struct odb_source *source = odb->sources; source; source = source->next) {
963 ret = odb_source_find_abbrev_len(source, oid, len, &len);
964 if (ret)
965 goto out;
966 }
967
968 ret = 0;
969 *out = len;
970
971 out:
972 return ret;
973 }
974
975 void odb_assert_oid_type(struct object_database *odb,
976 const struct object_id *oid, enum object_type expect)
977 {
978 enum object_type type = odb_read_object_info(odb, oid, NULL);
979 if (type < 0)
980 die(_("%s is not a valid object"), oid_to_hex(oid));
981 if (type != expect)
982 die(_("%s is not a valid '%s' object"), oid_to_hex(oid),
983 type_name(expect));
984 }
985
986 int odb_write_object_ext(struct object_database *odb,
987 const void *buf, unsigned long len,
988 enum object_type type,
989 struct object_id *oid,
990 const struct object_id *compat_oid_in,
991 enum odb_write_object_flags flags)
992 {
993 const struct git_hash_algo *compat = odb->repo->compat_hash_algo;
994 struct object_id compat_oid, *compat_oid_p = NULL;
995
996 hash_object_file(odb->repo->hash_algo, buf, len, type, oid);
997
998 /*
999 * We can skip the write in case we already have the object available.
1000 * In that case, we only freshen its mtime.
1001 */
1002 if (odb_freshen_object(odb, oid))
1003 return 0;
1004
1005 if (compat) {
1006 const struct git_hash_algo *algo = odb->repo->hash_algo;
1007
1008 if (compat_oid_in) {
1009 oidcpy(&compat_oid, compat_oid_in);
1010 } else if (type == OBJ_BLOB) {
1011 hash_object_file(compat, buf, len, type, &compat_oid);
1012 } else {
1013 struct strbuf converted = STRBUF_INIT;
1014 convert_object_file(odb->repo, &converted, algo, compat,
1015 buf, len, type, 0);
1016 hash_object_file(compat, converted.buf, converted.len,
1017 type, &compat_oid);
1018 strbuf_release(&converted);
1019 }
1020
1021 compat_oid_p = &compat_oid;
1022 }
1023
1024 return odb_source_write_object(odb->sources, buf, len, type,
1025 oid, compat_oid_p, NULL, flags);
1026 }
1027
1028 int odb_write_object_stream(struct object_database *odb,
1029 struct odb_write_stream *stream, size_t len,
1030 struct object_id *oid)
1031 {
1032 return odb_source_write_object_stream(odb->sources, stream, len, oid);
1033 }
1034
1035 int odb_optimize(struct object_database *odb,
1036 const struct odb_optimize_options *opts)
1037 {
1038 return odb_source_optimize(odb->sources, opts);
1039 }
1040
1041 bool odb_optimize_required(struct object_database *odb,
1042 const struct odb_optimize_options *opts)
1043 {
1044 return odb_source_optimize_required(odb->sources, opts);
1045 }
1046
1047 struct object_database *odb_new(struct repository *repo,
1048 const char *primary_source,
1049 const char *secondary_sources)
1050 {
1051 struct object_database *o = xmalloc(sizeof(*o));
1052 char *to_free = NULL;
1053
1054 memset(o, 0, sizeof(*o));
1055 o->repo = repo;
1056 pthread_mutex_init(&o->replace_mutex, NULL);
1057 string_list_init_dup(&o->submodule_source_paths);
1058
1059 if (!primary_source)
1060 primary_source = to_free = xstrfmt("%s/objects", repo->commondir);
1061 o->sources = odb_source_new(o, primary_source, true);
1062 o->sources_tail = &o->sources->next;
1063 o->alternate_db = xstrdup_or_null(secondary_sources);
1064 o->inmemory_objects = &odb_source_inmemory_new(o)->base;
1065
1066 free(to_free);
1067
1068 return o;
1069 }
1070
1071 void odb_close(struct object_database *o)
1072 {
1073 struct odb_source *source;
1074 for (source = o->sources; source; source = source->next)
1075 odb_source_close(source);
1076 close_commit_graph(o);
1077 }
1078
1079 static void odb_free_sources(struct object_database *o)
1080 {
1081 while (o->sources) {
1082 struct odb_source *next;
1083
1084 next = o->sources->next;
1085 odb_source_free(o->sources);
1086 o->sources = next;
1087 }
1088
1089 odb_source_free(o->inmemory_objects);
1090 o->inmemory_objects = NULL;
1091
1092 kh_destroy_odb_path_map(o->source_by_path);
1093 o->source_by_path = NULL;
1094 }
1095
1096 void odb_free(struct object_database *o)
1097 {
1098 if (!o)
1099 return;
1100
1101 free(o->alternate_db);
1102
1103 oidmap_clear(&o->replace_map, 1);
1104 pthread_mutex_destroy(&o->replace_mutex);
1105
1106 odb_close(o);
1107 odb_free_sources(o);
1108
1109 string_list_clear(&o->submodule_source_paths, 0);
1110
1111 free(o);
1112 }
1113
1114 void odb_prepare(struct object_database *o, enum odb_prepare_flags flags)
1115 {
1116 struct odb_source *source;
1117
1118 obj_read_lock();
1119
1120 /*
1121 * Reprepare alt odbs, in case the alternates file was modified
1122 * during the course of this process. This only _adds_ odbs to
1123 * the linked list, so existing odbs will continue to exist for
1124 * the lifetime of the process.
1125 */
1126 if (flags & ODB_PREPARE_FLUSH_CACHES) {
1127 o->loaded_alternates = 0;
1128 o->object_count_valid = 0;
1129 }
1130
1131 odb_prepare_alternates(o);
1132 for (source = o->sources; source; source = source->next)
1133 odb_source_prepare(source, flags);
1134
1135 obj_read_unlock();
1136 }
1137
1138 void odb_reprepare(struct object_database *o)
1139 {
1140 odb_prepare(o, ODB_PREPARE_FLUSH_CACHES);
1141 }