Coverage Report

Created: 2026-03-31 06:24

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(void);
8
int is_inside_work_tree(void);
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, const char *dir);
42
const char *read_gitfile_gently(const char *path, int *return_error_code);
43
0
#define read_gitfile(path) read_gitfile_gently((path), NULL)
44
const char *resolve_gitdir_gently(const char *suspect, int *return_error_code);
45
#define resolve_gitdir(path) resolve_gitdir_gently((path), NULL)
46
47
/*
48
 * Check if a repository is safe and die if it is not, by verifying the
49
 * ownership of the worktree (if any), the git directory, and the gitfile (if
50
 * any).
51
 *
52
 * Exemptions for known-safe repositories can be added via `safe.directory`
53
 * config settings; for non-bare repositories, their worktree needs to be
54
 * added, for bare ones their git directory.
55
 */
56
void die_upon_dubious_ownership(const char *gitfile, const char *worktree,
57
        const char *gitdir);
58
59
void setup_work_tree(void);
60
61
/*
62
 * discover_git_directory_reason() is similar to discover_git_directory(),
63
 * except it returns an enum value instead. It is important to note that
64
 * a zero-valued return here is actually GIT_DIR_NONE, which is different
65
 * from discover_git_directory.
66
 */
67
enum discovery_result {
68
  GIT_DIR_EXPLICIT = 1,
69
  GIT_DIR_DISCOVERED = 2,
70
  GIT_DIR_BARE = 3,
71
  /* these are errors */
72
  GIT_DIR_HIT_CEILING = -1,
73
  GIT_DIR_HIT_MOUNT_POINT = -2,
74
  GIT_DIR_INVALID_GITFILE = -3,
75
  GIT_DIR_INVALID_OWNERSHIP = -4,
76
  GIT_DIR_DISALLOWED_BARE = -5,
77
  GIT_DIR_INVALID_FORMAT = -6,
78
  GIT_DIR_CWD_FAILURE = -7,
79
};
80
enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
81
                struct strbuf *gitdir);
82
83
/*
84
 * Find the commondir and gitdir of the repository that contains the current
85
 * working directory, without changing the working directory or other global
86
 * state. The result is appended to commondir and gitdir.  If the discovered
87
 * gitdir does not correspond to a worktree, then 'commondir' and 'gitdir' will
88
 * both have the same result appended to the buffer.  The return value is
89
 * either 0 upon success and -1 if no repository was found.
90
 */
91
static inline int discover_git_directory(struct strbuf *commondir,
92
           struct strbuf *gitdir)
93
0
{
94
0
  if (discover_git_directory_reason(commondir, gitdir) <= 0)
95
0
    return -1;
96
0
  return 0;
97
0
}
Unexecuted instantiation: repo-settings.c:discover_git_directory
Unexecuted instantiation: repository.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: commit.c:discover_git_directory
Unexecuted instantiation: config.c:discover_git_directory
Unexecuted instantiation: diff.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: parse-options-cb.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: revision.c:discover_git_directory
Unexecuted instantiation: unpack-trees.c:discover_git_directory
Unexecuted instantiation: apply.c:discover_git_directory
Unexecuted instantiation: attr.c:discover_git_directory
Unexecuted instantiation: line-log.c:discover_git_directory
98
99
void set_git_work_tree(const char *tree);
100
101
/* Flags that can be passed to `enter_repo()`. */
102
enum {
103
  /*
104
   * Callers that require exact paths (as opposed to allowing known
105
   * suffixes like ".git", ".git/.git" to be omitted) can set this bit.
106
   */
107
  ENTER_REPO_STRICT = (1<<0),
108
109
  /*
110
   * Callers that are willing to run without ownership check can set this
111
   * bit.
112
   */
113
  ENTER_REPO_ANY_OWNER_OK = (1<<1),
114
};
115
116
/*
117
 * Discover and enter a repository.
118
 *
119
 * First, one directory to try is determined by the following algorithm.
120
 *
121
 * (0) If "strict" is given, the path is used as given and no DWIM is
122
 *     done. Otherwise:
123
 * (1) "~/path" to mean path under the running user's home directory;
124
 * (2) "~user/path" to mean path under named user's home directory;
125
 * (3) "relative/path" to mean cwd relative directory; or
126
 * (4) "/absolute/path" to mean absolute directory.
127
 *
128
 * Unless "strict" is given, we check "%s/.git", "%s", "%s.git/.git", "%s.git"
129
 * in this order. We select the first one that is a valid git repository, and
130
 * chdir() to it. If none match, or we fail to chdir, we return NULL.
131
 *
132
 * If all goes well, we return the directory we used to chdir() (but
133
 * before ~user is expanded), avoiding getcwd() resolving symbolic
134
 * links.  User relative paths are also returned as they are given,
135
 * except DWIM suffixing.
136
 */
137
const char *enter_repo(const char *path, unsigned flags);
138
139
const char *setup_git_directory_gently(int *);
140
const char *setup_git_directory(void);
141
char *prefix_path(const char *prefix, int len, const char *path);
142
char *prefix_path_gently(const char *prefix, int len, int *remaining, const char *path);
143
144
int check_filename(const char *prefix, const char *name);
145
void verify_filename(const char *prefix,
146
         const char *name,
147
         int diagnose_misspelt_rev);
