Coverage Report

Created: 2026-10-17 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/setup.h
Line
Count
Source
1
#ifndef SETUP_H
2
#define SETUP_H
3
4
#include "refs.h"
5
#include "string-list.h"
6
7
int is_inside_git_dir(struct repository *repo);
8
int is_inside_work_tree(struct repository *repo);
9
int get_common_dir_noenv(struct strbuf *sb, const char *gitdir);
10
int get_common_dir(struct strbuf *sb, const char *gitdir);
11
12
/*
13
 * Return true if the given path is a git directory; note that this _just_
14
 * looks at the directory itself. If you want to know whether "foo/.git"
15
 * is a repository, you must feed that path, not just "foo".
16
 */
17
int is_git_directory(const char *path);
18
19
/*
20
 * Return 1 if the given path is the root of a git repository or
21
 * submodule, else 0. Will not return 1 for bare repositories with the
22
 * exception of creating a bare repository in "foo/.git" and calling
23
 * is_git_repository("foo").
24
 *
25
 * If we run into read errors, we err on the side of saying "yes, it is",
26
 * as we usually consider sub-repos precious, and would prefer to err on the
27
 * side of not disrupting or deleting them.
28
 */
29
int is_nonbare_repository_dir(struct strbuf *path);
30
31
0
#define READ_GITFILE_ERR_STAT_FAILED 1
32
0
#define READ_GITFILE_ERR_NOT_A_FILE 2
33
0
#define READ_GITFILE_ERR_OPEN_FAILED 3
34
0
#define READ_GITFILE_ERR_READ_FAILED 4
35
0
#define READ_GITFILE_ERR_INVALID_FORMAT 5
36
0
#define READ_GITFILE_ERR_NO_PATH 6
37
0
#define READ_GITFILE_ERR_NOT_A_REPO 7
38
0
#define READ_GITFILE_ERR_TOO_LARGE 8
39
0
#define READ_GITFILE_ERR_MISSING 9
40
0
#define READ_GITFILE_ERR_IS_A_DIR 10
41
void read_gitfile_error_die(int error_code, const char *path);
42
const char *read_gitfile_gently(const char *path, int *return_error_code);
43
int read_gitfile_raw(struct strbuf *contents, const char *path);
44
0
#define read_gitfile(path) read_gitfile_gently((path), NULL)
45
const char *resolve_gitdir_gently(const char *suspect, int *return_error_code);
46
#define resolve_gitdir(path) resolve_gitdir_gently((path), NULL)
47
48
/*
49
 * Check if a repository is safe and die if it is not, by verifying the
50
 * ownership of the worktree (if any), the git directory, and the gitfile (if
51
 * any).
52
 *
53
 * Exemptions for known-safe repositories can be added via `safe.directory`
54
 * config settings; for non-bare repositories, their worktree needs to be
55
 * added, for bare ones their git directory.
56
 */
57
void die_upon_dubious_ownership(const char *gitfile, const char *worktree,
58
        const char *gitdir);
59
60
void setup_work_tree(struct repository *repo);
61
62
/*
63
 * discover_git_directory_reason() is similar to discover_git_directory(),
64
 * except it returns an enum value instead. It is important to note that
65
 * a zero-valued return here is actually GIT_DIR_NONE, which is different
66
 * from discover_git_directory.
67
 */
68
enum discovery_result {
69
  GIT_DIR_EXPLICIT = 1,
70
  GIT_DIR_DISCOVERED = 2,
71
  GIT_DIR_BARE = 3,
72
  /* these are errors */
73
  GIT_DIR_HIT_CEILING = -1,
74
  GIT_DIR_HIT_MOUNT_POINT = -2,
75
  GIT_DIR_INVALID_GITFILE = -3,
76
  GIT_DIR_INVALID_OWNERSHIP = -4,
77
  GIT_DIR_DISALLOWED_BARE = -5,
78
  GIT_DIR_INVALID_FORMAT = -6,
79
  GIT_DIR_CWD_FAILURE = -7,
80
};
81
enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
82
                struct strbuf *gitdir);
