Coverage Report

Created: 2026-12-01 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/odb.c
Line
Count
Source
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 "hashmap.h"
9
#include "hex.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-files.h"
18
#include "odb/source-inmemory.h"
19
#include "path.h"
20
#include "promisor-remote.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
static const char * const object_storage_name_by_format[] = {
32
  [OBJECT_STORAGE_FORMAT_UNKNOWN] = "unknown",
33
  [OBJECT_STORAGE_FORMAT_FILES] = "files",
34
};
35
36
enum object_storage_format object_storage_format_by_name(const char *name)
37
0
{
38
0
  for (size_t i = 0; i < ARRAY_SIZE(object_storage_name_by_format); i++)
39
0
    if (!strcmp(object_storage_name_by_format[i], name))
40
0
      return i;
41
0
  return OBJECT_STORAGE_FORMAT_UNKNOWN;
42
0
}
43
44
const char *object_storage_format_to_name(enum object_storage_format format)
45
0
{
46
0
  if (format < 0 || format >= ARRAY_SIZE(object_storage_name_by_format))
47
0
    format = OBJECT_STORAGE_FORMAT_UNKNOWN;
48
0
  return object_storage_name_by_format[format];
49
0
}
50
51
enum object_storage_format object_storage_format_by_uri(const char *uri, char **payload)
52
0
{
53
0
  enum object_storage_format format;
54
0
  const char *schema_end;
55
0
  char *name;
56
57
0
  schema_end = strstr(uri, "://");
58
0
  if (!schema_end) {
59
0
    name = xstrdup(uri);
60
0
    if (payload)
61
0
      *payload = NULL;
62
0
  } else {
63
0
    name = xstrndup(uri, schema_end - uri);
64
0
    if (payload)
65
0
      *payload = *(schema_end + 3) ? xstrdup(schema_end + 3) : NULL;
66
0
  }
67
68
0
  format = object_storage_format_by_name(name);
69
70
0
  free(name);
71
0
  return format;
72
0
}
73
74
int odb_mkstemp(struct object_database *odb,
75
    struct strbuf *temp_filename, const char *pattern)
76
0
{
77
0
  int fd;
78
  /*
79
   * we let the umask do its job, don't try to be more
80
   * restrictive except to remove write permission.
81
   */
82
0
  int mode = 0444;
83
0
  repo_git_path_replace(odb->repo, temp_filename, "objects/%s", pattern);
84
0
  fd = git_mkstemp_mode(temp_filename->buf, mode);
85
0
  if (0 <= fd)
86
0
    return fd;
87
88
  /* slow path */
89
  /* some mkstemp implementations erase temp_filename on failure */
90
0
  repo_git_path_replace(odb->repo, temp_filename, "objects/%s", pattern);
91
0
  safe_create_leading_directories(odb->repo, temp_filename->buf);
92
0
  return xmkstemp_mode(temp_filename->buf, mode);
93
0
}
94
95
char *compute_alternate_path(const char *path, struct strbuf *err)
96
0
{
97
0
  char *ref_git = NULL;
98
0
  const char *repo;
99
0
  int seen_error = 0;
100
101
0
  ref_git = real_pathdup(path, 0);
102
0
  if (!ref_git) {
103
0
    seen_error = 1;
104
0
    strbuf_addf(err, _("path '%s' does not exist"), path);
105
0
    goto out;
106
0
  }
107
108
0
  repo = read_gitfile(ref_git);
109
0
  if (!repo)
110
0
    repo = read_gitfile(mkpath("%s/.git", ref_git));
111
0
  if (repo) {
112
0
    free(ref_git);
113
0
    ref_git = xstrdup(repo);
114
0
  }
115
116
0
  if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) {
117
0
    char *ref_git_git = mkpathdup("%s/.git", ref_git);
118
0
    free(ref_git);
119
0
    ref_git = ref_git_git;
120
0
  } else if (!is_directory(mkpath("%s/objects", ref_git))) {
121
0
    struct strbuf sb = STRBUF_INIT;
122
0
    seen_error = 1;
123
0
    if (get_common_dir(&sb, ref_git)) {
124
0
      strbuf_addf(err,
125
0
            _("reference repository '%s' as a linked "
126
0
              "checkout is not supported yet."),
127
0
            path);
128
0
      goto out;
129
0
    }
130
131
0
    strbuf_addf(err, _("reference repository '%s' is not a "
132
0
          "local repository."), path);
133
0
    goto out;
134
0
  }
