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