83
84
/*
85
 * Find the commondir and gitdir of the repository that contains the current
86
 * working directory, without changing the working directory or other global
87
 * state. The result is appended to commondir and gitdir.  If the discovered
88
 * gitdir does not correspond to a worktree, then 'commondir' and 'gitdir' will
89
 * both have the same result appended to the buffer.  The return value is
90
 * either 0 upon success and -1 if no repository was found.
91
 */
92
static inline int discover_git_directory(struct strbuf *commondir,
93
           struct strbuf *gitdir)
94
0
{
95
0
  if (discover_git_directory_reason(commondir, gitdir) <= 0)
96
0
    return -1;
97
0
  return 0;
98
0
}
Unexecuted instantiation: commit.c:discover_git_directory
Unexecuted instantiation: config.c:discover_git_directory
Unexecuted instantiation: dir.c:discover_git_directory
Unexecuted instantiation: environment.c:discover_git_directory
Unexecuted instantiation: hook.c:discover_git_directory
Unexecuted instantiation: mailmap.c:discover_git_directory
Unexecuted instantiation: object-file.c:discover_git_directory
Unexecuted instantiation: object-name.c:discover_git_directory
Unexecuted instantiation: odb.c:discover_git_directory
Unexecuted instantiation: path.c:discover_git_directory
Unexecuted instantiation: pathspec.c:discover_git_directory
Unexecuted instantiation: refs.c:discover_git_directory
Unexecuted instantiation: files-backend.c:discover_git_directory
Unexecuted instantiation: reftable-backend.c:discover_git_directory
Unexecuted instantiation: remote.c:discover_git_directory
Unexecuted instantiation: repo-settings.c:discover_git_directory
Unexecuted instantiation: repository.c:discover_git_directory
Unexecuted instantiation: revision.c:discover_git_directory
Unexecuted instantiation: setup.c:discover_git_directory
Unexecuted instantiation: submodule.c:discover_git_directory
Unexecuted instantiation: symlinks.c:discover_git_directory
Unexecuted instantiation: trace.c:discover_git_directory
Unexecuted instantiation: worktree.c:discover_git_directory
Unexecuted instantiation: wt-status.c:discover_git_directory
Unexecuted instantiation: attr.c:discover_git_directory
Unexecuted instantiation: diff.c:discover_git_directory
Unexecuted instantiation: line-log.c:discover_git_directory
Unexecuted instantiation: parse-options-cb.c:discover_git_directory
Unexecuted instantiation: unpack-trees.c:discover_git_directory
Unexecuted instantiation: apply.c:discover_git_directory
99
100
/* Flags that can be passed to `enter_repo()`. */
101
enum {
102
  /*
103
   * Callers that require exact paths (as opposed to allowing known
104
   * suffixes like ".git", ".git/.git" to be omitted) can set this bit.
105
   */
106
  ENTER_REPO_STRICT = (1<<0),
107
108
  /*
109
   * Callers that are willing to run without ownership check can set this
110
   * bit.
111
   */
112
  ENTER_REPO_ANY_OWNER_OK = (1<<1),
113
};
114
115
/*
116
 * Discover and enter a repository.
117
 *
118
 * First, one directory to try is determined by the following algorithm.
119
 *
120
 * (0) If "strict" is given, the path is used as given and no DWIM is
121
 *     done. Otherwise:
122
 * (1) "~/path" to mean path under the running user's home directory;
123
 * (2) "~user/path" to mean path under named user's home directory;
124
 * (3) "relative/path" to mean cwd relative directory; or
125
 * (4) "/absolute/path" to mean absolute directory.
126
 *
127
 * Unless "strict" is given, we check "%s/.git", "%s", "%s.git/.git", "%s.git"
128
 * in this order. We select the first one that is a valid git repository, and
129
 * chdir() to it. If none match, or we fail to chdir, we return NULL.
130
 *
131
 * If all goes well, we return the directory we used to chdir() (but
132
 * before ~user is expanded), avoiding getcwd() resolving symbolic
133
 * links.  User relative paths are also returned as they are given,
134
 * except DWIM suffixing.
135
 */