135
136
0
  if (!access(mkpath("%s/shallow", ref_git), F_OK)) {
137
0
    strbuf_addf(err, _("reference repository '%s' is shallow"),
138
0
          path);
139
0
    seen_error = 1;
140
0
    goto out;
141
0
  }
142
143
0
  if (!access(mkpath("%s/info/grafts", ref_git), F_OK)) {
144
0
    strbuf_addf(err,
145
0
          _("reference repository '%s' is grafted"),
146
0
          path);
147
0
    seen_error = 1;
148
0
    goto out;
149
0
  }
150
151
0
out:
152
0
  if (seen_error) {
153
0
    FREE_AND_NULL(ref_git);
154
0
  }
155
156
0
  return ref_git;
157
0
}
158
159
static void fill_alternate_refs_command(struct repository *repo,
160
          struct child_process *cmd,
161
          const char *repo_path)
162
0
{
163
0
  const char *value;
164
165
0
  if (!repo_config_get_string_tmp(repo, "core.alternateRefsCommand", &value)) {
166
0
    cmd->use_shell = 1;
167
168
0
    strvec_push(&cmd->args, value);
169
0
    strvec_push(&cmd->args, repo_path);
170
0
  } else {
171
0
    cmd->git_cmd = 1;
172
173
0
    strvec_pushf(&cmd->args, "--git-dir=%s", repo_path);
174
0
    strvec_push(&cmd->args, "for-each-ref");
175
0
    strvec_push(&cmd->args, "--format=%(objectname)");
176
177
0
    if (!repo_config_get_string_tmp(repo, "core.alternateRefsPrefixes", &value)) {
178
0
      strvec_push(&cmd->args, "--");
179
0
      strvec_split(&cmd->args, value);
180
0
    }
181
0
  }
182
183
0
  strvec_pushv(&cmd->env, (const char **)local_repo_env);
184
0
  cmd->out = -1;
185
0
}
186
187
static void read_alternate_refs(struct repository *repo,
188
        const char *path,
189
        odb_for_each_alternate_ref_fn *cb,
190
        void *payload)
191
0
{
192
0
  struct child_process cmd = CHILD_PROCESS_INIT;
193
0
  struct strbuf line = STRBUF_INIT;
194
0
  FILE *fh;
195
196
0
  fill_alternate_refs_command(repo, &cmd, path);
197
198
0
  if (start_command(&cmd))
199
0
    return;
200
201
0
  fh = xfdopen(cmd.out, "r");
202
0
  while (strbuf_getline_lf(&line, fh) != EOF) {
203
0
    struct object_id oid;
204
0
    const char *p;
205
206
0
    if (parse_oid_hex_algop(line.buf, &oid, &p, repo->hash_algo) || *p) {
207
0
      warning(_("invalid line while parsing alternate refs: %s"),
208
0
        line.buf);
209
0
      break;
210
0
    }
211
212
0
    cb(&oid, payload);
213
0
  }
214
215
0
  fclose(fh);
216
0
  finish_command(&cmd);
217
0
  strbuf_release(&line);
218
0
}
219
220
struct alternate_refs_data {
221
  struct repository *repo;
222
  odb_for_each_alternate_ref_fn *fn;
223
  void *payload;
224
};
225
226
static int refs_from_alternate_cb(struct odb_files_dir *alternate,
227
          void *payload)
