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