148
void verify_non_filename(const char *prefix, const char *name);
149
int path_inside_repo(const char *prefix, const char *path);
150
151
void sanitize_stdfds(void);
152
int daemonize(void);
153
154
/*
155
 * GIT_REPO_VERSION is the version we write by default. The
156
 * _READ variant is the highest number we know how to
157
 * handle.
158
 */
159
0
#define GIT_REPO_VERSION 0
160
0
#define GIT_REPO_VERSION_READ 1
161
162
/*
163
 * You _have_ to initialize a `struct repository_format` using
164
 * `= REPOSITORY_FORMAT_INIT` before calling `read_repository_format()`.
165
 */
166
struct repository_format {
167
  int version;
168
  int precious_objects;
169
  char *partial_clone; /* value of extensions.partialclone */
170
  int worktree_config;
171
  int relative_worktrees;
172
  int submodule_path_cfg;
173
  int is_bare;
174
  int hash_algo;
175
  int compat_hash_algo;
176
  enum ref_storage_format ref_storage_format;
177
  char *ref_storage_payload;
178
  int sparse_index;
179
  char *work_tree;
180
  struct string_list unknown_extensions;
181
  struct string_list v1_only_extensions;
182
};
183
184
/*
185
 * Always use this to initialize a `struct repository_format`
186
 * to a well-defined, default state before calling
187
 * `read_repository()`.
188
 */
189
0
#define REPOSITORY_FORMAT_INIT \
190
0
{ \
191
0
  .version = -1, \
192
0
  .is_bare = -1, \
193
0
  .hash_algo = GIT_HASH_DEFAULT, \
194
0
  .ref_storage_format = REF_STORAGE_FORMAT_FILES, \
195
0
  .unknown_extensions = STRING_LIST_INIT_DUP, \
196
0
  .v1_only_extensions = STRING_LIST_INIT_DUP, \
197
0
}
198
199
/*
200
 * Read the repository format characteristics from the config file "path" into
201
 * "format" struct. Returns the numeric version. On error, or if no version is
202
 * found in the configuration, -1 is returned, format->version is set to -1,
203
 * and all other fields in the struct are set to the default configuration
204
 * (REPOSITORY_FORMAT_INIT). Always initialize the struct using
205
 * REPOSITORY_FORMAT_INIT before calling this function.
206
 */
207
int read_repository_format(struct repository_format *format, const char *path);
208
209
/*
210
 * Free the memory held onto by `format`, but not the struct itself.
211
 * (No need to use this after `read_repository_format()` fails.)
212
 */
213
void clear_repository_format(struct repository_format *format);
214
215
/*
216
 * Verify that the repository described by repository_format is something we
217
 * can read. If it is, return 0. Otherwise, return -1, and "err" will describe
218
 * any errors encountered.
219
 */
220
int verify_repository_format(const struct repository_format *format,
221
           struct strbuf *err);
222
223
/*
224
 * Check the repository format version in the path found in repo_get_git_dir(the_repository),
225
 * and die if it is a version we don't understand. Generally one would
226
 * set_git_dir() before calling this, and use it only for "are we in a valid
227
 * repo?".
228
 *
229
 * If successful and fmt is not NULL, fill fmt with data.
230
 */
231
void check_repository_format(struct repository_format *fmt);
232
233
const char *get_template_dir(const char *option_template);
234
235
0
#define INIT_DB_QUIET      (1 << 0)
236
0
#define INIT_DB_EXIST_OK   (1 << 1)
237
0
#define INIT_DB_SKIP_REFDB (1 << 2)
238
239
int init_db(const char *git_dir, const char *real_git_dir,
240
      const char *template_dir, int hash_algo,
241
      enum ref_storage_format ref_storage_format,
242
      const char *initial_branch, int init_shared_repository,
243
      unsigned int flags);
244
void initialize_repository_version(int hash_algo,
245
           enum ref_storage_format ref_storage_format,
246
           int reinit);
247
void create_reference_database(const char *initial_branch, int quiet);
248
249
/*
250
 * NOTE NOTE NOTE!!
251
 *
252
 * PERM_UMASK, OLD_PERM_GROUP and OLD_PERM_EVERYBODY enumerations must
253
 * not be changed. Old repositories have core.sharedrepository written in
254
 * numeric format, and therefore these values are preserved for compatibility
255
 * reasons.
256
 */
257
enum sharedrepo {
258
  PERM_UMASK          = 0,
259
  OLD_PERM_GROUP      = 1,
260
  OLD_PERM_EVERYBODY  = 2,
261
  PERM_GROUP          = 0660,
262
  PERM_EVERYBODY      = 0664
263
};
264
int git_config_perm(const char *var, const char *value);
265
266
struct startup_info {
267
  int have_repository;
268
  const char *prefix;
269
  const char *original_cwd;
270
};
271
extern struct startup_info *startup_info;
272
extern const char *tmp_original_cwd;
273
274
#endif /* SETUP_H */