Coverage Report

Created: 2026-03-31 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/repository.c
Line
Count
Source
1
#include "git-compat-util.h"
2
#include "abspath.h"
3
#include "repository.h"
4
#include "hook.h"
5
#include "odb.h"
6
#include "odb/source.h"
7
#include "config.h"
8
#include "gettext.h"
9
#include "object.h"
10
#include "lockfile.h"
11
#include "path.h"
12
#include "read-cache-ll.h"
13
#include "remote.h"
14
#include "setup.h"
15
#include "loose.h"
16
#include "submodule-config.h"
17
#include "sparse-index.h"
18
#include "trace2.h"
19
#include "promisor-remote.h"
20
#include "refs.h"
21
22
/*
23
 * We do not define `USE_THE_REPOSITORY_VARIABLE` in this file because we do
24
 * not want to rely on functions that implicitly use `the_repository`. This
25
 * means that the `extern` declaration of `the_repository` isn't visible here,
26
 * which makes sparse unhappy. We thus declare it here.
27
 */
28
extern struct repository *the_repository;
29
30
/* The main repository */
31
static struct repository the_repo;
32
struct repository *the_repository = &the_repo;
33
34
/*
35
 * An escape hatch: if we hit a bug in the production code that fails
36
 * to set an appropriate hash algorithm (most likely to happen when
37
 * running outside a repository), we can tell the user who reported
38
 * the crash to set the environment variable to "sha1" (all lowercase)
39
 * to revert to the historical behaviour of defaulting to SHA-1.
40
 */
41
static void set_default_hash_algo(struct repository *repo)
42
0
{
43
0
  const char *hash_name;
44
0
  uint32_t algo;
45
46
0
  hash_name = getenv("GIT_TEST_DEFAULT_HASH_ALGO");
47
0
  if (!hash_name)
48
0
    return;
49
0
  algo = hash_algo_by_name(hash_name);
50
0
  if (algo == GIT_HASH_UNKNOWN)
51
0
    return;
52
53
0
  repo_set_hash_algo(repo, algo);
54
0
}
55
56
struct repo_config_values *repo_config_values(struct repository *repo)
57
0
{
58
0
  if (repo != the_repository)
59
0
    BUG("trying to read config from wrong repository instance");
60
0
  if (!repo->initialized)
61
0
    BUG("config values from uninitialized repository");
62
0
  return &repo->config_values_private_;
63
0
}
64
65
void initialize_repository(struct repository *repo)
66
0
{
67
0
  if (repo->initialized)
68
0
    BUG("repository initialized already");
69
0
  repo->initialized = true;
70
71
0
  repo->remote_state = remote_state_new();
72
0
  repo->parsed_objects = parsed_object_pool_new(repo);
73
0
  ALLOC_ARRAY(repo->index, 1);
74
0
  index_state_init(repo->index, repo);
75
0
  repo->check_deprecated_config = true;
76
0
  repo_config_values_init(&repo->config_values_private_);
77
78
  /*
79
   * When a command runs inside a repository, it learns what
80
   * hash algorithm is in use from the repository, but some
81
   * commands are designed to work outside a repository, yet
82
   * they want to access the_hash_algo, if only for the length
83
   * of the hashed value to see if their input looks like a
84
   * plausible hash value.
85
   *
86
   * We are in the process of identifying such code paths and
87
   * giving them an appropriate default individually; any
88
   * unconverted code paths that try to access the_hash_algo
89
   * will thus fail.  The end-users however have an escape hatch
90
   * to set GIT_TEST_DEFAULT_HASH_ALGO environment variable to
91
   * "sha1" to get back the old behaviour of defaulting to SHA-1.
92
   *
93
   * This escape hatch is deliberately kept unadvertised, so
94
   * that they see crashes and we can get a report before
95
   * telling them about it.
96
   */
97
0
  if (repo == the_repository)
98
0
    set_default_hash_algo(repo);
99
0
}
100
101
static void expand_base_dir(char **out, const char *in,
102
          const char *base_dir, const char *def_in)
