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