Coverage Report

Created: 2025-12-31 07:01

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