103
0
{
104
0
  free(*out);
105
0
  if (in)
106
0
    *out = xstrdup(in);
107
0
  else
108
0
    *out = xstrfmt("%s/%s", base_dir, def_in);
109
0
}
110
111
const char *repo_get_git_dir(struct repository *repo)
112
0
{
113
0
  if (!repo->gitdir)
114
0
    BUG("repository hasn't been set up");
115
0
  return repo->gitdir;
116
0
}
117
118
const char *repo_get_common_dir(struct repository *repo)
119
0
{
120
0
  if (!repo->commondir)
121
0
    BUG("repository hasn't been set up");
122
0
  return repo->commondir;
123
0
}
124
125
const char *repo_get_object_directory(struct repository *repo)
126
0
{
127
0
  if (!repo->objects->sources)
128
0
    BUG("repository hasn't been set up");
129
0
  return repo->objects->sources->path;
130
0
}
131
132
const char *repo_get_index_file(struct repository *repo)
133
0
{
134
0
  if (!repo->index_file)
135
0
    BUG("repository hasn't been set up");
136
0
  return repo->index_file;
137
0
}
138
139
const char *repo_get_graft_file(struct repository *repo)
140
0
{
141
0
  if (!repo->graft_file)
142
0
    BUG("repository hasn't been set up");
143
0
  return repo->graft_file;
144
0
}
145
146
const char *repo_get_work_tree(struct repository *repo)
147
0
{
148
0
  return repo->worktree;
149
0
}
150
151
static void repo_set_commondir(struct repository *repo,
152
             const char *commondir)
153
0
{
154
0
  struct strbuf sb = STRBUF_INIT;
155
156
0
  free(repo->commondir);
157
158
0
  if (commondir) {
159
0
    repo->different_commondir = 1;
160
0
    repo->commondir = xstrdup(commondir);
161
0
    return;
162
0
  }
163
164
0
  repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
165
0
  repo->commondir = strbuf_detach(&sb, NULL);
166
0
}
167
168
void repo_set_gitdir(struct repository *repo,
169
         const char *root,
170
         const struct set_gitdir_args *o)
171
0
{
172
0
  const char *gitfile = read_gitfile(root);
173
  /*
174
   * repo->gitdir is saved because the caller could pass "root"
175
   * that also points to repo->gitdir. We want to keep it alive
176
   * until after xstrdup(root). Then we can free it.
177
   */
178
0
  char *old_gitdir = repo->gitdir;
179
180
0
  repo->gitdir = xstrdup(gitfile ? gitfile : root);
181
0
  free(old_gitdir);
182
183
0
  repo_set_commondir(repo, o->commondir);
184
185
0
  if (!repo->objects)
186
0
    repo->objects = odb_new(repo, o->object_dir, o->alternate_db);
187
0
  else if (!o->skip_initializing_odb)
188
0
    BUG("cannot reinitialize an already-initialized object directory");
189
190
0
  repo->disable_ref_updates = o->disable_ref_updates;
191
192
0
  expand_base_dir(&repo->graft_file, o->graft_file,
193
0
      repo->commondir, "info/grafts");
194
0
  expand_base_dir(&repo->index_file, o->index_file,
195
0
      repo->gitdir, "index");
196
0
}
197
198
void repo_set_hash_algo(struct repository *repo, uint32_t hash_algo)
199
0
{
200
0
  repo->hash_algo = &hash_algos[hash_algo];
201
0
}
202
203
void repo_set_compat_hash_algo(struct repository *repo MAYBE_UNUSED, uint32_t algo)
204
0
{
205
#ifdef WITH_RUST
206
  if (hash_algo_by_ptr(repo->hash_algo) == algo)
207
    BUG("hash_algo and compat_hash_algo match");
208
  repo->compat_hash_algo = algo ? &hash_algos[algo] : NULL;
209
  if (repo->compat_hash_algo)
210
    repo_read_loose_object_map(repo);
211
#else
212
0
  if (algo)
213
0
    die(_("compatibility hash algorithm support requires Rust"));
214
0
#endif
215
0
}
216
217
void repo_set_ref_storage_format(struct repository *repo,
218
         enum ref_storage_format format,
219
         const char *payload)
220
0
{
221
0
  repo->ref_storage_format = format;
222
0
  free(repo->ref_storage_payload);
223
0
  repo->ref_storage_payload = xstrdup_or_null(payload);
224
0
}
225
226
/*
227
 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
228
 * Return 0 upon success and a non-zero value upon failure.
229
 */