228
0
{
229
0
  struct strbuf path = STRBUF_INIT;
230
0
  struct strbuf repo = STRBUF_INIT;
231
0
  size_t base_len;
232
0
  struct alternate_refs_data *cb = payload;
233
234
0
  if (!strbuf_realpath(&path, alternate->abspath, 0))
235
0
    goto out;
236
0
  if (!strbuf_strip_suffix(&path, "/objects"))
237
0
    goto out;
238
0
  base_len = path.len;
239
240
  /* Is this a git repository with refs? */
241
0
  strbuf_addstr(&path, "/refs");
242
0
  if (!is_directory(path.buf))
243
0
    goto out;
244
0
  strbuf_setlen(&path, base_len);
245
246
  /* A shallow or grafted repository may lack the history behind its refs. */
247
0
  strbuf_addstr(&path, "/shallow");
248
0
  if (!access(path.buf, F_OK))
249
0
    goto out;
250
0
  strbuf_setlen(&path, base_len);
251
252
0
  strbuf_addstr(&path, "/info/grafts");
253
0
  if (!access(path.buf, F_OK))
254
0
    goto out;
255
0
  strbuf_setlen(&path, base_len);
256
257
  /* Our own repository is not a source of alternate refs. */
258
0
  if (strbuf_realpath(&repo, cb->repo->commondir, 0) &&
259
0
      !fspathcmp(path.buf, repo.buf))
260
0
    goto out;
261
262
0
  read_alternate_refs(cb->repo, path.buf, cb->fn, cb->payload);
263
264
0
out:
265
0
  strbuf_release(&path);
266
0
  strbuf_release(&repo);
267
0
  return 0;
268
0
}
269
270
void odb_for_each_alternate_ref(struct object_database *odb,
271
        odb_for_each_alternate_ref_fn cb, void *payload)
272
0
{
273
0
  struct alternate_refs_data data = {
274
0
    .fn = cb,
275
0
    .payload = payload,
276
0
    .repo = odb->repo,
277
0
  };
278
0
  odb_for_each_alternate(odb, refs_from_alternate_cb, &data);
279
0
}
280
281
int odb_for_each_alternate(struct object_database *odb,
282
       odb_for_each_alternate_fn cb, void *payload)
283
0
{
284
0
  struct odb_source_files *files = odb_source_files_downcast(odb->source);
285
0
  int r = 0;
286
287
0
  for (struct odb_files_dir *dir = files->dirs->next; dir; dir = dir->next) {
288
0
    r = cb(dir, payload);
289
0
    if (r)
290
0
      break;
291
0
  }
292
0
  return r;
293
0
}
294
295
int odb_has_alternates(struct object_database *odb)
296
0
{
297
0
  if (odb->source->type != ODB_SOURCE_FILES)
298
0
    return 0;
299
0
  return !!odb_source_files_downcast(odb->source)->dirs->next;
300
0
}
301
302
int obj_read_use_lock = 0;
303
pthread_mutex_t obj_read_mutex;
304
305
void enable_obj_read_lock(void)
306
0
{
307
0
  if (obj_read_use_lock)
308
0
    return;
309
310
0
  obj_read_use_lock = 1;
311
0
  init_recursive_mutex(&obj_read_mutex);
312
0
}
313
314
void disable_obj_read_lock(void)
315
0
{
316
0
  if (!obj_read_use_lock)
317
0
    return;
318
319
0
  obj_read_use_lock = 0;
320
0
  pthread_mutex_destroy(&obj_read_mutex);
321
0
}
322
323
static enum odb_read_status do_oid_object_info_extended(struct object_database *odb,
324
              const struct object_id *oid,
325
              struct object_info *oi, unsigned flags)
