]> git.proxmox.com Git - libgit2.git/blob - src/repository.c
Merge pull request #4088 from chescock/packfile-name-using-complete-hash
[libgit2.git] / src / repository.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
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.
6 */
7 #include <ctype.h>
8
9 #include "git2/object.h"
10 #include "git2/refdb.h"
11 #include "git2/sys/repository.h"
12
13 #include "common.h"
14 #include "repository.h"
15 #include "commit.h"
16 #include "tag.h"
17 #include "blob.h"
18 #include "fileops.h"
19 #include "sysdir.h"
20 #include "filebuf.h"
21 #include "index.h"
22 #include "config.h"
23 #include "refs.h"
24 #include "filter.h"
25 #include "odb.h"
26 #include "remote.h"
27 #include "merge.h"
28 #include "diff_driver.h"
29 #include "annotated_commit.h"
30 #include "submodule.h"
31 #include "worktree.h"
32
33 #include "strmap.h"
34
35 #ifdef GIT_WIN32
36 # include "win32/w32_util.h"
37 #endif
38
39 bool git_repository__fsync_gitdir = false;
40
41 static const struct {
42 git_repository_item_t parent;
43 const char *name;
44 bool directory;
45 } items[] = {
46 { GIT_REPOSITORY_ITEM_GITDIR, NULL, true },
47 { GIT_REPOSITORY_ITEM_WORKDIR, NULL, true },
48 { GIT_REPOSITORY_ITEM_COMMONDIR, NULL, true },
49 { GIT_REPOSITORY_ITEM_GITDIR, "index", false },
50 { GIT_REPOSITORY_ITEM_COMMONDIR, "objects", true },
51 { GIT_REPOSITORY_ITEM_COMMONDIR, "refs", true },
52 { GIT_REPOSITORY_ITEM_COMMONDIR, "packed-refs", false },
53 { GIT_REPOSITORY_ITEM_COMMONDIR, "remotes", true },
54 { GIT_REPOSITORY_ITEM_COMMONDIR, "config", false },
55 { GIT_REPOSITORY_ITEM_COMMONDIR, "info", true },
56 { GIT_REPOSITORY_ITEM_COMMONDIR, "hooks", true },
57 { GIT_REPOSITORY_ITEM_COMMONDIR, "logs", true },
58 { GIT_REPOSITORY_ITEM_GITDIR, "modules", true },
59 { GIT_REPOSITORY_ITEM_COMMONDIR, "worktrees", true }
60 };
61
62 static int check_repositoryformatversion(git_config *config);
63
64 #define GIT_COMMONDIR_FILE "commondir"
65 #define GIT_GITDIR_FILE "gitdir"
66
67 #define GIT_FILE_CONTENT_PREFIX "gitdir:"
68
69 #define GIT_BRANCH_MASTER "master"
70
71 #define GIT_REPO_VERSION 0
72
73 git_buf git_repository__reserved_names_win32[] = {
74 { DOT_GIT, 0, CONST_STRLEN(DOT_GIT) },
75 { GIT_DIR_SHORTNAME, 0, CONST_STRLEN(GIT_DIR_SHORTNAME) }
76 };
77 size_t git_repository__reserved_names_win32_len = 2;
78
79 git_buf git_repository__reserved_names_posix[] = {
80 { DOT_GIT, 0, CONST_STRLEN(DOT_GIT) },
81 };
82 size_t git_repository__reserved_names_posix_len = 1;
83
84 static void set_odb(git_repository *repo, git_odb *odb)
85 {
86 if (odb) {
87 GIT_REFCOUNT_OWN(odb, repo);
88 GIT_REFCOUNT_INC(odb);
89 }
90
91 if ((odb = git__swap(repo->_odb, odb)) != NULL) {
92 GIT_REFCOUNT_OWN(odb, NULL);
93 git_odb_free(odb);
94 }
95 }
96
97 static void set_refdb(git_repository *repo, git_refdb *refdb)
98 {
99 if (refdb) {
100 GIT_REFCOUNT_OWN(refdb, repo);
101 GIT_REFCOUNT_INC(refdb);
102 }
103
104 if ((refdb = git__swap(repo->_refdb, refdb)) != NULL) {
105 GIT_REFCOUNT_OWN(refdb, NULL);
106 git_refdb_free(refdb);
107 }
108 }
109
110 static void set_config(git_repository *repo, git_config *config)
111 {
112 if (config) {
113 GIT_REFCOUNT_OWN(config, repo);
114 GIT_REFCOUNT_INC(config);
115 }
116
117 if ((config = git__swap(repo->_config, config)) != NULL) {
118 GIT_REFCOUNT_OWN(config, NULL);
119 git_config_free(config);
120 }
121
122 git_repository__cvar_cache_clear(repo);
123 }
124
125 static void set_index(git_repository *repo, git_index *index)
126 {
127 if (index) {
128 GIT_REFCOUNT_OWN(index, repo);
129 GIT_REFCOUNT_INC(index);
130 }
131
132 if ((index = git__swap(repo->_index, index)) != NULL) {
133 GIT_REFCOUNT_OWN(index, NULL);
134 git_index_free(index);
135 }
136 }
137
138 void git_repository__cleanup(git_repository *repo)
139 {
140 assert(repo);
141
142 git_repository_submodule_cache_clear(repo);
143 git_cache_clear(&repo->objects);
144 git_attr_cache_flush(repo);
145
146 set_config(repo, NULL);
147 set_index(repo, NULL);
148 set_odb(repo, NULL);
149 set_refdb(repo, NULL);
150 }
151
152 void git_repository_free(git_repository *repo)
153 {
154 size_t i;
155
156 if (repo == NULL)
157 return;
158
159 git_repository__cleanup(repo);
160
161 git_cache_free(&repo->objects);
162
163 git_diff_driver_registry_free(repo->diff_drivers);
164 repo->diff_drivers = NULL;
165
166 for (i = 0; i < repo->reserved_names.size; i++)
167 git_buf_free(git_array_get(repo->reserved_names, i));
168 git_array_clear(repo->reserved_names);
169
170 git__free(repo->gitlink);
171 git__free(repo->gitdir);
172 git__free(repo->commondir);
173 git__free(repo->workdir);
174 git__free(repo->namespace);
175 git__free(repo->ident_name);
176 git__free(repo->ident_email);
177
178 git__memzero(repo, sizeof(*repo));
179 git__free(repo);
180 }
181
182 /*
183 * Git repository open methods
184 *
185 * Open a repository object from its path
186 */
187 static bool valid_repository_path(git_buf *repository_path, git_buf *common_path)
188 {
189 /* Check if we have a separate commondir (e.g. we have a
190 * worktree) */
191 if (git_path_contains_file(repository_path, GIT_COMMONDIR_FILE)) {
192 git_buf common_link = GIT_BUF_INIT;
193 git_buf_joinpath(&common_link, repository_path->ptr, GIT_COMMONDIR_FILE);
194
195 git_futils_readbuffer(&common_link, common_link.ptr);
196 git_buf_rtrim(&common_link);
197
198 if (git_path_is_relative(common_link.ptr)) {
199 git_buf_joinpath(common_path, repository_path->ptr, common_link.ptr);
200 } else {
201 git_buf_swap(common_path, &common_link);
202 }
203
204 git_buf_free(&common_link);
205 }
206 else {
207 git_buf_set(common_path, repository_path->ptr, repository_path->size);
208 }
209
210 /* Make sure the commondir path always has a trailing * slash */
211 if (git_buf_rfind(common_path, '/') != (ssize_t)common_path->size - 1)
212 git_buf_putc(common_path, '/');
213
214 /* Ensure HEAD file exists */
215 if (git_path_contains_file(repository_path, GIT_HEAD_FILE) == false)
216 return false;
217
218 /* Check files in common dir */
219 if (git_path_contains_dir(common_path, GIT_OBJECTS_DIR) == false)
220 return false;
221 if (git_path_contains_dir(common_path, GIT_REFS_DIR) == false)
222 return false;
223
224 return true;
225 }
226
227 static git_repository *repository_alloc(void)
228 {
229 git_repository *repo = git__calloc(1, sizeof(git_repository));
230
231 if (repo == NULL ||
232 git_cache_init(&repo->objects) < 0)
233 goto on_error;
234
235 git_array_init_to_size(repo->reserved_names, 4);
236 if (!repo->reserved_names.ptr)
237 goto on_error;
238
239 /* set all the entries in the cvar cache to `unset` */
240 git_repository__cvar_cache_clear(repo);
241
242 return repo;
243
244 on_error:
245 if (repo)
246 git_cache_free(&repo->objects);
247
248 git__free(repo);
249 return NULL;
250 }
251
252 int git_repository_new(git_repository **out)
253 {
254 git_repository *repo;
255
256 *out = repo = repository_alloc();
257 GITERR_CHECK_ALLOC(repo);
258
259 repo->is_bare = 1;
260 repo->is_worktree = 0;
261
262 return 0;
263 }
264
265 static int load_config_data(git_repository *repo, const git_config *config)
266 {
267 int is_bare;
268
269 /* Try to figure out if it's bare, default to non-bare if it's not set */
270 if (git_config_get_bool(&is_bare, config, "core.bare") < 0)
271 repo->is_bare = 0;
272 else
273 repo->is_bare = is_bare;
274
275 return 0;
276 }
277
278 static int load_workdir(git_repository *repo, git_config *config, git_buf *parent_path)
279 {
280 int error;
281 git_config_entry *ce;
282 git_buf worktree = GIT_BUF_INIT;
283 git_buf path = GIT_BUF_INIT;
284
285 if (repo->is_bare)
286 return 0;
287
288 if ((error = git_config__lookup_entry(
289 &ce, config, "core.worktree", false)) < 0)
290 return error;
291
292 if (repo->is_worktree) {
293 char *gitlink = git_worktree__read_link(repo->gitdir, GIT_GITDIR_FILE);
294 if (!gitlink) {
295 error = -1;
296 goto cleanup;
297 }
298
299 git_buf_attach(&worktree, gitlink, 0);
300
301 if ((git_path_dirname_r(&worktree, worktree.ptr)) < 0 ||
302 git_path_to_dir(&worktree) < 0) {
303 error = -1;
304 goto cleanup;
305 }
306
307 repo->workdir = git_buf_detach(&worktree);
308 }
309 else if (ce && ce->value) {
310 if ((error = git_path_prettify_dir(
311 &worktree, ce->value, repo->gitdir)) < 0)
312 goto cleanup;
313
314 repo->workdir = git_buf_detach(&worktree);
315 }
316 else if (parent_path && git_path_isdir(parent_path->ptr))
317 repo->workdir = git_buf_detach(parent_path);
318 else {
319 if (git_path_dirname_r(&worktree, repo->gitdir) < 0 ||
320 git_path_to_dir(&worktree) < 0) {
321 error = -1;
322 goto cleanup;
323 }
324
325 repo->workdir = git_buf_detach(&worktree);
326 }
327
328 GITERR_CHECK_ALLOC(repo->workdir);
329 cleanup:
330 git_buf_free(&path);
331 git_config_entry_free(ce);
332 return error;
333 }
334
335 /*
336 * This function returns furthest offset into path where a ceiling dir
337 * is found, so we can stop processing the path at that point.
338 *
339 * Note: converting this to use git_bufs instead of GIT_PATH_MAX buffers on
340 * the stack could remove directories name limits, but at the cost of doing
341 * repeated malloc/frees inside the loop below, so let's not do it now.
342 */
343 static size_t find_ceiling_dir_offset(
344 const char *path,
345 const char *ceiling_directories)
346 {
347 char buf[GIT_PATH_MAX + 1];
348 char buf2[GIT_PATH_MAX + 1];
349 const char *ceil, *sep;
350 size_t len, max_len = 0, min_len;
351
352 assert(path);
353
354 min_len = (size_t)(git_path_root(path) + 1);
355
356 if (ceiling_directories == NULL || min_len == 0)
357 return min_len;
358
359 for (sep = ceil = ceiling_directories; *sep; ceil = sep + 1) {
360 for (sep = ceil; *sep && *sep != GIT_PATH_LIST_SEPARATOR; sep++);
361 len = sep - ceil;
362
363 if (len == 0 || len >= sizeof(buf) || git_path_root(ceil) == -1)
364 continue;
365
366 strncpy(buf, ceil, len);
367 buf[len] = '\0';
368
369 if (p_realpath(buf, buf2) == NULL)
370 continue;
371
372 len = strlen(buf2);
373 if (len > 0 && buf2[len-1] == '/')
374 buf[--len] = '\0';
375
376 if (!strncmp(path, buf2, len) &&
377 (path[len] == '/' || !path[len]) &&
378 len > max_len)
379 {
380 max_len = len;
381 }
382 }
383
384 return (max_len <= min_len ? min_len : max_len);
385 }
386
387 /*
388 * Read the contents of `file_path` and set `path_out` to the repo dir that
389 * it points to. Before calling, set `path_out` to the base directory that
390 * should be used if the contents of `file_path` are a relative path.
391 */
392 static int read_gitfile(git_buf *path_out, const char *file_path)
393 {
394 int error = 0;
395 git_buf file = GIT_BUF_INIT;
396 size_t prefix_len = strlen(GIT_FILE_CONTENT_PREFIX);
397
398 assert(path_out && file_path);
399
400 if (git_futils_readbuffer(&file, file_path) < 0)
401 return -1;
402
403 git_buf_rtrim(&file);
404 /* apparently on Windows, some people use backslashes in paths */
405 git_path_mkposix(file.ptr);
406
407 if (git_buf_len(&file) <= prefix_len ||
408 memcmp(git_buf_cstr(&file), GIT_FILE_CONTENT_PREFIX, prefix_len) != 0)
409 {
410 giterr_set(GITERR_REPOSITORY,
411 "the `.git` file at '%s' is malformed", file_path);
412 error = -1;
413 }
414 else if ((error = git_path_dirname_r(path_out, file_path)) >= 0) {
415 const char *gitlink = git_buf_cstr(&file) + prefix_len;
416 while (*gitlink && git__isspace(*gitlink)) gitlink++;
417
418 error = git_path_prettify_dir(
419 path_out, gitlink, git_buf_cstr(path_out));
420 }
421
422 git_buf_free(&file);
423 return error;
424 }
425
426 static int find_repo(
427 git_buf *gitdir_path,
428 git_buf *workdir_path,
429 git_buf *gitlink_path,
430 git_buf *commondir_path,
431 const char *start_path,
432 uint32_t flags,
433 const char *ceiling_dirs)
434 {
435 int error;
436 git_buf path = GIT_BUF_INIT;
437 git_buf repo_link = GIT_BUF_INIT;
438 git_buf common_link = GIT_BUF_INIT;
439 struct stat st;
440 dev_t initial_device = 0;
441 int min_iterations;
442 bool in_dot_git;
443 size_t ceiling_offset = 0;
444
445 git_buf_clear(gitdir_path);
446
447 error = git_path_prettify(&path, start_path, NULL);
448 if (error < 0)
449 return error;
450
451 /* in_dot_git toggles each loop:
452 * /a/b/c/.git, /a/b/c, /a/b/.git, /a/b, /a/.git, /a
453 * With GIT_REPOSITORY_OPEN_BARE or GIT_REPOSITORY_OPEN_NO_DOTGIT, we
454 * assume we started with /a/b/c.git and don't append .git the first
455 * time through.
456 * min_iterations indicates the number of iterations left before going
457 * further counts as a search. */
458 if (flags & (GIT_REPOSITORY_OPEN_BARE | GIT_REPOSITORY_OPEN_NO_DOTGIT)) {
459 in_dot_git = true;
460 min_iterations = 1;
461 } else {
462 in_dot_git = false;
463 min_iterations = 2;
464 }
465
466 for (;;) {
467 if (!(flags & GIT_REPOSITORY_OPEN_NO_DOTGIT)) {
468 if (!in_dot_git) {
469 error = git_buf_joinpath(&path, path.ptr, DOT_GIT);
470 if (error < 0)
471 break;
472 }
473 in_dot_git = !in_dot_git;
474 }
475
476 if (p_stat(path.ptr, &st) == 0) {
477 /* check that we have not crossed device boundaries */
478 if (initial_device == 0)
479 initial_device = st.st_dev;
480 else if (st.st_dev != initial_device &&
481 !(flags & GIT_REPOSITORY_OPEN_CROSS_FS))
482 break;
483
484 if (S_ISDIR(st.st_mode)) {
485 if (valid_repository_path(&path, &common_link)) {
486 git_path_to_dir(&path);
487 git_buf_set(gitdir_path, path.ptr, path.size);
488
489 if (gitlink_path)
490 git_buf_attach(gitlink_path,
491 git_worktree__read_link(path.ptr, GIT_GITDIR_FILE), 0);
492 if (commondir_path)
493 git_buf_swap(&common_link, commondir_path);
494
495 break;
496 }
497 }
498 else if (S_ISREG(st.st_mode) && git__suffixcmp(path.ptr, "/" DOT_GIT) == 0) {
499 error = read_gitfile(&repo_link, path.ptr);
500 if (error < 0)
501 break;
502 if (valid_repository_path(&repo_link, &common_link)) {
503 git_buf_swap(gitdir_path, &repo_link);
504
505 if (gitlink_path)
506 error = git_buf_put(gitlink_path, path.ptr, path.size);
507 if (commondir_path)
508 git_buf_swap(&common_link, commondir_path);
509 }
510 break;
511 }
512 }
513
514 /* Move up one directory. If we're in_dot_git, we'll search the
515 * parent itself next. If we're !in_dot_git, we'll search .git
516 * in the parent directory next (added at the top of the loop). */
517 if (git_path_dirname_r(&path, path.ptr) < 0) {
518 error = -1;
519 break;
520 }
521
522 /* Once we've checked the directory (and .git if applicable),
523 * find the ceiling for a search. */
524 if (min_iterations && (--min_iterations == 0))
525 ceiling_offset = find_ceiling_dir_offset(path.ptr, ceiling_dirs);
526
527 /* Check if we should stop searching here. */
528 if (min_iterations == 0
529 && (path.ptr[ceiling_offset] == 0
530 || (flags & GIT_REPOSITORY_OPEN_NO_SEARCH)))
531 break;
532 }
533
534 if (!error && workdir_path && !(flags & GIT_REPOSITORY_OPEN_BARE)) {
535 if (!git_buf_len(gitdir_path))
536 git_buf_clear(workdir_path);
537 else {
538 git_path_dirname_r(workdir_path, path.ptr);
539 git_path_to_dir(workdir_path);
540 }
541 if (git_buf_oom(workdir_path))
542 return -1;
543 }
544
545 /* If we didn't find the repository, and we don't have any other error
546 * to report, report that. */
547 if (!git_buf_len(gitdir_path) && !error) {
548 giterr_set(GITERR_REPOSITORY,
549 "could not find repository from '%s'", start_path);
550 error = GIT_ENOTFOUND;
551 }
552
553 git_buf_free(&path);
554 git_buf_free(&repo_link);
555 git_buf_free(&common_link);
556 return error;
557 }
558
559 int git_repository_open_bare(
560 git_repository **repo_ptr,
561 const char *bare_path)
562 {
563 int error;
564 git_buf path = GIT_BUF_INIT, common_path = GIT_BUF_INIT;
565 git_repository *repo = NULL;
566
567 if ((error = git_path_prettify_dir(&path, bare_path, NULL)) < 0)
568 return error;
569
570 if (!valid_repository_path(&path, &common_path)) {
571 git_buf_free(&path);
572 git_buf_free(&common_path);
573 giterr_set(GITERR_REPOSITORY, "path is not a repository: %s", bare_path);
574 return GIT_ENOTFOUND;
575 }
576
577 repo = repository_alloc();
578 GITERR_CHECK_ALLOC(repo);
579
580 repo->gitdir = git_buf_detach(&path);
581 GITERR_CHECK_ALLOC(repo->gitdir);
582 repo->commondir = git_buf_detach(&common_path);
583 GITERR_CHECK_ALLOC(repo->commondir);
584
585 /* of course we're bare! */
586 repo->is_bare = 1;
587 repo->is_worktree = 0;
588 repo->workdir = NULL;
589
590 *repo_ptr = repo;
591 return 0;
592 }
593
594 static int _git_repository_open_ext_from_env(
595 git_repository **out,
596 const char *start_path)
597 {
598 git_repository *repo = NULL;
599 git_index *index = NULL;
600 git_odb *odb = NULL;
601 git_buf dir_buf = GIT_BUF_INIT;
602 git_buf ceiling_dirs_buf = GIT_BUF_INIT;
603 git_buf across_fs_buf = GIT_BUF_INIT;
604 git_buf index_file_buf = GIT_BUF_INIT;
605 git_buf namespace_buf = GIT_BUF_INIT;
606 git_buf object_dir_buf = GIT_BUF_INIT;
607 git_buf alts_buf = GIT_BUF_INIT;
608 git_buf work_tree_buf = GIT_BUF_INIT;
609 git_buf common_dir_buf = GIT_BUF_INIT;
610 const char *ceiling_dirs = NULL;
611 unsigned flags = 0;
612 int error;
613
614 if (!start_path) {
615 error = git__getenv(&dir_buf, "GIT_DIR");
616 if (error == GIT_ENOTFOUND) {
617 giterr_clear();
618 start_path = ".";
619 } else if (error < 0)
620 goto error;
621 else {
622 start_path = git_buf_cstr(&dir_buf);
623 flags |= GIT_REPOSITORY_OPEN_NO_SEARCH;
624 flags |= GIT_REPOSITORY_OPEN_NO_DOTGIT;
625 }
626 }
627
628 error = git__getenv(&ceiling_dirs_buf, "GIT_CEILING_DIRECTORIES");
629 if (error == GIT_ENOTFOUND)
630 giterr_clear();
631 else if (error < 0)
632 goto error;
633 else
634 ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf);
635
636 error = git__getenv(&across_fs_buf, "GIT_DISCOVERY_ACROSS_FILESYSTEM");
637 if (error == GIT_ENOTFOUND)
638 giterr_clear();
639 else if (error < 0)
640 goto error;
641 else {
642 int across_fs = 0;
643 error = git_config_parse_bool(&across_fs, git_buf_cstr(&across_fs_buf));
644 if (error < 0)
645 goto error;
646 if (across_fs)
647 flags |= GIT_REPOSITORY_OPEN_CROSS_FS;
648 }
649
650 error = git__getenv(&index_file_buf, "GIT_INDEX_FILE");
651 if (error == GIT_ENOTFOUND)
652 giterr_clear();
653 else if (error < 0)
654 goto error;
655 else {
656 error = git_index_open(&index, git_buf_cstr(&index_file_buf));
657 if (error < 0)
658 goto error;
659 }
660
661 error = git__getenv(&namespace_buf, "GIT_NAMESPACE");
662 if (error == GIT_ENOTFOUND)
663 giterr_clear();
664 else if (error < 0)
665 goto error;
666
667 error = git__getenv(&object_dir_buf, "GIT_OBJECT_DIRECTORY");
668 if (error == GIT_ENOTFOUND)
669 giterr_clear();
670 else if (error < 0)
671 goto error;
672 else {
673 error = git_odb_open(&odb, git_buf_cstr(&object_dir_buf));
674 if (error < 0)
675 goto error;
676 }
677
678 error = git__getenv(&work_tree_buf, "GIT_WORK_TREE");
679 if (error == GIT_ENOTFOUND)
680 giterr_clear();
681 else if (error < 0)
682 goto error;
683 else {
684 giterr_set(GITERR_INVALID, "GIT_WORK_TREE unimplemented");
685 error = GIT_ERROR;
686 goto error;
687 }
688
689 error = git__getenv(&work_tree_buf, "GIT_COMMON_DIR");
690 if (error == GIT_ENOTFOUND)
691 giterr_clear();
692 else if (error < 0)
693 goto error;
694 else {
695 giterr_set(GITERR_INVALID, "GIT_COMMON_DIR unimplemented");
696 error = GIT_ERROR;
697 goto error;
698 }
699
700 error = git_repository_open_ext(&repo, start_path, flags, ceiling_dirs);
701 if (error < 0)
702 goto error;
703
704 if (odb)
705 git_repository_set_odb(repo, odb);
706
707 error = git__getenv(&alts_buf, "GIT_ALTERNATE_OBJECT_DIRECTORIES");
708 if (error == GIT_ENOTFOUND) {
709 giterr_clear();
710 error = 0;
711 } else if (error < 0)
712 goto error;
713 else {
714 const char *end;
715 char *alt, *sep;
716 if (!odb) {
717 error = git_repository_odb(&odb, repo);
718 if (error < 0)
719 goto error;
720 }
721
722 end = git_buf_cstr(&alts_buf) + git_buf_len(&alts_buf);
723 for (sep = alt = alts_buf.ptr; sep != end; alt = sep+1) {
724 for (sep = alt; *sep && *sep != GIT_PATH_LIST_SEPARATOR; sep++)
725 ;
726 if (*sep)
727 *sep = '\0';
728 error = git_odb_add_disk_alternate(odb, alt);
729 if (error < 0)
730 goto error;
731 }
732 }
733
734 if (git_buf_len(&namespace_buf)) {
735 error = git_repository_set_namespace(repo, git_buf_cstr(&namespace_buf));
736 if (error < 0)
737 goto error;
738 }
739
740 git_repository_set_index(repo, index);
741
742 if (out) {
743 *out = repo;
744 goto success;
745 }
746 error:
747 git_repository_free(repo);
748 success:
749 git_odb_free(odb);
750 git_index_free(index);
751 git_buf_free(&common_dir_buf);
752 git_buf_free(&work_tree_buf);
753 git_buf_free(&alts_buf);
754 git_buf_free(&object_dir_buf);
755 git_buf_free(&namespace_buf);
756 git_buf_free(&index_file_buf);
757 git_buf_free(&across_fs_buf);
758 git_buf_free(&ceiling_dirs_buf);
759 git_buf_free(&dir_buf);
760 return error;
761 }
762
763 static int repo_is_worktree(unsigned *out, const git_repository *repo)
764 {
765 git_buf gitdir_link = GIT_BUF_INIT;
766 int error;
767
768 /* Worktrees cannot have the same commondir and gitdir */
769 if (repo->commondir && repo->gitdir
770 && !strcmp(repo->commondir, repo->gitdir)) {
771 *out = 0;
772 return 0;
773 }
774
775 if ((error = git_buf_joinpath(&gitdir_link, repo->gitdir, "gitdir")) < 0)
776 return -1;
777
778 /* A 'gitdir' file inside a git directory is currently
779 * only used when the repository is a working tree. */
780 *out = !!git_path_exists(gitdir_link.ptr);
781
782 git_buf_free(&gitdir_link);
783 return error;
784 }
785
786 int git_repository_open_ext(
787 git_repository **repo_ptr,
788 const char *start_path,
789 unsigned int flags,
790 const char *ceiling_dirs)
791 {
792 int error;
793 unsigned is_worktree;
794 git_buf gitdir = GIT_BUF_INIT, workdir = GIT_BUF_INIT,
795 gitlink = GIT_BUF_INIT, commondir = GIT_BUF_INIT;
796 git_repository *repo;
797 git_config *config = NULL;
798
799 if (flags & GIT_REPOSITORY_OPEN_FROM_ENV)
800 return _git_repository_open_ext_from_env(repo_ptr, start_path);
801
802 if (repo_ptr)
803 *repo_ptr = NULL;
804
805 error = find_repo(
806 &gitdir, &workdir, &gitlink, &commondir, start_path, flags, ceiling_dirs);
807
808 if (error < 0 || !repo_ptr)
809 return error;
810
811 repo = repository_alloc();
812 GITERR_CHECK_ALLOC(repo);
813
814 repo->gitdir = git_buf_detach(&gitdir);
815 GITERR_CHECK_ALLOC(repo->gitdir);
816
817 if (gitlink.size) {
818 repo->gitlink = git_buf_detach(&gitlink);
819 GITERR_CHECK_ALLOC(repo->gitlink);
820 }
821 if (commondir.size) {
822 repo->commondir = git_buf_detach(&commondir);
823 GITERR_CHECK_ALLOC(repo->commondir);
824 }
825
826 if ((error = repo_is_worktree(&is_worktree, repo)) < 0)
827 goto cleanup;
828 repo->is_worktree = is_worktree;
829
830 /*
831 * We'd like to have the config, but git doesn't particularly
832 * care if it's not there, so we need to deal with that.
833 */
834
835 error = git_repository_config_snapshot(&config, repo);
836 if (error < 0 && error != GIT_ENOTFOUND)
837 goto cleanup;
838
839 if (config && (error = check_repositoryformatversion(config)) < 0)
840 goto cleanup;
841
842 if ((flags & GIT_REPOSITORY_OPEN_BARE) != 0)
843 repo->is_bare = 1;
844 else {
845
846 if (config &&
847 ((error = load_config_data(repo, config)) < 0 ||
848 (error = load_workdir(repo, config, &workdir)) < 0))
849 goto cleanup;
850 }
851
852 cleanup:
853 git_buf_free(&gitdir);
854 git_buf_free(&workdir);
855 git_config_free(config);
856
857 if (error < 0)
858 git_repository_free(repo);
859 else
860 *repo_ptr = repo;
861
862 return error;
863 }
864
865 int git_repository_open(git_repository **repo_out, const char *path)
866 {
867 return git_repository_open_ext(
868 repo_out, path, GIT_REPOSITORY_OPEN_NO_SEARCH, NULL);
869 }
870
871 int git_repository_open_from_worktree(git_repository **repo_out, git_worktree *wt)
872 {
873 git_buf path = GIT_BUF_INIT;
874 git_repository *repo = NULL;
875 int len, err;
876
877 assert(repo_out && wt);
878
879 *repo_out = NULL;
880 len = strlen(wt->gitlink_path);
881
882 if (len <= 4 || strcasecmp(wt->gitlink_path + len - 4, ".git")) {
883 err = -1;
884 goto out;
885 }
886
887 if ((err = git_buf_set(&path, wt->gitlink_path, len - 4)) < 0)
888 goto out;
889
890 if ((err = git_repository_open(&repo, path.ptr)) < 0)
891 goto out;
892
893 *repo_out = repo;
894
895 out:
896 git_buf_free(&path);
897
898 return err;
899 }
900
901 int git_repository_wrap_odb(git_repository **repo_out, git_odb *odb)
902 {
903 git_repository *repo;
904
905 repo = repository_alloc();
906 GITERR_CHECK_ALLOC(repo);
907
908 git_repository_set_odb(repo, odb);
909 *repo_out = repo;
910
911 return 0;
912 }
913
914 int git_repository_discover(
915 git_buf *out,
916 const char *start_path,
917 int across_fs,
918 const char *ceiling_dirs)
919 {
920 uint32_t flags = across_fs ? GIT_REPOSITORY_OPEN_CROSS_FS : 0;
921
922 assert(start_path);
923
924 git_buf_sanitize(out);
925
926 return find_repo(out, NULL, NULL, NULL, start_path, flags, ceiling_dirs);
927 }
928
929 static int load_config(
930 git_config **out,
931 git_repository *repo,
932 const char *global_config_path,
933 const char *xdg_config_path,
934 const char *system_config_path,
935 const char *programdata_path)
936 {
937 int error;
938 git_buf config_path = GIT_BUF_INIT;
939 git_config *cfg = NULL;
940
941 assert(repo && out);
942
943 if ((error = git_config_new(&cfg)) < 0)
944 return error;
945
946 error = git_repository_item_path(&config_path, repo, GIT_REPOSITORY_ITEM_CONFIG);
947 if (error < 0)
948 goto on_error;
949
950 if ((error = git_config_add_file_ondisk(
951 cfg, config_path.ptr, GIT_CONFIG_LEVEL_LOCAL, 0)) < 0 &&
952 error != GIT_ENOTFOUND)
953 goto on_error;
954
955 git_buf_free(&config_path);
956
957 if (global_config_path != NULL &&
958 (error = git_config_add_file_ondisk(
959 cfg, global_config_path, GIT_CONFIG_LEVEL_GLOBAL, 0)) < 0 &&
960 error != GIT_ENOTFOUND)
961 goto on_error;
962
963 if (xdg_config_path != NULL &&
964 (error = git_config_add_file_ondisk(
965 cfg, xdg_config_path, GIT_CONFIG_LEVEL_XDG, 0)) < 0 &&
966 error != GIT_ENOTFOUND)
967 goto on_error;
968
969 if (system_config_path != NULL &&
970 (error = git_config_add_file_ondisk(
971 cfg, system_config_path, GIT_CONFIG_LEVEL_SYSTEM, 0)) < 0 &&
972 error != GIT_ENOTFOUND)
973 goto on_error;
974
975 if (programdata_path != NULL &&
976 (error = git_config_add_file_ondisk(
977 cfg, programdata_path, GIT_CONFIG_LEVEL_PROGRAMDATA, 0)) < 0 &&
978 error != GIT_ENOTFOUND)
979 goto on_error;
980
981 giterr_clear(); /* clear any lingering ENOTFOUND errors */
982
983 *out = cfg;
984 return 0;
985
986 on_error:
987 git_buf_free(&config_path);
988 git_config_free(cfg);
989 *out = NULL;
990 return error;
991 }
992
993 static const char *path_unless_empty(git_buf *buf)
994 {
995 return git_buf_len(buf) > 0 ? git_buf_cstr(buf) : NULL;
996 }
997
998 int git_repository_config__weakptr(git_config **out, git_repository *repo)
999 {
1000 int error = 0;
1001
1002 if (repo->_config == NULL) {
1003 git_buf global_buf = GIT_BUF_INIT;
1004 git_buf xdg_buf = GIT_BUF_INIT;
1005 git_buf system_buf = GIT_BUF_INIT;
1006 git_buf programdata_buf = GIT_BUF_INIT;
1007 git_config *config;
1008
1009 git_config_find_global(&global_buf);
1010 git_config_find_xdg(&xdg_buf);
1011 git_config_find_system(&system_buf);
1012 git_config_find_programdata(&programdata_buf);
1013
1014 /* If there is no global file, open a backend for it anyway */
1015 if (git_buf_len(&global_buf) == 0)
1016 git_config__global_location(&global_buf);
1017
1018 error = load_config(
1019 &config, repo,
1020 path_unless_empty(&global_buf),
1021 path_unless_empty(&xdg_buf),
1022 path_unless_empty(&system_buf),
1023 path_unless_empty(&programdata_buf));
1024 if (!error) {
1025 GIT_REFCOUNT_OWN(config, repo);
1026
1027 config = git__compare_and_swap(&repo->_config, NULL, config);
1028 if (config != NULL) {
1029 GIT_REFCOUNT_OWN(config, NULL);
1030 git_config_free(config);
1031 }
1032 }
1033
1034 git_buf_free(&global_buf);
1035 git_buf_free(&xdg_buf);
1036 git_buf_free(&system_buf);
1037 git_buf_free(&programdata_buf);
1038 }
1039
1040 *out = repo->_config;
1041 return error;
1042 }
1043
1044 int git_repository_config(git_config **out, git_repository *repo)
1045 {
1046 if (git_repository_config__weakptr(out, repo) < 0)
1047 return -1;
1048
1049 GIT_REFCOUNT_INC(*out);
1050 return 0;
1051 }
1052
1053 int git_repository_config_snapshot(git_config **out, git_repository *repo)
1054 {
1055 int error;
1056 git_config *weak;
1057
1058 if ((error = git_repository_config__weakptr(&weak, repo)) < 0)
1059 return error;
1060
1061 return git_config_snapshot(out, weak);
1062 }
1063
1064 void git_repository_set_config(git_repository *repo, git_config *config)
1065 {
1066 assert(repo && config);
1067 set_config(repo, config);
1068 }
1069
1070 int git_repository_odb__weakptr(git_odb **out, git_repository *repo)
1071 {
1072 int error = 0;
1073
1074 assert(repo && out);
1075
1076 if (repo->_odb == NULL) {
1077 git_buf odb_path = GIT_BUF_INIT;
1078 git_odb *odb;
1079
1080 if ((error = git_repository_item_path(&odb_path, repo,
1081 GIT_REPOSITORY_ITEM_OBJECTS)) < 0 ||
1082 (error = git_odb_new(&odb)) < 0)
1083 return error;
1084
1085 GIT_REFCOUNT_OWN(odb, repo);
1086
1087 if ((error = git_odb__set_caps(odb, GIT_ODB_CAP_FROM_OWNER)) < 0 ||
1088 (error = git_odb__add_default_backends(odb, odb_path.ptr, 0, 0)) < 0) {
1089 git_odb_free(odb);
1090 return error;
1091 }
1092
1093 odb = git__compare_and_swap(&repo->_odb, NULL, odb);
1094 if (odb != NULL) {
1095 GIT_REFCOUNT_OWN(odb, NULL);
1096 git_odb_free(odb);
1097 }
1098
1099 git_buf_free(&odb_path);
1100 }
1101
1102 *out = repo->_odb;
1103 return error;
1104 }
1105
1106 int git_repository_odb(git_odb **out, git_repository *repo)
1107 {
1108 if (git_repository_odb__weakptr(out, repo) < 0)
1109 return -1;
1110
1111 GIT_REFCOUNT_INC(*out);
1112 return 0;
1113 }
1114
1115 void git_repository_set_odb(git_repository *repo, git_odb *odb)
1116 {
1117 assert(repo && odb);
1118 set_odb(repo, odb);
1119 }
1120
1121 int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo)
1122 {
1123 int error = 0;
1124
1125 assert(out && repo);
1126
1127 if (repo->_refdb == NULL) {
1128 git_refdb *refdb;
1129
1130 error = git_refdb_open(&refdb, repo);
1131 if (!error) {
1132 GIT_REFCOUNT_OWN(refdb, repo);
1133
1134 refdb = git__compare_and_swap(&repo->_refdb, NULL, refdb);
1135 if (refdb != NULL) {
1136 GIT_REFCOUNT_OWN(refdb, NULL);
1137 git_refdb_free(refdb);
1138 }
1139 }
1140 }
1141
1142 *out = repo->_refdb;
1143 return error;
1144 }
1145
1146 int git_repository_refdb(git_refdb **out, git_repository *repo)
1147 {
1148 if (git_repository_refdb__weakptr(out, repo) < 0)
1149 return -1;
1150
1151 GIT_REFCOUNT_INC(*out);
1152 return 0;
1153 }
1154
1155 void git_repository_set_refdb(git_repository *repo, git_refdb *refdb)
1156 {
1157 assert(repo && refdb);
1158 set_refdb(repo, refdb);
1159 }
1160
1161 int git_repository_index__weakptr(git_index **out, git_repository *repo)
1162 {
1163 int error = 0;
1164
1165 assert(out && repo);
1166
1167 if (repo->_index == NULL) {
1168 git_buf index_path = GIT_BUF_INIT;
1169 git_index *index;
1170
1171 if ((error = git_buf_joinpath(&index_path, repo->gitdir, GIT_INDEX_FILE)) < 0)
1172 return error;
1173
1174 error = git_index_open(&index, index_path.ptr);
1175 if (!error) {
1176 GIT_REFCOUNT_OWN(index, repo);
1177
1178 index = git__compare_and_swap(&repo->_index, NULL, index);
1179 if (index != NULL) {
1180 GIT_REFCOUNT_OWN(index, NULL);
1181 git_index_free(index);
1182 }
1183
1184 error = git_index_set_caps(repo->_index, GIT_INDEXCAP_FROM_OWNER);
1185 }
1186
1187 git_buf_free(&index_path);
1188 }
1189
1190 *out = repo->_index;
1191 return error;
1192 }
1193
1194 int git_repository_index(git_index **out, git_repository *repo)
1195 {
1196 if (git_repository_index__weakptr(out, repo) < 0)
1197 return -1;
1198
1199 GIT_REFCOUNT_INC(*out);
1200 return 0;
1201 }
1202
1203 void git_repository_set_index(git_repository *repo, git_index *index)
1204 {
1205 assert(repo);
1206 set_index(repo, index);
1207 }
1208
1209 int git_repository_set_namespace(git_repository *repo, const char *namespace)
1210 {
1211 git__free(repo->namespace);
1212
1213 if (namespace == NULL) {
1214 repo->namespace = NULL;
1215 return 0;
1216 }
1217
1218 return (repo->namespace = git__strdup(namespace)) ? 0 : -1;
1219 }
1220
1221 const char *git_repository_get_namespace(git_repository *repo)
1222 {
1223 return repo->namespace;
1224 }
1225
1226 #ifdef GIT_WIN32
1227 static int reserved_names_add8dot3(git_repository *repo, const char *path)
1228 {
1229 char *name = git_win32_path_8dot3_name(path);
1230 const char *def = GIT_DIR_SHORTNAME;
1231 const char *def_dot_git = DOT_GIT;
1232 size_t name_len, def_len = CONST_STRLEN(GIT_DIR_SHORTNAME);
1233 size_t def_dot_git_len = CONST_STRLEN(DOT_GIT);
1234 git_buf *buf;
1235
1236 if (!name)
1237 return 0;
1238
1239 name_len = strlen(name);
1240
1241 if ((name_len == def_len && memcmp(name, def, def_len) == 0) ||
1242 (name_len == def_dot_git_len && memcmp(name, def_dot_git, def_dot_git_len) == 0)) {
1243 git__free(name);
1244 return 0;
1245 }
1246
1247 if ((buf = git_array_alloc(repo->reserved_names)) == NULL)
1248 return -1;
1249
1250 git_buf_attach(buf, name, name_len);
1251 return true;
1252 }
1253
1254 bool git_repository__reserved_names(
1255 git_buf **out, size_t *outlen, git_repository *repo, bool include_ntfs)
1256 {
1257 GIT_UNUSED(include_ntfs);
1258
1259 if (repo->reserved_names.size == 0) {
1260 git_buf *buf;
1261 size_t i;
1262
1263 /* Add the static defaults */
1264 for (i = 0; i < git_repository__reserved_names_win32_len; i++) {
1265 if ((buf = git_array_alloc(repo->reserved_names)) == NULL)
1266 goto on_error;
1267
1268 buf->ptr = git_repository__reserved_names_win32[i].ptr;
1269 buf->size = git_repository__reserved_names_win32[i].size;
1270 }
1271
1272 /* Try to add any repo-specific reserved names - the gitlink file
1273 * within a submodule or the repository (if the repository directory
1274 * is beneath the workdir). These are typically `.git`, but should
1275 * be protected in case they are not. Note, repo and workdir paths
1276 * are always prettified to end in `/`, so a prefixcmp is safe.
1277 */
1278 if (!repo->is_bare) {
1279 int (*prefixcmp)(const char *, const char *);
1280 int error, ignorecase;
1281
1282 error = git_repository__cvar(
1283 &ignorecase, repo, GIT_CVAR_IGNORECASE);
1284 prefixcmp = (error || ignorecase) ? git__prefixcmp_icase :
1285 git__prefixcmp;
1286
1287 if (repo->gitlink &&
1288 reserved_names_add8dot3(repo, repo->gitlink) < 0)
1289 goto on_error;
1290
1291 if (repo->gitdir &&
1292 prefixcmp(repo->gitdir, repo->workdir) == 0 &&
1293 reserved_names_add8dot3(repo, repo->gitdir) < 0)
1294 goto on_error;
1295 }
1296 }
1297
1298 *out = repo->reserved_names.ptr;
1299 *outlen = repo->reserved_names.size;
1300
1301 return true;
1302
1303 /* Always give good defaults, even on OOM */
1304 on_error:
1305 *out = git_repository__reserved_names_win32;
1306 *outlen = git_repository__reserved_names_win32_len;
1307
1308 return false;
1309 }
1310 #else
1311 bool git_repository__reserved_names(
1312 git_buf **out, size_t *outlen, git_repository *repo, bool include_ntfs)
1313 {
1314 GIT_UNUSED(repo);
1315
1316 if (include_ntfs) {
1317 *out = git_repository__reserved_names_win32;
1318 *outlen = git_repository__reserved_names_win32_len;
1319 } else {
1320 *out = git_repository__reserved_names_posix;
1321 *outlen = git_repository__reserved_names_posix_len;
1322 }
1323
1324 return true;
1325 }
1326 #endif
1327
1328 static int check_repositoryformatversion(git_config *config)
1329 {
1330 int version, error;
1331
1332 error = git_config_get_int32(&version, config, "core.repositoryformatversion");
1333 /* git ignores this if the config variable isn't there */
1334 if (error == GIT_ENOTFOUND)
1335 return 0;
1336
1337 if (error < 0)
1338 return -1;
1339
1340 if (GIT_REPO_VERSION < version) {
1341 giterr_set(GITERR_REPOSITORY,
1342 "unsupported repository version %d. Only versions up to %d are supported.",
1343 version, GIT_REPO_VERSION);
1344 return -1;
1345 }
1346
1347 return 0;
1348 }
1349
1350 int git_repository_create_head(const char *git_dir, const char *ref_name)
1351 {
1352 git_buf ref_path = GIT_BUF_INIT;
1353 git_filebuf ref = GIT_FILEBUF_INIT;
1354 const char *fmt;
1355
1356 if (git_buf_joinpath(&ref_path, git_dir, GIT_HEAD_FILE) < 0 ||
1357 git_filebuf_open(&ref, ref_path.ptr, 0, GIT_REFS_FILE_MODE) < 0)
1358 goto fail;
1359
1360 if (!ref_name)
1361 ref_name = GIT_BRANCH_MASTER;
1362
1363 if (git__prefixcmp(ref_name, GIT_REFS_DIR) == 0)
1364 fmt = "ref: %s\n";
1365 else
1366 fmt = "ref: " GIT_REFS_HEADS_DIR "%s\n";
1367
1368 if (git_filebuf_printf(&ref, fmt, ref_name) < 0 ||
1369 git_filebuf_commit(&ref) < 0)
1370 goto fail;
1371
1372 git_buf_free(&ref_path);
1373 return 0;
1374
1375 fail:
1376 git_buf_free(&ref_path);
1377 git_filebuf_cleanup(&ref);
1378 return -1;
1379 }
1380
1381 static bool is_chmod_supported(const char *file_path)
1382 {
1383 struct stat st1, st2;
1384
1385 if (p_stat(file_path, &st1) < 0)
1386 return false;
1387
1388 if (p_chmod(file_path, st1.st_mode ^ S_IXUSR) < 0)
1389 return false;
1390
1391 if (p_stat(file_path, &st2) < 0)
1392 return false;
1393
1394 return (st1.st_mode != st2.st_mode);
1395 }
1396
1397 static bool is_filesystem_case_insensitive(const char *gitdir_path)
1398 {
1399 git_buf path = GIT_BUF_INIT;
1400 int is_insensitive = -1;
1401
1402 if (!git_buf_joinpath(&path, gitdir_path, "CoNfIg"))
1403 is_insensitive = git_path_exists(git_buf_cstr(&path));
1404
1405 git_buf_free(&path);
1406 return is_insensitive;
1407 }
1408
1409 static bool are_symlinks_supported(const char *wd_path)
1410 {
1411 git_buf path = GIT_BUF_INIT;
1412 int fd;
1413 struct stat st;
1414 int symlinks_supported = -1;
1415
1416 if ((fd = git_futils_mktmp(&path, wd_path, 0666)) < 0 ||
1417 p_close(fd) < 0 ||
1418 p_unlink(path.ptr) < 0 ||
1419 p_symlink("testing", path.ptr) < 0 ||
1420 p_lstat(path.ptr, &st) < 0)
1421 symlinks_supported = false;
1422 else
1423 symlinks_supported = (S_ISLNK(st.st_mode) != 0);
1424
1425 (void)p_unlink(path.ptr);
1426 git_buf_free(&path);
1427
1428 return symlinks_supported;
1429 }
1430
1431 static int create_empty_file(const char *path, mode_t mode)
1432 {
1433 int fd;
1434
1435 if ((fd = p_creat(path, mode)) < 0) {
1436 giterr_set(GITERR_OS, "error while creating '%s'", path);
1437 return -1;
1438 }
1439
1440 if (p_close(fd) < 0) {
1441 giterr_set(GITERR_OS, "error while closing '%s'", path);
1442 return -1;
1443 }
1444
1445 return 0;
1446 }
1447
1448 static int repo_local_config(
1449 git_config **out,
1450 git_buf *config_dir,
1451 git_repository *repo,
1452 const char *repo_dir)
1453 {
1454 int error = 0;
1455 git_config *parent;
1456 const char *cfg_path;
1457
1458 if (git_buf_joinpath(config_dir, repo_dir, GIT_CONFIG_FILENAME_INREPO) < 0)
1459 return -1;
1460 cfg_path = git_buf_cstr(config_dir);
1461
1462 /* make LOCAL config if missing */
1463 if (!git_path_isfile(cfg_path) &&
1464 (error = create_empty_file(cfg_path, GIT_CONFIG_FILE_MODE)) < 0)
1465 return error;
1466
1467 /* if no repo, just open that file directly */
1468 if (!repo)
1469 return git_config_open_ondisk(out, cfg_path);
1470
1471 /* otherwise, open parent config and get that level */
1472 if ((error = git_repository_config__weakptr(&parent, repo)) < 0)
1473 return error;
1474
1475 if (git_config_open_level(out, parent, GIT_CONFIG_LEVEL_LOCAL) < 0) {
1476 giterr_clear();
1477
1478 if (!(error = git_config_add_file_ondisk(
1479 parent, cfg_path, GIT_CONFIG_LEVEL_LOCAL, false)))
1480 error = git_config_open_level(out, parent, GIT_CONFIG_LEVEL_LOCAL);
1481 }
1482
1483 git_config_free(parent);
1484
1485 return error;
1486 }
1487
1488 static int repo_init_fs_configs(
1489 git_config *cfg,
1490 const char *cfg_path,
1491 const char *repo_dir,
1492 const char *work_dir,
1493 bool update_ignorecase)
1494 {
1495 int error = 0;
1496
1497 if (!work_dir)
1498 work_dir = repo_dir;
1499
1500 if ((error = git_config_set_bool(
1501 cfg, "core.filemode", is_chmod_supported(cfg_path))) < 0)
1502 return error;
1503
1504 if (!are_symlinks_supported(work_dir)) {
1505 if ((error = git_config_set_bool(cfg, "core.symlinks", false)) < 0)
1506 return error;
1507 } else if (git_config_delete_entry(cfg, "core.symlinks") < 0)
1508 giterr_clear();
1509
1510 if (update_ignorecase) {
1511 if (is_filesystem_case_insensitive(repo_dir)) {
1512 if ((error = git_config_set_bool(cfg, "core.ignorecase", true)) < 0)
1513 return error;
1514 } else if (git_config_delete_entry(cfg, "core.ignorecase") < 0)
1515 giterr_clear();
1516 }
1517
1518 #ifdef GIT_USE_ICONV
1519 if ((error = git_config_set_bool(
1520 cfg, "core.precomposeunicode",
1521 git_path_does_fs_decompose_unicode(work_dir))) < 0)
1522 return error;
1523 /* on non-iconv platforms, don't even set core.precomposeunicode */
1524 #endif
1525
1526 return 0;
1527 }
1528
1529 static int repo_init_config(
1530 const char *repo_dir,
1531 const char *work_dir,
1532 uint32_t flags,
1533 uint32_t mode)
1534 {
1535 int error = 0;
1536 git_buf cfg_path = GIT_BUF_INIT, worktree_path = GIT_BUF_INIT;
1537 git_config *config = NULL;
1538 bool is_bare = ((flags & GIT_REPOSITORY_INIT_BARE) != 0);
1539 bool is_reinit = ((flags & GIT_REPOSITORY_INIT__IS_REINIT) != 0);
1540
1541 if ((error = repo_local_config(&config, &cfg_path, NULL, repo_dir)) < 0)
1542 goto cleanup;
1543
1544 if (is_reinit && (error = check_repositoryformatversion(config)) < 0)
1545 goto cleanup;
1546
1547 #define SET_REPO_CONFIG(TYPE, NAME, VAL) do { \
1548 if ((error = git_config_set_##TYPE(config, NAME, VAL)) < 0) \
1549 goto cleanup; } while (0)
1550
1551 SET_REPO_CONFIG(bool, "core.bare", is_bare);
1552 SET_REPO_CONFIG(int32, "core.repositoryformatversion", GIT_REPO_VERSION);
1553
1554 if ((error = repo_init_fs_configs(
1555 config, cfg_path.ptr, repo_dir, work_dir, !is_reinit)) < 0)
1556 goto cleanup;
1557
1558 if (!is_bare) {
1559 SET_REPO_CONFIG(bool, "core.logallrefupdates", true);
1560
1561 if (!(flags & GIT_REPOSITORY_INIT__NATURAL_WD)) {
1562 if ((error = git_buf_sets(&worktree_path, work_dir)) < 0)
1563 goto cleanup;
1564
1565 if ((flags & GIT_REPOSITORY_INIT_RELATIVE_GITLINK))
1566 if ((error = git_path_make_relative(&worktree_path, repo_dir)) < 0)
1567 goto cleanup;
1568
1569 SET_REPO_CONFIG(string, "core.worktree", worktree_path.ptr);
1570 } else if (is_reinit) {
1571 if (git_config_delete_entry(config, "core.worktree") < 0)
1572 giterr_clear();
1573 }
1574 }
1575
1576 if (mode == GIT_REPOSITORY_INIT_SHARED_GROUP) {
1577 SET_REPO_CONFIG(int32, "core.sharedrepository", 1);
1578 SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
1579 }
1580 else if (mode == GIT_REPOSITORY_INIT_SHARED_ALL) {
1581 SET_REPO_CONFIG(int32, "core.sharedrepository", 2);
1582 SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
1583 }
1584
1585 cleanup:
1586 git_buf_free(&cfg_path);
1587 git_buf_free(&worktree_path);
1588 git_config_free(config);
1589
1590 return error;
1591 }
1592
1593 static int repo_reinit_submodule_fs(git_submodule *sm, const char *n, void *p)
1594 {
1595 git_repository *smrepo = NULL;
1596 GIT_UNUSED(n); GIT_UNUSED(p);
1597
1598 if (git_submodule_open(&smrepo, sm) < 0 ||
1599 git_repository_reinit_filesystem(smrepo, true) < 0)
1600 giterr_clear();
1601 git_repository_free(smrepo);
1602
1603 return 0;
1604 }
1605
1606 int git_repository_reinit_filesystem(git_repository *repo, int recurse)
1607 {
1608 int error = 0;
1609 git_buf path = GIT_BUF_INIT;
1610 git_config *config = NULL;
1611 const char *repo_dir = git_repository_path(repo);
1612
1613 if (!(error = repo_local_config(&config, &path, repo, repo_dir)))
1614 error = repo_init_fs_configs(
1615 config, path.ptr, repo_dir, git_repository_workdir(repo), true);
1616
1617 git_config_free(config);
1618 git_buf_free(&path);
1619
1620 git_repository__cvar_cache_clear(repo);
1621
1622 if (!repo->is_bare && recurse)
1623 (void)git_submodule_foreach(repo, repo_reinit_submodule_fs, NULL);
1624
1625 return error;
1626 }
1627
1628 static int repo_write_template(
1629 const char *git_dir,
1630 bool allow_overwrite,
1631 const char *file,
1632 mode_t mode,
1633 bool hidden,
1634 const char *content)
1635 {
1636 git_buf path = GIT_BUF_INIT;
1637 int fd, error = 0, flags;
1638
1639 if (git_buf_joinpath(&path, git_dir, file) < 0)
1640 return -1;
1641
1642 if (allow_overwrite)
1643 flags = O_WRONLY | O_CREAT | O_TRUNC;
1644 else
1645 flags = O_WRONLY | O_CREAT | O_EXCL;
1646
1647 fd = p_open(git_buf_cstr(&path), flags, mode);
1648
1649 if (fd >= 0) {
1650 error = p_write(fd, content, strlen(content));
1651
1652 p_close(fd);
1653 }
1654 else if (errno != EEXIST)
1655 error = fd;
1656
1657 #ifdef GIT_WIN32
1658 if (!error && hidden) {
1659 if (git_win32__set_hidden(path.ptr, true) < 0)
1660 error = -1;
1661 }
1662 #else
1663 GIT_UNUSED(hidden);
1664 #endif
1665
1666 git_buf_free(&path);
1667
1668 if (error)
1669 giterr_set(GITERR_OS,
1670 "failed to initialize repository with template '%s'", file);
1671
1672 return error;
1673 }
1674
1675 static int repo_write_gitlink(
1676 const char *in_dir, const char *to_repo, bool use_relative_path)
1677 {
1678 int error;
1679 git_buf buf = GIT_BUF_INIT;
1680 git_buf path_to_repo = GIT_BUF_INIT;
1681 struct stat st;
1682
1683 git_path_dirname_r(&buf, to_repo);
1684 git_path_to_dir(&buf);
1685 if (git_buf_oom(&buf))
1686 return -1;
1687
1688 /* don't write gitlink to natural workdir */
1689 if (git__suffixcmp(to_repo, "/" DOT_GIT "/") == 0 &&
1690 strcmp(in_dir, buf.ptr) == 0)
1691 {
1692 error = GIT_PASSTHROUGH;
1693 goto cleanup;
1694 }
1695
1696 if ((error = git_buf_joinpath(&buf, in_dir, DOT_GIT)) < 0)
1697 goto cleanup;
1698
1699 if (!p_stat(buf.ptr, &st) && !S_ISREG(st.st_mode)) {
1700 giterr_set(GITERR_REPOSITORY,
1701 "cannot overwrite gitlink file into path '%s'", in_dir);
1702 error = GIT_EEXISTS;
1703 goto cleanup;
1704 }
1705
1706 git_buf_clear(&buf);
1707
1708 error = git_buf_sets(&path_to_repo, to_repo);
1709
1710 if (!error && use_relative_path)
1711 error = git_path_make_relative(&path_to_repo, in_dir);
1712
1713 if (!error)
1714 error = git_buf_join(&buf, ' ', GIT_FILE_CONTENT_PREFIX, path_to_repo.ptr);
1715
1716 if (!error)
1717 error = repo_write_template(in_dir, true, DOT_GIT, 0666, true, buf.ptr);
1718
1719 cleanup:
1720 git_buf_free(&buf);
1721 git_buf_free(&path_to_repo);
1722 return error;
1723 }
1724
1725 static mode_t pick_dir_mode(git_repository_init_options *opts)
1726 {
1727 if (opts->mode == GIT_REPOSITORY_INIT_SHARED_UMASK)
1728 return 0777;
1729 if (opts->mode == GIT_REPOSITORY_INIT_SHARED_GROUP)
1730 return (0775 | S_ISGID);
1731 if (opts->mode == GIT_REPOSITORY_INIT_SHARED_ALL)
1732 return (0777 | S_ISGID);
1733 return opts->mode;
1734 }
1735
1736 #include "repo_template.h"
1737
1738 static int repo_init_structure(
1739 const char *repo_dir,
1740 const char *work_dir,
1741 git_repository_init_options *opts)
1742 {
1743 int error = 0;
1744 repo_template_item *tpl;
1745 bool external_tpl =
1746 ((opts->flags & GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE) != 0);
1747 mode_t dmode = pick_dir_mode(opts);
1748 bool chmod = opts->mode != GIT_REPOSITORY_INIT_SHARED_UMASK;
1749
1750 /* Hide the ".git" directory */
1751 #ifdef GIT_WIN32
1752 if ((opts->flags & GIT_REPOSITORY_INIT__HAS_DOTGIT) != 0) {
1753 if (git_win32__set_hidden(repo_dir, true) < 0) {
1754 giterr_set(GITERR_OS,
1755 "failed to mark Git repository folder as hidden");
1756 return -1;
1757 }
1758 }
1759 #endif
1760
1761 /* Create the .git gitlink if appropriate */
1762 if ((opts->flags & GIT_REPOSITORY_INIT_BARE) == 0 &&
1763 (opts->flags & GIT_REPOSITORY_INIT__NATURAL_WD) == 0)
1764 {
1765 if (repo_write_gitlink(work_dir, repo_dir, opts->flags & GIT_REPOSITORY_INIT_RELATIVE_GITLINK) < 0)
1766 return -1;
1767 }
1768
1769 /* Copy external template if requested */
1770 if (external_tpl) {
1771 git_config *cfg = NULL;
1772 const char *tdir = NULL;
1773 bool default_template = false;
1774 git_buf template_buf = GIT_BUF_INIT;
1775
1776 if (opts->template_path)
1777 tdir = opts->template_path;
1778 else if ((error = git_config_open_default(&cfg)) >= 0) {
1779 if (!git_config_get_path(&template_buf, cfg, "init.templatedir"))
1780 tdir = template_buf.ptr;
1781 giterr_clear();
1782 }
1783
1784 if (!tdir) {
1785 if (!(error = git_sysdir_find_template_dir(&template_buf)))
1786 tdir = template_buf.ptr;
1787 default_template = true;
1788 }
1789
1790 if (tdir) {
1791 uint32_t cpflags = GIT_CPDIR_COPY_SYMLINKS |
1792 GIT_CPDIR_SIMPLE_TO_MODE |
1793 GIT_CPDIR_COPY_DOTFILES;
1794 if (opts->mode != GIT_REPOSITORY_INIT_SHARED_UMASK)
1795 cpflags |= GIT_CPDIR_CHMOD_DIRS;
1796 error = git_futils_cp_r(tdir, repo_dir, cpflags, dmode);
1797 }
1798
1799 git_buf_free(&template_buf);
1800 git_config_free(cfg);
1801
1802 if (error < 0) {
1803 if (!default_template)
1804 return error;
1805
1806 /* if template was default, ignore error and use internal */
1807 giterr_clear();
1808 external_tpl = false;
1809 error = 0;
1810 }
1811 }
1812
1813 /* Copy internal template
1814 * - always ensure existence of dirs
1815 * - only create files if no external template was specified
1816 */
1817 for (tpl = repo_template; !error && tpl->path; ++tpl) {
1818 if (!tpl->content) {
1819 uint32_t mkdir_flags = GIT_MKDIR_PATH;
1820 if (chmod)
1821 mkdir_flags |= GIT_MKDIR_CHMOD;
1822
1823 error = git_futils_mkdir_relative(
1824 tpl->path, repo_dir, dmode, mkdir_flags, NULL);
1825 }
1826 else if (!external_tpl) {
1827 const char *content = tpl->content;
1828
1829 if (opts->description && strcmp(tpl->path, GIT_DESC_FILE) == 0)
1830 content = opts->description;
1831
1832 error = repo_write_template(
1833 repo_dir, false, tpl->path, tpl->mode, false, content);
1834 }
1835 }
1836
1837 return error;
1838 }
1839
1840 static int mkdir_parent(git_buf *buf, uint32_t mode, bool skip2)
1841 {
1842 /* When making parent directories during repository initialization
1843 * don't try to set gid or grant world write access
1844 */
1845 return git_futils_mkdir(
1846 buf->ptr, mode & ~(S_ISGID | 0002),
1847 GIT_MKDIR_PATH | GIT_MKDIR_VERIFY_DIR |
1848 (skip2 ? GIT_MKDIR_SKIP_LAST2 : GIT_MKDIR_SKIP_LAST));
1849 }
1850
1851 static int repo_init_directories(
1852 git_buf *repo_path,
1853 git_buf *wd_path,
1854 const char *given_repo,
1855 git_repository_init_options *opts)
1856 {
1857 int error = 0;
1858 bool is_bare, add_dotgit, has_dotgit, natural_wd;
1859 mode_t dirmode;
1860
1861 /* There are three possible rules for what we are allowed to create:
1862 * - MKPATH means anything we need
1863 * - MKDIR means just the .git directory and its parent and the workdir
1864 * - Neither means only the .git directory can be created
1865 *
1866 * There are 5 "segments" of path that we might need to deal with:
1867 * 1. The .git directory
1868 * 2. The parent of the .git directory
1869 * 3. Everything above the parent of the .git directory
1870 * 4. The working directory (often the same as #2)
1871 * 5. Everything above the working directory (often the same as #3)
1872 *
1873 * For all directories created, we start with the init_mode value for
1874 * permissions and then strip off bits in some cases:
1875 *
1876 * For MKPATH, we create #3 (and #5) paths without S_ISGID or S_IWOTH
1877 * For MKPATH and MKDIR, we create #2 (and #4) without S_ISGID
1878 * For all rules, we create #1 using the untouched init_mode
1879 */
1880
1881 /* set up repo path */
1882
1883 is_bare = ((opts->flags & GIT_REPOSITORY_INIT_BARE) != 0);
1884
1885 add_dotgit =
1886 (opts->flags & GIT_REPOSITORY_INIT_NO_DOTGIT_DIR) == 0 &&
1887 !is_bare &&
1888 git__suffixcmp(given_repo, "/" DOT_GIT) != 0 &&
1889 git__suffixcmp(given_repo, "/" GIT_DIR) != 0;
1890
1891 if (git_buf_joinpath(repo_path, given_repo, add_dotgit ? GIT_DIR : "") < 0)
1892 return -1;
1893
1894 has_dotgit = (git__suffixcmp(repo_path->ptr, "/" GIT_DIR) == 0);
1895 if (has_dotgit)
1896 opts->flags |= GIT_REPOSITORY_INIT__HAS_DOTGIT;
1897
1898 /* set up workdir path */
1899
1900 if (!is_bare) {
1901 if (opts->workdir_path) {
1902 if (git_path_join_unrooted(
1903 wd_path, opts->workdir_path, repo_path->ptr, NULL) < 0)
1904 return -1;
1905 } else if (has_dotgit) {
1906 if (git_path_dirname_r(wd_path, repo_path->ptr) < 0)
1907 return -1;
1908 } else {
1909 giterr_set(GITERR_REPOSITORY, "cannot pick working directory"
1910 " for non-bare repository that isn't a '.git' directory");
1911 return -1;
1912 }
1913
1914 if (git_path_to_dir(wd_path) < 0)
1915 return -1;
1916 } else {
1917 git_buf_clear(wd_path);
1918 }
1919
1920 natural_wd =
1921 has_dotgit &&
1922 wd_path->size > 0 &&
1923 wd_path->size + strlen(GIT_DIR) == repo_path->size &&
1924 memcmp(repo_path->ptr, wd_path->ptr, wd_path->size) == 0;
1925 if (natural_wd)
1926 opts->flags |= GIT_REPOSITORY_INIT__NATURAL_WD;
1927
1928 /* create directories as needed / requested */
1929
1930 dirmode = pick_dir_mode(opts);
1931
1932 if ((opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0) {
1933 /* create path #5 */
1934 if (wd_path->size > 0 &&
1935 (error = mkdir_parent(wd_path, dirmode, false)) < 0)
1936 return error;
1937
1938 /* create path #3 (if not the same as #5) */
1939 if (!natural_wd &&
1940 (error = mkdir_parent(repo_path, dirmode, has_dotgit)) < 0)
1941 return error;
1942 }
1943
1944 if ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0 ||
1945 (opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0)
1946 {
1947 /* create path #4 */
1948 if (wd_path->size > 0 &&
1949 (error = git_futils_mkdir(
1950 wd_path->ptr, dirmode & ~S_ISGID,
1951 GIT_MKDIR_VERIFY_DIR)) < 0)
1952 return error;
1953
1954 /* create path #2 (if not the same as #4) */
1955 if (!natural_wd &&
1956 (error = git_futils_mkdir(
1957 repo_path->ptr, dirmode & ~S_ISGID,
1958 GIT_MKDIR_VERIFY_DIR | GIT_MKDIR_SKIP_LAST)) < 0)
1959 return error;
1960 }
1961
1962 if ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0 ||
1963 (opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0 ||
1964 has_dotgit)
1965 {
1966 /* create path #1 */
1967 error = git_futils_mkdir(repo_path->ptr, dirmode,
1968 GIT_MKDIR_VERIFY_DIR | ((dirmode & S_ISGID) ? GIT_MKDIR_CHMOD : 0));
1969 }
1970
1971 /* prettify both directories now that they are created */
1972
1973 if (!error) {
1974 error = git_path_prettify_dir(repo_path, repo_path->ptr, NULL);
1975
1976 if (!error && wd_path->size > 0)
1977 error = git_path_prettify_dir(wd_path, wd_path->ptr, NULL);
1978 }
1979
1980 return error;
1981 }
1982
1983 static int repo_init_create_origin(git_repository *repo, const char *url)
1984 {
1985 int error;
1986 git_remote *remote;
1987
1988 if (!(error = git_remote_create(&remote, repo, GIT_REMOTE_ORIGIN, url))) {
1989 git_remote_free(remote);
1990 }
1991
1992 return error;
1993 }
1994
1995 int git_repository_init(
1996 git_repository **repo_out, const char *path, unsigned is_bare)
1997 {
1998 git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
1999
2000 opts.flags = GIT_REPOSITORY_INIT_MKPATH; /* don't love this default */
2001 if (is_bare)
2002 opts.flags |= GIT_REPOSITORY_INIT_BARE;
2003
2004 return git_repository_init_ext(repo_out, path, &opts);
2005 }
2006
2007 int git_repository_init_ext(
2008 git_repository **out,
2009 const char *given_repo,
2010 git_repository_init_options *opts)
2011 {
2012 int error;
2013 git_buf repo_path = GIT_BUF_INIT, wd_path = GIT_BUF_INIT,
2014 common_path = GIT_BUF_INIT;
2015 const char *wd;
2016
2017 assert(out && given_repo && opts);
2018
2019 GITERR_CHECK_VERSION(opts, GIT_REPOSITORY_INIT_OPTIONS_VERSION, "git_repository_init_options");
2020
2021 error = repo_init_directories(&repo_path, &wd_path, given_repo, opts);
2022 if (error < 0)
2023 goto cleanup;
2024
2025 wd = (opts->flags & GIT_REPOSITORY_INIT_BARE) ? NULL : git_buf_cstr(&wd_path);
2026 if (valid_repository_path(&repo_path, &common_path)) {
2027
2028 if ((opts->flags & GIT_REPOSITORY_INIT_NO_REINIT) != 0) {
2029 giterr_set(GITERR_REPOSITORY,
2030 "attempt to reinitialize '%s'", given_repo);
2031 error = GIT_EEXISTS;
2032 goto cleanup;
2033 }
2034
2035 opts->flags |= GIT_REPOSITORY_INIT__IS_REINIT;
2036
2037 error = repo_init_config(
2038 repo_path.ptr, wd, opts->flags, opts->mode);
2039
2040 /* TODO: reinitialize the templates */
2041 }
2042 else {
2043 if (!(error = repo_init_structure(
2044 repo_path.ptr, wd, opts)) &&
2045 !(error = repo_init_config(
2046 repo_path.ptr, wd, opts->flags, opts->mode)))
2047 error = git_repository_create_head(
2048 repo_path.ptr, opts->initial_head);
2049 }
2050 if (error < 0)
2051 goto cleanup;
2052
2053 error = git_repository_open(out, repo_path.ptr);
2054
2055 if (!error && opts->origin_url)
2056 error = repo_init_create_origin(*out, opts->origin_url);
2057
2058 cleanup:
2059 git_buf_free(&common_path);
2060 git_buf_free(&repo_path);
2061 git_buf_free(&wd_path);
2062
2063 return error;
2064 }
2065
2066 int git_repository_head_detached(git_repository *repo)
2067 {
2068 git_reference *ref;
2069 git_odb *odb = NULL;
2070 int exists;
2071
2072 if (git_repository_odb__weakptr(&odb, repo) < 0)
2073 return -1;
2074
2075 if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0)
2076 return -1;
2077
2078 if (git_reference_type(ref) == GIT_REF_SYMBOLIC) {
2079 git_reference_free(ref);
2080 return 0;
2081 }
2082
2083 exists = git_odb_exists(odb, git_reference_target(ref));
2084
2085 git_reference_free(ref);
2086 return exists;
2087 }
2088
2089 static int get_worktree_file_path(git_buf *out, git_repository *repo, const char *worktree, const char *file)
2090 {
2091 git_buf_clear(out);
2092 return git_buf_printf(out, "%s/worktrees/%s/%s", repo->commondir, worktree, file);
2093 }
2094
2095 int git_repository_head_detached_for_worktree(git_repository *repo, const char *name)
2096 {
2097 git_reference *ref = NULL;
2098 int error;
2099
2100 assert(repo && name);
2101
2102 if ((error = git_repository_head_for_worktree(&ref, repo, name)) < 0)
2103 goto out;
2104
2105 error = (git_reference_type(ref) != GIT_REF_SYMBOLIC);
2106 out:
2107 git_reference_free(ref);
2108
2109 return error;
2110 }
2111
2112 int git_repository_head(git_reference **head_out, git_repository *repo)
2113 {
2114 git_reference *head;
2115 int error;
2116
2117 if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0)
2118 return error;
2119
2120 if (git_reference_type(head) == GIT_REF_OID) {
2121 *head_out = head;
2122 return 0;
2123 }
2124
2125 error = git_reference_lookup_resolved(head_out, repo, git_reference_symbolic_target(head), -1);
2126 git_reference_free(head);
2127
2128 return error == GIT_ENOTFOUND ? GIT_EUNBORNBRANCH : error;
2129 }
2130
2131 int git_repository_head_for_worktree(git_reference **out, git_repository *repo, const char *name)
2132 {
2133 git_buf path = GIT_BUF_INIT;
2134 git_reference *head = NULL;
2135 int error;
2136
2137 assert(out && repo && name);
2138
2139 *out = NULL;
2140
2141 if ((error = get_worktree_file_path(&path, repo, name, GIT_HEAD_FILE)) < 0 ||
2142 (error = git_reference__read_head(&head, repo, path.ptr)) < 0)
2143 goto out;
2144
2145 if (git_reference_type(head) != GIT_REF_OID) {
2146 git_reference *resolved;
2147
2148 error = git_reference_lookup_resolved(&resolved, repo, git_reference_symbolic_target(head), -1);
2149 git_reference_free(head);
2150 head = resolved;
2151 }
2152
2153 *out = head;
2154
2155 out:
2156 if (error)
2157 git_reference_free(head);
2158
2159 git_buf_free(&path);
2160
2161 return error;
2162 }
2163
2164 int git_repository_foreach_head(git_repository *repo, git_repository_foreach_head_cb cb, void *payload)
2165 {
2166 git_strarray worktrees = GIT_VECTOR_INIT;
2167 git_buf path = GIT_BUF_INIT;
2168 int error;
2169 size_t i;
2170
2171 /* Execute callback for HEAD of commondir */
2172 if ((error = git_buf_joinpath(&path, repo->commondir, GIT_HEAD_FILE)) < 0 ||
2173 (error = cb(repo, path.ptr, payload) != 0))
2174 goto out;
2175
2176 if ((error = git_worktree_list(&worktrees, repo)) < 0) {
2177 error = 0;
2178 goto out;
2179 }
2180
2181 /* Execute callback for all worktree HEADs */
2182 for (i = 0; i < worktrees.count; i++) {
2183 if (get_worktree_file_path(&path, repo, worktrees.strings[i], GIT_HEAD_FILE) < 0)
2184 continue;
2185
2186 if ((error = cb(repo, path.ptr, payload)) != 0)
2187 goto out;
2188 }
2189
2190 out:
2191 git_buf_free(&path);
2192 git_strarray_free(&worktrees);
2193 return error;
2194 }
2195
2196 int git_repository_head_unborn(git_repository *repo)
2197 {
2198 git_reference *ref = NULL;
2199 int error;
2200
2201 error = git_repository_head(&ref, repo);
2202 git_reference_free(ref);
2203
2204 if (error == GIT_EUNBORNBRANCH) {
2205 giterr_clear();
2206 return 1;
2207 }
2208
2209 if (error < 0)
2210 return -1;
2211
2212 return 0;
2213 }
2214
2215 static int at_least_one_cb(const char *refname, void *payload)
2216 {
2217 GIT_UNUSED(refname);
2218 GIT_UNUSED(payload);
2219 return GIT_PASSTHROUGH;
2220 }
2221
2222 static int repo_contains_no_reference(git_repository *repo)
2223 {
2224 int error = git_reference_foreach_name(repo, &at_least_one_cb, NULL);
2225
2226 if (error == GIT_PASSTHROUGH)
2227 return 0;
2228
2229 if (!error)
2230 return 1;
2231
2232 return error;
2233 }
2234
2235 int git_repository_is_empty(git_repository *repo)
2236 {
2237 git_reference *head = NULL;
2238 int is_empty = 0;
2239
2240 if (git_reference_lookup(&head, repo, GIT_HEAD_FILE) < 0)
2241 return -1;
2242
2243 if (git_reference_type(head) == GIT_REF_SYMBOLIC)
2244 is_empty =
2245 (strcmp(git_reference_symbolic_target(head),
2246 GIT_REFS_HEADS_DIR "master") == 0) &&
2247 repo_contains_no_reference(repo);
2248
2249 git_reference_free(head);
2250
2251 return is_empty;
2252 }
2253
2254 int git_repository_item_path(git_buf *out, git_repository *repo, git_repository_item_t item)
2255 {
2256 const char *parent;
2257
2258 switch (items[item].parent) {
2259 case GIT_REPOSITORY_ITEM_GITDIR:
2260 parent = git_repository_path(repo);
2261 break;
2262 case GIT_REPOSITORY_ITEM_WORKDIR:
2263 parent = git_repository_workdir(repo);
2264 break;
2265 case GIT_REPOSITORY_ITEM_COMMONDIR:
2266 parent = git_repository_commondir(repo);
2267 break;
2268 default:
2269 giterr_set(GITERR_INVALID, "Invalid item directory");
2270 return -1;
2271 }
2272
2273 if (parent == NULL) {
2274 giterr_set(GITERR_INVALID, "Path cannot exist in repository");
2275 return -1;
2276 }
2277
2278 if (git_buf_sets(out, parent) < 0)
2279 return -1;
2280
2281 if (items[item].name) {
2282 if (git_buf_joinpath(out, parent, items[item].name) < 0)
2283 return -1;
2284 }
2285
2286 if (items[item].directory) {
2287 if (git_path_to_dir(out) < 0)
2288 return -1;
2289 }
2290
2291 return 0;
2292 }
2293
2294 const char *git_repository_path(git_repository *repo)
2295 {
2296 assert(repo);
2297 return repo->gitdir;
2298 }
2299
2300 const char *git_repository_workdir(git_repository *repo)
2301 {
2302 assert(repo);
2303
2304 if (repo->is_bare)
2305 return NULL;
2306
2307 return repo->workdir;
2308 }
2309
2310 const char *git_repository_commondir(git_repository *repo)
2311 {
2312 assert(repo);
2313 return repo->commondir;
2314 }
2315
2316 int git_repository_set_workdir(
2317 git_repository *repo, const char *workdir, int update_gitlink)
2318 {
2319 int error = 0;
2320 git_buf path = GIT_BUF_INIT;
2321
2322 assert(repo && workdir);
2323
2324 if (git_path_prettify_dir(&path, workdir, NULL) < 0)
2325 return -1;
2326
2327 if (repo->workdir && strcmp(repo->workdir, path.ptr) == 0)
2328 return 0;
2329
2330 if (update_gitlink) {
2331 git_config *config;
2332
2333 if (git_repository_config__weakptr(&config, repo) < 0)
2334 return -1;
2335
2336 error = repo_write_gitlink(path.ptr, git_repository_path(repo), false);
2337
2338 /* passthrough error means gitlink is unnecessary */
2339 if (error == GIT_PASSTHROUGH)
2340 error = git_config_delete_entry(config, "core.worktree");
2341 else if (!error)
2342 error = git_config_set_string(config, "core.worktree", path.ptr);
2343
2344 if (!error)
2345 error = git_config_set_bool(config, "core.bare", false);
2346 }
2347
2348 if (!error) {
2349 char *old_workdir = repo->workdir;
2350
2351 repo->workdir = git_buf_detach(&path);
2352 repo->is_bare = 0;
2353
2354 git__free(old_workdir);
2355 }
2356
2357 return error;
2358 }
2359
2360 int git_repository_is_bare(git_repository *repo)
2361 {
2362 assert(repo);
2363 return repo->is_bare;
2364 }
2365
2366 int git_repository_is_worktree(git_repository *repo)
2367 {
2368 assert(repo);
2369 return repo->is_worktree;
2370 }
2371
2372 int git_repository_set_bare(git_repository *repo)
2373 {
2374 int error;
2375 git_config *config;
2376
2377 assert(repo);
2378
2379 if (repo->is_bare)
2380 return 0;
2381
2382 if ((error = git_repository_config__weakptr(&config, repo)) < 0)
2383 return error;
2384
2385 if ((error = git_config_set_bool(config, "core.bare", true)) < 0)
2386 return error;
2387
2388 if ((error = git_config__update_entry(config, "core.worktree", NULL, true, true)) < 0)
2389 return error;
2390
2391 git__free(repo->workdir);
2392 repo->workdir = NULL;
2393 repo->is_bare = 1;
2394
2395 return 0;
2396 }
2397
2398 int git_repository_head_tree(git_tree **tree, git_repository *repo)
2399 {
2400 git_reference *head;
2401 git_object *obj;
2402 int error;
2403
2404 if ((error = git_repository_head(&head, repo)) < 0)
2405 return error;
2406
2407 if ((error = git_reference_peel(&obj, head, GIT_OBJ_TREE)) < 0)
2408 goto cleanup;
2409
2410 *tree = (git_tree *)obj;
2411
2412 cleanup:
2413 git_reference_free(head);
2414 return error;
2415 }
2416
2417 int git_repository__set_orig_head(git_repository *repo, const git_oid *orig_head)
2418 {
2419 git_filebuf file = GIT_FILEBUF_INIT;
2420 git_buf file_path = GIT_BUF_INIT;
2421 char orig_head_str[GIT_OID_HEXSZ];
2422 int error = 0;
2423
2424 git_oid_fmt(orig_head_str, orig_head);
2425
2426 if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_ORIG_HEAD_FILE)) == 0 &&
2427 (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_MERGE_FILE_MODE)) == 0 &&
2428 (error = git_filebuf_printf(&file, "%.*s\n", GIT_OID_HEXSZ, orig_head_str)) == 0)
2429 error = git_filebuf_commit(&file);
2430
2431 if (error < 0)
2432 git_filebuf_cleanup(&file);
2433
2434 git_buf_free(&file_path);
2435
2436 return error;
2437 }
2438
2439 int git_repository_message(git_buf *out, git_repository *repo)
2440 {
2441 git_buf path = GIT_BUF_INIT;
2442 struct stat st;
2443 int error;
2444
2445 git_buf_sanitize(out);
2446
2447 if (git_buf_joinpath(&path, repo->gitdir, GIT_MERGE_MSG_FILE) < 0)
2448 return -1;
2449
2450 if ((error = p_stat(git_buf_cstr(&path), &st)) < 0) {
2451 if (errno == ENOENT)
2452 error = GIT_ENOTFOUND;
2453 giterr_set(GITERR_OS, "could not access message file");
2454 } else {
2455 error = git_futils_readbuffer(out, git_buf_cstr(&path));
2456 }
2457
2458 git_buf_free(&path);
2459
2460 return error;
2461 }
2462
2463 int git_repository_message_remove(git_repository *repo)
2464 {
2465 git_buf path = GIT_BUF_INIT;
2466 int error;
2467
2468 if (git_buf_joinpath(&path, repo->gitdir, GIT_MERGE_MSG_FILE) < 0)
2469 return -1;
2470
2471 error = p_unlink(git_buf_cstr(&path));
2472 git_buf_free(&path);
2473
2474 return error;
2475 }
2476
2477 int git_repository_hashfile(
2478 git_oid *out,
2479 git_repository *repo,
2480 const char *path,
2481 git_otype type,
2482 const char *as_path)
2483 {
2484 int error;
2485 git_filter_list *fl = NULL;
2486 git_file fd = -1;
2487 git_off_t len;
2488 git_buf full_path = GIT_BUF_INIT;
2489
2490 assert(out && path && repo); /* as_path can be NULL */
2491
2492 /* At some point, it would be nice if repo could be NULL to just
2493 * apply filter rules defined in system and global files, but for
2494 * now that is not possible because git_filters_load() needs it.
2495 */
2496
2497 error = git_path_join_unrooted(
2498 &full_path, path, git_repository_workdir(repo), NULL);
2499 if (error < 0)
2500 return error;
2501
2502 if (!as_path)
2503 as_path = path;
2504
2505 /* passing empty string for "as_path" indicated --no-filters */
2506 if (strlen(as_path) > 0) {
2507 error = git_filter_list_load(
2508 &fl, repo, NULL, as_path,
2509 GIT_FILTER_TO_ODB, GIT_FILTER_DEFAULT);
2510 if (error < 0)
2511 return error;
2512 } else {
2513 error = 0;
2514 }
2515
2516 /* at this point, error is a count of the number of loaded filters */
2517
2518 fd = git_futils_open_ro(full_path.ptr);
2519 if (fd < 0) {
2520 error = fd;
2521 goto cleanup;
2522 }
2523
2524 len = git_futils_filesize(fd);
2525 if (len < 0) {
2526 error = (int)len;
2527 goto cleanup;
2528 }
2529
2530 if (!git__is_sizet(len)) {
2531 giterr_set(GITERR_OS, "file size overflow for 32-bit systems");
2532 error = -1;
2533 goto cleanup;
2534 }
2535
2536 error = git_odb__hashfd_filtered(out, fd, (size_t)len, type, fl);
2537
2538 cleanup:
2539 if (fd >= 0)
2540 p_close(fd);
2541 git_filter_list_free(fl);
2542 git_buf_free(&full_path);
2543
2544 return error;
2545 }
2546
2547 static int checkout_message(git_buf *out, git_reference *old, const char *new)
2548 {
2549 git_buf_puts(out, "checkout: moving from ");
2550
2551 if (git_reference_type(old) == GIT_REF_SYMBOLIC)
2552 git_buf_puts(out, git_reference__shorthand(git_reference_symbolic_target(old)));
2553 else
2554 git_buf_puts(out, git_oid_tostr_s(git_reference_target(old)));
2555
2556 git_buf_puts(out, " to ");
2557
2558 if (git_reference__is_branch(new) ||
2559 git_reference__is_tag(new) ||
2560 git_reference__is_remote(new))
2561 git_buf_puts(out, git_reference__shorthand(new));
2562 else
2563 git_buf_puts(out, new);
2564
2565 if (git_buf_oom(out))
2566 return -1;
2567
2568 return 0;
2569 }
2570
2571 static int detach(git_repository *repo, const git_oid *id, const char *new)
2572 {
2573 int error;
2574 git_buf log_message = GIT_BUF_INIT;
2575 git_object *object = NULL, *peeled = NULL;
2576 git_reference *new_head = NULL, *current = NULL;
2577
2578 assert(repo && id);
2579
2580 if ((error = git_reference_lookup(&current, repo, GIT_HEAD_FILE)) < 0)
2581 return error;
2582
2583 if ((error = git_object_lookup(&object, repo, id, GIT_OBJ_ANY)) < 0)
2584 goto cleanup;
2585
2586 if ((error = git_object_peel(&peeled, object, GIT_OBJ_COMMIT)) < 0)
2587 goto cleanup;
2588
2589 if (new == NULL)
2590 new = git_oid_tostr_s(git_object_id(peeled));
2591
2592 if ((error = checkout_message(&log_message, current, new)) < 0)
2593 goto cleanup;
2594
2595 error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_object_id(peeled), true, git_buf_cstr(&log_message));
2596
2597 cleanup:
2598 git_buf_free(&log_message);
2599 git_object_free(object);
2600 git_object_free(peeled);
2601 git_reference_free(current);
2602 git_reference_free(new_head);
2603 return error;
2604 }
2605
2606 int git_repository_set_head(
2607 git_repository* repo,
2608 const char* refname)
2609 {
2610 git_reference *ref = NULL, *current = NULL, *new_head = NULL;
2611 git_buf log_message = GIT_BUF_INIT;
2612 int error;
2613
2614 assert(repo && refname);
2615
2616 if ((error = git_reference_lookup(&current, repo, GIT_HEAD_FILE)) < 0)
2617 return error;
2618
2619 if ((error = checkout_message(&log_message, current, refname)) < 0)
2620 goto cleanup;
2621
2622 error = git_reference_lookup(&ref, repo, refname);
2623 if (error < 0 && error != GIT_ENOTFOUND)
2624 goto cleanup;
2625
2626 if (ref && current->type == GIT_REF_SYMBOLIC && git__strcmp(current->target.symbolic, ref->name) &&
2627 git_reference_is_branch(ref) && git_branch_is_checked_out(ref)) {
2628 giterr_set(GITERR_REPOSITORY, "cannot set HEAD to reference '%s' as it is the current HEAD "
2629 "of a linked repository.", git_reference_name(ref));
2630 error = -1;
2631 goto cleanup;
2632 }
2633
2634 if (!error) {
2635 if (git_reference_is_branch(ref)) {
2636 error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE,
2637 git_reference_name(ref), true, git_buf_cstr(&log_message));
2638 } else {
2639 error = detach(repo, git_reference_target(ref),
2640 git_reference_is_tag(ref) || git_reference_is_remote(ref) ? refname : NULL);
2641 }
2642 } else if (git_reference__is_branch(refname)) {
2643 error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, refname,
2644 true, git_buf_cstr(&log_message));
2645 }
2646
2647 cleanup:
2648 git_buf_free(&log_message);
2649 git_reference_free(current);
2650 git_reference_free(ref);
2651 git_reference_free(new_head);
2652 return error;
2653 }
2654
2655 int git_repository_set_head_detached(
2656 git_repository* repo,
2657 const git_oid* commitish)
2658 {
2659 return detach(repo, commitish, NULL);
2660 }
2661
2662 int git_repository_set_head_detached_from_annotated(
2663 git_repository *repo,
2664 const git_annotated_commit *commitish)
2665 {
2666 assert(repo && commitish);
2667
2668 return detach(repo, git_annotated_commit_id(commitish), commitish->description);
2669 }
2670
2671 int git_repository_detach_head(git_repository* repo)
2672 {
2673 git_reference *old_head = NULL, *new_head = NULL, *current = NULL;
2674 git_object *object = NULL;
2675 git_buf log_message = GIT_BUF_INIT;
2676 int error;
2677
2678 assert(repo);
2679
2680 if ((error = git_reference_lookup(&current, repo, GIT_HEAD_FILE)) < 0)
2681 return error;
2682
2683 if ((error = git_repository_head(&old_head, repo)) < 0)
2684 goto cleanup;
2685
2686 if ((error = git_object_lookup(&object, repo, git_reference_target(old_head), GIT_OBJ_COMMIT)) < 0)
2687 goto cleanup;
2688
2689 if ((error = checkout_message(&log_message, current, git_oid_tostr_s(git_object_id(object)))) < 0)
2690 goto cleanup;
2691
2692 error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_reference_target(old_head),
2693 1, git_buf_cstr(&log_message));
2694
2695 cleanup:
2696 git_buf_free(&log_message);
2697 git_object_free(object);
2698 git_reference_free(old_head);
2699 git_reference_free(new_head);
2700 git_reference_free(current);
2701 return error;
2702 }
2703
2704 /**
2705 * Loosely ported from git.git
2706 * https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh#L198-289
2707 */
2708 int git_repository_state(git_repository *repo)
2709 {
2710 git_buf repo_path = GIT_BUF_INIT;
2711 int state = GIT_REPOSITORY_STATE_NONE;
2712
2713 assert(repo);
2714
2715 if (git_buf_puts(&repo_path, repo->gitdir) < 0)
2716 return -1;
2717
2718 if (git_path_contains_file(&repo_path, GIT_REBASE_MERGE_INTERACTIVE_FILE))
2719 state = GIT_REPOSITORY_STATE_REBASE_INTERACTIVE;
2720 else if (git_path_contains_dir(&repo_path, GIT_REBASE_MERGE_DIR))
2721 state = GIT_REPOSITORY_STATE_REBASE_MERGE;
2722 else if (git_path_contains_file(&repo_path, GIT_REBASE_APPLY_REBASING_FILE))
2723 state = GIT_REPOSITORY_STATE_REBASE;
2724 else if (git_path_contains_file(&repo_path, GIT_REBASE_APPLY_APPLYING_FILE))
2725 state = GIT_REPOSITORY_STATE_APPLY_MAILBOX;
2726 else if (git_path_contains_dir(&repo_path, GIT_REBASE_APPLY_DIR))
2727 state = GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE;
2728 else if (git_path_contains_file(&repo_path, GIT_MERGE_HEAD_FILE))
2729 state = GIT_REPOSITORY_STATE_MERGE;
2730 else if (git_path_contains_file(&repo_path, GIT_REVERT_HEAD_FILE)) {
2731 state = GIT_REPOSITORY_STATE_REVERT;
2732 if (git_path_contains_file(&repo_path, GIT_SEQUENCER_TODO_FILE)) {
2733 state = GIT_REPOSITORY_STATE_REVERT_SEQUENCE;
2734 }
2735 } else if (git_path_contains_file(&repo_path, GIT_CHERRYPICK_HEAD_FILE)) {
2736 state = GIT_REPOSITORY_STATE_CHERRYPICK;
2737 if (git_path_contains_file(&repo_path, GIT_SEQUENCER_TODO_FILE)) {
2738 state = GIT_REPOSITORY_STATE_CHERRYPICK_SEQUENCE;
2739 }
2740 } else if (git_path_contains_file(&repo_path, GIT_BISECT_LOG_FILE))
2741 state = GIT_REPOSITORY_STATE_BISECT;
2742
2743 git_buf_free(&repo_path);
2744 return state;
2745 }
2746
2747 int git_repository__cleanup_files(
2748 git_repository *repo, const char *files[], size_t files_len)
2749 {
2750 git_buf buf = GIT_BUF_INIT;
2751 size_t i;
2752 int error;
2753
2754 for (error = 0, i = 0; !error && i < files_len; ++i) {
2755 const char *path;
2756
2757 if (git_buf_joinpath(&buf, repo->gitdir, files[i]) < 0)
2758 return -1;
2759
2760 path = git_buf_cstr(&buf);
2761
2762 if (git_path_isfile(path)) {
2763 error = p_unlink(path);
2764 } else if (git_path_isdir(path)) {
2765 error = git_futils_rmdir_r(path, NULL,
2766 GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS);
2767 }
2768
2769 git_buf_clear(&buf);
2770 }
2771
2772 git_buf_free(&buf);
2773 return error;
2774 }
2775
2776 static const char *state_files[] = {
2777 GIT_MERGE_HEAD_FILE,
2778 GIT_MERGE_MODE_FILE,
2779 GIT_MERGE_MSG_FILE,
2780 GIT_REVERT_HEAD_FILE,
2781 GIT_CHERRYPICK_HEAD_FILE,
2782 GIT_BISECT_LOG_FILE,
2783 GIT_REBASE_MERGE_DIR,
2784 GIT_REBASE_APPLY_DIR,
2785 GIT_SEQUENCER_DIR,
2786 };
2787
2788 int git_repository_state_cleanup(git_repository *repo)
2789 {
2790 assert(repo);
2791
2792 return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files));
2793 }
2794
2795 int git_repository_is_shallow(git_repository *repo)
2796 {
2797 git_buf path = GIT_BUF_INIT;
2798 struct stat st;
2799 int error;
2800
2801 if ((error = git_buf_joinpath(&path, repo->gitdir, "shallow")) < 0)
2802 return error;
2803
2804 error = git_path_lstat(path.ptr, &st);
2805 git_buf_free(&path);
2806
2807 if (error == GIT_ENOTFOUND) {
2808 giterr_clear();
2809 return 0;
2810 }
2811
2812 if (error < 0)
2813 return error;
2814 return st.st_size == 0 ? 0 : 1;
2815 }
2816
2817 int git_repository_init_init_options(
2818 git_repository_init_options *opts, unsigned int version)
2819 {
2820 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
2821 opts, version, git_repository_init_options,
2822 GIT_REPOSITORY_INIT_OPTIONS_INIT);
2823 return 0;
2824 }
2825
2826 int git_repository_ident(const char **name, const char **email, const git_repository *repo)
2827 {
2828 *name = repo->ident_name;
2829 *email = repo->ident_email;
2830
2831 return 0;
2832 }
2833
2834 int git_repository_set_ident(git_repository *repo, const char *name, const char *email)
2835 {
2836 char *tmp_name = NULL, *tmp_email = NULL;
2837
2838 if (name) {
2839 tmp_name = git__strdup(name);
2840 GITERR_CHECK_ALLOC(tmp_name);
2841 }
2842
2843 if (email) {
2844 tmp_email = git__strdup(email);
2845 GITERR_CHECK_ALLOC(tmp_email);
2846 }
2847
2848 tmp_name = git__swap(repo->ident_name, tmp_name);
2849 tmp_email = git__swap(repo->ident_email, tmp_email);
2850
2851 git__free(tmp_name);
2852 git__free(tmp_email);
2853
2854 return 0;
2855 }
2856
2857 int git_repository_submodule_cache_all(git_repository *repo)
2858 {
2859 int error;
2860
2861 assert(repo);
2862
2863 if ((error = git_strmap_alloc(&repo->submodule_cache)))
2864 return error;
2865
2866 error = git_submodule__map(repo, repo->submodule_cache);
2867 return error;
2868 }
2869
2870 int git_repository_submodule_cache_clear(git_repository *repo)
2871 {
2872 git_submodule *sm;
2873 assert(repo);
2874 if (repo->submodule_cache == NULL) {
2875 return 0;
2876 }
2877 git_strmap_foreach_value(repo->submodule_cache, sm, {
2878 git_submodule_free(sm);
2879 });
2880 git_strmap_free(repo->submodule_cache);
2881 repo->submodule_cache = 0;
2882 return 0;
2883 }