Coverage Report

Created: 2024-09-08 06:23

/src/git/repository.c
Line
Count
Source (jump to first uncovered line)
1
#include "git-compat-util.h"
2
#include "abspath.h"
3
#include "repository.h"
4
#include "object-store-ll.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
0
{
40
0
  const char *hash_name;
41
0
  int algo;
42
43
0
  hash_name = getenv("GIT_TEST_DEFAULT_HASH_ALGO");
44
0
  if (!hash_name)
45
0
    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
0
{
55
0
  repo->objects = raw_object_store_new();
56
0
  repo->remote_state = remote_state_new();
57
0
  repo->parsed_objects = parsed_object_pool_new();
58
0
  ALLOC_ARRAY(repo->index, 1);
59
0
  index_state_init(repo->index, repo);
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
0
  if (repo == the_repository)
81
0
    set_default_hash_algo(repo);
82
0
}
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
static void repo_set_commondir(struct repository *repo,
95
             const char *commondir)
96
0
{
97
0
  struct strbuf sb = STRBUF_INIT;
98
99
0
  free(repo->commondir);
100
101
0
  if (commondir) {
102
0
    repo->different_commondir = 1;
103
0
    repo->commondir = xstrdup(commondir);
104
0
    return;
105
0
  }
106
107
0
  repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
108
0
  repo->commondir = strbuf_detach(&sb, NULL);
109
0
}
110
111
void repo_set_gitdir(struct repository *repo,
112
         const char *root,
113
         const struct set_gitdir_args *o)
114
0
{
115
0
  const char *gitfile = read_gitfile(root);
116
  /*
117
   * repo->gitdir is saved because the caller could pass "root"
118
   * that also points to repo->gitdir. We want to keep it alive
119
   * until after xstrdup(root). Then we can free it.
120
   */
121
0
  char *old_gitdir = repo->gitdir;
122
123
0
  repo->gitdir = xstrdup(gitfile ? gitfile : root);
124
0
  free(old_gitdir);
125
126
0
  repo_set_commondir(repo, o->commondir);
127
128
0
  if (!repo->objects->odb) {
129
0
    CALLOC_ARRAY(repo->objects->odb, 1);
130
0
    repo->objects->odb_tail = &repo->objects->odb->next;
131
0
  }
132
0
  expand_base_dir(&repo->objects->odb->path, o->object_dir,
133
0
      repo->commondir, "objects");
134
135
0
  repo->objects->odb->disable_ref_updates = o->disable_ref_updates;
136
137
0
  free(repo->objects->alternate_db);
138
0
  repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
139
0
  expand_base_dir(&repo->graft_file, o->graft_file,
140
0
      repo->commondir, "info/grafts");
141
0
  expand_base_dir(&repo->index_file, o->index_file,
142
0
      repo->gitdir, "index");
143
0
}
144
145
void repo_set_hash_algo(struct repository *repo, int hash_algo)
146
0
{
147
0
  repo->hash_algo = &hash_algos[hash_algo];
148
0
}
149
150
void repo_set_compat_hash_algo(struct repository *repo, int algo)
151
0
{
152
0
  if (hash_algo_by_ptr(repo->hash_algo) == algo)
153
0
    BUG("hash_algo and compat_hash_algo match");
154
0
  repo->compat_hash_algo = algo ? &hash_algos[algo] : NULL;
155
0
  if (repo->compat_hash_algo)
156
0
    repo_read_loose_object_map(repo);
157
0
}
158
159
void repo_set_ref_storage_format(struct repository *repo,
160
         enum ref_storage_format format)
161
0
{
162
0
  repo->ref_storage_format = format;
163
0
}
164
165
/*
166
 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
167
 * Return 0 upon success and a non-zero value upon failure.
168
 */
169
static int repo_init_gitdir(struct repository *repo, const char *gitdir)
170
0
{
171
0
  int ret = 0;
172
0
  int error = 0;
173
0
  char *abspath = NULL;
174
0
  const char *resolved_gitdir;
175
0
  struct set_gitdir_args args = { NULL };
176
177
0
  abspath = real_pathdup(gitdir, 0);
178
0
  if (!abspath) {
179
0
    ret = -1;
180
0
    goto out;
181
0
  }
182
183
  /* 'gitdir' must reference the gitdir directly */
184
0
  resolved_gitdir = resolve_gitdir_gently(abspath, &error);
185
0
  if (!resolved_gitdir) {
186
0
    ret = -1;
187
0
    goto out;
188
0
  }
189
190
0
  repo_set_gitdir(repo, resolved_gitdir, &args);
191
192
0
out:
193
0
  free(abspath);
194
0
  return ret;
195
0
}
196
197
void repo_set_worktree(struct repository *repo, const char *path)
198
0
{
199
0
  repo->worktree = real_pathdup(path, 1);
200
201
0
  trace2_def_repo(repo);
202
0
}
203
204
static int read_and_verify_repository_format(struct repository_format *format,
205
               const char *commondir)
206
0
{
207
0
  int ret = 0;
208
0
  struct strbuf sb = STRBUF_INIT;
209
210
0
  strbuf_addf(&sb, "%s/config", commondir);
211
0
  read_repository_format(format, sb.buf);
212
0
  strbuf_reset(&sb);
213
214
0
  if (verify_repository_format(format, &sb) < 0) {
215
0
    warning("%s", sb.buf);
216
0
    ret = -1;
217
0
  }
218
219
0
  strbuf_release(&sb);
220
0
  return ret;
221
0
}
222
223
/*
224
 * Initialize 'repo' based on the provided 'gitdir'.
225
 * Return 0 upon success and a non-zero value upon failure.
226
 */
227
int repo_init(struct repository *repo,
228
        const char *gitdir,
229
        const char *worktree)
230
0
{
231
0
  struct repository_format format = REPOSITORY_FORMAT_INIT;
232
0
  memset(repo, 0, sizeof(*repo));
233
234
0
  initialize_repository(repo);
235
236
0
  if (repo_init_gitdir(repo, gitdir))
237
0
    goto error;
238
239
0
  if (read_and_verify_repository_format(&format, repo->commondir))
240
0
    goto error;
241
242
0
  repo_set_hash_algo(repo, format.hash_algo);
243
0
  repo_set_compat_hash_algo(repo, format.compat_hash_algo);
244
0
  repo_set_ref_storage_format(repo, format.ref_storage_format);
245
0
  repo->repository_format_worktree_config = format.worktree_config;
246
247
  /* take ownership of format.partial_clone */
248
0
  repo->repository_format_partial_clone = format.partial_clone;
249
0
  format.partial_clone = NULL;
250
251
0
  if (worktree)
252
0
    repo_set_worktree(repo, worktree);
253
254
0
  if (repo->compat_hash_algo)
255
0
    repo_read_loose_object_map(repo);
256
257
0
  clear_repository_format(&format);
258
0
  return 0;
259
260
0
error:
261
0
  repo_clear(repo);
262
0
  return -1;
263
0
}
264
265
int repo_submodule_init(struct repository *subrepo,
266
      struct repository *superproject,
267
      const char *path,
268
      const struct object_id *treeish_name)
269
0
{
270
0
  struct strbuf gitdir = STRBUF_INIT;
271
0
  struct strbuf worktree = STRBUF_INIT;
272
0
  int ret = 0;
273
274
0
  strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
275
0
  strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
276
277
0
  if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
278
    /*
279
     * If initialization fails then it may be due to the submodule
280
     * not being populated in the superproject's worktree.  Instead
281
     * we can try to initialize the submodule by finding it's gitdir
282
     * in the superproject's 'modules' directory.  In this case the
283
     * submodule would not have a worktree.
284
     */
285
0
    const struct submodule *sub =
286
0
      submodule_from_path(superproject, treeish_name, path);
287
0
    if (!sub) {
288
0
      ret = -1;
289
0
      goto out;
290
0
    }
291
292
0
    strbuf_reset(&gitdir);
293
0
    submodule_name_to_gitdir(&gitdir, superproject, sub->name);
294
295
0
    if (repo_init(subrepo, gitdir.buf, NULL)) {
296
0
      ret = -1;
297
0
      goto out;
298
0
    }
299
0
  }
300
301
0
  subrepo->submodule_prefix = xstrfmt("%s%s/",
302
0
              superproject->submodule_prefix ?
303
0
              superproject->submodule_prefix :
304
0
              "", path);
305
306
0
out:
307
0
  strbuf_release(&gitdir);
308
0
  strbuf_release(&worktree);
309
0
  return ret;
310
0
}
311
312
static void repo_clear_path_cache(struct repo_path_cache *cache)
313
0
{
314
0
  FREE_AND_NULL(cache->squash_msg);
315
0
  FREE_AND_NULL(cache->squash_msg);
316
0
  FREE_AND_NULL(cache->merge_msg);
317
0
  FREE_AND_NULL(cache->merge_rr);
318
0
  FREE_AND_NULL(cache->merge_mode);
319
0
  FREE_AND_NULL(cache->merge_head);
320
0
  FREE_AND_NULL(cache->fetch_head);
321
0
  FREE_AND_NULL(cache->shallow);
322
0
}
323
324
void repo_clear(struct repository *repo)
325
0
{
326
0
  struct hashmap_iter iter;
327
0
  struct strmap_entry *e;
328
329
0
  FREE_AND_NULL(repo->gitdir);
330
0
  FREE_AND_NULL(repo->commondir);
331
0
  FREE_AND_NULL(repo->graft_file);
332
0
  FREE_AND_NULL(repo->index_file);
333
0
  FREE_AND_NULL(repo->worktree);
334
0
  FREE_AND_NULL(repo->submodule_prefix);
335
336
0
  raw_object_store_clear(repo->objects);
337
0
  FREE_AND_NULL(repo->objects);
338
339
0
  parsed_object_pool_clear(repo->parsed_objects);
340
0
  FREE_AND_NULL(repo->parsed_objects);
341
342
0
  FREE_AND_NULL(repo->settings.fsmonitor);
343
344
0
  if (repo->config) {
345
0
    git_configset_clear(repo->config);
346
0
    FREE_AND_NULL(repo->config);
347
0
  }
348
349
0
  if (repo->submodule_cache) {
350
0
    submodule_cache_free(repo->submodule_cache);
351
0
    repo->submodule_cache = NULL;
352
0
  }
353
354
0
  if (repo->index) {
355
0
    discard_index(repo->index);
356
0
    FREE_AND_NULL(repo->index);
357
0
  }
358
359
0
  if (repo->promisor_remote_config) {
360
0
    promisor_remote_clear(repo->promisor_remote_config);
361
0
    FREE_AND_NULL(repo->promisor_remote_config);
362
0
  }
363
364
0
  if (repo->remote_state) {
365
0
    remote_state_clear(repo->remote_state);
366
0
    FREE_AND_NULL(repo->remote_state);
367
0
  }
368
369
0
  strmap_for_each_entry(&repo->submodule_ref_stores, &iter, e)
370
0
    ref_store_release(e->value);
371
0
  strmap_clear(&repo->submodule_ref_stores, 1);
372
373
0
  strmap_for_each_entry(&repo->worktree_ref_stores, &iter, e)
374
0
    ref_store_release(e->value);
375
0
  strmap_clear(&repo->worktree_ref_stores, 1);
376
377
0
  repo_clear_path_cache(&repo->cached_paths);
378
0
}
379
380
int repo_read_index(struct repository *repo)
381
0
{
382
0
  int res;
383
384
  /* Complete the double-reference */
385
0
  if (!repo->index) {
386
0
    ALLOC_ARRAY(repo->index, 1);
387
0
    index_state_init(repo->index, repo);
388
0
  } else if (repo->index->repo != repo) {
389
0
    BUG("repo's index should point back at itself");
390
0
  }
391
392
0
  res = read_index_from(repo->index, repo->index_file, repo->gitdir);
393
394
0
  prepare_repo_settings(repo);
395
0
  if (repo->settings.command_requires_full_index)
396
0
    ensure_full_index(repo->index);
397
398
  /*
399
   * If sparse checkouts are in use, check whether paths with the
400
   * SKIP_WORKTREE attribute are missing from the worktree; if not,
401
   * clear that attribute for that path.
402
   */
403
0
  clear_skip_worktree_from_present_files(repo->index);
404
405
0
  return res;
406
0
}
407
408
int repo_hold_locked_index(struct repository *repo,
409
         struct lock_file *lf,
410
         int flags)
411
0
{
412
0
  if (!repo->index_file)
413
0
    BUG("the repo hasn't been setup");
414
0
  return hold_lock_file_for_update(lf, repo->index_file, flags);
415
0
}