326
0
{
327
0
  struct strbuf corrupt_err = STRBUF_INIT;
328
0
  const struct object_id *real = oid;
329
0
  enum odb_read_status ret;
330
331
0
  if (flags & OBJECT_INFO_LOOKUP_REPLACE)
332
0
    real = lookup_replace_object(odb->repo, oid);
333
334
0
  if (is_null_oid(real))
335
0
    return -1;
336
337
0
  if (!odb_source_read_object_info(odb->inmemory_objects, oid, oi, flags, NULL))
338
0
    return 0;
339
340
0
  ret = odb_source_read_object_info(odb->source, real, oi, flags,
341
0
            &corrupt_err);
342
0
  if (ret == ODB_READ_NOT_FOUND &&
343
0
      odb->repo->fetch_if_missing &&
344
0
      !(flags & OBJECT_INFO_SKIP_FETCH_OBJECT) &&
345
0
      repo_has_promisor_remote(odb->repo)) {
346
    /* Try to fetch the missing object from a promisor remote. */
347
0
    promisor_remote_get_direct(odb->repo, real, 1);
348
0
    ret = odb_source_read_object_info(odb->source, real, oi, flags,
349
0
              &corrupt_err);
350
0
  }
351
352
0
  if (ret < 0 && (flags & OBJECT_INFO_DIE_IF_CORRUPT)) {
353
0
    if ((flags & OBJECT_INFO_LOOKUP_REPLACE) && !oideq(real, oid))
354
0
      die(_("replacement %s not found for %s"),
355
0
          oid_to_hex(real), oid_to_hex(oid));
356
0
    if (ret != ODB_READ_NOT_FOUND) {
357
0
      if (corrupt_err.len)
358
0
        die("%s", corrupt_err.buf);
359
0
      die(_("object %s is corrupt"), oid_to_hex(real));
360
0
    }
361
0
  }
362
363
0
  strbuf_release(&corrupt_err);
364
0
  return ret;
365
0
}
366
367
static int oid_object_info_convert(struct repository *r,
368
           const struct object_id *input_oid,
369
           struct object_info *input_oi, unsigned flags)
370
0
{
371
0
  const struct git_hash_algo *input_algo = &hash_algos[input_oid->algo];
372
0
  int do_die = flags & OBJECT_INFO_DIE_IF_CORRUPT;
373
0
  enum object_type type;
374
0
  struct object_id oid, delta_base_oid;
375
0
  struct object_info new_oi, *oi;
376
0
  size_t size;
377
0
  void *content;
378
0
  int ret;
379
380
0
  if (repo_oid_to_algop(r, input_oid, r->hash_algo, &oid)) {
381
0
    if (do_die)
382
0
      die(_("missing mapping of %s to %s"),
383
0
          oid_to_hex(input_oid), r->hash_algo->name);
384
0
    return -1;
385
0
  }
386
387
  /* Is new_oi needed? */
388
0
  oi = input_oi;
389
0
  if (input_oi && (input_oi->delta_base_oid || input_oi->sizep ||
390
0
       input_oi->contentp)) {
391
0
    new_oi = *input_oi;
392
    /* Does delta_base_oid need to be converted? */
393
0
    if (input_oi->delta_base_oid)
394
0
      new_oi.delta_base_oid = &delta_base_oid;
395
    /* Will the attributes differ when converted? */
396
0
    if (input_oi->sizep || input_oi->contentp) {
397
0
      new_oi.contentp = &content;
398
0
      new_oi.sizep = &size;
399
0
      new_oi.typep = &type;
400
0
    }
401
0
    oi = &new_oi;
402
0
  }
403
404
0
  ret = odb_read_object_info_extended(r->objects, &oid, oi, flags);
405
0
  if (ret)
406
0
    return -1;
407
0
  if (oi == input_oi)
408
0
    return ret;
409
410
0
  if (new_oi.contentp) {
411
0
    struct strbuf outbuf = STRBUF_INIT;
412
413
0
    if (type != OBJ_BLOB) {
414
0
      ret = convert_object_file(r, &outbuf,
415
0
              r->hash_algo, input_algo,
416
0
              content, size, type, !do_die);
417
0
      free(content);
418
0
      if (ret == -1)
419
0
        return -1;
420
0
      size = outbuf.len;
421
0
      content = strbuf_detach(&outbuf, NULL);
422
0
    }
423
0
    if (input_oi->sizep)
424
0
      *input_oi->sizep = size;
425
0
    if (input_oi->contentp)
426
0
      *input_oi->contentp = content;
427
0
    else
428
0
      free(content);
429
0
    if (input_oi->typep)
430
0
      *input_oi->typep = type;
431
0
  }
432
0
  if (new_oi.delta_base_oid == &delta_base_oid) {
433
0
    if (repo_oid_to_algop(r, &delta_base_oid, input_algo,
434
0
         input_oi->delta_base_oid)) {
435
0
      if (do_die)
436
0
        die(_("missing mapping of %s to %s"),
437
0
            oid_to_hex(&delta_base_oid),
438
0
            input_algo->name);
439
0
      return -1;
440
0
    }
441
0
  }
442
0
  if (input_oi->source_infop)
443
0
    *input_oi->source_infop = *new_oi.source_infop;
444
0
  return ret;
445
0
}
446
447
enum odb_read_status odb_read_object_info_extended(struct object_database *odb,
448
               const struct object_id *oid,
449
               struct object_info *oi,
450
               enum object_info_flags flags)
