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