230
static int repo_init_gitdir(struct repository *repo, const char *gitdir)
231
0
{
232
0
  int ret = 0;
233
0
  int error = 0;
234
0
  char *abspath = NULL;
235
0
  const char *resolved_gitdir;
236
0
  struct set_gitdir_args args = { NULL };
237
238
0
  abspath = real_pathdup(gitdir, 0);
239
0
  if (!abspath) {
240
0
    ret = -1;
241
0
    goto out;
242
0
  }
243
244
  /* 'gitdir' must reference the gitdir directly */
245
0
  resolved_gitdir = resolve_gitdir_gently(abspath, &error);
246
0
  if (!resolved_gitdir) {
247
0
    ret = -1;
248
0
    goto out;
249
0
  }
250
251
0
  repo_set_gitdir(repo, resolved_gitdir, &args);
252
253
0
out:
254
0
  free(abspath);
255
0
  return ret;
256
0
}
257
258
void repo_set_worktree(struct repository *repo, const char *path)
259
0
{
260
0
  repo->worktree = real_pathdup(path, 1);
261
262
0
  trace2_def_repo(repo);
263
0
}
264
265
static int read_and_verify_repository_format(struct repository_format *format,
266
               const char *commondir)
267
0
{
268
0
  int ret = 0;
269
0
  struct strbuf sb = STRBUF_INIT;
270
271
0
  strbuf_addf(&sb, "%s/config", commondir);
272
0
  read_repository_format(format, sb.buf);
273
0
  strbuf_reset(&sb);
274
275
0
  if (verify_repository_format(format, &sb) < 0) {
276
0
    warning("%s", sb.buf);
277
0
    ret = -1;
278
0
  }
279
280
0
  strbuf_release(&sb);
281
0
  return ret;
282
0
}
283
284
/*
285
 * Initialize 'repo' based on the provided 'gitdir'.
286
 * Return 0 upon success and a non-zero value upon failure.
287
 */
288
int repo_init(struct repository *repo,
289
        const char *gitdir,
290
        const char *worktree)
291
0
{
292
0
  struct repository_format format = REPOSITORY_FORMAT_INIT;
293
0
  memset(repo, 0, sizeof(*repo));
294
295
0
  initialize_repository(repo);
296
297
0
  if (repo_init_gitdir(repo, gitdir))
298
0
    goto error;
299
300
0
  if (read_and_verify_repository_format(&format, repo->commondir))
301
0
    goto error;
302
303
0
  repo_set_hash_algo(repo, format.hash_algo);
304
0
  repo_set_compat_hash_algo(repo, format.compat_hash_algo);
305
0
  repo_set_ref_storage_format(repo, format.ref_storage_format,
306
0
            format.ref_storage_payload);
307
0
  repo->repository_format_worktree_config = format.worktree_config;
308
0
  repo->repository_format_relative_worktrees = format.relative_worktrees;
309
0
  repo->repository_format_precious_objects = format.precious_objects;
310
0
  repo->repository_format_submodule_path_cfg = format.submodule_path_cfg;
311
312
  /* take ownership of format.partial_clone */
313
0
  repo->repository_format_partial_clone = format.partial_clone;
314
0
  format.partial_clone = NULL;
315
316
0
  if (worktree)
317
0
    repo_set_worktree(repo, worktree);
318
319
0
  if (repo->compat_hash_algo)
320
0
    repo_read_loose_object_map(repo);
321
322
0
  clear_repository_format(&format);
323
0
  return 0;
324
325
0
error:
326
0
  repo_clear(repo);
327
0
  return -1;
328
0
}
329
330
int repo_submodule_init(struct repository *subrepo,
331
      struct repository *superproject,
332
      const char *path,
333
      const struct object_id *treeish_name)
334
0
{
335
0
  struct strbuf gitdir = STRBUF_INIT;
336
0
  struct strbuf worktree = STRBUF_INIT;
337
0
  int ret = 0;
338
339
0
  repo_worktree_path_append(superproject, &gitdir, "%s/.git", path);
340
0
  repo_worktree_path_append(superproject, &worktree, "%s", path);
341
342
0
  if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
343
    /*
344
     * If initialization fails then it may be due to the submodule
345
     * not being populated in the superproject's worktree.  Instead
346
     * we can try to initialize the submodule by finding it's gitdir
347
     * in the superproject's 'modules' directory.  In this case the
348
     * submodule would not have a worktree.
349
     */
350
0
    const struct submodule *sub =
351
0
      submodule_from_path(superproject, treeish_name, path);
352
0
    if (!sub) {
353
0
      ret = -1;
354
0
      goto out;
355
0
    }
356
357
0
    strbuf_reset(&gitdir);
358
0
    submodule_name_to_gitdir(&gitdir, superproject, sub->name);
359
360
0
    if (repo_init(subrepo, gitdir.buf, NULL)) {
361
0
      ret = -1;
362
0
      goto out;
363
0
    }
364
0
  }
365
366
0
  subrepo->submodule_prefix = xstrfmt("%s%s/",
367
0
              superproject->submodule_prefix ?
368
0
              superproject->submodule_prefix :
369
0
              "", path);
