Coverage Report

Created: 2026-11-12 06:03

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