Coverage Report

Created: 2026-03-21 06:46

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