370
371
0
out:
372
0
  strbuf_release(&gitdir);
373
0
  strbuf_release(&worktree);
374
0
  return ret;
375
0
}
376
377
static void repo_clear_path_cache(struct repo_path_cache *cache)
378
0
{
379
0
  FREE_AND_NULL(cache->squash_msg);
380
0
  FREE_AND_NULL(cache->merge_msg);
381
0
  FREE_AND_NULL(cache->merge_rr);
382
0
  FREE_AND_NULL(cache->merge_mode);
383
0
  FREE_AND_NULL(cache->merge_head);
384
0
  FREE_AND_NULL(cache->fetch_head);
385
0
  FREE_AND_NULL(cache->shallow);
386
0
}
387
388
void repo_clear(struct repository *repo)
389
0
{
390
0
  struct hashmap_iter iter;
391
0
  struct strmap_entry *e;
392
393
0
  FREE_AND_NULL(repo->gitdir);
394
0
  FREE_AND_NULL(repo->commondir);
395
0
  FREE_AND_NULL(repo->graft_file);
396
0
  FREE_AND_NULL(repo->index_file);
397
0
  FREE_AND_NULL(repo->worktree);
398
0
  FREE_AND_NULL(repo->submodule_prefix);
399
0
  FREE_AND_NULL(repo->ref_storage_payload);
400
401
0
  odb_free(repo->objects);
402
0
  repo->objects = NULL;
403
404
0
  parsed_object_pool_clear(repo->parsed_objects);
405
0
  FREE_AND_NULL(repo->parsed_objects);
406
407
0
  repo_settings_clear(repo);
408
409
0
  if (repo->config) {
410
0
    git_configset_clear(repo->config);
411
0
    FREE_AND_NULL(repo->config);
412
0
  }
413
414
0
  if (repo->submodule_cache) {
415
0
    submodule_cache_free(repo->submodule_cache);
416
0
    repo->submodule_cache = NULL;
417
0
  }
418
419
0
  if (repo->index) {
420
0
    discard_index(repo->index);
421
0
    FREE_AND_NULL(repo->index);
422
0
  }
423
424
0
  if (repo->hook_config_cache) {
425
0
    hook_cache_clear(repo->hook_config_cache);
426
0
    FREE_AND_NULL(repo->hook_config_cache);
427
0
  }
428
429
0
  if (repo->promisor_remote_config) {
430
0
    promisor_remote_clear(repo->promisor_remote_config);
431
0
    FREE_AND_NULL(repo->promisor_remote_config);
432
0
  }
433
434
0
  if (repo->remote_state) {
435
0
    remote_state_clear(repo->remote_state);
436
0
    FREE_AND_NULL(repo->remote_state);
437
0
  }
438
439
0
  strmap_for_each_entry(&repo->submodule_ref_stores, &iter, e)
440
0
    ref_store_release(e->value);
441
0
  strmap_clear(&repo->submodule_ref_stores, 1);
442
443
0
  strmap_for_each_entry(&repo->worktree_ref_stores, &iter, e)
444
0
    ref_store_release(e->value);
445
0
  strmap_clear(&repo->worktree_ref_stores, 1);
446
447
0
  repo_clear_path_cache(&repo->cached_paths);
448
0
}
449
450
int repo_read_index(struct repository *repo)
451
0
{
452
0
  int res;
453
454
  /* Complete the double-reference */
455
0
  if (!repo->index) {
456
0
    ALLOC_ARRAY(repo->index, 1);
457
0
    index_state_init(repo->index, repo);
458
0
  } else if (repo->index->repo != repo) {
459
0
    BUG("repo's index should point back at itself");
460
0
  }
461
462
0
  res = read_index_from(repo->index, repo->index_file, repo->gitdir);
463
464
0
  prepare_repo_settings(repo);
465
0
  if (repo->settings.command_requires_full_index)
466
0
    ensure_full_index(repo->index);
467
468
  /*
469
   * If sparse checkouts are in use, check whether paths with the
470
   * SKIP_WORKTREE attribute are missing from the worktree; if not,
471
   * clear that attribute for that path.
472
   */
473
0
  clear_skip_worktree_from_present_files(repo->index);
474
475
0
  return res;
476
0
}
477
478
int repo_hold_locked_index(struct repository *repo,
479
         struct lock_file *lf,
480
         int flags)
481
0
{
482
0
  if (!repo->index_file)
483
0
    BUG("the repo hasn't been setup");
484
0
  return hold_lock_file_for_update(lf, repo->index_file, flags);
485
0
}