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