]>
Commit | Line | Data |
---|---|---|
3315782c | 1 | /* |
359fc2d2 | 2 | * Copyright (C) the libgit2 contributors. All rights reserved. |
3315782c | 3 | * |
bb742ede VM |
4 | * This file is part of libgit2, distributed under the GNU GPL v2 with |
5 | * a Linking Exception. For full terms see the included COPYING file. | |
3315782c | 6 | */ |
eae0bfdc PP |
7 | |
8 | #include "repository.h" | |
9 | ||
7784bcbb | 10 | #include <ctype.h> |
3315782c | 11 | |
44908fe7 | 12 | #include "git2/object.h" |
1384b688 | 13 | #include "git2/sys/repository.h" |
d12299fe | 14 | |
e579e0f7 | 15 | #include "buf.h" |
3315782c | 16 | #include "common.h" |
3315782c VM |
17 | #include "commit.h" |
18 | #include "tag.h" | |
237da401 | 19 | #include "blob.h" |
22a2d3d5 | 20 | #include "futils.h" |
83634d38 | 21 | #include "sysdir.h" |
114f5a6c RB |
22 | #include "filebuf.h" |
23 | #include "index.h" | |
b22d1479 | 24 | #include "config.h" |
9282e921 | 25 | #include "refs.h" |
47bfa0be RB |
26 | #include "filter.h" |
27 | #include "odb.h" | |
eae0bfdc | 28 | #include "refdb.h" |
096d9e94 | 29 | #include "remote.h" |
632d8b23 | 30 | #include "merge.h" |
114f5a6c | 31 | #include "diff_driver.h" |
62d38a1d | 32 | #include "annotated_commit.h" |
4d99c4cf | 33 | #include "submodule.h" |
8c8d726e | 34 | #include "worktree.h" |
e579e0f7 | 35 | #include "path.h" |
4d99c4cf | 36 | #include "strmap.h" |
9282e921 | 37 | |
c2c81615 PK |
38 | #ifdef GIT_WIN32 |
39 | # include "win32/w32_util.h" | |
40 | #endif | |
41 | ||
e579e0f7 | 42 | bool git_repository__validate_ownership = true; |
6c23704d PS |
43 | bool git_repository__fsync_gitdir = false; |
44 | ||
cb3269c9 PS |
45 | static const struct { |
46 | git_repository_item_t parent; | |
22a2d3d5 | 47 | git_repository_item_t fallback; |
cb3269c9 PS |
48 | const char *name; |
49 | bool directory; | |
50 | } items[] = { | |
22a2d3d5 UG |
51 | { GIT_REPOSITORY_ITEM_GITDIR, GIT_REPOSITORY_ITEM__LAST, NULL, true }, |
52 | { GIT_REPOSITORY_ITEM_WORKDIR, GIT_REPOSITORY_ITEM__LAST, NULL, true }, | |
53 | { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM__LAST, NULL, true }, | |
54 | { GIT_REPOSITORY_ITEM_GITDIR, GIT_REPOSITORY_ITEM__LAST, "index", false }, | |
55 | { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM_GITDIR, "objects", true }, | |
56 | { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM_GITDIR, "refs", true }, | |
57 | { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM_GITDIR, "packed-refs", false }, | |
58 | { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM_GITDIR, "remotes", true }, | |
59 | { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM_GITDIR, "config", false }, | |
60 | { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM_GITDIR, "info", true }, | |
61 | { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM_GITDIR, "hooks", true }, | |
62 | { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM_GITDIR, "logs", true }, | |
63 | { GIT_REPOSITORY_ITEM_GITDIR, GIT_REPOSITORY_ITEM__LAST, "modules", true }, | |
64 | { GIT_REPOSITORY_ITEM_COMMONDIR, GIT_REPOSITORY_ITEM_GITDIR, "worktrees", true } | |
cb3269c9 PS |
65 | }; |
66 | ||
22a2d3d5 UG |
67 | static int check_repositoryformatversion(int *version, git_config *config); |
68 | static int check_extensions(git_config *config, int version); | |
e579e0f7 | 69 | static int load_global_config(git_config **config); |
16c73d38 | 70 | |
c09fd54e | 71 | #define GIT_COMMONDIR_FILE "commondir" |
39abd3ad | 72 | #define GIT_GITDIR_FILE "gitdir" |
c09fd54e | 73 | |
7784bcbb | 74 | #define GIT_FILE_CONTENT_PREFIX "gitdir:" |
6a01b6bd | 75 | |
22a2d3d5 | 76 | #define GIT_BRANCH_DEFAULT "master" |
28990938 | 77 | |
29e948de | 78 | #define GIT_REPO_VERSION 0 |
22a2d3d5 | 79 | #define GIT_REPO_MAX_VERSION 1 |
691aa968 | 80 | |
e579e0f7 | 81 | git_str git_repository__reserved_names_win32[] = { |
4196dd8e ET |
82 | { DOT_GIT, 0, CONST_STRLEN(DOT_GIT) }, |
83 | { GIT_DIR_SHORTNAME, 0, CONST_STRLEN(GIT_DIR_SHORTNAME) } | |
84 | }; | |
85 | size_t git_repository__reserved_names_win32_len = 2; | |
86 | ||
e579e0f7 | 87 | git_str git_repository__reserved_names_posix[] = { |
4196dd8e ET |
88 | { DOT_GIT, 0, CONST_STRLEN(DOT_GIT) }, |
89 | }; | |
90 | size_t git_repository__reserved_names_posix_len = 1; | |
a64119e3 | 91 | |
e976b56d | 92 | static void set_odb(git_repository *repo, git_odb *odb) |
9462c471 | 93 | { |
e976b56d RB |
94 | if (odb) { |
95 | GIT_REFCOUNT_OWN(odb, repo); | |
96 | GIT_REFCOUNT_INC(odb); | |
97 | } | |
98 | ||
c25aa7cd | 99 | if ((odb = git_atomic_swap(repo->_odb, odb)) != NULL) { |
e976b56d RB |
100 | GIT_REFCOUNT_OWN(odb, NULL); |
101 | git_odb_free(odb); | |
eb2f3b47 | 102 | } |
9462c471 | 103 | } |
691aa968 | 104 | |
e976b56d | 105 | static void set_refdb(git_repository *repo, git_refdb *refdb) |
d00d5464 | 106 | { |
e976b56d RB |
107 | if (refdb) { |
108 | GIT_REFCOUNT_OWN(refdb, repo); | |
109 | GIT_REFCOUNT_INC(refdb); | |
110 | } | |
111 | ||
c25aa7cd | 112 | if ((refdb = git_atomic_swap(repo->_refdb, refdb)) != NULL) { |
e976b56d RB |
113 | GIT_REFCOUNT_OWN(refdb, NULL); |
114 | git_refdb_free(refdb); | |
d00d5464 ET |
115 | } |
116 | } | |
117 | ||
e976b56d | 118 | static void set_config(git_repository *repo, git_config *config) |
9462c471 | 119 | { |
e976b56d RB |
120 | if (config) { |
121 | GIT_REFCOUNT_OWN(config, repo); | |
122 | GIT_REFCOUNT_INC(config); | |
123 | } | |
124 | ||
c25aa7cd | 125 | if ((config = git_atomic_swap(repo->_config, config)) != NULL) { |
e976b56d RB |
126 | GIT_REFCOUNT_OWN(config, NULL); |
127 | git_config_free(config); | |
eb2f3b47 | 128 | } |
e976b56d | 129 | |
22a2d3d5 | 130 | git_repository__configmap_lookup_cache_clear(repo); |
691aa968 VM |
131 | } |
132 | ||
e976b56d | 133 | static void set_index(git_repository *repo, git_index *index) |
6fd195d7 | 134 | { |
e976b56d RB |
135 | if (index) { |
136 | GIT_REFCOUNT_OWN(index, repo); | |
137 | GIT_REFCOUNT_INC(index); | |
138 | } | |
139 | ||
c25aa7cd | 140 | if ((index = git_atomic_swap(repo->_index, index)) != NULL) { |
e976b56d RB |
141 | GIT_REFCOUNT_OWN(index, NULL); |
142 | git_index_free(index); | |
9462c471 | 143 | } |
e0011be3 VM |
144 | } |
145 | ||
22a2d3d5 | 146 | int git_repository__cleanup(git_repository *repo) |
e0011be3 | 147 | { |
c25aa7cd | 148 | GIT_ASSERT_ARG(repo); |
6fd195d7 | 149 | |
4d99c4cf | 150 | git_repository_submodule_cache_clear(repo); |
879458e7 | 151 | git_cache_clear(&repo->objects); |
73b51450 | 152 | git_attr_cache_flush(repo); |
6fd195d7 | 153 | |
e976b56d RB |
154 | set_config(repo, NULL); |
155 | set_index(repo, NULL); | |
156 | set_odb(repo, NULL); | |
157 | set_refdb(repo, NULL); | |
22a2d3d5 UG |
158 | |
159 | return 0; | |
879458e7 VM |
160 | } |
161 | ||
162 | void git_repository_free(git_repository *repo) | |
163 | { | |
4196dd8e ET |
164 | size_t i; |
165 | ||
879458e7 VM |
166 | if (repo == NULL) |
167 | return; | |
168 | ||
169 | git_repository__cleanup(repo); | |
170 | ||
22a2d3d5 | 171 | git_cache_dispose(&repo->objects); |
3eadfecd | 172 | |
114f5a6c | 173 | git_diff_driver_registry_free(repo->diff_drivers); |
3eadfecd | 174 | repo->diff_drivers = NULL; |
9462c471 | 175 | |
4196dd8e | 176 | for (i = 0; i < repo->reserved_names.size; i++) |
e579e0f7 | 177 | git_str_dispose(git_array_get(repo->reserved_names, i)); |
78db0239 | 178 | git_array_clear(repo->reserved_names); |
4196dd8e | 179 | |
84f56cb0 PS |
180 | git__free(repo->gitlink); |
181 | git__free(repo->gitdir); | |
c09fd54e | 182 | git__free(repo->commondir); |
53607868 | 183 | git__free(repo->workdir); |
bade5194 | 184 | git__free(repo->namespace); |
659cf202 CMN |
185 | git__free(repo->ident_name); |
186 | git__free(repo->ident_email); | |
53607868 | 187 | |
6de9b2ee | 188 | git__memzero(repo, sizeof(*repo)); |
9462c471 | 189 | git__free(repo); |
6fd195d7 VM |
190 | } |
191 | ||
c25aa7cd | 192 | /* Check if we have a separate commondir (e.g. we have a worktree) */ |
e579e0f7 | 193 | static int lookup_commondir(bool *separate, git_str *commondir, git_str *repository_path) |
c25aa7cd | 194 | { |
e579e0f7 | 195 | git_str common_link = GIT_STR_INIT; |
c25aa7cd PP |
196 | int error; |
197 | ||
198 | /* | |
199 | * If there's no commondir file, the repository path is the | |
200 | * common path, but it needs a trailing slash. | |
201 | */ | |
e579e0f7 MB |
202 | if (!git_fs_path_contains_file(repository_path, GIT_COMMONDIR_FILE)) { |
203 | if ((error = git_str_set(commondir, repository_path->ptr, repository_path->size)) == 0) | |
204 | error = git_fs_path_to_dir(commondir); | |
c25aa7cd PP |
205 | |
206 | *separate = false; | |
207 | goto done; | |
208 | } | |
209 | ||
210 | *separate = true; | |
211 | ||
e579e0f7 | 212 | if ((error = git_str_joinpath(&common_link, repository_path->ptr, GIT_COMMONDIR_FILE)) < 0 || |
c25aa7cd PP |
213 | (error = git_futils_readbuffer(&common_link, common_link.ptr)) < 0) |
214 | goto done; | |
215 | ||
e579e0f7 MB |
216 | git_str_rtrim(&common_link); |
217 | if (git_fs_path_is_relative(common_link.ptr)) { | |
218 | if ((error = git_str_joinpath(commondir, repository_path->ptr, common_link.ptr)) < 0) | |
c25aa7cd PP |
219 | goto done; |
220 | } else { | |
e579e0f7 | 221 | git_str_swap(commondir, &common_link); |
c25aa7cd PP |
222 | } |
223 | ||
e579e0f7 | 224 | git_str_dispose(&common_link); |
c25aa7cd PP |
225 | |
226 | /* Make sure the commondir path always has a trailing slash */ | |
e579e0f7 | 227 | error = git_fs_path_prettify_dir(commondir, commondir->ptr, NULL); |
c25aa7cd PP |
228 | |
229 | done: | |
230 | return error; | |
231 | } | |
232 | ||
e579e0f7 | 233 | GIT_INLINE(int) validate_repo_path(git_str *path) |
c25aa7cd PP |
234 | { |
235 | /* | |
236 | * The longest static path in a repository (or commondir) is the | |
237 | * packed refs file. (Loose refs may be longer since they | |
238 | * include the reference name, but will be validated when the | |
239 | * path is constructed.) | |
240 | */ | |
241 | static size_t suffix_len = | |
242 | CONST_STRLEN("objects/pack/pack-.pack.lock") + | |
243 | GIT_OID_HEXSZ; | |
244 | ||
e579e0f7 MB |
245 | return git_fs_path_validate_str_length_with_suffix( |
246 | path, suffix_len); | |
c25aa7cd PP |
247 | } |
248 | ||
9462c471 VM |
249 | /* |
250 | * Git repository open methods | |
251 | * | |
252 | * Open a repository object from its path | |
253 | */ | |
e579e0f7 | 254 | static int is_valid_repository_path(bool *out, git_str *repository_path, git_str *common_path) |
5ad739e8 | 255 | { |
c25aa7cd | 256 | bool separate_commondir = false; |
22a2d3d5 UG |
257 | int error; |
258 | ||
259 | *out = false; | |
260 | ||
c25aa7cd PP |
261 | if ((error = lookup_commondir(&separate_commondir, common_path, repository_path)) < 0) |
262 | return error; | |
5ad739e8 | 263 | |
97769280 | 264 | /* Ensure HEAD file exists */ |
e579e0f7 | 265 | if (git_fs_path_contains_file(repository_path, GIT_HEAD_FILE) == false) |
22a2d3d5 | 266 | return 0; |
c25aa7cd | 267 | |
c09fd54e | 268 | /* Check files in common dir */ |
e579e0f7 | 269 | if (git_fs_path_contains_dir(common_path, GIT_OBJECTS_DIR) == false) |
22a2d3d5 | 270 | return 0; |
e579e0f7 | 271 | if (git_fs_path_contains_dir(common_path, GIT_REFS_DIR) == false) |
22a2d3d5 | 272 | return 0; |
5ad739e8 | 273 | |
c25aa7cd PP |
274 | /* Ensure the repo (and commondir) are valid paths */ |
275 | if ((error = validate_repo_path(common_path)) < 0 || | |
276 | (separate_commondir && | |
277 | (error = validate_repo_path(repository_path)) < 0)) | |
278 | return error; | |
279 | ||
22a2d3d5 UG |
280 | *out = true; |
281 | return 0; | |
5ad739e8 VM |
282 | } |
283 | ||
51d00446 | 284 | static git_repository *repository_alloc(void) |
3315782c | 285 | { |
3e9e6cda | 286 | git_repository *repo = git__calloc(1, sizeof(git_repository)); |
3315782c | 287 | |
4196dd8e ET |
288 | if (repo == NULL || |
289 | git_cache_init(&repo->objects) < 0) | |
290 | goto on_error; | |
291 | ||
292 | git_array_init_to_size(repo->reserved_names, 4); | |
293 | if (!repo->reserved_names.ptr) | |
294 | goto on_error; | |
3315782c | 295 | |
22a2d3d5 UG |
296 | /* set all the entries in the configmap cache to `unset` */ |
297 | git_repository__configmap_lookup_cache_clear(repo); | |
f2c25d18 | 298 | |
6fd195d7 | 299 | return repo; |
4196dd8e ET |
300 | |
301 | on_error: | |
302 | if (repo) | |
22a2d3d5 | 303 | git_cache_dispose(&repo->objects); |
4196dd8e ET |
304 | |
305 | git__free(repo); | |
306 | return NULL; | |
6fd195d7 VM |
307 | } |
308 | ||
7cc3c920 JW |
309 | int git_repository_new(git_repository **out) |
310 | { | |
770aca94 ET |
311 | git_repository *repo; |
312 | ||
313 | *out = repo = repository_alloc(); | |
ac3d33df | 314 | GIT_ERROR_CHECK_ALLOC(repo); |
770aca94 ET |
315 | |
316 | repo->is_bare = 1; | |
79ab3ef6 | 317 | repo->is_worktree = 0; |
770aca94 | 318 | |
7cc3c920 JW |
319 | return 0; |
320 | } | |
321 | ||
29c4cb09 | 322 | static int load_config_data(git_repository *repo, const git_config *config) |
ec3c7a16 | 323 | { |
cb8a7961 | 324 | int is_bare; |
ec3c7a16 | 325 | |
6c7cee42 RD |
326 | int err = git_config_get_bool(&is_bare, config, "core.bare"); |
327 | if (err < 0 && err != GIT_ENOTFOUND) | |
328 | return err; | |
329 | ||
d3e9c4a5 | 330 | /* Try to figure out if it's bare, default to non-bare if it's not set */ |
6c7cee42 RD |
331 | if (err != GIT_ENOTFOUND) |
332 | repo->is_bare = is_bare && !repo->is_worktree; | |
d3e9c4a5 | 333 | else |
6c7cee42 | 334 | repo->is_bare = 0; |
c94785a9 | 335 | |
cb8a7961 | 336 | return 0; |
9462c471 | 337 | } |
ec3c7a16 | 338 | |
e579e0f7 | 339 | static int load_workdir(git_repository *repo, git_config *config, git_str *parent_path) |
9462c471 | 340 | { |
39abd3ad | 341 | int error; |
9a97f49e | 342 | git_config_entry *ce; |
e579e0f7 MB |
343 | git_str worktree = GIT_STR_INIT; |
344 | git_str path = GIT_STR_INIT; | |
ec3c7a16 | 345 | |
97769280 | 346 | if (repo->is_bare) |
cb8a7961 | 347 | return 0; |
ec3c7a16 | 348 | |
9f77b3f6 RB |
349 | if ((error = git_config__lookup_entry( |
350 | &ce, config, "core.worktree", false)) < 0) | |
351 | return error; | |
ec3c7a16 | 352 | |
39abd3ad PS |
353 | if (repo->is_worktree) { |
354 | char *gitlink = git_worktree__read_link(repo->gitdir, GIT_GITDIR_FILE); | |
355 | if (!gitlink) { | |
356 | error = -1; | |
357 | goto cleanup; | |
358 | } | |
359 | ||
e579e0f7 | 360 | git_str_attach(&worktree, gitlink, 0); |
39abd3ad | 361 | |
e579e0f7 MB |
362 | if ((git_fs_path_dirname_r(&worktree, worktree.ptr)) < 0 || |
363 | git_fs_path_to_dir(&worktree) < 0) { | |
39abd3ad PS |
364 | error = -1; |
365 | goto cleanup; | |
366 | } | |
367 | ||
e579e0f7 | 368 | repo->workdir = git_str_detach(&worktree); |
39abd3ad PS |
369 | } |
370 | else if (ce && ce->value) { | |
e579e0f7 | 371 | if ((error = git_fs_path_prettify_dir( |
84f56cb0 | 372 | &worktree, ce->value, repo->gitdir)) < 0) |
9a97f49e | 373 | goto cleanup; |
9f77b3f6 | 374 | |
e579e0f7 | 375 | repo->workdir = git_str_detach(&worktree); |
5f4a61ae | 376 | } |
e579e0f7 MB |
377 | else if (parent_path && git_fs_path_isdir(parent_path->ptr)) |
378 | repo->workdir = git_str_detach(parent_path); | |
7784bcbb | 379 | else { |
e579e0f7 MB |
380 | if (git_fs_path_dirname_r(&worktree, repo->gitdir) < 0 || |
381 | git_fs_path_to_dir(&worktree) < 0) { | |
9a97f49e CMN |
382 | error = -1; |
383 | goto cleanup; | |
384 | } | |
9f77b3f6 | 385 | |
e579e0f7 | 386 | repo->workdir = git_str_detach(&worktree); |
7784bcbb RB |
387 | } |
388 | ||
ac3d33df | 389 | GIT_ERROR_CHECK_ALLOC(repo->workdir); |
9a97f49e | 390 | cleanup: |
e579e0f7 | 391 | git_str_dispose(&path); |
9a97f49e CMN |
392 | git_config_entry_free(ce); |
393 | return error; | |
ec3c7a16 VM |
394 | } |
395 | ||
7784bcbb RB |
396 | /* |
397 | * This function returns furthest offset into path where a ceiling dir | |
398 | * is found, so we can stop processing the path at that point. | |
399 | * | |
e579e0f7 | 400 | * Note: converting this to use git_strs instead of GIT_PATH_MAX buffers on |
7784bcbb RB |
401 | * the stack could remove directories name limits, but at the cost of doing |
402 | * repeated malloc/frees inside the loop below, so let's not do it now. | |
403 | */ | |
b118f647 | 404 | static size_t find_ceiling_dir_offset( |
7784bcbb RB |
405 | const char *path, |
406 | const char *ceiling_directories) | |
407 | { | |
408 | char buf[GIT_PATH_MAX + 1]; | |
409 | char buf2[GIT_PATH_MAX + 1]; | |
410 | const char *ceil, *sep; | |
44ef8b1b | 411 | size_t len, max_len = 0, min_len; |
7784bcbb | 412 | |
c25aa7cd | 413 | GIT_ASSERT_ARG(path); |
7784bcbb | 414 | |
e579e0f7 | 415 | min_len = (size_t)(git_fs_path_root(path) + 1); |
7784bcbb RB |
416 | |
417 | if (ceiling_directories == NULL || min_len == 0) | |
b118f647 | 418 | return min_len; |
7784bcbb RB |
419 | |
420 | for (sep = ceil = ceiling_directories; *sep; ceil = sep + 1) { | |
421 | for (sep = ceil; *sep && *sep != GIT_PATH_LIST_SEPARATOR; sep++); | |
422 | len = sep - ceil; | |
423 | ||
e579e0f7 | 424 | if (len == 0 || len >= sizeof(buf) || git_fs_path_root(ceil) == -1) |
7784bcbb RB |
425 | continue; |
426 | ||
427 | strncpy(buf, ceil, len); | |
428 | buf[len] = '\0'; | |
429 | ||
430 | if (p_realpath(buf, buf2) == NULL) | |
431 | continue; | |
432 | ||
433 | len = strlen(buf2); | |
434 | if (len > 0 && buf2[len-1] == '/') | |
435 | buf[--len] = '\0'; | |
436 | ||
437 | if (!strncmp(path, buf2, len) && | |
3fe046cf | 438 | (path[len] == '/' || !path[len]) && |
7784bcbb RB |
439 | len > max_len) |
440 | { | |
441 | max_len = len; | |
442 | } | |
443 | } | |
444 | ||
b118f647 | 445 | return (max_len <= min_len ? min_len : max_len); |
7784bcbb RB |
446 | } |
447 | ||
448 | /* | |
449 | * Read the contents of `file_path` and set `path_out` to the repo dir that | |
450 | * it points to. Before calling, set `path_out` to the base directory that | |
451 | * should be used if the contents of `file_path` are a relative path. | |
452 | */ | |
e579e0f7 | 453 | static int read_gitfile(git_str *path_out, const char *file_path) |
7784bcbb RB |
454 | { |
455 | int error = 0; | |
e579e0f7 | 456 | git_str file = GIT_STR_INIT; |
7784bcbb RB |
457 | size_t prefix_len = strlen(GIT_FILE_CONTENT_PREFIX); |
458 | ||
c25aa7cd PP |
459 | GIT_ASSERT_ARG(path_out); |
460 | GIT_ASSERT_ARG(file_path); | |
7784bcbb RB |
461 | |
462 | if (git_futils_readbuffer(&file, file_path) < 0) | |
463 | return -1; | |
464 | ||
e579e0f7 | 465 | git_str_rtrim(&file); |
6b415f62 | 466 | /* apparently on Windows, some people use backslashes in paths */ |
e579e0f7 | 467 | git_fs_path_mkposix(file.ptr); |
7784bcbb | 468 | |
e579e0f7 MB |
469 | if (git_str_len(&file) <= prefix_len || |
470 | memcmp(git_str_cstr(&file), GIT_FILE_CONTENT_PREFIX, prefix_len) != 0) | |
7784bcbb | 471 | { |
ac3d33df | 472 | git_error_set(GIT_ERROR_REPOSITORY, |
909d5494 | 473 | "the `.git` file at '%s' is malformed", file_path); |
7784bcbb RB |
474 | error = -1; |
475 | } | |
e579e0f7 MB |
476 | else if ((error = git_fs_path_dirname_r(path_out, file_path)) >= 0) { |
477 | const char *gitlink = git_str_cstr(&file) + prefix_len; | |
0f49200c | 478 | while (*gitlink && git__isspace(*gitlink)) gitlink++; |
6b415f62 | 479 | |
e579e0f7 MB |
480 | error = git_fs_path_prettify_dir( |
481 | path_out, gitlink, git_str_cstr(path_out)); | |
482 | } | |
483 | ||
484 | git_str_dispose(&file); | |
485 | return error; | |
486 | } | |
487 | ||
488 | typedef struct { | |
489 | const char *repo_path; | |
490 | git_str tmp; | |
ad5611d8 | 491 | bool *is_safe; |
e579e0f7 MB |
492 | } validate_ownership_data; |
493 | ||
494 | static int validate_ownership_cb(const git_config_entry *entry, void *payload) | |
495 | { | |
496 | validate_ownership_data *data = payload; | |
497 | ||
498 | if (strcmp(entry->value, "") == 0) | |
ad5611d8 | 499 | *data->is_safe = false; |
e579e0f7 MB |
500 | |
501 | if (git_fs_path_prettify_dir(&data->tmp, entry->value, NULL) == 0 && | |
502 | strcmp(data->tmp.ptr, data->repo_path) == 0) | |
ad5611d8 | 503 | *data->is_safe = true; |
e579e0f7 MB |
504 | |
505 | return 0; | |
506 | } | |
507 | ||
ad5611d8 | 508 | static int validate_ownership_config(bool *is_safe, const char *path) |
e579e0f7 | 509 | { |
ad5611d8 TR |
510 | validate_ownership_data ownership_data = { |
511 | path, GIT_STR_INIT, is_safe | |
512 | }; | |
513 | git_config *config; | |
e579e0f7 MB |
514 | int error; |
515 | ||
ad5611d8 TR |
516 | if (load_global_config(&config) != 0) |
517 | return 0; | |
e579e0f7 | 518 | |
ad5611d8 TR |
519 | error = git_config_get_multivar_foreach(config, |
520 | "safe.directory", NULL, | |
521 | validate_ownership_cb, | |
522 | &ownership_data); | |
523 | ||
524 | git_config_free(config); | |
525 | git_str_dispose(&ownership_data.tmp); | |
526 | ||
527 | return error; | |
528 | } | |
7784bcbb | 529 | |
ad5611d8 TR |
530 | static int validate_ownership_path(bool *is_safe, const char *path) |
531 | { | |
532 | git_fs_path_owner_t owner_level = | |
533 | GIT_FS_PATH_OWNER_CURRENT_USER | | |
534 | GIT_FS_PATH_USER_IS_ADMINISTRATOR | | |
535 | GIT_FS_PATH_OWNER_RUNNING_SUDO; | |
536 | int error = 0; | |
537 | ||
538 | if (path) | |
539 | error = git_fs_path_owner_is(is_safe, path, owner_level); | |
540 | ||
541 | if (error == GIT_ENOTFOUND) { | |
542 | *is_safe = true; | |
e579e0f7 | 543 | error = 0; |
e579e0f7 MB |
544 | } |
545 | ||
ad5611d8 TR |
546 | return error; |
547 | } | |
e579e0f7 | 548 | |
ad5611d8 TR |
549 | static int validate_ownership(git_repository *repo) |
550 | { | |
551 | const char *validation_paths[3] = { NULL }, *path; | |
552 | size_t validation_len = 0, i; | |
553 | bool is_safe = false; | |
554 | int error = 0; | |
555 | ||
556 | /* | |
557 | * If there's a worktree, validate the permissions to it *and* | |
558 | * the git directory, and use the worktree as the configuration | |
559 | * key for allowlisting the directory. In a bare setup, only | |
560 | * look at the gitdir and use that as the allowlist. So we | |
561 | * examine all `validation_paths` but use only the first as | |
562 | * the configuration lookup. | |
563 | */ | |
564 | ||
565 | if (repo->workdir) | |
566 | validation_paths[validation_len++] = repo->workdir; | |
567 | ||
568 | if (repo->gitlink) | |
569 | validation_paths[validation_len++] = repo->gitlink; | |
570 | ||
571 | validation_paths[validation_len++] = repo->gitdir; | |
572 | ||
573 | for (i = 0; i < validation_len; i++) { | |
574 | path = validation_paths[i]; | |
575 | ||
576 | if ((error = validate_ownership_path(&is_safe, path)) < 0) | |
e579e0f7 | 577 | goto done; |
ad5611d8 TR |
578 | |
579 | if (!is_safe) | |
580 | break; | |
e579e0f7 MB |
581 | } |
582 | ||
ad5611d8 TR |
583 | if (is_safe || |
584 | (error = validate_ownership_config(&is_safe, validation_paths[0])) < 0) | |
585 | goto done; | |
586 | ||
587 | if (!is_safe) { | |
588 | git_error_set(GIT_ERROR_CONFIG, | |
589 | "repository path '%s' is not owned by current user", | |
590 | path); | |
591 | error = GIT_EOWNER; | |
592 | } | |
e579e0f7 MB |
593 | |
594 | done: | |
7784bcbb RB |
595 | return error; |
596 | } | |
597 | ||
598 | static int find_repo( | |
e579e0f7 MB |
599 | git_str *gitdir_path, |
600 | git_str *workdir_path, | |
601 | git_str *gitlink_path, | |
602 | git_str *commondir_path, | |
7784bcbb RB |
603 | const char *start_path, |
604 | uint32_t flags, | |
605 | const char *ceiling_dirs) | |
691aa968 | 606 | { |
e579e0f7 MB |
607 | git_str path = GIT_STR_INIT; |
608 | git_str repo_link = GIT_STR_INIT; | |
609 | git_str common_link = GIT_STR_INIT; | |
7784bcbb RB |
610 | struct stat st; |
611 | dev_t initial_device = 0; | |
ed577134 | 612 | int min_iterations; |
22a2d3d5 | 613 | bool in_dot_git, is_valid; |
b118f647 | 614 | size_t ceiling_offset = 0; |
22a2d3d5 | 615 | int error; |
7784bcbb | 616 | |
e579e0f7 | 617 | git_str_clear(gitdir_path); |
7784bcbb | 618 | |
e579e0f7 | 619 | error = git_fs_path_prettify(&path, start_path, NULL); |
2b490284 | 620 | if (error < 0) |
7784bcbb RB |
621 | return error; |
622 | ||
ed577134 JT |
623 | /* in_dot_git toggles each loop: |
624 | * /a/b/c/.git, /a/b/c, /a/b/.git, /a/b, /a/.git, /a | |
39c6fca3 JT |
625 | * With GIT_REPOSITORY_OPEN_BARE or GIT_REPOSITORY_OPEN_NO_DOTGIT, we |
626 | * assume we started with /a/b/c.git and don't append .git the first | |
627 | * time through. | |
ed577134 JT |
628 | * min_iterations indicates the number of iterations left before going |
629 | * further counts as a search. */ | |
39c6fca3 | 630 | if (flags & (GIT_REPOSITORY_OPEN_BARE | GIT_REPOSITORY_OPEN_NO_DOTGIT)) { |
ed577134 JT |
631 | in_dot_git = true; |
632 | min_iterations = 1; | |
633 | } else { | |
634 | in_dot_git = false; | |
635 | min_iterations = 2; | |
636 | } | |
7784bcbb | 637 | |
2b490284 | 638 | for (;;) { |
39c6fca3 | 639 | if (!(flags & GIT_REPOSITORY_OPEN_NO_DOTGIT)) { |
2b490284 | 640 | if (!in_dot_git) { |
e579e0f7 | 641 | if ((error = git_str_joinpath(&path, path.ptr, DOT_GIT)) < 0) |
22a2d3d5 | 642 | goto out; |
2b490284 | 643 | } |
39c6fca3 JT |
644 | in_dot_git = !in_dot_git; |
645 | } | |
7784bcbb | 646 | |
7784bcbb RB |
647 | if (p_stat(path.ptr, &st) == 0) { |
648 | /* check that we have not crossed device boundaries */ | |
649 | if (initial_device == 0) | |
650 | initial_device = st.st_dev; | |
651 | else if (st.st_dev != initial_device && | |
2b490284 | 652 | !(flags & GIT_REPOSITORY_OPEN_CROSS_FS)) |
7784bcbb RB |
653 | break; |
654 | ||
655 | if (S_ISDIR(st.st_mode)) { | |
22a2d3d5 UG |
656 | if ((error = is_valid_repository_path(&is_valid, &path, &common_link)) < 0) |
657 | goto out; | |
658 | ||
659 | if (is_valid) { | |
e579e0f7 MB |
660 | if ((error = git_fs_path_to_dir(&path)) < 0 || |
661 | (error = git_str_set(gitdir_path, path.ptr, path.size)) < 0) | |
22a2d3d5 | 662 | goto out; |
c09fd54e | 663 | |
32841973 | 664 | if (gitlink_path) |
e579e0f7 | 665 | if ((error = git_str_attach(gitlink_path, git_worktree__read_link(path.ptr, GIT_GITDIR_FILE), 0)) < 0) |
22a2d3d5 | 666 | goto out; |
32841973 | 667 | if (commondir_path) |
e579e0f7 | 668 | git_str_swap(&common_link, commondir_path); |
c09fd54e | 669 | |
7784bcbb RB |
670 | break; |
671 | } | |
22a2d3d5 UG |
672 | } else if (S_ISREG(st.st_mode) && git__suffixcmp(path.ptr, "/" DOT_GIT) == 0) { |
673 | if ((error = read_gitfile(&repo_link, path.ptr)) < 0 || | |
674 | (error = is_valid_repository_path(&is_valid, &repo_link, &common_link)) < 0) | |
675 | goto out; | |
676 | ||
677 | if (is_valid) { | |
e579e0f7 | 678 | git_str_swap(gitdir_path, &repo_link); |
2b490284 | 679 | |
32841973 | 680 | if (gitlink_path) |
e579e0f7 | 681 | if ((error = git_str_put(gitlink_path, path.ptr, path.size)) < 0) |
22a2d3d5 | 682 | goto out; |
32841973 | 683 | if (commondir_path) |
e579e0f7 | 684 | git_str_swap(&common_link, commondir_path); |
7784bcbb | 685 | } |
2b490284 | 686 | break; |
7784bcbb RB |
687 | } |
688 | } | |
689 | ||
2b490284 JT |
690 | /* Move up one directory. If we're in_dot_git, we'll search the |
691 | * parent itself next. If we're !in_dot_git, we'll search .git | |
692 | * in the parent directory next (added at the top of the loop). */ | |
e579e0f7 | 693 | if ((error = git_fs_path_dirname_r(&path, path.ptr)) < 0) |
22a2d3d5 | 694 | goto out; |
7784bcbb | 695 | |
ed577134 JT |
696 | /* Once we've checked the directory (and .git if applicable), |
697 | * find the ceiling for a search. */ | |
698 | if (min_iterations && (--min_iterations == 0)) | |
699 | ceiling_offset = find_ceiling_dir_offset(path.ptr, ceiling_dirs); | |
2b490284 JT |
700 | |
701 | /* Check if we should stop searching here. */ | |
22a2d3d5 UG |
702 | if (min_iterations == 0 && |
703 | (path.ptr[ceiling_offset] == 0 || (flags & GIT_REPOSITORY_OPEN_NO_SEARCH))) | |
2b490284 | 704 | break; |
7784bcbb RB |
705 | } |
706 | ||
22a2d3d5 | 707 | if (workdir_path && !(flags & GIT_REPOSITORY_OPEN_BARE)) { |
e579e0f7 MB |
708 | if (!git_str_len(gitdir_path)) |
709 | git_str_clear(workdir_path); | |
710 | else if ((error = git_fs_path_dirname_r(workdir_path, path.ptr)) < 0 || | |
711 | (error = git_fs_path_to_dir(workdir_path)) < 0) | |
22a2d3d5 | 712 | goto out; |
7784bcbb RB |
713 | } |
714 | ||
2b490284 JT |
715 | /* If we didn't find the repository, and we don't have any other error |
716 | * to report, report that. */ | |
e579e0f7 | 717 | if (!git_str_len(gitdir_path)) { |
22a2d3d5 | 718 | git_error_set(GIT_ERROR_REPOSITORY, "could not find repository from '%s'", start_path); |
904b67e6 | 719 | error = GIT_ENOTFOUND; |
22a2d3d5 | 720 | goto out; |
97769280 | 721 | } |
691aa968 | 722 | |
22a2d3d5 | 723 | out: |
e579e0f7 MB |
724 | git_str_dispose(&path); |
725 | git_str_dispose(&repo_link); | |
726 | git_str_dispose(&common_link); | |
7784bcbb RB |
727 | return error; |
728 | } | |
729 | ||
a442ed68 VM |
730 | int git_repository_open_bare( |
731 | git_repository **repo_ptr, | |
732 | const char *bare_path) | |
733 | { | |
e579e0f7 | 734 | git_str path = GIT_STR_INIT, common_path = GIT_STR_INIT; |
a442ed68 | 735 | git_repository *repo = NULL; |
22a2d3d5 UG |
736 | bool is_valid; |
737 | int error; | |
a442ed68 | 738 | |
e579e0f7 | 739 | if ((error = git_fs_path_prettify_dir(&path, bare_path, NULL)) < 0 || |
22a2d3d5 | 740 | (error = is_valid_repository_path(&is_valid, &path, &common_path)) < 0) |
a442ed68 VM |
741 | return error; |
742 | ||
22a2d3d5 | 743 | if (!is_valid) { |
e579e0f7 MB |
744 | git_str_dispose(&path); |
745 | git_str_dispose(&common_path); | |
ac3d33df | 746 | git_error_set(GIT_ERROR_REPOSITORY, "path is not a repository: %s", bare_path); |
a442ed68 VM |
747 | return GIT_ENOTFOUND; |
748 | } | |
749 | ||
750 | repo = repository_alloc(); | |
ac3d33df | 751 | GIT_ERROR_CHECK_ALLOC(repo); |
a442ed68 | 752 | |
e579e0f7 | 753 | repo->gitdir = git_str_detach(&path); |
ac3d33df | 754 | GIT_ERROR_CHECK_ALLOC(repo->gitdir); |
e579e0f7 | 755 | repo->commondir = git_str_detach(&common_path); |
ac3d33df | 756 | GIT_ERROR_CHECK_ALLOC(repo->commondir); |
a442ed68 VM |
757 | |
758 | /* of course we're bare! */ | |
759 | repo->is_bare = 1; | |
79ab3ef6 | 760 | repo->is_worktree = 0; |
a442ed68 VM |
761 | repo->workdir = NULL; |
762 | ||
763 | *repo_ptr = repo; | |
764 | return 0; | |
765 | } | |
766 | ||
0dd98b69 JT |
767 | static int _git_repository_open_ext_from_env( |
768 | git_repository **out, | |
769 | const char *start_path) | |
770 | { | |
771 | git_repository *repo = NULL; | |
772 | git_index *index = NULL; | |
773 | git_odb *odb = NULL; | |
e579e0f7 MB |
774 | git_str dir_buf = GIT_STR_INIT; |
775 | git_str ceiling_dirs_buf = GIT_STR_INIT; | |
776 | git_str across_fs_buf = GIT_STR_INIT; | |
777 | git_str index_file_buf = GIT_STR_INIT; | |
778 | git_str namespace_buf = GIT_STR_INIT; | |
779 | git_str object_dir_buf = GIT_STR_INIT; | |
780 | git_str alts_buf = GIT_STR_INIT; | |
781 | git_str work_tree_buf = GIT_STR_INIT; | |
782 | git_str common_dir_buf = GIT_STR_INIT; | |
0dd98b69 JT |
783 | const char *ceiling_dirs = NULL; |
784 | unsigned flags = 0; | |
785 | int error; | |
786 | ||
787 | if (!start_path) { | |
788 | error = git__getenv(&dir_buf, "GIT_DIR"); | |
789 | if (error == GIT_ENOTFOUND) { | |
ac3d33df | 790 | git_error_clear(); |
0dd98b69 JT |
791 | start_path = "."; |
792 | } else if (error < 0) | |
793 | goto error; | |
794 | else { | |
e579e0f7 | 795 | start_path = git_str_cstr(&dir_buf); |
0dd98b69 JT |
796 | flags |= GIT_REPOSITORY_OPEN_NO_SEARCH; |
797 | flags |= GIT_REPOSITORY_OPEN_NO_DOTGIT; | |
798 | } | |
799 | } | |
800 | ||
801 | error = git__getenv(&ceiling_dirs_buf, "GIT_CEILING_DIRECTORIES"); | |
802 | if (error == GIT_ENOTFOUND) | |
ac3d33df | 803 | git_error_clear(); |
0dd98b69 JT |
804 | else if (error < 0) |
805 | goto error; | |
806 | else | |
e579e0f7 | 807 | ceiling_dirs = git_str_cstr(&ceiling_dirs_buf); |
0dd98b69 JT |
808 | |
809 | error = git__getenv(&across_fs_buf, "GIT_DISCOVERY_ACROSS_FILESYSTEM"); | |
810 | if (error == GIT_ENOTFOUND) | |
ac3d33df | 811 | git_error_clear(); |
0dd98b69 JT |
812 | else if (error < 0) |
813 | goto error; | |
814 | else { | |
815 | int across_fs = 0; | |
e579e0f7 | 816 | error = git_config_parse_bool(&across_fs, git_str_cstr(&across_fs_buf)); |
0dd98b69 JT |
817 | if (error < 0) |
818 | goto error; | |
819 | if (across_fs) | |
820 | flags |= GIT_REPOSITORY_OPEN_CROSS_FS; | |
821 | } | |
822 | ||
823 | error = git__getenv(&index_file_buf, "GIT_INDEX_FILE"); | |
824 | if (error == GIT_ENOTFOUND) | |
ac3d33df | 825 | git_error_clear(); |
0dd98b69 JT |
826 | else if (error < 0) |
827 | goto error; | |
828 | else { | |
e579e0f7 | 829 | error = git_index_open(&index, git_str_cstr(&index_file_buf)); |
0dd98b69 JT |
830 | if (error < 0) |
831 | goto error; | |
832 | } | |
833 | ||
834 | error = git__getenv(&namespace_buf, "GIT_NAMESPACE"); | |
835 | if (error == GIT_ENOTFOUND) | |
ac3d33df | 836 | git_error_clear(); |
0dd98b69 JT |
837 | else if (error < 0) |
838 | goto error; | |
839 | ||
840 | error = git__getenv(&object_dir_buf, "GIT_OBJECT_DIRECTORY"); | |
841 | if (error == GIT_ENOTFOUND) | |
ac3d33df | 842 | git_error_clear(); |
0dd98b69 JT |
843 | else if (error < 0) |
844 | goto error; | |
845 | else { | |
e579e0f7 | 846 | error = git_odb_open(&odb, git_str_cstr(&object_dir_buf)); |
0dd98b69 JT |
847 | if (error < 0) |
848 | goto error; | |
849 | } | |
850 | ||
851 | error = git__getenv(&work_tree_buf, "GIT_WORK_TREE"); | |
852 | if (error == GIT_ENOTFOUND) | |
ac3d33df | 853 | git_error_clear(); |
0dd98b69 JT |
854 | else if (error < 0) |
855 | goto error; | |
856 | else { | |
ac3d33df | 857 | git_error_set(GIT_ERROR_INVALID, "GIT_WORK_TREE unimplemented"); |
0dd98b69 JT |
858 | error = GIT_ERROR; |
859 | goto error; | |
860 | } | |
861 | ||
862 | error = git__getenv(&work_tree_buf, "GIT_COMMON_DIR"); | |
863 | if (error == GIT_ENOTFOUND) | |
ac3d33df | 864 | git_error_clear(); |
0dd98b69 JT |
865 | else if (error < 0) |
866 | goto error; | |
867 | else { | |
ac3d33df | 868 | git_error_set(GIT_ERROR_INVALID, "GIT_COMMON_DIR unimplemented"); |
0dd98b69 JT |
869 | error = GIT_ERROR; |
870 | goto error; | |
871 | } | |
872 | ||
873 | error = git_repository_open_ext(&repo, start_path, flags, ceiling_dirs); | |
874 | if (error < 0) | |
875 | goto error; | |
876 | ||
877 | if (odb) | |
878 | git_repository_set_odb(repo, odb); | |
879 | ||
880 | error = git__getenv(&alts_buf, "GIT_ALTERNATE_OBJECT_DIRECTORIES"); | |
c9e967a1 | 881 | if (error == GIT_ENOTFOUND) { |
ac3d33df | 882 | git_error_clear(); |
c9e967a1 JT |
883 | error = 0; |
884 | } else if (error < 0) | |
0dd98b69 JT |
885 | goto error; |
886 | else { | |
887 | const char *end; | |
888 | char *alt, *sep; | |
889 | if (!odb) { | |
890 | error = git_repository_odb(&odb, repo); | |
891 | if (error < 0) | |
892 | goto error; | |
893 | } | |
894 | ||
e579e0f7 | 895 | end = git_str_cstr(&alts_buf) + git_str_len(&alts_buf); |
0dd98b69 JT |
896 | for (sep = alt = alts_buf.ptr; sep != end; alt = sep+1) { |
897 | for (sep = alt; *sep && *sep != GIT_PATH_LIST_SEPARATOR; sep++) | |
898 | ; | |
899 | if (*sep) | |
900 | *sep = '\0'; | |
901 | error = git_odb_add_disk_alternate(odb, alt); | |
902 | if (error < 0) | |
903 | goto error; | |
904 | } | |
905 | } | |
906 | ||
e579e0f7 MB |
907 | if (git_str_len(&namespace_buf)) { |
908 | error = git_repository_set_namespace(repo, git_str_cstr(&namespace_buf)); | |
c9e967a1 JT |
909 | if (error < 0) |
910 | goto error; | |
911 | } | |
0dd98b69 JT |
912 | |
913 | git_repository_set_index(repo, index); | |
914 | ||
915 | if (out) { | |
916 | *out = repo; | |
917 | goto success; | |
918 | } | |
919 | error: | |
920 | git_repository_free(repo); | |
921 | success: | |
922 | git_odb_free(odb); | |
923 | git_index_free(index); | |
e579e0f7 MB |
924 | git_str_dispose(&common_dir_buf); |
925 | git_str_dispose(&work_tree_buf); | |
926 | git_str_dispose(&alts_buf); | |
927 | git_str_dispose(&object_dir_buf); | |
928 | git_str_dispose(&namespace_buf); | |
929 | git_str_dispose(&index_file_buf); | |
930 | git_str_dispose(&across_fs_buf); | |
931 | git_str_dispose(&ceiling_dirs_buf); | |
932 | git_str_dispose(&dir_buf); | |
0dd98b69 JT |
933 | return error; |
934 | } | |
935 | ||
9f9fd05f PS |
936 | static int repo_is_worktree(unsigned *out, const git_repository *repo) |
937 | { | |
e579e0f7 | 938 | git_str gitdir_link = GIT_STR_INIT; |
9f9fd05f PS |
939 | int error; |
940 | ||
2696c5c3 PS |
941 | /* Worktrees cannot have the same commondir and gitdir */ |
942 | if (repo->commondir && repo->gitdir | |
943 | && !strcmp(repo->commondir, repo->gitdir)) { | |
944 | *out = 0; | |
945 | return 0; | |
946 | } | |
947 | ||
e579e0f7 | 948 | if ((error = git_str_joinpath(&gitdir_link, repo->gitdir, "gitdir")) < 0) |
9f9fd05f PS |
949 | return -1; |
950 | ||
951 | /* A 'gitdir' file inside a git directory is currently | |
952 | * only used when the repository is a working tree. */ | |
e579e0f7 | 953 | *out = !!git_fs_path_exists(gitdir_link.ptr); |
9f9fd05f | 954 | |
e579e0f7 | 955 | git_str_dispose(&gitdir_link); |
9f9fd05f PS |
956 | return error; |
957 | } | |
958 | ||
7784bcbb RB |
959 | int git_repository_open_ext( |
960 | git_repository **repo_ptr, | |
961 | const char *start_path, | |
c9fc4a6f | 962 | unsigned int flags, |
7784bcbb RB |
963 | const char *ceiling_dirs) |
964 | { | |
965 | int error; | |
9f9fd05f | 966 | unsigned is_worktree; |
e579e0f7 MB |
967 | git_str gitdir = GIT_STR_INIT, workdir = GIT_STR_INIT, |
968 | gitlink = GIT_STR_INIT, commondir = GIT_STR_INIT; | |
22a2d3d5 | 969 | git_repository *repo = NULL; |
16c73d38 | 970 | git_config *config = NULL; |
22a2d3d5 | 971 | int version = 0; |
7784bcbb | 972 | |
0dd98b69 JT |
973 | if (flags & GIT_REPOSITORY_OPEN_FROM_ENV) |
974 | return _git_repository_open_ext_from_env(repo_ptr, start_path); | |
975 | ||
662880ca RB |
976 | if (repo_ptr) |
977 | *repo_ptr = NULL; | |
7784bcbb | 978 | |
4196dd8e | 979 | error = find_repo( |
32841973 | 980 | &gitdir, &workdir, &gitlink, &commondir, start_path, flags, ceiling_dirs); |
4196dd8e | 981 | |
662880ca | 982 | if (error < 0 || !repo_ptr) |
22a2d3d5 | 983 | goto cleanup; |
7784bcbb | 984 | |
e52ed7a5 | 985 | repo = repository_alloc(); |
ac3d33df | 986 | GIT_ERROR_CHECK_ALLOC(repo); |
691aa968 | 987 | |
e579e0f7 | 988 | repo->gitdir = git_str_detach(&gitdir); |
ac3d33df | 989 | GIT_ERROR_CHECK_ALLOC(repo->gitdir); |
691aa968 | 990 | |
32841973 | 991 | if (gitlink.size) { |
e579e0f7 | 992 | repo->gitlink = git_str_detach(&gitlink); |
ac3d33df | 993 | GIT_ERROR_CHECK_ALLOC(repo->gitlink); |
4196dd8e | 994 | } |
32841973 | 995 | if (commondir.size) { |
e579e0f7 | 996 | repo->commondir = git_str_detach(&commondir); |
ac3d33df | 997 | GIT_ERROR_CHECK_ALLOC(repo->commondir); |
c09fd54e | 998 | } |
4196dd8e | 999 | |
9f9fd05f | 1000 | if ((error = repo_is_worktree(&is_worktree, repo)) < 0) |
39abd3ad | 1001 | goto cleanup; |
9f9fd05f | 1002 | repo->is_worktree = is_worktree; |
79ab3ef6 | 1003 | |
16c73d38 CMN |
1004 | /* |
1005 | * We'd like to have the config, but git doesn't particularly | |
1006 | * care if it's not there, so we need to deal with that. | |
1007 | */ | |
1008 | ||
1009 | error = git_repository_config_snapshot(&config, repo); | |
1010 | if (error < 0 && error != GIT_ENOTFOUND) | |
1011 | goto cleanup; | |
1012 | ||
22a2d3d5 UG |
1013 | if (config && (error = check_repositoryformatversion(&version, config)) < 0) |
1014 | goto cleanup; | |
1015 | ||
1016 | if ((error = check_extensions(config, version)) < 0) | |
16c73d38 CMN |
1017 | goto cleanup; |
1018 | ||
e579e0f7 | 1019 | if ((flags & GIT_REPOSITORY_OPEN_BARE) != 0) { |
3fe046cf | 1020 | repo->is_bare = 1; |
e579e0f7 | 1021 | } else { |
16c73d38 CMN |
1022 | if (config && |
1023 | ((error = load_config_data(repo, config)) < 0 || | |
32841973 | 1024 | (error = load_workdir(repo, config, &workdir)) < 0)) |
16c73d38 | 1025 | goto cleanup; |
7784bcbb | 1026 | } |
691aa968 | 1027 | |
e579e0f7 | 1028 | /* |
ad5611d8 TR |
1029 | * Ensure that the git directory and worktree are |
1030 | * owned by the current user. | |
e579e0f7 | 1031 | */ |
e579e0f7 | 1032 | if (git_repository__validate_ownership && |
ad5611d8 | 1033 | (error = validate_ownership(repo)) < 0) |
e579e0f7 MB |
1034 | goto cleanup; |
1035 | ||
16c73d38 | 1036 | cleanup: |
e579e0f7 MB |
1037 | git_str_dispose(&gitdir); |
1038 | git_str_dispose(&workdir); | |
1039 | git_str_dispose(&gitlink); | |
1040 | git_str_dispose(&commondir); | |
16c73d38 CMN |
1041 | git_config_free(config); |
1042 | ||
1043 | if (error < 0) | |
1044 | git_repository_free(repo); | |
22a2d3d5 | 1045 | else if (repo_ptr) |
16c73d38 | 1046 | *repo_ptr = repo; |
2b52a0bf RB |
1047 | |
1048 | return error; | |
7784bcbb | 1049 | } |
97769280 | 1050 | |
7784bcbb RB |
1051 | int git_repository_open(git_repository **repo_out, const char *path) |
1052 | { | |
1053 | return git_repository_open_ext( | |
1054 | repo_out, path, GIT_REPOSITORY_OPEN_NO_SEARCH, NULL); | |
1055 | } | |
1056 | ||
8c8d726e PS |
1057 | int git_repository_open_from_worktree(git_repository **repo_out, git_worktree *wt) |
1058 | { | |
e579e0f7 | 1059 | git_str path = GIT_STR_INIT; |
8c8d726e | 1060 | git_repository *repo = NULL; |
22a2d3d5 UG |
1061 | size_t len; |
1062 | int err; | |
8c8d726e | 1063 | |
c25aa7cd PP |
1064 | GIT_ASSERT_ARG(repo_out); |
1065 | GIT_ASSERT_ARG(wt); | |
8c8d726e PS |
1066 | |
1067 | *repo_out = NULL; | |
1068 | len = strlen(wt->gitlink_path); | |
1069 | ||
1070 | if (len <= 4 || strcasecmp(wt->gitlink_path + len - 4, ".git")) { | |
1071 | err = -1; | |
1072 | goto out; | |
1073 | } | |
1074 | ||
e579e0f7 | 1075 | if ((err = git_str_set(&path, wt->gitlink_path, len - 4)) < 0) |
8c8d726e PS |
1076 | goto out; |
1077 | ||
1078 | if ((err = git_repository_open(&repo, path.ptr)) < 0) | |
1079 | goto out; | |
1080 | ||
1081 | *repo_out = repo; | |
1082 | ||
1083 | out: | |
e579e0f7 | 1084 | git_str_dispose(&path); |
8c8d726e PS |
1085 | |
1086 | return err; | |
1087 | } | |
1088 | ||
6782245e CMN |
1089 | int git_repository_wrap_odb(git_repository **repo_out, git_odb *odb) |
1090 | { | |
1091 | git_repository *repo; | |
1092 | ||
1093 | repo = repository_alloc(); | |
ac3d33df | 1094 | GIT_ERROR_CHECK_ALLOC(repo); |
6782245e CMN |
1095 | |
1096 | git_repository_set_odb(repo, odb); | |
1097 | *repo_out = repo; | |
1098 | ||
1099 | return 0; | |
1100 | } | |
1101 | ||
7784bcbb | 1102 | int git_repository_discover( |
7a3bd1e7 | 1103 | git_buf *out, |
7784bcbb RB |
1104 | const char *start_path, |
1105 | int across_fs, | |
1106 | const char *ceiling_dirs) | |
1107 | { | |
7784bcbb | 1108 | uint32_t flags = across_fs ? GIT_REPOSITORY_OPEN_CROSS_FS : 0; |
7784bcbb | 1109 | |
c25aa7cd | 1110 | GIT_ASSERT_ARG(start_path); |
7784bcbb | 1111 | |
e579e0f7 | 1112 | GIT_BUF_WRAP_PRIVATE(out, find_repo, NULL, NULL, NULL, start_path, flags, ceiling_dirs); |
691aa968 VM |
1113 | } |
1114 | ||
9462c471 | 1115 | static int load_config( |
7784bcbb RB |
1116 | git_config **out, |
1117 | git_repository *repo, | |
1118 | const char *global_config_path, | |
4258d483 | 1119 | const char *xdg_config_path, |
8c7c5fa5 CMN |
1120 | const char *system_config_path, |
1121 | const char *programdata_path) | |
b22d1479 | 1122 | { |
cc6b4162 | 1123 | int error; |
e579e0f7 | 1124 | git_str config_path = GIT_STR_INIT; |
9462c471 | 1125 | git_config *cfg = NULL; |
b22d1479 | 1126 | |
c25aa7cd | 1127 | GIT_ASSERT_ARG(out); |
07ff8817 | 1128 | |
cc6b4162 RB |
1129 | if ((error = git_config_new(&cfg)) < 0) |
1130 | return error; | |
b22d1479 | 1131 | |
ac3d33df | 1132 | if (repo) { |
e579e0f7 | 1133 | if ((error = git_repository__item_path(&config_path, repo, GIT_REPOSITORY_ITEM_CONFIG)) == 0) |
ac3d33df | 1134 | error = git_config_add_file_ondisk(cfg, config_path.ptr, GIT_CONFIG_LEVEL_LOCAL, repo, 0); |
97769280 | 1135 | |
ac3d33df JK |
1136 | if (error && error != GIT_ENOTFOUND) |
1137 | goto on_error; | |
cb8a7961 | 1138 | |
e579e0f7 | 1139 | git_str_dispose(&config_path); |
ac3d33df | 1140 | } |
b22d1479 | 1141 | |
cc6b4162 RB |
1142 | if (global_config_path != NULL && |
1143 | (error = git_config_add_file_ondisk( | |
eae0bfdc | 1144 | cfg, global_config_path, GIT_CONFIG_LEVEL_GLOBAL, repo, 0)) < 0 && |
cc6b4162 RB |
1145 | error != GIT_ENOTFOUND) |
1146 | goto on_error; | |
8b4f9b17 | 1147 | |
cc6b4162 RB |
1148 | if (xdg_config_path != NULL && |
1149 | (error = git_config_add_file_ondisk( | |
eae0bfdc | 1150 | cfg, xdg_config_path, GIT_CONFIG_LEVEL_XDG, repo, 0)) < 0 && |
cc6b4162 RB |
1151 | error != GIT_ENOTFOUND) |
1152 | goto on_error; | |
b22d1479 | 1153 | |
cc6b4162 RB |
1154 | if (system_config_path != NULL && |
1155 | (error = git_config_add_file_ondisk( | |
eae0bfdc | 1156 | cfg, system_config_path, GIT_CONFIG_LEVEL_SYSTEM, repo, 0)) < 0 && |
cc6b4162 RB |
1157 | error != GIT_ENOTFOUND) |
1158 | goto on_error; | |
9ba9e513 | 1159 | |
8c7c5fa5 CMN |
1160 | if (programdata_path != NULL && |
1161 | (error = git_config_add_file_ondisk( | |
eae0bfdc | 1162 | cfg, programdata_path, GIT_CONFIG_LEVEL_PROGRAMDATA, repo, 0)) < 0 && |
8c7c5fa5 CMN |
1163 | error != GIT_ENOTFOUND) |
1164 | goto on_error; | |
1165 | ||
ac3d33df | 1166 | git_error_clear(); /* clear any lingering ENOTFOUND errors */ |
38f7d026 | 1167 | |
9462c471 | 1168 | *out = cfg; |
cb8a7961 | 1169 | return 0; |
b22d1479 | 1170 | |
cb8a7961 | 1171 | on_error: |
e579e0f7 | 1172 | git_str_dispose(&config_path); |
9462c471 VM |
1173 | git_config_free(cfg); |
1174 | *out = NULL; | |
cc6b4162 | 1175 | return error; |
b22d1479 CMN |
1176 | } |
1177 | ||
e579e0f7 | 1178 | static const char *path_unless_empty(git_str *buf) |
40fe5fbe | 1179 | { |
e579e0f7 | 1180 | return git_str_len(buf) > 0 ? git_str_cstr(buf) : NULL; |
53607868 | 1181 | } |
8b4f9b17 | 1182 | |
53607868 RB |
1183 | int git_repository_config__weakptr(git_config **out, git_repository *repo) |
1184 | { | |
1185 | int error = 0; | |
40fe5fbe | 1186 | |
53607868 | 1187 | if (repo->_config == NULL) { |
e579e0f7 MB |
1188 | git_str global_buf = GIT_STR_INIT; |
1189 | git_str xdg_buf = GIT_STR_INIT; | |
1190 | git_str system_buf = GIT_STR_INIT; | |
1191 | git_str programdata_buf = GIT_STR_INIT; | |
53607868 RB |
1192 | git_config *config; |
1193 | ||
e579e0f7 MB |
1194 | git_config__find_global(&global_buf); |
1195 | git_config__find_xdg(&xdg_buf); | |
1196 | git_config__find_system(&system_buf); | |
1197 | git_config__find_programdata(&programdata_buf); | |
53607868 | 1198 | |
a4b75dcf | 1199 | /* If there is no global file, open a backend for it anyway */ |
e579e0f7 | 1200 | if (git_str_len(&global_buf) == 0) |
a4b75dcf CMN |
1201 | git_config__global_location(&global_buf); |
1202 | ||
53607868 RB |
1203 | error = load_config( |
1204 | &config, repo, | |
1205 | path_unless_empty(&global_buf), | |
1206 | path_unless_empty(&xdg_buf), | |
8c7c5fa5 CMN |
1207 | path_unless_empty(&system_buf), |
1208 | path_unless_empty(&programdata_buf)); | |
53607868 RB |
1209 | if (!error) { |
1210 | GIT_REFCOUNT_OWN(config, repo); | |
1211 | ||
c25aa7cd | 1212 | if (git_atomic_compare_and_swap(&repo->_config, NULL, config) != NULL) { |
53607868 RB |
1213 | GIT_REFCOUNT_OWN(config, NULL); |
1214 | git_config_free(config); | |
1215 | } | |
1216 | } | |
97769280 | 1217 | |
e579e0f7 MB |
1218 | git_str_dispose(&global_buf); |
1219 | git_str_dispose(&xdg_buf); | |
1220 | git_str_dispose(&system_buf); | |
1221 | git_str_dispose(&programdata_buf); | |
9462c471 | 1222 | } |
40fe5fbe | 1223 | |
9462c471 | 1224 | *out = repo->_config; |
53607868 | 1225 | return error; |
40fe5fbe CMN |
1226 | } |
1227 | ||
9462c471 | 1228 | int git_repository_config(git_config **out, git_repository *repo) |
fd0574e5 | 1229 | { |
cb8a7961 VM |
1230 | if (git_repository_config__weakptr(out, repo) < 0) |
1231 | return -1; | |
fd0574e5 | 1232 | |
cb8a7961 VM |
1233 | GIT_REFCOUNT_INC(*out); |
1234 | return 0; | |
9462c471 VM |
1235 | } |
1236 | ||
ac99d86b CMN |
1237 | int git_repository_config_snapshot(git_config **out, git_repository *repo) |
1238 | { | |
b1914c36 | 1239 | int error; |
ac99d86b CMN |
1240 | git_config *weak; |
1241 | ||
b1914c36 RB |
1242 | if ((error = git_repository_config__weakptr(&weak, repo)) < 0) |
1243 | return error; | |
ac99d86b CMN |
1244 | |
1245 | return git_config_snapshot(out, weak); | |
1246 | } | |
1247 | ||
22a2d3d5 | 1248 | int git_repository_set_config(git_repository *repo, git_config *config) |
9462c471 | 1249 | { |
c25aa7cd PP |
1250 | GIT_ASSERT_ARG(repo); |
1251 | GIT_ASSERT_ARG(config); | |
1252 | ||
e976b56d | 1253 | set_config(repo, config); |
22a2d3d5 | 1254 | return 0; |
9462c471 VM |
1255 | } |
1256 | ||
1257 | int git_repository_odb__weakptr(git_odb **out, git_repository *repo) | |
1258 | { | |
53607868 RB |
1259 | int error = 0; |
1260 | ||
c25aa7cd PP |
1261 | GIT_ASSERT_ARG(repo); |
1262 | GIT_ASSERT_ARG(out); | |
9462c471 | 1263 | |
c25aa7cd PP |
1264 | *out = git_atomic_load(repo->_odb); |
1265 | if (*out == NULL) { | |
e579e0f7 | 1266 | git_str odb_path = GIT_STR_INIT; |
53607868 | 1267 | git_odb *odb; |
9462c471 | 1268 | |
e579e0f7 | 1269 | if ((error = git_repository__item_path(&odb_path, repo, |
1c04a96b ET |
1270 | GIT_REPOSITORY_ITEM_OBJECTS)) < 0 || |
1271 | (error = git_odb_new(&odb)) < 0) | |
3dbd9a0e | 1272 | return error; |
9462c471 | 1273 | |
1c04a96b | 1274 | GIT_REFCOUNT_OWN(odb, repo); |
cb8a7961 | 1275 | |
1c04a96b ET |
1276 | if ((error = git_odb__set_caps(odb, GIT_ODB_CAP_FROM_OWNER)) < 0 || |
1277 | (error = git_odb__add_default_backends(odb, odb_path.ptr, 0, 0)) < 0) { | |
1278 | git_odb_free(odb); | |
1279 | return error; | |
1280 | } | |
1281 | ||
c25aa7cd | 1282 | if (git_atomic_compare_and_swap(&repo->_odb, NULL, odb) != NULL) { |
1c04a96b ET |
1283 | GIT_REFCOUNT_OWN(odb, NULL); |
1284 | git_odb_free(odb); | |
53607868 | 1285 | } |
9462c471 | 1286 | |
e579e0f7 | 1287 | git_str_dispose(&odb_path); |
c25aa7cd | 1288 | *out = git_atomic_load(repo->_odb); |
9462c471 | 1289 | } |
fd0574e5 | 1290 | |
53607868 | 1291 | return error; |
fd0574e5 RG |
1292 | } |
1293 | ||
9462c471 | 1294 | int git_repository_odb(git_odb **out, git_repository *repo) |
6fd195d7 | 1295 | { |
cb8a7961 VM |
1296 | if (git_repository_odb__weakptr(out, repo) < 0) |
1297 | return -1; | |
1795f879 | 1298 | |
cb8a7961 VM |
1299 | GIT_REFCOUNT_INC(*out); |
1300 | return 0; | |
9462c471 | 1301 | } |
6fd195d7 | 1302 | |
22a2d3d5 | 1303 | int git_repository_set_odb(git_repository *repo, git_odb *odb) |
9462c471 | 1304 | { |
c25aa7cd PP |
1305 | GIT_ASSERT_ARG(repo); |
1306 | GIT_ASSERT_ARG(odb); | |
1307 | ||
e976b56d | 1308 | set_odb(repo, odb); |
22a2d3d5 | 1309 | return 0; |
9462c471 VM |
1310 | } |
1311 | ||
d00d5464 ET |
1312 | int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo) |
1313 | { | |
53607868 RB |
1314 | int error = 0; |
1315 | ||
c25aa7cd PP |
1316 | GIT_ASSERT_ARG(out); |
1317 | GIT_ASSERT_ARG(repo); | |
d00d5464 ET |
1318 | |
1319 | if (repo->_refdb == NULL) { | |
53607868 | 1320 | git_refdb *refdb; |
d00d5464 | 1321 | |
53607868 RB |
1322 | error = git_refdb_open(&refdb, repo); |
1323 | if (!error) { | |
1324 | GIT_REFCOUNT_OWN(refdb, repo); | |
d00d5464 | 1325 | |
c25aa7cd | 1326 | if (git_atomic_compare_and_swap(&repo->_refdb, NULL, refdb) != NULL) { |
53607868 RB |
1327 | GIT_REFCOUNT_OWN(refdb, NULL); |
1328 | git_refdb_free(refdb); | |
1329 | } | |
1330 | } | |
d00d5464 ET |
1331 | } |
1332 | ||
1333 | *out = repo->_refdb; | |
53607868 | 1334 | return error; |
d00d5464 ET |
1335 | } |
1336 | ||
1337 | int git_repository_refdb(git_refdb **out, git_repository *repo) | |
1338 | { | |
1339 | if (git_repository_refdb__weakptr(out, repo) < 0) | |
1340 | return -1; | |
1341 | ||
1342 | GIT_REFCOUNT_INC(*out); | |
1343 | return 0; | |
1344 | } | |
1345 | ||
22a2d3d5 | 1346 | int git_repository_set_refdb(git_repository *repo, git_refdb *refdb) |
d00d5464 | 1347 | { |
c25aa7cd PP |
1348 | GIT_ASSERT_ARG(repo); |
1349 | GIT_ASSERT_ARG(refdb); | |
1350 | ||
e976b56d | 1351 | set_refdb(repo, refdb); |
22a2d3d5 | 1352 | return 0; |
d00d5464 ET |
1353 | } |
1354 | ||
9462c471 VM |
1355 | int git_repository_index__weakptr(git_index **out, git_repository *repo) |
1356 | { | |
53607868 RB |
1357 | int error = 0; |
1358 | ||
c25aa7cd PP |
1359 | GIT_ASSERT_ARG(out); |
1360 | GIT_ASSERT_ARG(repo); | |
9462c471 | 1361 | |
9462c471 | 1362 | if (repo->_index == NULL) { |
e579e0f7 | 1363 | git_str index_path = GIT_STR_INIT; |
53607868 | 1364 | git_index *index; |
9462c471 | 1365 | |
e579e0f7 | 1366 | if ((error = git_str_joinpath(&index_path, repo->gitdir, GIT_INDEX_FILE)) < 0) |
3dbd9a0e | 1367 | return error; |
9462c471 | 1368 | |
53607868 RB |
1369 | error = git_index_open(&index, index_path.ptr); |
1370 | if (!error) { | |
1371 | GIT_REFCOUNT_OWN(index, repo); | |
cb8a7961 | 1372 | |
c25aa7cd | 1373 | if (git_atomic_compare_and_swap(&repo->_index, NULL, index) != NULL) { |
53607868 RB |
1374 | GIT_REFCOUNT_OWN(index, NULL); |
1375 | git_index_free(index); | |
1376 | } | |
9462c471 | 1377 | |
ac3d33df JK |
1378 | error = git_index_set_caps(repo->_index, |
1379 | GIT_INDEX_CAPABILITY_FROM_OWNER); | |
53607868 | 1380 | } |
da825c92 | 1381 | |
e579e0f7 | 1382 | git_str_dispose(&index_path); |
9462c471 VM |
1383 | } |
1384 | ||
9462c471 | 1385 | *out = repo->_index; |
53607868 | 1386 | return error; |
9462c471 | 1387 | } |
1795f879 | 1388 | |
9462c471 VM |
1389 | int git_repository_index(git_index **out, git_repository *repo) |
1390 | { | |
cb8a7961 VM |
1391 | if (git_repository_index__weakptr(out, repo) < 0) |
1392 | return -1; | |
9462c471 | 1393 | |
cb8a7961 VM |
1394 | GIT_REFCOUNT_INC(*out); |
1395 | return 0; | |
3315782c VM |
1396 | } |
1397 | ||
22a2d3d5 | 1398 | int git_repository_set_index(git_repository *repo, git_index *index) |
9462c471 | 1399 | { |
c25aa7cd | 1400 | GIT_ASSERT_ARG(repo); |
e976b56d | 1401 | set_index(repo, index); |
22a2d3d5 | 1402 | return 0; |
9462c471 VM |
1403 | } |
1404 | ||
bade5194 VM |
1405 | int git_repository_set_namespace(git_repository *repo, const char *namespace) |
1406 | { | |
1407 | git__free(repo->namespace); | |
1408 | ||
1409 | if (namespace == NULL) { | |
1410 | repo->namespace = NULL; | |
1411 | return 0; | |
1412 | } | |
1413 | ||
1414 | return (repo->namespace = git__strdup(namespace)) ? 0 : -1; | |
1415 | } | |
1416 | ||
1417 | const char *git_repository_get_namespace(git_repository *repo) | |
1418 | { | |
1419 | return repo->namespace; | |
1420 | } | |
1421 | ||
4196dd8e ET |
1422 | #ifdef GIT_WIN32 |
1423 | static int reserved_names_add8dot3(git_repository *repo, const char *path) | |
a64119e3 | 1424 | { |
4196dd8e ET |
1425 | char *name = git_win32_path_8dot3_name(path); |
1426 | const char *def = GIT_DIR_SHORTNAME; | |
526f91f5 | 1427 | const char *def_dot_git = DOT_GIT; |
4196dd8e | 1428 | size_t name_len, def_len = CONST_STRLEN(GIT_DIR_SHORTNAME); |
526f91f5 | 1429 | size_t def_dot_git_len = CONST_STRLEN(DOT_GIT); |
e579e0f7 | 1430 | git_str *buf; |
a64119e3 | 1431 | |
4196dd8e ET |
1432 | if (!name) |
1433 | return 0; | |
1434 | ||
1435 | name_len = strlen(name); | |
1436 | ||
eae0bfdc | 1437 | if ((name_len == def_len && memcmp(name, def, def_len) == 0) || |
526f91f5 | 1438 | (name_len == def_dot_git_len && memcmp(name, def_dot_git, def_dot_git_len) == 0)) { |
4196dd8e ET |
1439 | git__free(name); |
1440 | return 0; | |
1441 | } | |
1442 | ||
1443 | if ((buf = git_array_alloc(repo->reserved_names)) == NULL) | |
1444 | return -1; | |
1445 | ||
e579e0f7 | 1446 | git_str_attach(buf, name, name_len); |
4196dd8e ET |
1447 | return true; |
1448 | } | |
1449 | ||
1450 | bool git_repository__reserved_names( | |
e579e0f7 | 1451 | git_str **out, size_t *outlen, git_repository *repo, bool include_ntfs) |
4196dd8e ET |
1452 | { |
1453 | GIT_UNUSED(include_ntfs); | |
1454 | ||
1455 | if (repo->reserved_names.size == 0) { | |
e579e0f7 | 1456 | git_str *buf; |
4196dd8e ET |
1457 | size_t i; |
1458 | ||
1459 | /* Add the static defaults */ | |
1460 | for (i = 0; i < git_repository__reserved_names_win32_len; i++) { | |
1461 | if ((buf = git_array_alloc(repo->reserved_names)) == NULL) | |
1462 | goto on_error; | |
1463 | ||
1464 | buf->ptr = git_repository__reserved_names_win32[i].ptr; | |
1465 | buf->size = git_repository__reserved_names_win32[i].size; | |
1466 | } | |
1467 | ||
538dfc88 ET |
1468 | /* Try to add any repo-specific reserved names - the gitlink file |
1469 | * within a submodule or the repository (if the repository directory | |
1470 | * is beneath the workdir). These are typically `.git`, but should | |
1471 | * be protected in case they are not. Note, repo and workdir paths | |
1472 | * are always prettified to end in `/`, so a prefixcmp is safe. | |
1473 | */ | |
a64119e3 | 1474 | if (!repo->is_bare) { |
538dfc88 ET |
1475 | int (*prefixcmp)(const char *, const char *); |
1476 | int error, ignorecase; | |
a64119e3 | 1477 | |
22a2d3d5 UG |
1478 | error = git_repository__configmap_lookup( |
1479 | &ignorecase, repo, GIT_CONFIGMAP_IGNORECASE); | |
538dfc88 ET |
1480 | prefixcmp = (error || ignorecase) ? git__prefixcmp_icase : |
1481 | git__prefixcmp; | |
1482 | ||
84f56cb0 PS |
1483 | if (repo->gitlink && |
1484 | reserved_names_add8dot3(repo, repo->gitlink) < 0) | |
538dfc88 ET |
1485 | goto on_error; |
1486 | ||
84f56cb0 PS |
1487 | if (repo->gitdir && |
1488 | prefixcmp(repo->gitdir, repo->workdir) == 0 && | |
1489 | reserved_names_add8dot3(repo, repo->gitdir) < 0) | |
4196dd8e | 1490 | goto on_error; |
a64119e3 | 1491 | } |
a64119e3 ET |
1492 | } |
1493 | ||
4196dd8e ET |
1494 | *out = repo->reserved_names.ptr; |
1495 | *outlen = repo->reserved_names.size; | |
1496 | ||
1497 | return true; | |
1498 | ||
1499 | /* Always give good defaults, even on OOM */ | |
1500 | on_error: | |
1501 | *out = git_repository__reserved_names_win32; | |
1502 | *outlen = git_repository__reserved_names_win32_len; | |
1503 | ||
1504 | return false; | |
a64119e3 | 1505 | } |
4196dd8e ET |
1506 | #else |
1507 | bool git_repository__reserved_names( | |
e579e0f7 | 1508 | git_str **out, size_t *outlen, git_repository *repo, bool include_ntfs) |
4196dd8e ET |
1509 | { |
1510 | GIT_UNUSED(repo); | |
1511 | ||
1512 | if (include_ntfs) { | |
1513 | *out = git_repository__reserved_names_win32; | |
1514 | *outlen = git_repository__reserved_names_win32_len; | |
1515 | } else { | |
1516 | *out = git_repository__reserved_names_posix; | |
1517 | *outlen = git_repository__reserved_names_posix_len; | |
1518 | } | |
1519 | ||
1520 | return true; | |
1521 | } | |
1522 | #endif | |
a64119e3 | 1523 | |
22a2d3d5 | 1524 | static int check_repositoryformatversion(int *version, git_config *config) |
40c44d2f | 1525 | { |
22a2d3d5 | 1526 | int error; |
5663e61a | 1527 | |
22a2d3d5 | 1528 | error = git_config_get_int32(version, config, "core.repositoryformatversion"); |
99e11cdd CMN |
1529 | /* git ignores this if the config variable isn't there */ |
1530 | if (error == GIT_ENOTFOUND) | |
1531 | return 0; | |
1532 | ||
1533 | if (error < 0) | |
cb8a7961 | 1534 | return -1; |
5663e61a | 1535 | |
22a2d3d5 | 1536 | if (GIT_REPO_MAX_VERSION < *version) { |
ac3d33df | 1537 | git_error_set(GIT_ERROR_REPOSITORY, |
22a2d3d5 UG |
1538 | "unsupported repository version %d; only versions up to %d are supported", |
1539 | *version, GIT_REPO_MAX_VERSION); | |
cb8a7961 VM |
1540 | return -1; |
1541 | } | |
5663e61a | 1542 | |
cb8a7961 | 1543 | return 0; |
5663e61a | 1544 | } |
1545 | ||
c25aa7cd PP |
1546 | static const char *builtin_extensions[] = { |
1547 | "noop" | |
1548 | }; | |
1549 | ||
1550 | static git_vector user_extensions = GIT_VECTOR_INIT; | |
1551 | ||
22a2d3d5 UG |
1552 | static int check_valid_extension(const git_config_entry *entry, void *payload) |
1553 | { | |
e579e0f7 | 1554 | git_str cfg = GIT_STR_INIT; |
c25aa7cd PP |
1555 | bool reject; |
1556 | const char *extension; | |
1557 | size_t i; | |
1558 | int error = 0; | |
1559 | ||
22a2d3d5 UG |
1560 | GIT_UNUSED(payload); |
1561 | ||
c25aa7cd | 1562 | git_vector_foreach (&user_extensions, i, extension) { |
e579e0f7 | 1563 | git_str_clear(&cfg); |
c25aa7cd PP |
1564 | |
1565 | /* | |
1566 | * Users can specify that they don't want to support an | |
1567 | * extension with a '!' prefix. | |
1568 | */ | |
1569 | if ((reject = (extension[0] == '!')) == true) | |
1570 | extension = &extension[1]; | |
1571 | ||
e579e0f7 | 1572 | if ((error = git_str_printf(&cfg, "extensions.%s", extension)) < 0) |
c25aa7cd PP |
1573 | goto done; |
1574 | ||
1575 | if (strcmp(entry->name, cfg.ptr) == 0) { | |
1576 | if (reject) | |
1577 | goto fail; | |
1578 | ||
1579 | goto done; | |
1580 | } | |
1581 | } | |
1582 | ||
1583 | for (i = 0; i < ARRAY_SIZE(builtin_extensions); i++) { | |
ad5611d8 | 1584 | git_str_clear(&cfg); |
c25aa7cd | 1585 | extension = builtin_extensions[i]; |
22a2d3d5 | 1586 | |
e579e0f7 | 1587 | if ((error = git_str_printf(&cfg, "extensions.%s", extension)) < 0) |
c25aa7cd PP |
1588 | goto done; |
1589 | ||
1590 | if (strcmp(entry->name, cfg.ptr) == 0) | |
1591 | goto done; | |
1592 | } | |
1593 | ||
1594 | fail: | |
22a2d3d5 | 1595 | git_error_set(GIT_ERROR_REPOSITORY, "unsupported extension name %s", entry->name); |
c25aa7cd PP |
1596 | error = -1; |
1597 | ||
1598 | done: | |
e579e0f7 | 1599 | git_str_dispose(&cfg); |
c25aa7cd | 1600 | return error; |
22a2d3d5 UG |
1601 | } |
1602 | ||
1603 | static int check_extensions(git_config *config, int version) | |
1604 | { | |
1605 | if (version < 1) | |
1606 | return 0; | |
1607 | ||
1608 | return git_config_foreach_match(config, "^extensions\\.", check_valid_extension, NULL); | |
1609 | } | |
1610 | ||
c25aa7cd PP |
1611 | int git_repository__extensions(char ***out, size_t *out_len) |
1612 | { | |
1613 | git_vector extensions; | |
1614 | const char *builtin, *user; | |
1615 | char *extension; | |
1616 | size_t i, j; | |
1617 | ||
1618 | if (git_vector_init(&extensions, 8, NULL) < 0) | |
1619 | return -1; | |
1620 | ||
1621 | for (i = 0; i < ARRAY_SIZE(builtin_extensions); i++) { | |
1622 | bool match = false; | |
1623 | ||
1624 | builtin = builtin_extensions[i]; | |
1625 | ||
1626 | git_vector_foreach (&user_extensions, j, user) { | |
1627 | if (user[0] == '!' && strcmp(builtin, &user[1]) == 0) { | |
1628 | match = true; | |
1629 | break; | |
1630 | } | |
1631 | } | |
1632 | ||
1633 | if (match) | |
1634 | continue; | |
1635 | ||
1636 | if ((extension = git__strdup(builtin)) == NULL || | |
1637 | git_vector_insert(&extensions, extension) < 0) | |
1638 | return -1; | |
1639 | } | |
1640 | ||
1641 | git_vector_foreach (&user_extensions, i, user) { | |
1642 | if (user[0] == '!') | |
1643 | continue; | |
1644 | ||
1645 | if ((extension = git__strdup(user)) == NULL || | |
1646 | git_vector_insert(&extensions, extension) < 0) | |
1647 | return -1; | |
1648 | } | |
1649 | ||
1650 | *out = (char **)git_vector_detach(out_len, NULL, &extensions); | |
1651 | return 0; | |
1652 | } | |
1653 | ||
1654 | int git_repository__set_extensions(const char **extensions, size_t len) | |
1655 | { | |
1656 | char *extension; | |
1657 | size_t i; | |
1658 | ||
1659 | git_repository__free_extensions(); | |
1660 | ||
1661 | for (i = 0; i < len; i++) { | |
1662 | if ((extension = git__strdup(extensions[i])) == NULL || | |
1663 | git_vector_insert(&user_extensions, extension) < 0) | |
1664 | return -1; | |
1665 | } | |
1666 | ||
1667 | return 0; | |
1668 | } | |
1669 | ||
1670 | void git_repository__free_extensions(void) | |
1671 | { | |
1672 | git_vector_free_deep(&user_extensions); | |
1673 | } | |
1674 | ||
854b5c70 | 1675 | int git_repository_create_head(const char *git_dir, const char *ref_name) |
e1f8cad0 | 1676 | { |
e579e0f7 | 1677 | git_str ref_path = GIT_STR_INIT; |
9462c471 | 1678 | git_filebuf ref = GIT_FILEBUF_INIT; |
662880ca | 1679 | const char *fmt; |
22a2d3d5 | 1680 | int error; |
9462c471 | 1681 | |
e579e0f7 | 1682 | if ((error = git_str_joinpath(&ref_path, git_dir, GIT_HEAD_FILE)) < 0 || |
22a2d3d5 UG |
1683 | (error = git_filebuf_open(&ref, ref_path.ptr, 0, GIT_REFS_FILE_MODE)) < 0) |
1684 | goto out; | |
662880ca | 1685 | |
74a24005 | 1686 | if (git__prefixcmp(ref_name, GIT_REFS_DIR) == 0) |
662880ca RB |
1687 | fmt = "ref: %s\n"; |
1688 | else | |
74a24005 | 1689 | fmt = "ref: " GIT_REFS_HEADS_DIR "%s\n"; |
662880ca | 1690 | |
22a2d3d5 UG |
1691 | if ((error = git_filebuf_printf(&ref, fmt, ref_name)) < 0 || |
1692 | (error = git_filebuf_commit(&ref)) < 0) | |
1693 | goto out; | |
662880ca | 1694 | |
22a2d3d5 | 1695 | out: |
e579e0f7 | 1696 | git_str_dispose(&ref_path); |
662880ca | 1697 | git_filebuf_cleanup(&ref); |
22a2d3d5 | 1698 | return error; |
9462c471 VM |
1699 | } |
1700 | ||
fac66990 | 1701 | static bool is_chmod_supported(const char *file_path) |
1702 | { | |
1703 | struct stat st1, st2; | |
fac66990 | 1704 | |
1705 | if (p_stat(file_path, &st1) < 0) | |
1706 | return false; | |
1707 | ||
1708 | if (p_chmod(file_path, st1.st_mode ^ S_IXUSR) < 0) | |
1709 | return false; | |
1710 | ||
1711 | if (p_stat(file_path, &st2) < 0) | |
1712 | return false; | |
1713 | ||
6b7991e2 | 1714 | return (st1.st_mode != st2.st_mode); |
fac66990 | 1715 | } |
1716 | ||
693b23c0 | 1717 | static bool is_filesystem_case_insensitive(const char *gitdir_path) |
1718 | { | |
e579e0f7 | 1719 | git_str path = GIT_STR_INIT; |
6b7991e2 | 1720 | int is_insensitive = -1; |
693b23c0 | 1721 | |
e579e0f7 MB |
1722 | if (!git_str_joinpath(&path, gitdir_path, "CoNfIg")) |
1723 | is_insensitive = git_fs_path_exists(git_str_cstr(&path)); | |
693b23c0 | 1724 | |
e579e0f7 | 1725 | git_str_dispose(&path); |
6b7991e2 | 1726 | return is_insensitive; |
693b23c0 | 1727 | } |
1728 | ||
e579e0f7 MB |
1729 | /* |
1730 | * Return a configuration object with only the global and system | |
1731 | * configurations; no repository-level configuration. | |
1732 | */ | |
1733 | static int load_global_config(git_config **config) | |
1734 | { | |
1735 | git_str global_buf = GIT_STR_INIT; | |
1736 | git_str xdg_buf = GIT_STR_INIT; | |
1737 | git_str system_buf = GIT_STR_INIT; | |
1738 | git_str programdata_buf = GIT_STR_INIT; | |
1739 | int error; | |
1740 | ||
1741 | git_config__find_global(&global_buf); | |
1742 | git_config__find_xdg(&xdg_buf); | |
1743 | git_config__find_system(&system_buf); | |
1744 | git_config__find_programdata(&programdata_buf); | |
1745 | ||
1746 | error = load_config(config, NULL, | |
1747 | path_unless_empty(&global_buf), | |
1748 | path_unless_empty(&xdg_buf), | |
1749 | path_unless_empty(&system_buf), | |
1750 | path_unless_empty(&programdata_buf)); | |
1751 | ||
1752 | git_str_dispose(&global_buf); | |
1753 | git_str_dispose(&xdg_buf); | |
1754 | git_str_dispose(&system_buf); | |
1755 | git_str_dispose(&programdata_buf); | |
1756 | ||
1757 | return error; | |
1758 | } | |
1759 | ||
ca1b6e54 RB |
1760 | static bool are_symlinks_supported(const char *wd_path) |
1761 | { | |
ac3d33df | 1762 | git_config *config = NULL; |
ac3d33df JK |
1763 | int symlinks = 0; |
1764 | ||
1765 | /* | |
1766 | * To emulate Git for Windows, symlinks on Windows must be explicitly | |
1767 | * opted-in. We examine the system configuration for a core.symlinks | |
1768 | * set to true. If found, we then examine the filesystem to see if | |
1769 | * symlinks are _actually_ supported by the current user. If that is | |
1770 | * _not_ set, then we do not test or enable symlink support. | |
1771 | */ | |
1772 | #ifdef GIT_WIN32 | |
e579e0f7 MB |
1773 | if (load_global_config(&config) < 0 || |
1774 | git_config_get_bool(&symlinks, config, "core.symlinks") < 0 || | |
1775 | !symlinks) | |
ac3d33df JK |
1776 | goto done; |
1777 | #endif | |
ca1b6e54 | 1778 | |
e579e0f7 | 1779 | if (!(symlinks = git_fs_path_supports_symlinks(wd_path))) |
ac3d33df JK |
1780 | goto done; |
1781 | ||
ac3d33df | 1782 | done: |
ac3d33df JK |
1783 | git_config_free(config); |
1784 | return symlinks != 0; | |
6b7991e2 RB |
1785 | } |
1786 | ||
270160b9 | 1787 | static int create_empty_file(const char *path, mode_t mode) |
1788 | { | |
1789 | int fd; | |
1790 | ||
1791 | if ((fd = p_creat(path, mode)) < 0) { | |
ac3d33df | 1792 | git_error_set(GIT_ERROR_OS, "error while creating '%s'", path); |
270160b9 | 1793 | return -1; |
1794 | } | |
1795 | ||
1796 | if (p_close(fd) < 0) { | |
ac3d33df | 1797 | git_error_set(GIT_ERROR_OS, "error while closing '%s'", path); |
270160b9 | 1798 | return -1; |
1799 | } | |
1800 | ||
1801 | return 0; | |
1802 | } | |
1803 | ||
14997dc5 RB |
1804 | static int repo_local_config( |
1805 | git_config **out, | |
e579e0f7 | 1806 | git_str *config_dir, |
14997dc5 RB |
1807 | git_repository *repo, |
1808 | const char *repo_dir) | |
9462c471 | 1809 | { |
ca1b6e54 | 1810 | int error = 0; |
14997dc5 RB |
1811 | git_config *parent; |
1812 | const char *cfg_path; | |
9462c471 | 1813 | |
e579e0f7 | 1814 | if (git_str_joinpath(config_dir, repo_dir, GIT_CONFIG_FILENAME_INREPO) < 0) |
cb8a7961 | 1815 | return -1; |
e579e0f7 | 1816 | cfg_path = git_str_cstr(config_dir); |
9462c471 | 1817 | |
14997dc5 | 1818 | /* make LOCAL config if missing */ |
e579e0f7 | 1819 | if (!git_fs_path_isfile(cfg_path) && |
5173ea92 | 1820 | (error = create_empty_file(cfg_path, GIT_CONFIG_FILE_MODE)) < 0) |
14997dc5 | 1821 | return error; |
270160b9 | 1822 | |
14997dc5 RB |
1823 | /* if no repo, just open that file directly */ |
1824 | if (!repo) | |
1825 | return git_config_open_ondisk(out, cfg_path); | |
1826 | ||
1827 | /* otherwise, open parent config and get that level */ | |
1828 | if ((error = git_repository_config__weakptr(&parent, repo)) < 0) | |
1829 | return error; | |
1830 | ||
1831 | if (git_config_open_level(out, parent, GIT_CONFIG_LEVEL_LOCAL) < 0) { | |
ac3d33df | 1832 | git_error_clear(); |
5173ea92 RB |
1833 | |
1834 | if (!(error = git_config_add_file_ondisk( | |
eae0bfdc | 1835 | parent, cfg_path, GIT_CONFIG_LEVEL_LOCAL, repo, false))) |
14997dc5 | 1836 | error = git_config_open_level(out, parent, GIT_CONFIG_LEVEL_LOCAL); |
cb8a7961 | 1837 | } |
9462c471 | 1838 | |
14997dc5 RB |
1839 | git_config_free(parent); |
1840 | ||
1841 | return error; | |
1842 | } | |
1843 | ||
1844 | static int repo_init_fs_configs( | |
1845 | git_config *cfg, | |
1846 | const char *cfg_path, | |
1847 | const char *repo_dir, | |
1848 | const char *work_dir, | |
1849 | bool update_ignorecase) | |
1850 | { | |
1851 | int error = 0; | |
1852 | ||
1853 | if (!work_dir) | |
1854 | work_dir = repo_dir; | |
1855 | ||
1856 | if ((error = git_config_set_bool( | |
1857 | cfg, "core.filemode", is_chmod_supported(cfg_path))) < 0) | |
1858 | return error; | |
1859 | ||
1860 | if (!are_symlinks_supported(work_dir)) { | |
1861 | if ((error = git_config_set_bool(cfg, "core.symlinks", false)) < 0) | |
1862 | return error; | |
1863 | } else if (git_config_delete_entry(cfg, "core.symlinks") < 0) | |
ac3d33df | 1864 | git_error_clear(); |
2c227b8b | 1865 | |
14997dc5 RB |
1866 | if (update_ignorecase) { |
1867 | if (is_filesystem_case_insensitive(repo_dir)) { | |
1868 | if ((error = git_config_set_bool(cfg, "core.ignorecase", true)) < 0) | |
1869 | return error; | |
1870 | } else if (git_config_delete_entry(cfg, "core.ignorecase") < 0) | |
ac3d33df | 1871 | git_error_clear(); |
14997dc5 | 1872 | } |
662880ca | 1873 | |
af302aca | 1874 | #ifdef GIT_USE_ICONV |
14997dc5 RB |
1875 | if ((error = git_config_set_bool( |
1876 | cfg, "core.precomposeunicode", | |
e579e0f7 | 1877 | git_fs_path_does_decompose_unicode(work_dir))) < 0) |
14997dc5 | 1878 | return error; |
43a04135 | 1879 | /* on non-iconv platforms, don't even set core.precomposeunicode */ |
6b7991e2 RB |
1880 | #endif |
1881 | ||
14997dc5 RB |
1882 | return 0; |
1883 | } | |
7623b1b6 | 1884 | |
14997dc5 RB |
1885 | static int repo_init_config( |
1886 | const char *repo_dir, | |
1887 | const char *work_dir, | |
1888 | uint32_t flags, | |
1889 | uint32_t mode) | |
1890 | { | |
1891 | int error = 0; | |
e579e0f7 | 1892 | git_str cfg_path = GIT_STR_INIT, worktree_path = GIT_STR_INIT; |
14997dc5 RB |
1893 | git_config *config = NULL; |
1894 | bool is_bare = ((flags & GIT_REPOSITORY_INIT_BARE) != 0); | |
1895 | bool is_reinit = ((flags & GIT_REPOSITORY_INIT__IS_REINIT) != 0); | |
22a2d3d5 | 1896 | int version = 0; |
14997dc5 RB |
1897 | |
1898 | if ((error = repo_local_config(&config, &cfg_path, NULL, repo_dir)) < 0) | |
1899 | goto cleanup; | |
1900 | ||
22a2d3d5 UG |
1901 | if (is_reinit && (error = check_repositoryformatversion(&version, config)) < 0) |
1902 | goto cleanup; | |
1903 | ||
1904 | if ((error = check_extensions(config, version)) < 0) | |
14997dc5 RB |
1905 | goto cleanup; |
1906 | ||
1907 | #define SET_REPO_CONFIG(TYPE, NAME, VAL) do { \ | |
1908 | if ((error = git_config_set_##TYPE(config, NAME, VAL)) < 0) \ | |
1909 | goto cleanup; } while (0) | |
1910 | ||
1911 | SET_REPO_CONFIG(bool, "core.bare", is_bare); | |
1912 | SET_REPO_CONFIG(int32, "core.repositoryformatversion", GIT_REPO_VERSION); | |
1913 | ||
1914 | if ((error = repo_init_fs_configs( | |
1915 | config, cfg_path.ptr, repo_dir, work_dir, !is_reinit)) < 0) | |
1916 | goto cleanup; | |
5173ea92 | 1917 | |
6b7991e2 RB |
1918 | if (!is_bare) { |
1919 | SET_REPO_CONFIG(bool, "core.logallrefupdates", true); | |
ca1b6e54 | 1920 | |
bc737620 | 1921 | if (!(flags & GIT_REPOSITORY_INIT__NATURAL_WD)) { |
e579e0f7 | 1922 | if ((error = git_str_sets(&worktree_path, work_dir)) < 0) |
bc737620 JM |
1923 | goto cleanup; |
1924 | ||
1925 | if ((flags & GIT_REPOSITORY_INIT_RELATIVE_GITLINK)) | |
e579e0f7 | 1926 | if ((error = git_fs_path_make_relative(&worktree_path, repo_dir)) < 0) |
bc737620 JM |
1927 | goto cleanup; |
1928 | ||
1929 | SET_REPO_CONFIG(string, "core.worktree", worktree_path.ptr); | |
1930 | } else if (is_reinit) { | |
54b2a37a | 1931 | if (git_config_delete_entry(config, "core.worktree") < 0) |
ac3d33df | 1932 | git_error_clear(); |
ca1b6e54 | 1933 | } |
ca1b6e54 RB |
1934 | } |
1935 | ||
5173ea92 | 1936 | if (mode == GIT_REPOSITORY_INIT_SHARED_GROUP) { |
662880ca RB |
1937 | SET_REPO_CONFIG(int32, "core.sharedrepository", 1); |
1938 | SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true); | |
ca1b6e54 | 1939 | } |
5173ea92 | 1940 | else if (mode == GIT_REPOSITORY_INIT_SHARED_ALL) { |
662880ca RB |
1941 | SET_REPO_CONFIG(int32, "core.sharedrepository", 2); |
1942 | SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true); | |
1943 | } | |
9462c471 | 1944 | |
ca1b6e54 | 1945 | cleanup: |
e579e0f7 MB |
1946 | git_str_dispose(&cfg_path); |
1947 | git_str_dispose(&worktree_path); | |
9462c471 | 1948 | git_config_free(config); |
662880ca | 1949 | |
ca1b6e54 | 1950 | return error; |
d2d6912e | 1951 | } |
e1f8cad0 | 1952 | |
867f7c9b | 1953 | static int repo_reinit_submodule_fs(git_submodule *sm, const char *n, void *p) |
5173ea92 | 1954 | { |
14997dc5 RB |
1955 | git_repository *smrepo = NULL; |
1956 | GIT_UNUSED(n); GIT_UNUSED(p); | |
5173ea92 | 1957 | |
14997dc5 | 1958 | if (git_submodule_open(&smrepo, sm) < 0 || |
867f7c9b | 1959 | git_repository_reinit_filesystem(smrepo, true) < 0) |
ac3d33df | 1960 | git_error_clear(); |
14997dc5 | 1961 | git_repository_free(smrepo); |
5173ea92 | 1962 | |
14997dc5 RB |
1963 | return 0; |
1964 | } | |
5173ea92 | 1965 | |
867f7c9b | 1966 | int git_repository_reinit_filesystem(git_repository *repo, int recurse) |
14997dc5 RB |
1967 | { |
1968 | int error = 0; | |
e579e0f7 | 1969 | git_str path = GIT_STR_INIT; |
14997dc5 RB |
1970 | git_config *config = NULL; |
1971 | const char *repo_dir = git_repository_path(repo); | |
5173ea92 | 1972 | |
14997dc5 RB |
1973 | if (!(error = repo_local_config(&config, &path, repo, repo_dir))) |
1974 | error = repo_init_fs_configs( | |
1975 | config, path.ptr, repo_dir, git_repository_workdir(repo), true); | |
5173ea92 | 1976 | |
14997dc5 | 1977 | git_config_free(config); |
e579e0f7 | 1978 | git_str_dispose(&path); |
5173ea92 | 1979 | |
22a2d3d5 | 1980 | git_repository__configmap_lookup_cache_clear(repo); |
5173ea92 | 1981 | |
14997dc5 | 1982 | if (!repo->is_bare && recurse) |
867f7c9b | 1983 | (void)git_submodule_foreach(repo, repo_reinit_submodule_fs, NULL); |
14997dc5 | 1984 | |
5173ea92 RB |
1985 | return error; |
1986 | } | |
1987 | ||
dc34da6e | 1988 | static int repo_write_template( |
991a56c7 RB |
1989 | const char *git_dir, |
1990 | bool allow_overwrite, | |
1991 | const char *file, | |
1992 | mode_t mode, | |
662880ca | 1993 | bool hidden, |
991a56c7 | 1994 | const char *content) |
dc34da6e | 1995 | { |
e579e0f7 | 1996 | git_str path = GIT_STR_INIT; |
991a56c7 | 1997 | int fd, error = 0, flags; |
dc34da6e | 1998 | |
e579e0f7 | 1999 | if (git_str_joinpath(&path, git_dir, file) < 0) |
dc34da6e RB |
2000 | return -1; |
2001 | ||
991a56c7 RB |
2002 | if (allow_overwrite) |
2003 | flags = O_WRONLY | O_CREAT | O_TRUNC; | |
2004 | else | |
2005 | flags = O_WRONLY | O_CREAT | O_EXCL; | |
2006 | ||
e579e0f7 | 2007 | fd = p_open(git_str_cstr(&path), flags, mode); |
dc34da6e | 2008 | |
db628072 RB |
2009 | if (fd >= 0) { |
2010 | error = p_write(fd, content, strlen(content)); | |
dc34da6e | 2011 | |
db628072 RB |
2012 | p_close(fd); |
2013 | } | |
2014 | else if (errno != EEXIST) | |
2015 | error = fd; | |
dc34da6e | 2016 | |
662880ca RB |
2017 | #ifdef GIT_WIN32 |
2018 | if (!error && hidden) { | |
bdec3363 | 2019 | if (git_win32__set_hidden(path.ptr, true) < 0) |
662880ca RB |
2020 | error = -1; |
2021 | } | |
2022 | #else | |
2023 | GIT_UNUSED(hidden); | |
2024 | #endif | |
2025 | ||
e579e0f7 | 2026 | git_str_dispose(&path); |
db628072 RB |
2027 | |
2028 | if (error) | |
ac3d33df | 2029 | git_error_set(GIT_ERROR_OS, |
909d5494 | 2030 | "failed to initialize repository with template '%s'", file); |
db628072 RB |
2031 | |
2032 | return error; | |
dc34da6e RB |
2033 | } |
2034 | ||
662880ca | 2035 | static int repo_write_gitlink( |
bc737620 | 2036 | const char *in_dir, const char *to_repo, bool use_relative_path) |
4b8e27c8 | 2037 | { |
662880ca | 2038 | int error; |
e579e0f7 MB |
2039 | git_str buf = GIT_STR_INIT; |
2040 | git_str path_to_repo = GIT_STR_INIT; | |
662880ca RB |
2041 | struct stat st; |
2042 | ||
e579e0f7 MB |
2043 | git_fs_path_dirname_r(&buf, to_repo); |
2044 | git_fs_path_to_dir(&buf); | |
2045 | if (git_str_oom(&buf)) | |
cb8a7961 | 2046 | return -1; |
a67a096a | 2047 | |
662880ca RB |
2048 | /* don't write gitlink to natural workdir */ |
2049 | if (git__suffixcmp(to_repo, "/" DOT_GIT "/") == 0 && | |
2050 | strcmp(in_dir, buf.ptr) == 0) | |
2051 | { | |
2052 | error = GIT_PASSTHROUGH; | |
2053 | goto cleanup; | |
2054 | } | |
2055 | ||
e579e0f7 | 2056 | if ((error = git_str_joinpath(&buf, in_dir, DOT_GIT)) < 0) |
662880ca RB |
2057 | goto cleanup; |
2058 | ||
2059 | if (!p_stat(buf.ptr, &st) && !S_ISREG(st.st_mode)) { | |
ac3d33df | 2060 | git_error_set(GIT_ERROR_REPOSITORY, |
909d5494 | 2061 | "cannot overwrite gitlink file into path '%s'", in_dir); |
662880ca RB |
2062 | error = GIT_EEXISTS; |
2063 | goto cleanup; | |
2064 | } | |
2065 | ||
e579e0f7 | 2066 | git_str_clear(&buf); |
662880ca | 2067 | |
e579e0f7 | 2068 | error = git_str_sets(&path_to_repo, to_repo); |
bc737620 JM |
2069 | |
2070 | if (!error && use_relative_path) | |
e579e0f7 | 2071 | error = git_fs_path_make_relative(&path_to_repo, in_dir); |
bc737620 JM |
2072 | |
2073 | if (!error) | |
e579e0f7 | 2074 | error = git_str_join(&buf, ' ', GIT_FILE_CONTENT_PREFIX, path_to_repo.ptr); |
662880ca RB |
2075 | |
2076 | if (!error) | |
18f08264 | 2077 | error = repo_write_template(in_dir, true, DOT_GIT, 0666, true, buf.ptr); |
662880ca RB |
2078 | |
2079 | cleanup: | |
e579e0f7 MB |
2080 | git_str_dispose(&buf); |
2081 | git_str_dispose(&path_to_repo); | |
662880ca RB |
2082 | return error; |
2083 | } | |
2084 | ||
ca1b6e54 RB |
2085 | static mode_t pick_dir_mode(git_repository_init_options *opts) |
2086 | { | |
2087 | if (opts->mode == GIT_REPOSITORY_INIT_SHARED_UMASK) | |
18f08264 | 2088 | return 0777; |
ca1b6e54 RB |
2089 | if (opts->mode == GIT_REPOSITORY_INIT_SHARED_GROUP) |
2090 | return (0775 | S_ISGID); | |
2091 | if (opts->mode == GIT_REPOSITORY_INIT_SHARED_ALL) | |
2092 | return (0777 | S_ISGID); | |
2093 | return opts->mode; | |
2094 | } | |
2095 | ||
662880ca RB |
2096 | #include "repo_template.h" |
2097 | ||
2098 | static int repo_init_structure( | |
2099 | const char *repo_dir, | |
2100 | const char *work_dir, | |
2101 | git_repository_init_options *opts) | |
2102 | { | |
ca1b6e54 | 2103 | int error = 0; |
662880ca | 2104 | repo_template_item *tpl; |
ca1b6e54 RB |
2105 | bool external_tpl = |
2106 | ((opts->flags & GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE) != 0); | |
2107 | mode_t dmode = pick_dir_mode(opts); | |
bafaf790 | 2108 | bool chmod = opts->mode != GIT_REPOSITORY_INIT_SHARED_UMASK; |
662880ca RB |
2109 | |
2110 | /* Hide the ".git" directory */ | |
17837602 | 2111 | #ifdef GIT_WIN32 |
2eb4edf5 | 2112 | if ((opts->flags & GIT_REPOSITORY_INIT__HAS_DOTGIT) != 0) { |
bdec3363 | 2113 | if (git_win32__set_hidden(repo_dir, true) < 0) { |
ac3d33df | 2114 | git_error_set(GIT_ERROR_OS, |
909d5494 | 2115 | "failed to mark Git repository folder as hidden"); |
cb8a7961 VM |
2116 | return -1; |
2117 | } | |
17837602 | 2118 | } |
2eb4edf5 RB |
2119 | #endif |
2120 | ||
2121 | /* Create the .git gitlink if appropriate */ | |
2122 | if ((opts->flags & GIT_REPOSITORY_INIT_BARE) == 0 && | |
2123 | (opts->flags & GIT_REPOSITORY_INIT__NATURAL_WD) == 0) | |
2124 | { | |
bc737620 | 2125 | if (repo_write_gitlink(work_dir, repo_dir, opts->flags & GIT_REPOSITORY_INIT_RELATIVE_GITLINK) < 0) |
cb8a7961 | 2126 | return -1; |
97769280 | 2127 | } |
1c2c7c0d | 2128 | |
ca1b6e54 RB |
2129 | /* Copy external template if requested */ |
2130 | if (external_tpl) { | |
0cd1c3bb L |
2131 | git_config *cfg = NULL; |
2132 | const char *tdir = NULL; | |
2133 | bool default_template = false; | |
e579e0f7 | 2134 | git_str template_buf = GIT_STR_INIT; |
a025907e | 2135 | |
ca1b6e54 RB |
2136 | if (opts->template_path) |
2137 | tdir = opts->template_path; | |
0cd1c3bb | 2138 | else if ((error = git_config_open_default(&cfg)) >= 0) { |
e579e0f7 | 2139 | if (!git_config__get_path(&template_buf, cfg, "init.templatedir")) |
cc36f424 | 2140 | tdir = template_buf.ptr; |
ac3d33df | 2141 | git_error_clear(); |
0cd1c3bb L |
2142 | } |
2143 | ||
2144 | if (!tdir) { | |
83634d38 | 2145 | if (!(error = git_sysdir_find_template_dir(&template_buf))) |
417472e3 | 2146 | tdir = template_buf.ptr; |
0cd1c3bb | 2147 | default_template = true; |
662880ca | 2148 | } |
ca1b6e54 | 2149 | |
eae0bfdc PP |
2150 | /* |
2151 | * If tdir was the empty string, treat it like tdir was a path to an | |
2152 | * empty directory (so, don't do any copying). This is the behavior | |
2153 | * that git(1) exhibits, although it doesn't seem to be officially | |
2154 | * documented. | |
2155 | */ | |
2156 | if (tdir && git__strcmp(tdir, "") != 0) { | |
62602547 ET |
2157 | uint32_t cpflags = GIT_CPDIR_COPY_SYMLINKS | |
2158 | GIT_CPDIR_SIMPLE_TO_MODE | | |
2159 | GIT_CPDIR_COPY_DOTFILES; | |
517341c5 EL |
2160 | if (opts->mode != GIT_REPOSITORY_INIT_SHARED_UMASK) |
2161 | cpflags |= GIT_CPDIR_CHMOD_DIRS; | |
2162 | error = git_futils_cp_r(tdir, repo_dir, cpflags, dmode); | |
bafaf790 | 2163 | } |
ca1b6e54 | 2164 | |
e579e0f7 | 2165 | git_str_dispose(&template_buf); |
0cd1c3bb | 2166 | git_config_free(cfg); |
1ca3e49f | 2167 | |
e579e0f7 MB |
2168 | /* If tdir does not exist, then do not error out. This matches the |
2169 | * behaviour of git(1), which just prints a warning and continues. | |
2170 | * TODO: issue warning when warning API is available. | |
2171 | * `git` prints to stderr: 'warning: templates not found in /path/to/tdir' | |
2172 | */ | |
ca1b6e54 | 2173 | if (error < 0) { |
e579e0f7 | 2174 | if (!default_template && error != GIT_ENOTFOUND) |
ca1b6e54 RB |
2175 | return error; |
2176 | ||
2177 | /* if template was default, ignore error and use internal */ | |
ac3d33df | 2178 | git_error_clear(); |
ca1b6e54 | 2179 | external_tpl = false; |
b7b1acfd | 2180 | error = 0; |
ca1b6e54 RB |
2181 | } |
2182 | } | |
2183 | ||
2184 | /* Copy internal template | |
2185 | * - always ensure existence of dirs | |
2186 | * - only create files if no external template was specified | |
2187 | */ | |
2188 | for (tpl = repo_template; !error && tpl->path; ++tpl) { | |
bafaf790 | 2189 | if (!tpl->content) { |
517341c5 EL |
2190 | uint32_t mkdir_flags = GIT_MKDIR_PATH; |
2191 | if (chmod) | |
2192 | mkdir_flags |= GIT_MKDIR_CHMOD; | |
2193 | ||
ac2fba0e ET |
2194 | error = git_futils_mkdir_relative( |
2195 | tpl->path, repo_dir, dmode, mkdir_flags, NULL); | |
bafaf790 | 2196 | } |
ca1b6e54 | 2197 | else if (!external_tpl) { |
662880ca RB |
2198 | const char *content = tpl->content; |
2199 | ||
2200 | if (opts->description && strcmp(tpl->path, GIT_DESC_FILE) == 0) | |
2201 | content = opts->description; | |
2202 | ||
ca1b6e54 RB |
2203 | error = repo_write_template( |
2204 | repo_dir, false, tpl->path, tpl->mode, false, content); | |
662880ca | 2205 | } |
dc34da6e RB |
2206 | } |
2207 | ||
ca1b6e54 | 2208 | return error; |
4b8e27c8 | 2209 | } |
2210 | ||
e579e0f7 | 2211 | static int mkdir_parent(git_str *buf, uint32_t mode, bool skip2) |
3c42e4ef | 2212 | { |
0d1b094b RB |
2213 | /* When making parent directories during repository initialization |
2214 | * don't try to set gid or grant world write access | |
2215 | */ | |
3c42e4ef | 2216 | return git_futils_mkdir( |
ac2fba0e | 2217 | buf->ptr, mode & ~(S_ISGID | 0002), |
3c42e4ef RB |
2218 | GIT_MKDIR_PATH | GIT_MKDIR_VERIFY_DIR | |
2219 | (skip2 ? GIT_MKDIR_SKIP_LAST2 : GIT_MKDIR_SKIP_LAST)); | |
2220 | } | |
2221 | ||
662880ca | 2222 | static int repo_init_directories( |
e579e0f7 MB |
2223 | git_str *repo_path, |
2224 | git_str *wd_path, | |
662880ca RB |
2225 | const char *given_repo, |
2226 | git_repository_init_options *opts) | |
4b8e27c8 | 2227 | { |
662880ca | 2228 | int error = 0; |
3c42e4ef | 2229 | bool is_bare, add_dotgit, has_dotgit, natural_wd; |
ca1b6e54 | 2230 | mode_t dirmode; |
932d1baf | 2231 | |
3c42e4ef RB |
2232 | /* There are three possible rules for what we are allowed to create: |
2233 | * - MKPATH means anything we need | |
2234 | * - MKDIR means just the .git directory and its parent and the workdir | |
2235 | * - Neither means only the .git directory can be created | |
2236 | * | |
2237 | * There are 5 "segments" of path that we might need to deal with: | |
2238 | * 1. The .git directory | |
2239 | * 2. The parent of the .git directory | |
2240 | * 3. Everything above the parent of the .git directory | |
2241 | * 4. The working directory (often the same as #2) | |
2242 | * 5. Everything above the working directory (often the same as #3) | |
2243 | * | |
2244 | * For all directories created, we start with the init_mode value for | |
2245 | * permissions and then strip off bits in some cases: | |
2246 | * | |
2247 | * For MKPATH, we create #3 (and #5) paths without S_ISGID or S_IWOTH | |
2248 | * For MKPATH and MKDIR, we create #2 (and #4) without S_ISGID | |
2249 | * For all rules, we create #1 using the untouched init_mode | |
2250 | */ | |
2251 | ||
662880ca | 2252 | /* set up repo path */ |
4b8e27c8 | 2253 | |
3c42e4ef RB |
2254 | is_bare = ((opts->flags & GIT_REPOSITORY_INIT_BARE) != 0); |
2255 | ||
662880ca RB |
2256 | add_dotgit = |
2257 | (opts->flags & GIT_REPOSITORY_INIT_NO_DOTGIT_DIR) == 0 && | |
3c42e4ef | 2258 | !is_bare && |
662880ca RB |
2259 | git__suffixcmp(given_repo, "/" DOT_GIT) != 0 && |
2260 | git__suffixcmp(given_repo, "/" GIT_DIR) != 0; | |
4b8e27c8 | 2261 | |
e579e0f7 | 2262 | if (git_str_joinpath(repo_path, given_repo, add_dotgit ? GIT_DIR : "") < 0) |
662880ca | 2263 | return -1; |
693b23c0 | 2264 | |
662880ca RB |
2265 | has_dotgit = (git__suffixcmp(repo_path->ptr, "/" GIT_DIR) == 0); |
2266 | if (has_dotgit) | |
2267 | opts->flags |= GIT_REPOSITORY_INIT__HAS_DOTGIT; | |
2268 | ||
2269 | /* set up workdir path */ | |
2270 | ||
3c42e4ef | 2271 | if (!is_bare) { |
662880ca | 2272 | if (opts->workdir_path) { |
e579e0f7 | 2273 | if (git_fs_path_join_unrooted( |
ca1b6e54 RB |
2274 | wd_path, opts->workdir_path, repo_path->ptr, NULL) < 0) |
2275 | return -1; | |
662880ca | 2276 | } else if (has_dotgit) { |
e579e0f7 | 2277 | if (git_fs_path_dirname_r(wd_path, repo_path->ptr) < 0) |
662880ca RB |
2278 | return -1; |
2279 | } else { | |
ac3d33df | 2280 | git_error_set(GIT_ERROR_REPOSITORY, "cannot pick working directory" |
662880ca RB |
2281 | " for non-bare repository that isn't a '.git' directory"); |
2282 | return -1; | |
2283 | } | |
693b23c0 | 2284 | |
e579e0f7 | 2285 | if (git_fs_path_to_dir(wd_path) < 0) |
662880ca RB |
2286 | return -1; |
2287 | } else { | |
e579e0f7 | 2288 | git_str_clear(wd_path); |
662880ca RB |
2289 | } |
2290 | ||
2291 | natural_wd = | |
2292 | has_dotgit && | |
2293 | wd_path->size > 0 && | |
2294 | wd_path->size + strlen(GIT_DIR) == repo_path->size && | |
2295 | memcmp(repo_path->ptr, wd_path->ptr, wd_path->size) == 0; | |
2296 | if (natural_wd) | |
2297 | opts->flags |= GIT_REPOSITORY_INIT__NATURAL_WD; | |
2298 | ||
662880ca RB |
2299 | /* create directories as needed / requested */ |
2300 | ||
ca1b6e54 | 2301 | dirmode = pick_dir_mode(opts); |
662880ca | 2302 | |
3c42e4ef RB |
2303 | if ((opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0) { |
2304 | /* create path #5 */ | |
2305 | if (wd_path->size > 0 && | |
2306 | (error = mkdir_parent(wd_path, dirmode, false)) < 0) | |
2307 | return error; | |
2308 | ||
2309 | /* create path #3 (if not the same as #5) */ | |
2310 | if (!natural_wd && | |
2311 | (error = mkdir_parent(repo_path, dirmode, has_dotgit)) < 0) | |
2312 | return error; | |
2313 | } | |
2314 | ||
2315 | if ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0 || | |
2316 | (opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0) | |
2317 | { | |
2318 | /* create path #4 */ | |
2319 | if (wd_path->size > 0 && | |
2320 | (error = git_futils_mkdir( | |
ac2fba0e | 2321 | wd_path->ptr, dirmode & ~S_ISGID, |
3c42e4ef RB |
2322 | GIT_MKDIR_VERIFY_DIR)) < 0) |
2323 | return error; | |
2324 | ||
2325 | /* create path #2 (if not the same as #4) */ | |
2326 | if (!natural_wd && | |
2327 | (error = git_futils_mkdir( | |
ac2fba0e | 2328 | repo_path->ptr, dirmode & ~S_ISGID, |
3c42e4ef RB |
2329 | GIT_MKDIR_VERIFY_DIR | GIT_MKDIR_SKIP_LAST)) < 0) |
2330 | return error; | |
662880ca | 2331 | } |
662880ca | 2332 | |
ca1b6e54 RB |
2333 | if ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0 || |
2334 | (opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0 || | |
2335 | has_dotgit) | |
2336 | { | |
3c42e4ef | 2337 | /* create path #1 */ |
ac2fba0e | 2338 | error = git_futils_mkdir(repo_path->ptr, dirmode, |
18f08264 | 2339 | GIT_MKDIR_VERIFY_DIR | ((dirmode & S_ISGID) ? GIT_MKDIR_CHMOD : 0)); |
662880ca | 2340 | } |
ca1b6e54 | 2341 | |
662880ca RB |
2342 | /* prettify both directories now that they are created */ |
2343 | ||
2344 | if (!error) { | |
e579e0f7 | 2345 | error = git_fs_path_prettify_dir(repo_path, repo_path->ptr, NULL); |
662880ca RB |
2346 | |
2347 | if (!error && wd_path->size > 0) | |
e579e0f7 | 2348 | error = git_fs_path_prettify_dir(wd_path, wd_path->ptr, NULL); |
662880ca RB |
2349 | } |
2350 | ||
2351 | return error; | |
2352 | } | |
2353 | ||
22a2d3d5 UG |
2354 | static int repo_init_head(const char *repo_dir, const char *given) |
2355 | { | |
2356 | git_config *cfg = NULL; | |
e579e0f7 | 2357 | git_str head_path = GIT_STR_INIT, cfg_branch = GIT_STR_INIT; |
22a2d3d5 UG |
2358 | const char *initial_head = NULL; |
2359 | int error; | |
2360 | ||
e579e0f7 | 2361 | if ((error = git_str_joinpath(&head_path, repo_dir, GIT_HEAD_FILE)) < 0) |
22a2d3d5 UG |
2362 | goto out; |
2363 | ||
2364 | /* | |
2365 | * A template may have set a HEAD; use that unless it's been | |
2366 | * overridden by the caller's given initial head setting. | |
2367 | */ | |
e579e0f7 | 2368 | if (git_fs_path_exists(head_path.ptr) && !given) |
22a2d3d5 UG |
2369 | goto out; |
2370 | ||
2371 | if (given) { | |
2372 | initial_head = given; | |
2373 | } else if ((error = git_config_open_default(&cfg)) >= 0 && | |
e579e0f7 | 2374 | (error = git_config__get_string_buf(&cfg_branch, cfg, "init.defaultbranch")) >= 0 && |
c25aa7cd | 2375 | *cfg_branch.ptr) { |
22a2d3d5 UG |
2376 | initial_head = cfg_branch.ptr; |
2377 | } | |
2378 | ||
2379 | if (!initial_head) | |
2380 | initial_head = GIT_BRANCH_DEFAULT; | |
2381 | ||
2382 | error = git_repository_create_head(repo_dir, initial_head); | |
2383 | ||
2384 | out: | |
2385 | git_config_free(cfg); | |
e579e0f7 MB |
2386 | git_str_dispose(&head_path); |
2387 | git_str_dispose(&cfg_branch); | |
22a2d3d5 UG |
2388 | |
2389 | return error; | |
2390 | } | |
2391 | ||
662880ca RB |
2392 | static int repo_init_create_origin(git_repository *repo, const char *url) |
2393 | { | |
2394 | int error; | |
2395 | git_remote *remote; | |
2396 | ||
29f27599 | 2397 | if (!(error = git_remote_create(&remote, repo, GIT_REMOTE_ORIGIN, url))) { |
662880ca RB |
2398 | git_remote_free(remote); |
2399 | } | |
2400 | ||
2401 | return error; | |
2402 | } | |
2403 | ||
2404 | int git_repository_init( | |
2405 | git_repository **repo_out, const char *path, unsigned is_bare) | |
2406 | { | |
b4d13652 | 2407 | git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT; |
662880ca | 2408 | |
662880ca RB |
2409 | opts.flags = GIT_REPOSITORY_INIT_MKPATH; /* don't love this default */ |
2410 | if (is_bare) | |
2411 | opts.flags |= GIT_REPOSITORY_INIT_BARE; | |
2412 | ||
2413 | return git_repository_init_ext(repo_out, path, &opts); | |
2414 | } | |
2415 | ||
2416 | int git_repository_init_ext( | |
c9fc4a6f | 2417 | git_repository **out, |
662880ca RB |
2418 | const char *given_repo, |
2419 | git_repository_init_options *opts) | |
2420 | { | |
e579e0f7 MB |
2421 | git_str repo_path = GIT_STR_INIT, wd_path = GIT_STR_INIT, |
2422 | common_path = GIT_STR_INIT; | |
466d2e7a | 2423 | const char *wd; |
22a2d3d5 UG |
2424 | bool is_valid; |
2425 | int error; | |
662880ca | 2426 | |
c25aa7cd PP |
2427 | GIT_ASSERT_ARG(out); |
2428 | GIT_ASSERT_ARG(given_repo); | |
2429 | GIT_ASSERT_ARG(opts); | |
662880ca | 2430 | |
ac3d33df | 2431 | GIT_ERROR_CHECK_VERSION(opts, GIT_REPOSITORY_INIT_OPTIONS_VERSION, "git_repository_init_options"); |
b4d13652 | 2432 | |
22a2d3d5 UG |
2433 | if ((error = repo_init_directories(&repo_path, &wd_path, given_repo, opts)) < 0) |
2434 | goto out; | |
662880ca | 2435 | |
e579e0f7 | 2436 | wd = (opts->flags & GIT_REPOSITORY_INIT_BARE) ? NULL : git_str_cstr(&wd_path); |
662880ca | 2437 | |
22a2d3d5 UG |
2438 | if ((error = is_valid_repository_path(&is_valid, &repo_path, &common_path)) < 0) |
2439 | goto out; | |
2440 | ||
2441 | if (is_valid) { | |
662880ca | 2442 | if ((opts->flags & GIT_REPOSITORY_INIT_NO_REINIT) != 0) { |
ac3d33df | 2443 | git_error_set(GIT_ERROR_REPOSITORY, |
909d5494 | 2444 | "attempt to reinitialize '%s'", given_repo); |
662880ca | 2445 | error = GIT_EEXISTS; |
22a2d3d5 | 2446 | goto out; |
662880ca RB |
2447 | } |
2448 | ||
2449 | opts->flags |= GIT_REPOSITORY_INIT__IS_REINIT; | |
2450 | ||
22a2d3d5 UG |
2451 | if ((error = repo_init_config(repo_path.ptr, wd, opts->flags, opts->mode)) < 0) |
2452 | goto out; | |
662880ca RB |
2453 | |
2454 | /* TODO: reinitialize the templates */ | |
22a2d3d5 UG |
2455 | } else { |
2456 | if ((error = repo_init_structure(repo_path.ptr, wd, opts)) < 0 || | |
2457 | (error = repo_init_config(repo_path.ptr, wd, opts->flags, opts->mode)) < 0 || | |
2458 | (error = repo_init_head(repo_path.ptr, opts->initial_head)) < 0) | |
2459 | goto out; | |
662880ca | 2460 | } |
662880ca | 2461 | |
22a2d3d5 UG |
2462 | if ((error = git_repository_open(out, repo_path.ptr)) < 0) |
2463 | goto out; | |
d2d6912e | 2464 | |
22a2d3d5 UG |
2465 | if (opts->origin_url && |
2466 | (error = repo_init_create_origin(*out, opts->origin_url)) < 0) | |
2467 | goto out; | |
693b23c0 | 2468 | |
22a2d3d5 | 2469 | out: |
e579e0f7 MB |
2470 | git_str_dispose(&common_path); |
2471 | git_str_dispose(&repo_path); | |
2472 | git_str_dispose(&wd_path); | |
662880ca RB |
2473 | |
2474 | return error; | |
40c44d2f | 2475 | } |
35502d2e | 2476 | |
c682886e | 2477 | int git_repository_head_detached(git_repository *repo) |
35502d2e CMN |
2478 | { |
2479 | git_reference *ref; | |
9462c471 | 2480 | git_odb *odb = NULL; |
cb8a7961 | 2481 | int exists; |
9462c471 | 2482 | |
cb8a7961 VM |
2483 | if (git_repository_odb__weakptr(&odb, repo) < 0) |
2484 | return -1; | |
35502d2e | 2485 | |
cb8a7961 VM |
2486 | if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0) |
2487 | return -1; | |
35502d2e | 2488 | |
ac3d33df | 2489 | if (git_reference_type(ref) == GIT_REFERENCE_SYMBOLIC) { |
75abd2b9 | 2490 | git_reference_free(ref); |
35502d2e | 2491 | return 0; |
75abd2b9 | 2492 | } |
35502d2e | 2493 | |
2508cc66 | 2494 | exists = git_odb_exists(odb, git_reference_target(ref)); |
75abd2b9 MS |
2495 | |
2496 | git_reference_free(ref); | |
cb8a7961 | 2497 | return exists; |
35502d2e CMN |
2498 | } |
2499 | ||
04fb12ab PS |
2500 | int git_repository_head_detached_for_worktree(git_repository *repo, const char *name) |
2501 | { | |
3e84aa50 PS |
2502 | git_reference *ref = NULL; |
2503 | int error; | |
04fb12ab | 2504 | |
c25aa7cd PP |
2505 | GIT_ASSERT_ARG(repo); |
2506 | GIT_ASSERT_ARG(name); | |
04fb12ab | 2507 | |
3e84aa50 PS |
2508 | if ((error = git_repository_head_for_worktree(&ref, repo, name)) < 0) |
2509 | goto out; | |
04fb12ab | 2510 | |
ac3d33df | 2511 | error = (git_reference_type(ref) != GIT_REFERENCE_SYMBOLIC); |
3e84aa50 PS |
2512 | out: |
2513 | git_reference_free(ref); | |
04fb12ab | 2514 | |
3e84aa50 | 2515 | return error; |
04fb12ab PS |
2516 | } |
2517 | ||
3601c4bf | 2518 | int git_repository_head(git_reference **head_out, git_repository *repo) |
35502d2e | 2519 | { |
b1a3a70e | 2520 | git_reference *head; |
8b05bea8 | 2521 | int error; |
2522 | ||
c25aa7cd | 2523 | GIT_ASSERT_ARG(head_out); |
ac3d33df | 2524 | |
b1a3a70e | 2525 | if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0) |
2526 | return error; | |
2527 | ||
ac3d33df | 2528 | if (git_reference_type(head) == GIT_REFERENCE_DIRECT) { |
b1a3a70e | 2529 | *head_out = head; |
2530 | return 0; | |
2531 | } | |
2532 | ||
2508cc66 | 2533 | error = git_reference_lookup_resolved(head_out, repo, git_reference_symbolic_target(head), -1); |
b1a3a70e | 2534 | git_reference_free(head); |
8b05bea8 | 2535 | |
605da51a | 2536 | return error == GIT_ENOTFOUND ? GIT_EUNBORNBRANCH : error; |
3601c4bf | 2537 | } |
2538 | ||
04fb12ab PS |
2539 | int git_repository_head_for_worktree(git_reference **out, git_repository *repo, const char *name) |
2540 | { | |
22a2d3d5 UG |
2541 | git_repository *worktree_repo = NULL; |
2542 | git_worktree *worktree = NULL; | |
3e84aa50 PS |
2543 | git_reference *head = NULL; |
2544 | int error; | |
04fb12ab | 2545 | |
c25aa7cd PP |
2546 | GIT_ASSERT_ARG(out); |
2547 | GIT_ASSERT_ARG(repo); | |
2548 | GIT_ASSERT_ARG(name); | |
04fb12ab PS |
2549 | |
2550 | *out = NULL; | |
2551 | ||
22a2d3d5 UG |
2552 | if ((error = git_worktree_lookup(&worktree, repo, name)) < 0 || |
2553 | (error = git_repository_open_from_worktree(&worktree_repo, worktree)) < 0 || | |
2554 | (error = git_reference_lookup(&head, worktree_repo, GIT_HEAD_FILE)) < 0) | |
04fb12ab PS |
2555 | goto out; |
2556 | ||
ac3d33df | 2557 | if (git_reference_type(head) != GIT_REFERENCE_DIRECT) { |
22a2d3d5 UG |
2558 | if ((error = git_reference_lookup_resolved(out, worktree_repo, git_reference_symbolic_target(head), -1)) < 0) |
2559 | goto out; | |
2560 | } else { | |
2561 | *out = head; | |
2562 | head = NULL; | |
04fb12ab PS |
2563 | } |
2564 | ||
04fb12ab | 2565 | out: |
22a2d3d5 UG |
2566 | git_reference_free(head); |
2567 | git_worktree_free(worktree); | |
2568 | git_repository_free(worktree_repo); | |
3e84aa50 | 2569 | return error; |
04fb12ab PS |
2570 | } |
2571 | ||
22a2d3d5 UG |
2572 | int git_repository_foreach_worktree(git_repository *repo, |
2573 | git_repository_foreach_worktree_cb cb, | |
2574 | void *payload) | |
74511aa2 | 2575 | { |
22a2d3d5 UG |
2576 | git_strarray worktrees = {0}; |
2577 | git_repository *worktree_repo = NULL; | |
2578 | git_worktree *worktree = NULL; | |
74511aa2 PS |
2579 | int error; |
2580 | size_t i; | |
2581 | ||
c25aa7cd PP |
2582 | /* apply operation to repository supplied when commondir is empty, implying there's |
2583 | * no linked worktrees to iterate, which can occur when using custom odb/refdb | |
2584 | */ | |
2585 | if (!repo->commondir) | |
2586 | return cb(repo, payload); | |
2587 | ||
22a2d3d5 UG |
2588 | if ((error = git_repository_open(&worktree_repo, repo->commondir)) < 0 || |
2589 | (error = cb(worktree_repo, payload) != 0)) | |
74511aa2 PS |
2590 | goto out; |
2591 | ||
22a2d3d5 UG |
2592 | git_repository_free(worktree_repo); |
2593 | worktree_repo = NULL; | |
2594 | ||
2595 | if ((error = git_worktree_list(&worktrees, repo)) < 0) | |
74511aa2 | 2596 | goto out; |
74511aa2 | 2597 | |
74511aa2 | 2598 | for (i = 0; i < worktrees.count; i++) { |
22a2d3d5 UG |
2599 | git_repository_free(worktree_repo); |
2600 | worktree_repo = NULL; | |
2601 | git_worktree_free(worktree); | |
2602 | worktree = NULL; | |
2603 | ||
2604 | if ((error = git_worktree_lookup(&worktree, repo, worktrees.strings[i]) < 0) || | |
2605 | (error = git_repository_open_from_worktree(&worktree_repo, worktree)) < 0) { | |
2606 | if (error != GIT_ENOTFOUND) | |
2607 | goto out; | |
2608 | error = 0; | |
74511aa2 | 2609 | continue; |
22a2d3d5 | 2610 | } |
74511aa2 | 2611 | |
22a2d3d5 | 2612 | if ((error = cb(worktree_repo, payload)) != 0) |
74511aa2 PS |
2613 | goto out; |
2614 | } | |
2615 | ||
2616 | out: | |
22a2d3d5 UG |
2617 | git_strarray_dispose(&worktrees); |
2618 | git_repository_free(worktree_repo); | |
2619 | git_worktree_free(worktree); | |
74511aa2 PS |
2620 | return error; |
2621 | } | |
2622 | ||
605da51a | 2623 | int git_repository_head_unborn(git_repository *repo) |
3601c4bf | 2624 | { |
cb8a7961 | 2625 | git_reference *ref = NULL; |
3601c4bf | 2626 | int error; |
2627 | ||
2628 | error = git_repository_head(&ref, repo); | |
cb8a7961 | 2629 | git_reference_free(ref); |
35502d2e | 2630 | |
7c9bf891 | 2631 | if (error == GIT_EUNBORNBRANCH) { |
ac3d33df | 2632 | git_error_clear(); |
cb8a7961 | 2633 | return 1; |
7c9bf891 | 2634 | } |
75abd2b9 | 2635 | |
cb8a7961 VM |
2636 | if (error < 0) |
2637 | return -1; | |
2638 | ||
2639 | return 0; | |
35502d2e | 2640 | } |
e0011be3 | 2641 | |
6091457e | 2642 | static int repo_contains_no_reference(git_repository *repo) |
2643 | { | |
c25aa7cd PP |
2644 | git_reference_iterator *iter; |
2645 | const char *refname; | |
2646 | int error; | |
0f489fb2 | 2647 | |
c25aa7cd PP |
2648 | if ((error = git_reference_iterator_new(&iter, repo)) < 0) |
2649 | return error; | |
ec24e542 | 2650 | |
c25aa7cd PP |
2651 | error = git_reference_next_name(&refname, iter); |
2652 | git_reference_iterator_free(iter); | |
2653 | ||
2654 | if (error == GIT_ITEROVER) | |
e976b56d | 2655 | return 1; |
ec24e542 | 2656 | |
e976b56d | 2657 | return error; |
6091457e | 2658 | } |
75abd2b9 | 2659 | |
e579e0f7 | 2660 | int git_repository_initialbranch(git_str *out, git_repository *repo) |
22a2d3d5 UG |
2661 | { |
2662 | git_config *config; | |
2663 | git_config_entry *entry = NULL; | |
2664 | const char *branch; | |
c25aa7cd | 2665 | int valid, error; |
22a2d3d5 UG |
2666 | |
2667 | if ((error = git_repository_config__weakptr(&config, repo)) < 0) | |
2668 | return error; | |
2669 | ||
c25aa7cd PP |
2670 | if ((error = git_config_get_entry(&entry, config, "init.defaultbranch")) == 0 && |
2671 | *entry->value) { | |
22a2d3d5 UG |
2672 | branch = entry->value; |
2673 | } | |
c25aa7cd | 2674 | else if (!error || error == GIT_ENOTFOUND) { |
22a2d3d5 UG |
2675 | branch = GIT_BRANCH_DEFAULT; |
2676 | } | |
2677 | else { | |
2678 | goto done; | |
2679 | } | |
2680 | ||
e579e0f7 MB |
2681 | if ((error = git_str_puts(out, GIT_REFS_HEADS_DIR)) < 0 || |
2682 | (error = git_str_puts(out, branch)) < 0 || | |
c25aa7cd | 2683 | (error = git_reference_name_is_valid(&valid, out->ptr)) < 0) |
22a2d3d5 UG |
2684 | goto done; |
2685 | ||
c25aa7cd PP |
2686 | if (!valid) { |
2687 | git_error_set(GIT_ERROR_INVALID, "the value of init.defaultBranch is not a valid branch name"); | |
22a2d3d5 UG |
2688 | error = -1; |
2689 | } | |
2690 | ||
2691 | done: | |
2692 | git_config_entry_free(entry); | |
2693 | return error; | |
2694 | } | |
2695 | ||
6091457e | 2696 | int git_repository_is_empty(git_repository *repo) |
2697 | { | |
2698 | git_reference *head = NULL; | |
e579e0f7 | 2699 | git_str initialbranch = GIT_STR_INIT; |
22a2d3d5 | 2700 | int result = 0; |
cb8a7961 | 2701 | |
22a2d3d5 UG |
2702 | if ((result = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0 || |
2703 | (result = git_repository_initialbranch(&initialbranch, repo)) < 0) | |
2704 | goto done; | |
cb8a7961 | 2705 | |
22a2d3d5 UG |
2706 | result = (git_reference_type(head) == GIT_REFERENCE_SYMBOLIC && |
2707 | strcmp(git_reference_symbolic_target(head), initialbranch.ptr) == 0 && | |
2708 | repo_contains_no_reference(repo)); | |
6091457e | 2709 | |
22a2d3d5 | 2710 | done: |
6091457e | 2711 | git_reference_free(head); |
e579e0f7 | 2712 | git_str_dispose(&initialbranch); |
42181836 | 2713 | |
22a2d3d5 | 2714 | return result; |
41233c40 VM |
2715 | } |
2716 | ||
22a2d3d5 | 2717 | static const char *resolved_parent_path(const git_repository *repo, git_repository_item_t item, git_repository_item_t fallback) |
cb3269c9 PS |
2718 | { |
2719 | const char *parent; | |
2720 | ||
22a2d3d5 | 2721 | switch (item) { |
cb3269c9 PS |
2722 | case GIT_REPOSITORY_ITEM_GITDIR: |
2723 | parent = git_repository_path(repo); | |
2724 | break; | |
2725 | case GIT_REPOSITORY_ITEM_WORKDIR: | |
2726 | parent = git_repository_workdir(repo); | |
2727 | break; | |
2728 | case GIT_REPOSITORY_ITEM_COMMONDIR: | |
2729 | parent = git_repository_commondir(repo); | |
2730 | break; | |
2731 | default: | |
ac3d33df | 2732 | git_error_set(GIT_ERROR_INVALID, "invalid item directory"); |
22a2d3d5 | 2733 | return NULL; |
cb3269c9 | 2734 | } |
22a2d3d5 UG |
2735 | if (!parent && fallback != GIT_REPOSITORY_ITEM__LAST) |
2736 | return resolved_parent_path(repo, fallback, GIT_REPOSITORY_ITEM__LAST); | |
2737 | ||
2738 | return parent; | |
2739 | } | |
cb3269c9 | 2740 | |
e579e0f7 MB |
2741 | int git_repository_item_path( |
2742 | git_buf *out, | |
2743 | const git_repository *repo, | |
2744 | git_repository_item_t item) | |
2745 | { | |
2746 | GIT_BUF_WRAP_PRIVATE(out, git_repository__item_path, repo, item); | |
2747 | } | |
2748 | ||
2749 | int git_repository__item_path( | |
2750 | git_str *out, | |
2751 | const git_repository *repo, | |
2752 | git_repository_item_t item) | |
22a2d3d5 UG |
2753 | { |
2754 | const char *parent = resolved_parent_path(repo, items[item].parent, items[item].fallback); | |
cb3269c9 | 2755 | if (parent == NULL) { |
ac3d33df | 2756 | git_error_set(GIT_ERROR_INVALID, "path cannot exist in repository"); |
9d49a43c | 2757 | return GIT_ENOTFOUND; |
cb3269c9 PS |
2758 | } |
2759 | ||
e579e0f7 | 2760 | if (git_str_sets(out, parent) < 0) |
cb3269c9 PS |
2761 | return -1; |
2762 | ||
2763 | if (items[item].name) { | |
e579e0f7 | 2764 | if (git_str_joinpath(out, parent, items[item].name) < 0) |
cb3269c9 PS |
2765 | return -1; |
2766 | } | |
2767 | ||
2768 | if (items[item].directory) { | |
e579e0f7 | 2769 | if (git_fs_path_to_dir(out) < 0) |
cb3269c9 PS |
2770 | return -1; |
2771 | } | |
2772 | ||
2773 | return 0; | |
2774 | } | |
2775 | ||
eae0bfdc | 2776 | const char *git_repository_path(const git_repository *repo) |
4a34b3a9 | 2777 | { |
c25aa7cd | 2778 | GIT_ASSERT_ARG_WITH_RETVAL(repo, NULL); |
84f56cb0 | 2779 | return repo->gitdir; |
9462c471 | 2780 | } |
4a34b3a9 | 2781 | |
eae0bfdc | 2782 | const char *git_repository_workdir(const git_repository *repo) |
9462c471 | 2783 | { |
c25aa7cd | 2784 | GIT_ASSERT_ARG_WITH_RETVAL(repo, NULL); |
602ee38b | 2785 | |
9462c471 VM |
2786 | if (repo->is_bare) |
2787 | return NULL; | |
602ee38b | 2788 | |
9462c471 VM |
2789 | return repo->workdir; |
2790 | } | |
602ee38b | 2791 | |
c25aa7cd | 2792 | int git_repository_workdir_path( |
e579e0f7 | 2793 | git_str *out, git_repository *repo, const char *path) |
c25aa7cd PP |
2794 | { |
2795 | int error; | |
2796 | ||
2797 | if (!repo->workdir) { | |
2798 | git_error_set(GIT_ERROR_REPOSITORY, "repository has no working directory"); | |
2799 | return GIT_EBAREREPO; | |
2800 | } | |
2801 | ||
e579e0f7 MB |
2802 | if (!(error = git_str_joinpath(out, repo->workdir, path))) |
2803 | error = git_path_validate_str_length(repo, out); | |
c25aa7cd PP |
2804 | |
2805 | return error; | |
2806 | } | |
2807 | ||
eae0bfdc | 2808 | const char *git_repository_commondir(const git_repository *repo) |
c09fd54e | 2809 | { |
c25aa7cd | 2810 | GIT_ASSERT_ARG_WITH_RETVAL(repo, NULL); |
c09fd54e PS |
2811 | return repo->commondir; |
2812 | } | |
2813 | ||
991a56c7 RB |
2814 | int git_repository_set_workdir( |
2815 | git_repository *repo, const char *workdir, int update_gitlink) | |
9462c471 | 2816 | { |
991a56c7 | 2817 | int error = 0; |
e579e0f7 | 2818 | git_str path = GIT_STR_INIT; |
b78fb64d | 2819 | |
c25aa7cd PP |
2820 | GIT_ASSERT_ARG(repo); |
2821 | GIT_ASSERT_ARG(workdir); | |
602ee38b | 2822 | |
e579e0f7 | 2823 | if (git_fs_path_prettify_dir(&path, workdir, NULL) < 0) |
b78fb64d | 2824 | return -1; |
9462c471 | 2825 | |
991a56c7 RB |
2826 | if (repo->workdir && strcmp(repo->workdir, path.ptr) == 0) |
2827 | return 0; | |
9462c471 | 2828 | |
991a56c7 RB |
2829 | if (update_gitlink) { |
2830 | git_config *config; | |
2831 | ||
2832 | if (git_repository_config__weakptr(&config, repo) < 0) | |
2833 | return -1; | |
2834 | ||
bc737620 | 2835 | error = repo_write_gitlink(path.ptr, git_repository_path(repo), false); |
991a56c7 RB |
2836 | |
2837 | /* passthrough error means gitlink is unnecessary */ | |
2838 | if (error == GIT_PASSTHROUGH) | |
54b2a37a | 2839 | error = git_config_delete_entry(config, "core.worktree"); |
991a56c7 RB |
2840 | else if (!error) |
2841 | error = git_config_set_string(config, "core.worktree", path.ptr); | |
2842 | ||
2843 | if (!error) | |
2844 | error = git_config_set_bool(config, "core.bare", false); | |
2845 | } | |
2846 | ||
2847 | if (!error) { | |
2848 | char *old_workdir = repo->workdir; | |
2849 | ||
e579e0f7 | 2850 | repo->workdir = git_str_detach(&path); |
991a56c7 RB |
2851 | repo->is_bare = 0; |
2852 | ||
2853 | git__free(old_workdir); | |
2854 | } | |
2855 | ||
2856 | return error; | |
4a34b3a9 | 2857 | } |
fa9bcd81 | 2858 | |
eae0bfdc | 2859 | int git_repository_is_bare(const git_repository *repo) |
fa9bcd81 | 2860 | { |
c25aa7cd | 2861 | GIT_ASSERT_ARG(repo); |
fa9bcd81 | 2862 | return repo->is_bare; |
2863 | } | |
f917481e | 2864 | |
eae0bfdc | 2865 | int git_repository_is_worktree(const git_repository *repo) |
79ab3ef6 | 2866 | { |
c25aa7cd | 2867 | GIT_ASSERT_ARG(repo); |
79ab3ef6 PS |
2868 | return repo->is_worktree; |
2869 | } | |
2870 | ||
1fbeb2f0 RB |
2871 | int git_repository_set_bare(git_repository *repo) |
2872 | { | |
2873 | int error; | |
2874 | git_config *config; | |
2875 | ||
c25aa7cd | 2876 | GIT_ASSERT_ARG(repo); |
1fbeb2f0 RB |
2877 | |
2878 | if (repo->is_bare) | |
2879 | return 0; | |
2880 | ||
74240afb T |
2881 | if ((error = git_repository_config__weakptr(&config, repo)) < 0) |
2882 | return error; | |
2883 | ||
b2a7bcdb | 2884 | if ((error = git_config_set_bool(config, "core.bare", true)) < 0) |
74240afb | 2885 | return error; |
1fbeb2f0 | 2886 | |
74240afb T |
2887 | if ((error = git_config__update_entry(config, "core.worktree", NULL, true, true)) < 0) |
2888 | return error; | |
1fbeb2f0 RB |
2889 | |
2890 | git__free(repo->workdir); | |
2891 | repo->workdir = NULL; | |
1fbeb2f0 RB |
2892 | repo->is_bare = 1; |
2893 | ||
74240afb | 2894 | return 0; |
1fbeb2f0 RB |
2895 | } |
2896 | ||
f917481e RB |
2897 | int git_repository_head_tree(git_tree **tree, git_repository *repo) |
2898 | { | |
5cec896a | 2899 | git_reference *head; |
2900 | git_object *obj; | |
2901 | int error; | |
f917481e | 2902 | |
5cec896a | 2903 | if ((error = git_repository_head(&head, repo)) < 0) |
2904 | return error; | |
f917481e | 2905 | |
ac3d33df | 2906 | if ((error = git_reference_peel(&obj, head, GIT_OBJECT_TREE)) < 0) |
5cec896a | 2907 | goto cleanup; |
f917481e RB |
2908 | |
2909 | *tree = (git_tree *)obj; | |
5cec896a | 2910 | |
2911 | cleanup: | |
2912 | git_reference_free(head); | |
2913 | return error; | |
f917481e | 2914 | } |
074841ec | 2915 | |
867a36f3 ET |
2916 | int git_repository__set_orig_head(git_repository *repo, const git_oid *orig_head) |
2917 | { | |
2918 | git_filebuf file = GIT_FILEBUF_INIT; | |
e579e0f7 | 2919 | git_str file_path = GIT_STR_INIT; |
867a36f3 ET |
2920 | char orig_head_str[GIT_OID_HEXSZ]; |
2921 | int error = 0; | |
2922 | ||
2923 | git_oid_fmt(orig_head_str, orig_head); | |
2924 | ||
e579e0f7 | 2925 | if ((error = git_str_joinpath(&file_path, repo->gitdir, GIT_ORIG_HEAD_FILE)) == 0 && |
22a2d3d5 | 2926 | (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_MERGE_FILE_MODE)) == 0 && |
867a36f3 ET |
2927 | (error = git_filebuf_printf(&file, "%.*s\n", GIT_OID_HEXSZ, orig_head_str)) == 0) |
2928 | error = git_filebuf_commit(&file); | |
2929 | ||
2930 | if (error < 0) | |
2931 | git_filebuf_cleanup(&file); | |
2932 | ||
e579e0f7 | 2933 | git_str_dispose(&file_path); |
867a36f3 ET |
2934 | |
2935 | return error; | |
2936 | } | |
2937 | ||
e579e0f7 | 2938 | static int git_repository__message(git_str *out, git_repository *repo) |
074841ec | 2939 | { |
e579e0f7 | 2940 | git_str path = GIT_STR_INIT; |
0ac349a9 | 2941 | struct stat st; |
0ac349a9 | 2942 | int error; |
074841ec | 2943 | |
e579e0f7 | 2944 | if (git_str_joinpath(&path, repo->gitdir, GIT_MERGE_MSG_FILE) < 0) |
0ac349a9 | 2945 | return -1; |
074841ec | 2946 | |
e579e0f7 | 2947 | if ((error = p_stat(git_str_cstr(&path), &st)) < 0) { |
074841ec CMN |
2948 | if (errno == ENOENT) |
2949 | error = GIT_ENOTFOUND; | |
ac3d33df | 2950 | git_error_set(GIT_ERROR_OS, "could not access message file"); |
3158e2fe | 2951 | } else { |
e579e0f7 | 2952 | error = git_futils_readbuffer(out, git_str_cstr(&path)); |
0ac349a9 | 2953 | } |
074841ec | 2954 | |
e579e0f7 | 2955 | git_str_dispose(&path); |
e9ca852e RB |
2956 | |
2957 | return error; | |
074841ec CMN |
2958 | } |
2959 | ||
e579e0f7 MB |
2960 | int git_repository_message(git_buf *out, git_repository *repo) |
2961 | { | |
2962 | GIT_BUF_WRAP_PRIVATE(out, git_repository__message, repo); | |
2963 | } | |
2964 | ||
074841ec CMN |
2965 | int git_repository_message_remove(git_repository *repo) |
2966 | { | |
e579e0f7 | 2967 | git_str path = GIT_STR_INIT; |
0ac349a9 | 2968 | int error; |
074841ec | 2969 | |
e579e0f7 | 2970 | if (git_str_joinpath(&path, repo->gitdir, GIT_MERGE_MSG_FILE) < 0) |
0ac349a9 | 2971 | return -1; |
074841ec | 2972 | |
e579e0f7 MB |
2973 | error = p_unlink(git_str_cstr(&path)); |
2974 | git_str_dispose(&path); | |
074841ec CMN |
2975 | |
2976 | return error; | |
2977 | } | |
47bfa0be RB |
2978 | |
2979 | int git_repository_hashfile( | |
0cb16fe9 L |
2980 | git_oid *out, |
2981 | git_repository *repo, | |
2982 | const char *path, | |
ac3d33df | 2983 | git_object_t type, |
0cb16fe9 | 2984 | const char *as_path) |
47bfa0be RB |
2985 | { |
2986 | int error; | |
85d54812 | 2987 | git_filter_list *fl = NULL; |
b1127a30 | 2988 | git_file fd = -1; |
22a2d3d5 | 2989 | uint64_t len; |
e579e0f7 | 2990 | git_str full_path = GIT_STR_INIT; |
c25aa7cd | 2991 | const char *workdir = git_repository_workdir(repo); |
47bfa0be | 2992 | |
c25aa7cd PP |
2993 | /* as_path can be NULL */ |
2994 | GIT_ASSERT_ARG(out); | |
2995 | GIT_ASSERT_ARG(path); | |
2996 | GIT_ASSERT_ARG(repo); | |
a13fb55a | 2997 | |
e579e0f7 MB |
2998 | if ((error = git_fs_path_join_unrooted(&full_path, path, workdir, NULL)) < 0 || |
2999 | (error = git_path_validate_str_length(repo, &full_path)) < 0) | |
47bfa0be RB |
3000 | return error; |
3001 | ||
c25aa7cd PP |
3002 | /* |
3003 | * NULL as_path means that we should derive it from the | |
3004 | * given path. | |
3005 | */ | |
3006 | if (!as_path) { | |
3007 | if (workdir && !git__prefixcmp(full_path.ptr, workdir)) | |
3008 | as_path = full_path.ptr + strlen(workdir); | |
3009 | else | |
3010 | as_path = ""; | |
3011 | } | |
47bfa0be RB |
3012 | |
3013 | /* passing empty string for "as_path" indicated --no-filters */ | |
3014 | if (strlen(as_path) > 0) { | |
4b11f25a | 3015 | error = git_filter_list_load( |
5269008c | 3016 | &fl, repo, NULL, as_path, |
795eaccd | 3017 | GIT_FILTER_TO_ODB, GIT_FILTER_DEFAULT); |
c25aa7cd | 3018 | |
47bfa0be RB |
3019 | if (error < 0) |
3020 | return error; | |
47bfa0be RB |
3021 | } |
3022 | ||
3023 | /* at this point, error is a count of the number of loaded filters */ | |
3024 | ||
3025 | fd = git_futils_open_ro(full_path.ptr); | |
3026 | if (fd < 0) { | |
3027 | error = fd; | |
3028 | goto cleanup; | |
3029 | } | |
3030 | ||
22a2d3d5 | 3031 | if ((error = git_futils_filesize(&len, fd)) < 0) |
47bfa0be | 3032 | goto cleanup; |
47bfa0be RB |
3033 | |
3034 | if (!git__is_sizet(len)) { | |
ac3d33df | 3035 | git_error_set(GIT_ERROR_OS, "file size overflow for 32-bit systems"); |
47bfa0be RB |
3036 | error = -1; |
3037 | goto cleanup; | |
3038 | } | |
3039 | ||
85d54812 | 3040 | error = git_odb__hashfd_filtered(out, fd, (size_t)len, type, fl); |
47bfa0be RB |
3041 | |
3042 | cleanup: | |
b1127a30 SS |
3043 | if (fd >= 0) |
3044 | p_close(fd); | |
85d54812 | 3045 | git_filter_list_free(fl); |
e579e0f7 | 3046 | git_str_dispose(&full_path); |
47bfa0be RB |
3047 | |
3048 | return error; | |
3049 | } | |
3050 | ||
e579e0f7 | 3051 | static int checkout_message(git_str *out, git_reference *old, const char *new) |
44af67a8 | 3052 | { |
e579e0f7 | 3053 | git_str_puts(out, "checkout: moving from "); |
4e498646 | 3054 | |
ac3d33df | 3055 | if (git_reference_type(old) == GIT_REFERENCE_SYMBOLIC) |
e579e0f7 | 3056 | git_str_puts(out, git_reference__shorthand(git_reference_symbolic_target(old))); |
4e498646 | 3057 | else |
e579e0f7 | 3058 | git_str_puts(out, git_oid_tostr_s(git_reference_target(old))); |
4e498646 | 3059 | |
e579e0f7 | 3060 | git_str_puts(out, " to "); |
4e498646 | 3061 | |
e86d02f9 ET |
3062 | if (git_reference__is_branch(new) || |
3063 | git_reference__is_tag(new) || | |
3064 | git_reference__is_remote(new)) | |
e579e0f7 | 3065 | git_str_puts(out, git_reference__shorthand(new)); |
4e498646 | 3066 | else |
e579e0f7 | 3067 | git_str_puts(out, new); |
4e498646 | 3068 | |
e579e0f7 | 3069 | if (git_str_oom(out)) |
4e498646 CMN |
3070 | return -1; |
3071 | ||
3072 | return 0; | |
44af67a8 | 3073 | } |
3074 | ||
ea3bb5c0 ET |
3075 | static int detach(git_repository *repo, const git_oid *id, const char *new) |
3076 | { | |
3077 | int error; | |
e579e0f7 | 3078 | git_str log_message = GIT_STR_INIT; |
ea3bb5c0 ET |
3079 | git_object *object = NULL, *peeled = NULL; |
3080 | git_reference *new_head = NULL, *current = NULL; | |
3081 | ||
c25aa7cd PP |
3082 | GIT_ASSERT_ARG(repo); |
3083 | GIT_ASSERT_ARG(id); | |
ea3bb5c0 ET |
3084 | |
3085 | if ((error = git_reference_lookup(¤t, repo, GIT_HEAD_FILE)) < 0) | |
3086 | return error; | |
3087 | ||
ac3d33df | 3088 | if ((error = git_object_lookup(&object, repo, id, GIT_OBJECT_ANY)) < 0) |
ea3bb5c0 ET |
3089 | goto cleanup; |
3090 | ||
ac3d33df | 3091 | if ((error = git_object_peel(&peeled, object, GIT_OBJECT_COMMIT)) < 0) |
ea3bb5c0 ET |
3092 | goto cleanup; |
3093 | ||
3094 | if (new == NULL) | |
3095 | new = git_oid_tostr_s(git_object_id(peeled)); | |
3096 | ||
3097 | if ((error = checkout_message(&log_message, current, new)) < 0) | |
3098 | goto cleanup; | |
3099 | ||
e579e0f7 | 3100 | error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_object_id(peeled), true, git_str_cstr(&log_message)); |
ea3bb5c0 ET |
3101 | |
3102 | cleanup: | |
e579e0f7 | 3103 | git_str_dispose(&log_message); |
ea3bb5c0 ET |
3104 | git_object_free(object); |
3105 | git_object_free(peeled); | |
3106 | git_reference_free(current); | |
3107 | git_reference_free(new_head); | |
3108 | return error; | |
3109 | } | |
3110 | ||
44af67a8 | 3111 | int git_repository_set_head( |
c25aa7cd PP |
3112 | git_repository *repo, |
3113 | const char *refname) | |
44af67a8 | 3114 | { |
4e498646 | 3115 | git_reference *ref = NULL, *current = NULL, *new_head = NULL; |
e579e0f7 | 3116 | git_str log_message = GIT_STR_INIT; |
44af67a8 | 3117 | int error; |
3118 | ||
c25aa7cd PP |
3119 | GIT_ASSERT_ARG(repo); |
3120 | GIT_ASSERT_ARG(refname); | |
44af67a8 | 3121 | |
4e498646 CMN |
3122 | if ((error = git_reference_lookup(¤t, repo, GIT_HEAD_FILE)) < 0) |
3123 | return error; | |
3124 | ||
3125 | if ((error = checkout_message(&log_message, current, refname)) < 0) | |
3126 | goto cleanup; | |
3127 | ||
44af67a8 | 3128 | error = git_reference_lookup(&ref, repo, refname); |
3129 | if (error < 0 && error != GIT_ENOTFOUND) | |
4e498646 | 3130 | goto cleanup; |
44af67a8 | 3131 | |
ac3d33df | 3132 | if (ref && current->type == GIT_REFERENCE_SYMBOLIC && git__strcmp(current->target.symbolic, ref->name) && |
384518d0 | 3133 | git_reference_is_branch(ref) && git_branch_is_checked_out(ref)) { |
ac3d33df | 3134 | git_error_set(GIT_ERROR_REPOSITORY, "cannot set HEAD to reference '%s' as it is the current HEAD " |
8242cc1a | 3135 | "of a linked repository.", git_reference_name(ref)); |
384518d0 PS |
3136 | error = -1; |
3137 | goto cleanup; | |
3138 | } | |
3139 | ||
44af67a8 | 3140 | if (!error) { |
94f263f5 BS |
3141 | if (git_reference_is_branch(ref)) { |
3142 | error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, | |
e579e0f7 | 3143 | git_reference_name(ref), true, git_str_cstr(&log_message)); |
94f263f5 | 3144 | } else { |
ea3bb5c0 | 3145 | error = detach(repo, git_reference_target(ref), |
e86d02f9 | 3146 | git_reference_is_tag(ref) || git_reference_is_remote(ref) ? refname : NULL); |
94f263f5 | 3147 | } |
4e498646 | 3148 | } else if (git_reference__is_branch(refname)) { |
94f263f5 | 3149 | error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, refname, |
e579e0f7 | 3150 | true, git_str_cstr(&log_message)); |
94f263f5 | 3151 | } |
44af67a8 | 3152 | |
4e498646 | 3153 | cleanup: |
e579e0f7 | 3154 | git_str_dispose(&log_message); |
4e498646 | 3155 | git_reference_free(current); |
44af67a8 | 3156 | git_reference_free(ref); |
3157 | git_reference_free(new_head); | |
3158 | return error; | |
3159 | } | |
3160 | ||
62d38a1d | 3161 | int git_repository_set_head_detached( |
c25aa7cd | 3162 | git_repository *repo, |
e579e0f7 | 3163 | const git_oid *committish) |
62d38a1d | 3164 | { |
e579e0f7 | 3165 | return detach(repo, committish, NULL); |
62d38a1d CMN |
3166 | } |
3167 | ||
3168 | int git_repository_set_head_detached_from_annotated( | |
3169 | git_repository *repo, | |
e579e0f7 | 3170 | const git_annotated_commit *committish) |
62d38a1d | 3171 | { |
c25aa7cd | 3172 | GIT_ASSERT_ARG(repo); |
e579e0f7 | 3173 | GIT_ASSERT_ARG(committish); |
62d38a1d | 3174 | |
e579e0f7 | 3175 | return detach(repo, git_annotated_commit_id(committish), committish->description); |
62d38a1d CMN |
3176 | } |
3177 | ||
c25aa7cd | 3178 | int git_repository_detach_head(git_repository *repo) |
3f4c3072 | 3179 | { |
4e498646 | 3180 | git_reference *old_head = NULL, *new_head = NULL, *current = NULL; |
3f4c3072 | 3181 | git_object *object = NULL; |
e579e0f7 | 3182 | git_str log_message = GIT_STR_INIT; |
8b05bea8 | 3183 | int error; |
3f4c3072 | 3184 | |
c25aa7cd | 3185 | GIT_ASSERT_ARG(repo); |
3f4c3072 | 3186 | |
4e498646 | 3187 | if ((error = git_reference_lookup(¤t, repo, GIT_HEAD_FILE)) < 0) |
8b05bea8 | 3188 | return error; |
3f4c3072 | 3189 | |
4e498646 CMN |
3190 | if ((error = git_repository_head(&old_head, repo)) < 0) |
3191 | goto cleanup; | |
3192 | ||
ac3d33df | 3193 | if ((error = git_object_lookup(&object, repo, git_reference_target(old_head), GIT_OBJECT_COMMIT)) < 0) |
3f4c3072 | 3194 | goto cleanup; |
3195 | ||
4e498646 CMN |
3196 | if ((error = checkout_message(&log_message, current, git_oid_tostr_s(git_object_id(object)))) < 0) |
3197 | goto cleanup; | |
3198 | ||
010cec3a | 3199 | error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_reference_target(old_head), |
e579e0f7 | 3200 | 1, git_str_cstr(&log_message)); |
3f4c3072 | 3201 | |
3202 | cleanup: | |
e579e0f7 | 3203 | git_str_dispose(&log_message); |
3f4c3072 | 3204 | git_object_free(object); |
3205 | git_reference_free(old_head); | |
3206 | git_reference_free(new_head); | |
4e498646 | 3207 | git_reference_free(current); |
3f4c3072 | 3208 | return error; |
3209 | } | |
632d8b23 | 3210 | |
31966d20 | 3211 | /** |
3212 | * Loosely ported from git.git | |
3213 | * https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh#L198-289 | |
3214 | */ | |
632d8b23 ET |
3215 | int git_repository_state(git_repository *repo) |
3216 | { | |
e579e0f7 | 3217 | git_str repo_path = GIT_STR_INIT; |
632d8b23 ET |
3218 | int state = GIT_REPOSITORY_STATE_NONE; |
3219 | ||
c25aa7cd | 3220 | GIT_ASSERT_ARG(repo); |
632d8b23 | 3221 | |
e579e0f7 | 3222 | if (git_str_puts(&repo_path, repo->gitdir) < 0) |
632d8b23 ET |
3223 | return -1; |
3224 | ||
e579e0f7 | 3225 | if (git_fs_path_contains_file(&repo_path, GIT_REBASE_MERGE_INTERACTIVE_FILE)) |
31966d20 | 3226 | state = GIT_REPOSITORY_STATE_REBASE_INTERACTIVE; |
e579e0f7 | 3227 | else if (git_fs_path_contains_dir(&repo_path, GIT_REBASE_MERGE_DIR)) |
31966d20 | 3228 | state = GIT_REPOSITORY_STATE_REBASE_MERGE; |
e579e0f7 | 3229 | else if (git_fs_path_contains_file(&repo_path, GIT_REBASE_APPLY_REBASING_FILE)) |
31966d20 | 3230 | state = GIT_REPOSITORY_STATE_REBASE; |
e579e0f7 | 3231 | else if (git_fs_path_contains_file(&repo_path, GIT_REBASE_APPLY_APPLYING_FILE)) |
31966d20 | 3232 | state = GIT_REPOSITORY_STATE_APPLY_MAILBOX; |
e579e0f7 | 3233 | else if (git_fs_path_contains_dir(&repo_path, GIT_REBASE_APPLY_DIR)) |
31966d20 | 3234 | state = GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE; |
e579e0f7 | 3235 | else if (git_fs_path_contains_file(&repo_path, GIT_MERGE_HEAD_FILE)) |
632d8b23 | 3236 | state = GIT_REPOSITORY_STATE_MERGE; |
e579e0f7 | 3237 | else if (git_fs_path_contains_file(&repo_path, GIT_REVERT_HEAD_FILE)) { |
632d8b23 | 3238 | state = GIT_REPOSITORY_STATE_REVERT; |
e579e0f7 | 3239 | if (git_fs_path_contains_file(&repo_path, GIT_SEQUENCER_TODO_FILE)) { |
2ea40fda CMN |
3240 | state = GIT_REPOSITORY_STATE_REVERT_SEQUENCE; |
3241 | } | |
e579e0f7 | 3242 | } else if (git_fs_path_contains_file(&repo_path, GIT_CHERRYPICK_HEAD_FILE)) { |
0ba4dca5 | 3243 | state = GIT_REPOSITORY_STATE_CHERRYPICK; |
e579e0f7 | 3244 | if (git_fs_path_contains_file(&repo_path, GIT_SEQUENCER_TODO_FILE)) { |
2ea40fda CMN |
3245 | state = GIT_REPOSITORY_STATE_CHERRYPICK_SEQUENCE; |
3246 | } | |
e579e0f7 | 3247 | } else if (git_fs_path_contains_file(&repo_path, GIT_BISECT_LOG_FILE)) |
31966d20 | 3248 | state = GIT_REPOSITORY_STATE_BISECT; |
632d8b23 | 3249 | |
e579e0f7 | 3250 | git_str_dispose(&repo_path); |
632d8b23 ET |
3251 | return state; |
3252 | } | |
93d8f77f | 3253 | |
c3dcbe84 VM |
3254 | int git_repository__cleanup_files( |
3255 | git_repository *repo, const char *files[], size_t files_len) | |
bab0b9f2 | 3256 | { |
e579e0f7 | 3257 | git_str buf = GIT_STR_INIT; |
bab0b9f2 | 3258 | size_t i; |
c3dcbe84 | 3259 | int error; |
bab0b9f2 | 3260 | |
c3dcbe84 VM |
3261 | for (error = 0, i = 0; !error && i < files_len; ++i) { |
3262 | const char *path; | |
bab0b9f2 | 3263 | |
e579e0f7 | 3264 | if (git_str_joinpath(&buf, repo->gitdir, files[i]) < 0) |
c3dcbe84 | 3265 | return -1; |
bab0b9f2 | 3266 | |
e579e0f7 | 3267 | path = git_str_cstr(&buf); |
c3dcbe84 | 3268 | |
e579e0f7 | 3269 | if (git_fs_path_isfile(path)) { |
c3dcbe84 | 3270 | error = p_unlink(path); |
e579e0f7 | 3271 | } else if (git_fs_path_isdir(path)) { |
c3dcbe84 VM |
3272 | error = git_futils_rmdir_r(path, NULL, |
3273 | GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS); | |
3274 | } | |
eae0bfdc | 3275 | |
e579e0f7 | 3276 | git_str_clear(&buf); |
c3dcbe84 | 3277 | } |
bab0b9f2 | 3278 | |
e579e0f7 | 3279 | git_str_dispose(&buf); |
bab0b9f2 ET |
3280 | return error; |
3281 | } | |
3282 | ||
3283 | static const char *state_files[] = { | |
3284 | GIT_MERGE_HEAD_FILE, | |
3285 | GIT_MERGE_MODE_FILE, | |
3286 | GIT_MERGE_MSG_FILE, | |
3287 | GIT_REVERT_HEAD_FILE, | |
0ba4dca5 | 3288 | GIT_CHERRYPICK_HEAD_FILE, |
c0311295 JG |
3289 | GIT_BISECT_LOG_FILE, |
3290 | GIT_REBASE_MERGE_DIR, | |
3291 | GIT_REBASE_APPLY_DIR, | |
2ea40fda | 3292 | GIT_SEQUENCER_DIR, |
bab0b9f2 ET |
3293 | }; |
3294 | ||
3295 | int git_repository_state_cleanup(git_repository *repo) | |
3296 | { | |
c25aa7cd | 3297 | GIT_ASSERT_ARG(repo); |
bab0b9f2 ET |
3298 | |
3299 | return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files)); | |
3300 | } | |
3301 | ||
93d8f77f BS |
3302 | int git_repository_is_shallow(git_repository *repo) |
3303 | { | |
e579e0f7 | 3304 | git_str path = GIT_STR_INIT; |
93d8f77f | 3305 | struct stat st; |
6f0b8142 | 3306 | int error; |
93d8f77f | 3307 | |
e579e0f7 | 3308 | if ((error = git_str_joinpath(&path, repo->gitdir, "shallow")) < 0) |
3dbd9a0e JG |
3309 | return error; |
3310 | ||
e579e0f7 MB |
3311 | error = git_fs_path_lstat(path.ptr, &st); |
3312 | git_str_dispose(&path); | |
93d8f77f | 3313 | |
9bda5fb8 | 3314 | if (error == GIT_ENOTFOUND) { |
ac3d33df | 3315 | git_error_clear(); |
93d8f77f | 3316 | return 0; |
9bda5fb8 BS |
3317 | } |
3318 | ||
864535cf BS |
3319 | if (error < 0) |
3320 | return error; | |
3321 | return st.st_size == 0 ? 0 : 1; | |
93d8f77f | 3322 | } |
b9f81997 | 3323 | |
22a2d3d5 | 3324 | int git_repository_init_options_init( |
702efc89 | 3325 | git_repository_init_options *opts, unsigned int version) |
b9f81997 | 3326 | { |
702efc89 RB |
3327 | GIT_INIT_STRUCTURE_FROM_TEMPLATE( |
3328 | opts, version, git_repository_init_options, | |
3329 | GIT_REPOSITORY_INIT_OPTIONS_INIT); | |
3330 | return 0; | |
b9f81997 | 3331 | } |
659cf202 | 3332 | |
22a2d3d5 UG |
3333 | #ifndef GIT_DEPRECATE_HARD |
3334 | int git_repository_init_init_options( | |
3335 | git_repository_init_options *opts, unsigned int version) | |
3336 | { | |
3337 | return git_repository_init_options_init(opts, version); | |
3338 | } | |
3339 | #endif | |
3340 | ||
659cf202 CMN |
3341 | int git_repository_ident(const char **name, const char **email, const git_repository *repo) |
3342 | { | |
3343 | *name = repo->ident_name; | |
3344 | *email = repo->ident_email; | |
3345 | ||
3346 | return 0; | |
3347 | } | |
3348 | ||
3349 | int git_repository_set_ident(git_repository *repo, const char *name, const char *email) | |
3350 | { | |
3351 | char *tmp_name = NULL, *tmp_email = NULL; | |
3352 | ||
3353 | if (name) { | |
3354 | tmp_name = git__strdup(name); | |
ac3d33df | 3355 | GIT_ERROR_CHECK_ALLOC(tmp_name); |
659cf202 CMN |
3356 | } |
3357 | ||
3358 | if (email) { | |
3359 | tmp_email = git__strdup(email); | |
ac3d33df | 3360 | GIT_ERROR_CHECK_ALLOC(tmp_email); |
659cf202 CMN |
3361 | } |
3362 | ||
c25aa7cd PP |
3363 | tmp_name = git_atomic_swap(repo->ident_name, tmp_name); |
3364 | tmp_email = git_atomic_swap(repo->ident_email, tmp_email); | |
659cf202 CMN |
3365 | |
3366 | git__free(tmp_name); | |
3367 | git__free(tmp_email); | |
3368 | ||
3369 | return 0; | |
3370 | } | |
4d99c4cf BP |
3371 | |
3372 | int git_repository_submodule_cache_all(git_repository *repo) | |
3373 | { | |
c25aa7cd PP |
3374 | GIT_ASSERT_ARG(repo); |
3375 | return git_submodule_cache_init(&repo->submodule_cache, repo); | |
4d99c4cf BP |
3376 | } |
3377 | ||
3378 | int git_repository_submodule_cache_clear(git_repository *repo) | |
3379 | { | |
c25aa7cd PP |
3380 | int error = 0; |
3381 | GIT_ASSERT_ARG(repo); | |
3382 | ||
3383 | error = git_submodule_cache_free(repo->submodule_cache); | |
3384 | repo->submodule_cache = NULL; | |
3385 | return error; | |
4d99c4cf | 3386 | } |