136
const char *enter_repo(struct repository *repo, const char *path, unsigned flags);
137
138
const char *setup_git_directory_gently(struct repository *repo, int *);
139
const char *setup_git_directory(struct repository *repo);
140
char *prefix_path(struct repository *repo, const char *prefix, int len, const char *path);
141
char *prefix_path_gently(struct repository *repo, const char *prefix, int len, int *remaining, const char *path);
142
143
int check_filename(const char *prefix, const char *name);
144
void verify_filename(struct repository *repo,
145
         const char *prefix,
146
         const char *name,
147
         int diagnose_misspelt_rev);
148
void verify_non_filename(struct repository *repo, const char *prefix, const char *name);
149
int path_inside_repo(struct repository *repo, const char *prefix, const char *path);
150
151
void sanitize_stdfds(void);
152
153
/*
154
 * Daemonize the current process by forking and then exiting the parent
155
 * process. Returns 0 when successful, in which case the parent process will
156
 * have exited and it's the child process that continues to run the code.
157
 * Otherwise, a negative error code is returned and the parent process will
158
 * continue execution.
159
 *
160
 * Note that this function will also perform the following changes:
161
 *
162
 *   - Standard file descriptors in the child process are closed.
163
 *   - The child process is made a session leader via setsid(3p).
164
 *   - All tempfiles owned by the parent process are reassigned to the
165
 *     daemonized child process.
166
 */
167
int daemonize(void);
168
169
/*
170
 * GIT_REPO_VERSION is the version we write by default. The
171
 * _READ variant is the highest number we know how to
172
 * handle.
173
 */
174
0
#define GIT_REPO_VERSION 0
175
0
#define GIT_REPO_VERSION_READ 1
176
177
/*
178
 * You _have_ to initialize a `struct repository_format` using
179
 * `= REPOSITORY_FORMAT_INIT` before calling `read_repository_format()`.
180
 */
181
struct repository_format {
182
  int version;
183
  int precious_objects;
184
  char *partial_clone; /* value of extensions.partialclone */
185
  int worktree_config;
186
  int relative_worktrees;
187
  int submodule_path_cfg;
188
  int is_bare;
189
  int hash_algo;
190
  int compat_hash_algo;
191
  enum ref_storage_format ref_storage_format;
192
  char *ref_storage_payload;
193
  int sparse_index;
194
  char *work_tree;
195
  struct string_list unknown_extensions;
196
  struct string_list v1_only_extensions;
197
};
198
199
/*
200
 * Always use this to initialize a `struct repository_format`
201
 * to a well-defined, default state before calling
202
 * `read_repository()`.
203
 */
204
0
#define REPOSITORY_FORMAT_INIT \
205
0
{ \
206
0
  .version = -1, \
207
0
  .is_bare = -1, \
208
0
  .hash_algo = GIT_HASH_DEFAULT, \
209
0
  .ref_storage_format = REF_STORAGE_FORMAT_FILES, \
210
0
  .unknown_extensions = STRING_LIST_INIT_DUP, \
211
0
  .v1_only_extensions = STRING_LIST_INIT_DUP, \
212
0
}
213
214
/*
215
 * Read the repository format characteristics from the config file "path" into
216
 * "format" struct. Returns the numeric version. On error, or if no version is
217
 * found in the configuration, -1 is returned, format->version is set to -1,
218
 * and all other fields in the struct are set to the default configuration
219
 * (REPOSITORY_FORMAT_INIT). Always initialize the struct using
220
 * REPOSITORY_FORMAT_INIT before calling this function.
221
 */
