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