451
0
{
452
0
  enum odb_read_status ret;
453
454
0
  if (oid->algo && (hash_algo_by_ptr(odb->repo->hash_algo) != oid->algo))
455
0
    return oid_object_info_convert(odb->repo, oid, oi, flags);
456
457
0
  obj_read_lock();
458
0
  ret = do_oid_object_info_extended(odb, oid, oi, flags);
459
0
  obj_read_unlock();
460
0
  return ret;
461
0
}
462
463
464
/* returns enum object_type or negative */
465
int odb_read_object_info(struct object_database *odb,
466
       const struct object_id *oid,
467
       size_t *sizep)
468
0
{
469
0
  enum object_type type;
470
0
  struct object_info oi = OBJECT_INFO_INIT;
471
472
0
  oi.typep = &type;
473
0
  oi.sizep = sizep;
474
0
  if (odb_read_object_info_extended(odb, oid, &oi,
475
0
            OBJECT_INFO_LOOKUP_REPLACE) < 0)
476
0
    return -1;
477
0
  return type;
478
0
}
479
480
int odb_pretend_object(struct object_database *odb,
481
           void *buf, size_t len, enum object_type type,
482
           struct object_id *oid)
483
0
{
484
0
  hash_object_file(odb->repo->hash_algo, buf, len, type, oid);
485
0
  if (odb_has_object(odb, oid, 0))
486
0
    return 0;
487
488
0
  return odb_source_write_object(odb->inmemory_objects,
489
0
               buf, len, type, oid, NULL, NULL, 0);
490
0
}
491
492
void *odb_read_object(struct object_database *odb,
493
          const struct object_id *oid,
494
          enum object_type *type,
495
          size_t *size)
496
0
{
497
0
  struct object_info oi = OBJECT_INFO_INIT;
498
0
  unsigned flags = OBJECT_INFO_DIE_IF_CORRUPT | OBJECT_INFO_LOOKUP_REPLACE;
499
0
  void *data;
500
501
0
  oi.typep = type;
502
0
  oi.sizep = size;
503
0
  oi.contentp = &data;
504
0
  if (odb_read_object_info_extended(odb, oid, &oi, flags))
505
0
    return NULL;
506
507
0
  return data;
508
0
}
509
510
void *odb_read_object_peeled(struct object_database *odb,
511
           const struct object_id *oid,
512
           enum object_type required_type,
513
           size_t *size,
514
           struct object_id *actual_oid_return)
