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