222
int read_repository_format(struct repository_format *format, const char *path);
223
224
/*
225
 * Free the memory held onto by `format`, but not the struct itself.
226
 * (No need to use this after `read_repository_format()` fails.)
227
 */
228
void clear_repository_format(struct repository_format *format);
229
230
/*
231
 * Verify that the repository described by repository_format is something we
232
 * can read. If it is, return 0. Otherwise, return -1, and "err" will describe
233
 * any errors encountered.
234
 */
235
int verify_repository_format(const struct repository_format *format,
236
           struct strbuf *err);
237
238
enum apply_repository_format_flags {
239
  /*
240
   * Honor environment variables when applying the repository format to
241
   * the repository. For now, this only covers environment variables that
242
   * relate to the object database.
243
   */
244
  APPLY_REPOSITORY_FORMAT_HONOR_ENV = (1 << 0),
245
};
246
247
/*
248
 * Apply the given repository format to the repo. This initializes extensions
249
 * required for normal operation. Returns 0 on success, a negative error code
250
 * when the format is not valid as determined by
251
 * `verify_repository_format()`.
252
 */
253
int apply_repository_format(struct repository *repo,
254
          const struct repository_format *format,
255
          enum apply_repository_format_flags flags,
256
          struct strbuf *err);
257
258
const char *get_template_dir(const char *option_template);
259
260
/*
261
 * Create the repository by creating the necessary directory structures,
262
 * setting up the configuration and configuring the repository's format. If
263
 * `template_dir` is set, copy over templates from that directory. Furthermore,
264
 * if and only if `reinit_ok` is a non-NULL pointer, then the function may
265
 * reinitialize a preexisting repository. In that case, the pointer will be set
266
 * to `1` in case the repo was reinitialized and `0` if it didn't exist yet.
267
 *
268
 * Note that this function does not create the reference and object databases.
269
 */
270
void create_repository(struct repository *repo,
271
           const char *git_dir,
272
           const char *real_git_dir,
273
           const char *worktree,
274
           const char *template_dir,
275
           int hash_algo,
276
           const char *ref_storage_format_uri,
277
           int init_shared_repository,
278
           int *reinit_ok);
279
280
void initialize_repository_version(struct repository *repo,
281
           int hash_algo,
282
           enum ref_storage_format ref_storage_format,
283
           int reinit);
284
285
/*
286
 * Create the reference database for the repository. The repository and its ref
287
 * storage format must have already been configured properly before calling
288
 * this function. When set, `initial_branch` overrides the default branch that
289
 * HEAD will point to.
290
 */
291
void create_reference_database(struct repository *repo, const char *initial_branch, int quiet);
292
293
/*
294
 * Create the object database for the repository. The repository must have
295
 * already been configured properly before calling this function. When set,
296
 * `alternates` is the list of alternates that should be written into the
297
 * object database.
298
 */
299
void create_object_database(struct repository *repo, const struct strvec *alternates);
300
301
/*
302
 * NOTE NOTE NOTE!!
303
 *
304
 * PERM_UMASK, OLD_PERM_GROUP and OLD_PERM_EVERYBODY enumerations must
305
 * not be changed. Old repositories have core.sharedrepository written in
306
 * numeric format, and therefore these values are preserved for compatibility
307
 * reasons.
308
 */
309
enum sharedrepo {
310
  PERM_UMASK          = 0,
311
  OLD_PERM_GROUP      = 1,
312
  OLD_PERM_EVERYBODY  = 2,
313
  PERM_GROUP          = 0660,
314
  PERM_EVERYBODY      = 0664
315
};
316
int git_config_perm(const char *var, const char *value);
317
318
struct startup_info {
319
  /*
320
   * Whether the user is asking us to treat the repository as bare via
321
   * `git --bare`, even if it's not.
322
   */
323
  bool force_bare_repository;
324
325
  int have_repository;
326
  const char *original_cwd;
327
};
328
extern struct startup_info *startup_info;
329
extern const char *tmp_original_cwd;
330
331
#endif /* SETUP_H */