515
0
{
516
0
  enum object_type type;
517
0
  void *buffer;
518
0
  size_t isize;
519
0
  struct object_id actual_oid;
520
521
0
  oidcpy(&actual_oid, oid);
522
0
  while (1) {
523
0
    int ref_length = -1;
524
0
    const char *ref_type = NULL;
525
526
0
    buffer = odb_read_object(odb, &actual_oid, &type, &isize);
527
0
    if (!buffer)
528
0
      return NULL;
529
0
    if (type == required_type) {
530
0
      *size = isize;
531
0
      if (actual_oid_return)
532
0
        oidcpy(actual_oid_return, &actual_oid);
533
0
      return buffer;
534
0
    }
535
    /* Handle references */
536
0
    else if (type == OBJ_COMMIT)
537
0
      ref_type = "tree ";
538
0
    else if (type == OBJ_TAG)
539
0
      ref_type = "object ";
540
0
    else {
541
0
      free(buffer);
542
0
      return NULL;
543
0
    }
544
0
    ref_length = strlen(ref_type);
545
546
0
    if (ref_length + odb->repo->hash_algo->hexsz > isize ||
547
0
        memcmp(buffer, ref_type, ref_length) ||
548
0
        get_oid_hex_algop((char *) buffer + ref_length, &actual_oid,
549
0
              odb->repo->hash_algo)) {
550
0
      free(buffer);
551
0
      return NULL;
552
0
    }
553
0
    free(buffer);
554
    /* Now we have the ID of the referred-to object in
555
     * actual_oid.  Check again. */
556
0
  }
557
0
}
558
559
int odb_has_object(struct object_database *odb, const struct object_id *oid,
560
       enum odb_has_object_flags flags)
561
0
{
562
0
  unsigned object_info_flags = 0;
563
564
0
  if (!startup_info->have_repository)
565
0
    return 0;
566
0
  if (!(flags & ODB_HAS_OBJECT_RECHECK_PACKED))
567
0
    object_info_flags |= OBJECT_INFO_QUICK;
568
0
  if (!(flags & ODB_HAS_OBJECT_FETCH_PROMISOR))
569
0
    object_info_flags |= OBJECT_INFO_SKIP_FETCH_OBJECT;
570
571
0
  return odb_read_object_info_extended(odb, oid, NULL, object_info_flags) >= 0;
572
0
}
573
574
int odb_freshen_object(struct object_database *odb,
575
           const struct object_id *oid)
576
0
{
577
0
  return odb_source_freshen_object(odb->source, oid, NULL);
578
0
}
579
580
int odb_for_each_object_ext(struct object_database *odb,
581
          const struct object_info *request,
582
          odb_for_each_object_cb cb,
583
          void *cb_data,
584
          const struct odb_for_each_object_options *opts)
585
0
{
586
0
  return odb_source_for_each_object(odb->source, request, cb, cb_data, opts);
587
0
}
588
589
int odb_for_each_object(struct object_database *odb,
590
      const struct object_info *request,
591
      odb_for_each_object_cb cb,
592
      void *cb_data,
593
      enum odb_for_each_object_flags flags)
594
0
{
595
0
  struct odb_for_each_object_options opts = {
596
0
    .flags = flags,
597
0
  };
598
0
  return odb_for_each_object_ext(odb, request, cb, cb_data, &opts);
599
0
}
600
601
int odb_count_objects(struct object_database *odb,
602
          enum odb_count_objects_flags flags,
603
          unsigned long *out)
604
0
{
605
0
  unsigned long count = 0;
606
0
  int ret;
607
608
0
  if (odb->object_count_valid && odb->object_count_flags == flags) {
609
0
    *out = odb->object_count;
610
0
    return 0;
611
0
  }
612
613
0
  ret = odb_source_count_objects(odb->source, flags, &count);
614
0
  if (ret < 0)
615
0
    goto out;
616
617
0
  odb->object_count = count;
618
0
  odb->object_count_valid = 1;
619
0
  odb->object_count_flags = flags;
620
621
0
  *out = count;
622
0
  ret = 0;
623
624
0
out:
625
0
  return ret;
626
0
}
627
628
/*
629
 * Return the slot of the most-significant bit set in "val". There are various
630
 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
631
 * probably not a big deal here.
632
 */
633
static unsigned msb(unsigned long val)
634
0
{
635
0
  unsigned r = 0;
636
0
  while (val >>= 1)
637
0
    r++;
638
0
  return r;
639
0
}
640
641
int odb_find_abbrev_len(struct object_database *odb,
642
      const struct object_id *oid,
643
      int min_length,
644
      unsigned *out)
645
0
{
646
0
  const struct git_hash_algo *algo =
647
0
    oid->algo ? &hash_algos[oid->algo] : odb->repo->hash_algo;
648
0
  const unsigned hexsz = algo->hexsz;
649
0
  unsigned len;
650
0
  int ret;
651
652
0
  if (min_length < 0) {
653
0
    unsigned long count;
654
655
0
    if (odb_count_objects(odb, ODB_COUNT_OBJECTS_APPROXIMATE, &count) < 0)
656
0
      count = 0;
657
658
    /*
659
     * Add one because the MSB only tells us the highest bit set,
660
     * not including the value of all the _other_ bits (so "15"
661
     * is only one off of 2^4, but the MSB is the 3rd bit.
662
     */
663
0
    len = msb(count) + 1;
664
    /*
665
     * We now know we have on the order of 2^len objects, which
666
     * expects a collision at 2^(len/2). But we also care about hex
667
     * chars, not bits, and there are 4 bits per hex. So all
668
     * together we need to divide by 2 and round up.
669
     */
670
0
    len = DIV_ROUND_UP(len, 2);
671
    /*
672
     * For very small repos, we stick with our regular fallback.
673
     */
674
0
    if (len < FALLBACK_DEFAULT_ABBREV)
675
0
      len = FALLBACK_DEFAULT_ABBREV;
676
0
  } else {
677
0
    len = min_length;
678
0
  }
679
680
0
  if (len >= hexsz || !len) {
681
0
    *out = hexsz;
682
0
    ret = 0;
683
0
    goto out;
684
0
  }
685
686
0
  ret = odb_source_find_abbrev_len(odb->source, oid, len, &len);
687
0
  *out = len;
688
689
0
out:
690
0
  return ret;
691
0
}
692
693
void odb_assert_oid_type(struct object_database *odb,
694
       const struct object_id *oid, enum object_type expect)
695
0
{
696
0
  enum object_type type = odb_read_object_info(odb, oid, NULL);
697
0
  if (type < 0)
698
0
    die(_("%s is not a valid object"), oid_to_hex(oid));
699
0
  if (type != expect)
700
0
    die(_("%s is not a valid '%s' object"), oid_to_hex(oid),
701
0
        type_name(expect));
702
0
}
703
704
int odb_write_object_ext(struct object_database *odb,
705
       const void *buf, unsigned long len,
706
       enum object_type type,
707
       struct object_id *oid,
708
       const struct object_id *compat_oid_in,
709
       enum odb_write_object_flags flags)
710
0
{
711
0
  const struct git_hash_algo *compat = odb->repo->compat_hash_algo;
712
0
  struct object_id compat_oid, *compat_oid_p = NULL;
713
714
0
  hash_object_file(odb->repo->hash_algo, buf, len, type, oid);
715
716
  /*
717
   * We can skip the write in case we already have the object available.
718
   * In that case, we only freshen its mtime.
719
   */
720
0
  if (odb_freshen_object(odb, oid))
721
0
    return 0;
722
723
0
  if (compat) {
724
0
    const struct git_hash_algo *algo = odb->repo->hash_algo;
725
726
0
    if (compat_oid_in) {
727
0
      oidcpy(&compat_oid, compat_oid_in);
728
0
    } else if (type == OBJ_BLOB) {
729
0
      hash_object_file(compat, buf, len, type, &compat_oid);
730
0
    } else {
731
0
      struct strbuf converted = STRBUF_INIT;
732
0
      convert_object_file(odb->repo, &converted, algo, compat,
733
0
              buf, len, type, 0);
734
0
      hash_object_file(compat, converted.buf, converted.len,
735
0
           type, &compat_oid);
736
0
      strbuf_release(&converted);
737
0
    }
738
739
0
    compat_oid_p = &compat_oid;
740
0
  }
741
742
0
  return odb_source_write_object(odb->source, buf, len, type,
743
0
               oid, compat_oid_p, NULL, flags);
744
0
}
745
746
int odb_write_object_stream(struct object_database *odb,
747
          struct odb_stream *stream,
748
          struct object_id *oid)
749
0
{
750
0
  return odb_source_write_object_stream(odb->source, stream, oid);
751
0
}
752
753
int odb_optimize(struct object_database *odb,
754
     const struct odb_optimize_options *opts)
755
0
{
756
0
  return odb_source_optimize(odb->source, opts);
757
0
}
758
759
bool odb_optimize_required(struct object_database *odb,
760
         const struct odb_optimize_options *opts)
761
0
{
762
0
  return odb_source_optimize_required(odb->source, opts);
763
0
}
764
765
void odb_generate_pack_options_release(struct odb_generate_pack_options *opts)
766
0
{
767
0
  oid_array_clear(&opts->wants);
768
0
  oid_array_clear(&opts->haves);
769
0
  oid_array_clear(&opts->shallows);
770
0
}
771
772
int odb_generate_pack(struct object_database *odb,
773
          struct odb_pack_generator **out,
774
          const struct odb_generate_pack_options *opts)
775
0
{
776
0
  if (!odb->source->generate_pack)
777
0
    return error(_("primary object source does not support generating packfiles"));
778
0
  return odb_source_generate_pack(odb->source, out, opts);
779
0
}
780
781
int odb_pack_generator_finish(struct odb_pack_generator *generator)
782
0
{
783
0
  return generator->finish(generator);
784
0
}
785
786
struct object_database *odb_new(struct repository *repo,
787
        enum odb_new_flags flags)
788
0
{
789
0
  struct object_database *o;
790
791
0
  CALLOC_ARRAY(o, 1);
792
0
  o->repo = repo;
793
0
  pthread_mutex_init(&o->replace_mutex, NULL);
794
795
0
  o->source = odb_source_new(o, repo->object_storage_format,
796
0
           repo->object_storage_payload, flags);
797
0
  o->inmemory_objects = &odb_source_inmemory_new(o)->base;
798
799
0
  return o;
800
0
}
801
802
void odb_close(struct object_database *o)
803
0
{
804
0
  odb_source_close(o->source);
805
0
  commit_graph_close(o);
806
0
}
807
808
static void odb_free_sources(struct object_database *o)
809
0
{
810
0
  odb_source_free(o->source);
811
0
  odb_source_free(o->inmemory_objects);
812
0
  o->inmemory_objects = NULL;
813
0
}
814
815
void odb_free(struct object_database *o)
816
0
{
817
0
  if (!o)
818
0
    return;
819
820
0
  oidmap_clear(&o->replace_map, 1);
821
0
  pthread_mutex_destroy(&o->replace_mutex);
822
823
0
  odb_close(o);
824
0
  odb_free_sources(o);
825
826
0
  free(o);
827
0
}
828
829
void odb_prepare(struct object_database *o, enum odb_prepare_flags flags)
830
0
{
831
0
  obj_read_lock();
832
833
0
  if (flags & ODB_PREPARE_FLUSH_CACHES)
834
0
    o->object_count_valid = 0;
835
836
0
  odb_source_prepare(o->source, flags);
837
838
0
  obj_read_unlock();
839
0
}
840
841
void odb_reprepare(struct object_database *o)
842
0
{
843
0
  odb_prepare(o, ODB_PREPARE_FLUSH_CACHES);
844
0
}
845
846
int odb_fsck(struct object_database *odb, struct odb_fsck_options *options)
847
0
{
848
0
  return odb_source_fsck(odb->source, options);
849
0
}