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
set_odb(git_repository * repo,git_odb * odb)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
set_refdb(git_repository * repo,git_refdb * refdb)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
set_config(git_repository * repo,git_config * config)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
set_index(git_repository * repo,git_index * index)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
git_repository__cleanup(git_repository * repo)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
git_repository_free(git_repository * repo)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) */
lookup_commondir(bool * separate,git_buf * commondir,git_buf * repository_path)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
validate_repo_path(git_buf * path)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 */
is_valid_repository_path(bool * out,git_buf * repository_path,git_buf * common_path)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
repository_alloc(void)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
git_repository_new(git_repository ** out)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
load_config_data(git_repository * repo,const git_config * config)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
load_workdir(git_repository * repo,git_config * config,git_buf * parent_path)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 */
find_ceiling_dir_offset(const char * path,const char * ceiling_directories)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 */
read_gitfile(git_buf * path_out,const char * file_path)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
find_repo(git_buf * gitdir_path,git_buf * workdir_path,git_buf * gitlink_path,git_buf * commondir_path,const char * start_path,uint32_t flags,const char * ceiling_dirs)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
git_repository_open_bare(git_repository ** repo_ptr,const char * bare_path)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
_git_repository_open_ext_from_env(git_repository ** out,const char * start_path)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
repo_is_worktree(unsigned * out,const git_repository * repo)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
git_repository_open_ext(git_repository ** repo_ptr,const char * start_path,unsigned int flags,const char * ceiling_dirs)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
git_repository_open(git_repository ** repo_out,const char * path)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
git_repository_open_from_worktree(git_repository ** repo_out,git_worktree * wt)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
git_repository_wrap_odb(git_repository ** repo_out,git_odb * odb)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
git_repository_discover(git_buf * out,const char * start_path,int across_fs,const char * ceiling_dirs)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
load_config(git_config ** out,git_repository * repo,const char * global_config_path,const char * xdg_config_path,const char * system_config_path,const char * programdata_path)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
path_unless_empty(git_buf * buf)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
git_repository_config__weakptr(git_config ** out,git_repository * repo)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 config = git_atomic_compare_and_swap(&repo->_config, NULL, config);
1097 if (config != NULL) {
1098 GIT_REFCOUNT_OWN(config, NULL);
1099 git_config_free(config);
1100 }
1101 }
1102
1103 git_buf_dispose(&global_buf);
1104 git_buf_dispose(&xdg_buf);
1105 git_buf_dispose(&system_buf);
1106 git_buf_dispose(&programdata_buf);
1107 }
1108
1109 *out = repo->_config;
1110 return error;
1111 }
1112
git_repository_config(git_config ** out,git_repository * repo)1113 int git_repository_config(git_config **out, git_repository *repo)
1114 {
1115 if (git_repository_config__weakptr(out, repo) < 0)
1116 return -1;
1117
1118 GIT_REFCOUNT_INC(*out);
1119 return 0;
1120 }
1121
git_repository_config_snapshot(git_config ** out,git_repository * repo)1122 int git_repository_config_snapshot(git_config **out, git_repository *repo)
1123 {
1124 int error;
1125 git_config *weak;
1126
1127 if ((error = git_repository_config__weakptr(&weak, repo)) < 0)
1128 return error;
1129
1130 return git_config_snapshot(out, weak);
1131 }
1132
git_repository_set_config(git_repository * repo,git_config * config)1133 int git_repository_set_config(git_repository *repo, git_config *config)
1134 {
1135 GIT_ASSERT_ARG(repo);
1136 GIT_ASSERT_ARG(config);
1137
1138 set_config(repo, config);
1139 return 0;
1140 }
1141
git_repository_odb__weakptr(git_odb ** out,git_repository * repo)1142 int git_repository_odb__weakptr(git_odb **out, git_repository *repo)
1143 {
1144 int error = 0;
1145
1146 GIT_ASSERT_ARG(repo);
1147 GIT_ASSERT_ARG(out);
1148
1149 *out = git_atomic_load(repo->_odb);
1150 if (*out == NULL) {
1151 git_buf odb_path = GIT_BUF_INIT;
1152 git_odb *odb;
1153
1154 if ((error = git_repository_item_path(&odb_path, repo,
1155 GIT_REPOSITORY_ITEM_OBJECTS)) < 0 ||
1156 (error = git_odb_new(&odb)) < 0)
1157 return error;
1158
1159 GIT_REFCOUNT_OWN(odb, repo);
1160
1161 if ((error = git_odb__set_caps(odb, GIT_ODB_CAP_FROM_OWNER)) < 0 ||
1162 (error = git_odb__add_default_backends(odb, odb_path.ptr, 0, 0)) < 0) {
1163 git_odb_free(odb);
1164 return error;
1165 }
1166
1167 odb = git_atomic_compare_and_swap(&repo->_odb, NULL, odb);
1168 if (odb != NULL) {
1169 GIT_REFCOUNT_OWN(odb, NULL);
1170 git_odb_free(odb);
1171 }
1172
1173 git_buf_dispose(&odb_path);
1174 *out = git_atomic_load(repo->_odb);
1175 }
1176
1177 return error;
1178 }
1179
git_repository_odb(git_odb ** out,git_repository * repo)1180 int git_repository_odb(git_odb **out, git_repository *repo)
1181 {
1182 if (git_repository_odb__weakptr(out, repo) < 0)
1183 return -1;
1184
1185 GIT_REFCOUNT_INC(*out);
1186 return 0;
1187 }
1188
git_repository_set_odb(git_repository * repo,git_odb * odb)1189 int git_repository_set_odb(git_repository *repo, git_odb *odb)
1190 {
1191 GIT_ASSERT_ARG(repo);
1192 GIT_ASSERT_ARG(odb);
1193
1194 set_odb(repo, odb);
1195 return 0;
1196 }
1197
git_repository_refdb__weakptr(git_refdb ** out,git_repository * repo)1198 int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo)
1199 {
1200 int error = 0;
1201
1202 GIT_ASSERT_ARG(out);
1203 GIT_ASSERT_ARG(repo);
1204
1205 if (repo->_refdb == NULL) {
1206 git_refdb *refdb;
1207
1208 error = git_refdb_open(&refdb, repo);
1209 if (!error) {
1210 GIT_REFCOUNT_OWN(refdb, repo);
1211
1212 refdb = git_atomic_compare_and_swap(&repo->_refdb, NULL, refdb);
1213 if (refdb != NULL) {
1214 GIT_REFCOUNT_OWN(refdb, NULL);
1215 git_refdb_free(refdb);
1216 }
1217 }
1218 }
1219
1220 *out = repo->_refdb;
1221 return error;
1222 }
1223
git_repository_refdb(git_refdb ** out,git_repository * repo)1224 int git_repository_refdb(git_refdb **out, git_repository *repo)
1225 {
1226 if (git_repository_refdb__weakptr(out, repo) < 0)
1227 return -1;
1228
1229 GIT_REFCOUNT_INC(*out);
1230 return 0;
1231 }
1232
git_repository_set_refdb(git_repository * repo,git_refdb * refdb)1233 int git_repository_set_refdb(git_repository *repo, git_refdb *refdb)
1234 {
1235 GIT_ASSERT_ARG(repo);
1236 GIT_ASSERT_ARG(refdb);
1237
1238 set_refdb(repo, refdb);
1239 return 0;
1240 }
1241
git_repository_index__weakptr(git_index ** out,git_repository * repo)1242 int git_repository_index__weakptr(git_index **out, git_repository *repo)
1243 {
1244 int error = 0;
1245
1246 GIT_ASSERT_ARG(out);
1247 GIT_ASSERT_ARG(repo);
1248
1249 if (repo->_index == NULL) {
1250 git_buf index_path = GIT_BUF_INIT;
1251 git_index *index;
1252
1253 if ((error = git_buf_joinpath(&index_path, repo->gitdir, GIT_INDEX_FILE)) < 0)
1254 return error;
1255
1256 error = git_index_open(&index, index_path.ptr);
1257 if (!error) {
1258 GIT_REFCOUNT_OWN(index, repo);
1259
1260 index = git_atomic_compare_and_swap(&repo->_index, NULL, index);
1261 if (index != NULL) {
1262 GIT_REFCOUNT_OWN(index, NULL);
1263 git_index_free(index);
1264 }
1265
1266 error = git_index_set_caps(repo->_index,
1267 GIT_INDEX_CAPABILITY_FROM_OWNER);
1268 }
1269
1270 git_buf_dispose(&index_path);
1271 }
1272
1273 *out = repo->_index;
1274 return error;
1275 }
1276
git_repository_index(git_index ** out,git_repository * repo)1277 int git_repository_index(git_index **out, git_repository *repo)
1278 {
1279 if (git_repository_index__weakptr(out, repo) < 0)
1280 return -1;
1281
1282 GIT_REFCOUNT_INC(*out);
1283 return 0;
1284 }
1285
git_repository_set_index(git_repository * repo,git_index * index)1286 int git_repository_set_index(git_repository *repo, git_index *index)
1287 {
1288 GIT_ASSERT_ARG(repo);
1289 set_index(repo, index);
1290 return 0;
1291 }
1292
git_repository_set_namespace(git_repository * repo,const char * namespace)1293 int git_repository_set_namespace(git_repository *repo, const char *namespace)
1294 {
1295 git__free(repo->namespace);
1296
1297 if (namespace == NULL) {
1298 repo->namespace = NULL;
1299 return 0;
1300 }
1301
1302 return (repo->namespace = git__strdup(namespace)) ? 0 : -1;
1303 }
1304
git_repository_get_namespace(git_repository * repo)1305 const char *git_repository_get_namespace(git_repository *repo)
1306 {
1307 return repo->namespace;
1308 }
1309
1310 #ifdef GIT_WIN32
reserved_names_add8dot3(git_repository * repo,const char * path)1311 static int reserved_names_add8dot3(git_repository *repo, const char *path)
1312 {
1313 char *name = git_win32_path_8dot3_name(path);
1314 const char *def = GIT_DIR_SHORTNAME;
1315 const char *def_dot_git = DOT_GIT;
1316 size_t name_len, def_len = CONST_STRLEN(GIT_DIR_SHORTNAME);
1317 size_t def_dot_git_len = CONST_STRLEN(DOT_GIT);
1318 git_buf *buf;
1319
1320 if (!name)
1321 return 0;
1322
1323 name_len = strlen(name);
1324
1325 if ((name_len == def_len && memcmp(name, def, def_len) == 0) ||
1326 (name_len == def_dot_git_len && memcmp(name, def_dot_git, def_dot_git_len) == 0)) {
1327 git__free(name);
1328 return 0;
1329 }
1330
1331 if ((buf = git_array_alloc(repo->reserved_names)) == NULL)
1332 return -1;
1333
1334 git_buf_attach(buf, name, name_len);
1335 return true;
1336 }
1337
git_repository__reserved_names(git_buf ** out,size_t * outlen,git_repository * repo,bool include_ntfs)1338 bool git_repository__reserved_names(
1339 git_buf **out, size_t *outlen, git_repository *repo, bool include_ntfs)
1340 {
1341 GIT_UNUSED(include_ntfs);
1342
1343 if (repo->reserved_names.size == 0) {
1344 git_buf *buf;
1345 size_t i;
1346
1347 /* Add the static defaults */
1348 for (i = 0; i < git_repository__reserved_names_win32_len; i++) {
1349 if ((buf = git_array_alloc(repo->reserved_names)) == NULL)
1350 goto on_error;
1351
1352 buf->ptr = git_repository__reserved_names_win32[i].ptr;
1353 buf->size = git_repository__reserved_names_win32[i].size;
1354 }
1355
1356 /* Try to add any repo-specific reserved names - the gitlink file
1357 * within a submodule or the repository (if the repository directory
1358 * is beneath the workdir). These are typically `.git`, but should
1359 * be protected in case they are not. Note, repo and workdir paths
1360 * are always prettified to end in `/`, so a prefixcmp is safe.
1361 */
1362 if (!repo->is_bare) {
1363 int (*prefixcmp)(const char *, const char *);
1364 int error, ignorecase;
1365
1366 error = git_repository__configmap_lookup(
1367 &ignorecase, repo, GIT_CONFIGMAP_IGNORECASE);
1368 prefixcmp = (error || ignorecase) ? git__prefixcmp_icase :
1369 git__prefixcmp;
1370
1371 if (repo->gitlink &&
1372 reserved_names_add8dot3(repo, repo->gitlink) < 0)
1373 goto on_error;
1374
1375 if (repo->gitdir &&
1376 prefixcmp(repo->gitdir, repo->workdir) == 0 &&
1377 reserved_names_add8dot3(repo, repo->gitdir) < 0)
1378 goto on_error;
1379 }
1380 }
1381
1382 *out = repo->reserved_names.ptr;
1383 *outlen = repo->reserved_names.size;
1384
1385 return true;
1386
1387 /* Always give good defaults, even on OOM */
1388 on_error:
1389 *out = git_repository__reserved_names_win32;
1390 *outlen = git_repository__reserved_names_win32_len;
1391
1392 return false;
1393 }
1394 #else
git_repository__reserved_names(git_buf ** out,size_t * outlen,git_repository * repo,bool include_ntfs)1395 bool git_repository__reserved_names(
1396 git_buf **out, size_t *outlen, git_repository *repo, bool include_ntfs)
1397 {
1398 GIT_UNUSED(repo);
1399
1400 if (include_ntfs) {
1401 *out = git_repository__reserved_names_win32;
1402 *outlen = git_repository__reserved_names_win32_len;
1403 } else {
1404 *out = git_repository__reserved_names_posix;
1405 *outlen = git_repository__reserved_names_posix_len;
1406 }
1407
1408 return true;
1409 }
1410 #endif
1411
check_repositoryformatversion(int * version,git_config * config)1412 static int check_repositoryformatversion(int *version, git_config *config)
1413 {
1414 int error;
1415
1416 error = git_config_get_int32(version, config, "core.repositoryformatversion");
1417 /* git ignores this if the config variable isn't there */
1418 if (error == GIT_ENOTFOUND)
1419 return 0;
1420
1421 if (error < 0)
1422 return -1;
1423
1424 if (GIT_REPO_MAX_VERSION < *version) {
1425 git_error_set(GIT_ERROR_REPOSITORY,
1426 "unsupported repository version %d; only versions up to %d are supported",
1427 *version, GIT_REPO_MAX_VERSION);
1428 return -1;
1429 }
1430
1431 return 0;
1432 }
1433
check_valid_extension(const git_config_entry * entry,void * payload)1434 static int check_valid_extension(const git_config_entry *entry, void *payload)
1435 {
1436 GIT_UNUSED(payload);
1437
1438 if (!strcmp(entry->name, "extensions.noop"))
1439 return 0;
1440
1441 git_error_set(GIT_ERROR_REPOSITORY, "unsupported extension name %s", entry->name);
1442 return -1;
1443 }
1444
check_extensions(git_config * config,int version)1445 static int check_extensions(git_config *config, int version)
1446 {
1447 if (version < 1)
1448 return 0;
1449
1450 return git_config_foreach_match(config, "^extensions\\.", check_valid_extension, NULL);
1451 }
1452
git_repository_create_head(const char * git_dir,const char * ref_name)1453 int git_repository_create_head(const char *git_dir, const char *ref_name)
1454 {
1455 git_buf ref_path = GIT_BUF_INIT;
1456 git_filebuf ref = GIT_FILEBUF_INIT;
1457 const char *fmt;
1458 int error;
1459
1460 if ((error = git_buf_joinpath(&ref_path, git_dir, GIT_HEAD_FILE)) < 0 ||
1461 (error = git_filebuf_open(&ref, ref_path.ptr, 0, GIT_REFS_FILE_MODE)) < 0)
1462 goto out;
1463
1464 if (git__prefixcmp(ref_name, GIT_REFS_DIR) == 0)
1465 fmt = "ref: %s\n";
1466 else
1467 fmt = "ref: " GIT_REFS_HEADS_DIR "%s\n";
1468
1469 if ((error = git_filebuf_printf(&ref, fmt, ref_name)) < 0 ||
1470 (error = git_filebuf_commit(&ref)) < 0)
1471 goto out;
1472
1473 out:
1474 git_buf_dispose(&ref_path);
1475 git_filebuf_cleanup(&ref);
1476 return error;
1477 }
1478
is_chmod_supported(const char * file_path)1479 static bool is_chmod_supported(const char *file_path)
1480 {
1481 struct stat st1, st2;
1482
1483 if (p_stat(file_path, &st1) < 0)
1484 return false;
1485
1486 if (p_chmod(file_path, st1.st_mode ^ S_IXUSR) < 0)
1487 return false;
1488
1489 if (p_stat(file_path, &st2) < 0)
1490 return false;
1491
1492 return (st1.st_mode != st2.st_mode);
1493 }
1494
is_filesystem_case_insensitive(const char * gitdir_path)1495 static bool is_filesystem_case_insensitive(const char *gitdir_path)
1496 {
1497 git_buf path = GIT_BUF_INIT;
1498 int is_insensitive = -1;
1499
1500 if (!git_buf_joinpath(&path, gitdir_path, "CoNfIg"))
1501 is_insensitive = git_path_exists(git_buf_cstr(&path));
1502
1503 git_buf_dispose(&path);
1504 return is_insensitive;
1505 }
1506
are_symlinks_supported(const char * wd_path)1507 static bool are_symlinks_supported(const char *wd_path)
1508 {
1509 git_config *config = NULL;
1510 git_buf global_buf = GIT_BUF_INIT;
1511 git_buf xdg_buf = GIT_BUF_INIT;
1512 git_buf system_buf = GIT_BUF_INIT;
1513 git_buf programdata_buf = GIT_BUF_INIT;
1514 int symlinks = 0;
1515
1516 /*
1517 * To emulate Git for Windows, symlinks on Windows must be explicitly
1518 * opted-in. We examine the system configuration for a core.symlinks
1519 * set to true. If found, we then examine the filesystem to see if
1520 * symlinks are _actually_ supported by the current user. If that is
1521 * _not_ set, then we do not test or enable symlink support.
1522 */
1523 #ifdef GIT_WIN32
1524 git_config_find_global(&global_buf);
1525 git_config_find_xdg(&xdg_buf);
1526 git_config_find_system(&system_buf);
1527 git_config_find_programdata(&programdata_buf);
1528
1529 if (load_config(&config, NULL,
1530 path_unless_empty(&global_buf),
1531 path_unless_empty(&xdg_buf),
1532 path_unless_empty(&system_buf),
1533 path_unless_empty(&programdata_buf)) < 0)
1534 goto done;
1535
1536 if (git_config_get_bool(&symlinks, config, "core.symlinks") < 0 || !symlinks)
1537 goto done;
1538 #endif
1539
1540 if (!(symlinks = git_path_supports_symlinks(wd_path)))
1541 goto done;
1542
1543 done:
1544 git_buf_dispose(&global_buf);
1545 git_buf_dispose(&xdg_buf);
1546 git_buf_dispose(&system_buf);
1547 git_buf_dispose(&programdata_buf);
1548 git_config_free(config);
1549 return symlinks != 0;
1550 }
1551
create_empty_file(const char * path,mode_t mode)1552 static int create_empty_file(const char *path, mode_t mode)
1553 {
1554 int fd;
1555
1556 if ((fd = p_creat(path, mode)) < 0) {
1557 git_error_set(GIT_ERROR_OS, "error while creating '%s'", path);
1558 return -1;
1559 }
1560
1561 if (p_close(fd) < 0) {
1562 git_error_set(GIT_ERROR_OS, "error while closing '%s'", path);
1563 return -1;
1564 }
1565
1566 return 0;
1567 }
1568
repo_local_config(git_config ** out,git_buf * config_dir,git_repository * repo,const char * repo_dir)1569 static int repo_local_config(
1570 git_config **out,
1571 git_buf *config_dir,
1572 git_repository *repo,
1573 const char *repo_dir)
1574 {
1575 int error = 0;
1576 git_config *parent;
1577 const char *cfg_path;
1578
1579 if (git_buf_joinpath(config_dir, repo_dir, GIT_CONFIG_FILENAME_INREPO) < 0)
1580 return -1;
1581 cfg_path = git_buf_cstr(config_dir);
1582
1583 /* make LOCAL config if missing */
1584 if (!git_path_isfile(cfg_path) &&
1585 (error = create_empty_file(cfg_path, GIT_CONFIG_FILE_MODE)) < 0)
1586 return error;
1587
1588 /* if no repo, just open that file directly */
1589 if (!repo)
1590 return git_config_open_ondisk(out, cfg_path);
1591
1592 /* otherwise, open parent config and get that level */
1593 if ((error = git_repository_config__weakptr(&parent, repo)) < 0)
1594 return error;
1595
1596 if (git_config_open_level(out, parent, GIT_CONFIG_LEVEL_LOCAL) < 0) {
1597 git_error_clear();
1598
1599 if (!(error = git_config_add_file_ondisk(
1600 parent, cfg_path, GIT_CONFIG_LEVEL_LOCAL, repo, false)))
1601 error = git_config_open_level(out, parent, GIT_CONFIG_LEVEL_LOCAL);
1602 }
1603
1604 git_config_free(parent);
1605
1606 return error;
1607 }
1608
repo_init_fs_configs(git_config * cfg,const char * cfg_path,const char * repo_dir,const char * work_dir,bool update_ignorecase)1609 static int repo_init_fs_configs(
1610 git_config *cfg,
1611 const char *cfg_path,
1612 const char *repo_dir,
1613 const char *work_dir,
1614 bool update_ignorecase)
1615 {
1616 int error = 0;
1617
1618 if (!work_dir)
1619 work_dir = repo_dir;
1620
1621 if ((error = git_config_set_bool(
1622 cfg, "core.filemode", is_chmod_supported(cfg_path))) < 0)
1623 return error;
1624
1625 if (!are_symlinks_supported(work_dir)) {
1626 if ((error = git_config_set_bool(cfg, "core.symlinks", false)) < 0)
1627 return error;
1628 } else if (git_config_delete_entry(cfg, "core.symlinks") < 0)
1629 git_error_clear();
1630
1631 if (update_ignorecase) {
1632 if (is_filesystem_case_insensitive(repo_dir)) {
1633 if ((error = git_config_set_bool(cfg, "core.ignorecase", true)) < 0)
1634 return error;
1635 } else if (git_config_delete_entry(cfg, "core.ignorecase") < 0)
1636 git_error_clear();
1637 }
1638
1639 #ifdef GIT_USE_ICONV
1640 if ((error = git_config_set_bool(
1641 cfg, "core.precomposeunicode",
1642 git_path_does_fs_decompose_unicode(work_dir))) < 0)
1643 return error;
1644 /* on non-iconv platforms, don't even set core.precomposeunicode */
1645 #endif
1646
1647 return 0;
1648 }
1649
repo_init_config(const char * repo_dir,const char * work_dir,uint32_t flags,uint32_t mode)1650 static int repo_init_config(
1651 const char *repo_dir,
1652 const char *work_dir,
1653 uint32_t flags,
1654 uint32_t mode)
1655 {
1656 int error = 0;
1657 git_buf cfg_path = GIT_BUF_INIT, worktree_path = GIT_BUF_INIT;
1658 git_config *config = NULL;
1659 bool is_bare = ((flags & GIT_REPOSITORY_INIT_BARE) != 0);
1660 bool is_reinit = ((flags & GIT_REPOSITORY_INIT__IS_REINIT) != 0);
1661 int version = 0;
1662
1663 if ((error = repo_local_config(&config, &cfg_path, NULL, repo_dir)) < 0)
1664 goto cleanup;
1665
1666 if (is_reinit && (error = check_repositoryformatversion(&version, config)) < 0)
1667 goto cleanup;
1668
1669 if ((error = check_extensions(config, version)) < 0)
1670 goto cleanup;
1671
1672 #define SET_REPO_CONFIG(TYPE, NAME, VAL) do { \
1673 if ((error = git_config_set_##TYPE(config, NAME, VAL)) < 0) \
1674 goto cleanup; } while (0)
1675
1676 SET_REPO_CONFIG(bool, "core.bare", is_bare);
1677 SET_REPO_CONFIG(int32, "core.repositoryformatversion", GIT_REPO_VERSION);
1678
1679 if ((error = repo_init_fs_configs(
1680 config, cfg_path.ptr, repo_dir, work_dir, !is_reinit)) < 0)
1681 goto cleanup;
1682
1683 if (!is_bare) {
1684 SET_REPO_CONFIG(bool, "core.logallrefupdates", true);
1685
1686 if (!(flags & GIT_REPOSITORY_INIT__NATURAL_WD)) {
1687 if ((error = git_buf_sets(&worktree_path, work_dir)) < 0)
1688 goto cleanup;
1689
1690 if ((flags & GIT_REPOSITORY_INIT_RELATIVE_GITLINK))
1691 if ((error = git_path_make_relative(&worktree_path, repo_dir)) < 0)
1692 goto cleanup;
1693
1694 SET_REPO_CONFIG(string, "core.worktree", worktree_path.ptr);
1695 } else if (is_reinit) {
1696 if (git_config_delete_entry(config, "core.worktree") < 0)
1697 git_error_clear();
1698 }
1699 }
1700
1701 if (mode == GIT_REPOSITORY_INIT_SHARED_GROUP) {
1702 SET_REPO_CONFIG(int32, "core.sharedrepository", 1);
1703 SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
1704 }
1705 else if (mode == GIT_REPOSITORY_INIT_SHARED_ALL) {
1706 SET_REPO_CONFIG(int32, "core.sharedrepository", 2);
1707 SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
1708 }
1709
1710 cleanup:
1711 git_buf_dispose(&cfg_path);
1712 git_buf_dispose(&worktree_path);
1713 git_config_free(config);
1714
1715 return error;
1716 }
1717
repo_reinit_submodule_fs(git_submodule * sm,const char * n,void * p)1718 static int repo_reinit_submodule_fs(git_submodule *sm, const char *n, void *p)
1719 {
1720 git_repository *smrepo = NULL;
1721 GIT_UNUSED(n); GIT_UNUSED(p);
1722
1723 if (git_submodule_open(&smrepo, sm) < 0 ||
1724 git_repository_reinit_filesystem(smrepo, true) < 0)
1725 git_error_clear();
1726 git_repository_free(smrepo);
1727
1728 return 0;
1729 }
1730
git_repository_reinit_filesystem(git_repository * repo,int recurse)1731 int git_repository_reinit_filesystem(git_repository *repo, int recurse)
1732 {
1733 int error = 0;
1734 git_buf path = GIT_BUF_INIT;
1735 git_config *config = NULL;
1736 const char *repo_dir = git_repository_path(repo);
1737
1738 if (!(error = repo_local_config(&config, &path, repo, repo_dir)))
1739 error = repo_init_fs_configs(
1740 config, path.ptr, repo_dir, git_repository_workdir(repo), true);
1741
1742 git_config_free(config);
1743 git_buf_dispose(&path);
1744
1745 git_repository__configmap_lookup_cache_clear(repo);
1746
1747 if (!repo->is_bare && recurse)
1748 (void)git_submodule_foreach(repo, repo_reinit_submodule_fs, NULL);
1749
1750 return error;
1751 }
1752
repo_write_template(const char * git_dir,bool allow_overwrite,const char * file,mode_t mode,bool hidden,const char * content)1753 static int repo_write_template(
1754 const char *git_dir,
1755 bool allow_overwrite,
1756 const char *file,
1757 mode_t mode,
1758 bool hidden,
1759 const char *content)
1760 {
1761 git_buf path = GIT_BUF_INIT;
1762 int fd, error = 0, flags;
1763
1764 if (git_buf_joinpath(&path, git_dir, file) < 0)
1765 return -1;
1766
1767 if (allow_overwrite)
1768 flags = O_WRONLY | O_CREAT | O_TRUNC;
1769 else
1770 flags = O_WRONLY | O_CREAT | O_EXCL;
1771
1772 fd = p_open(git_buf_cstr(&path), flags, mode);
1773
1774 if (fd >= 0) {
1775 error = p_write(fd, content, strlen(content));
1776
1777 p_close(fd);
1778 }
1779 else if (errno != EEXIST)
1780 error = fd;
1781
1782 #ifdef GIT_WIN32
1783 if (!error && hidden) {
1784 if (git_win32__set_hidden(path.ptr, true) < 0)
1785 error = -1;
1786 }
1787 #else
1788 GIT_UNUSED(hidden);
1789 #endif
1790
1791 git_buf_dispose(&path);
1792
1793 if (error)
1794 git_error_set(GIT_ERROR_OS,
1795 "failed to initialize repository with template '%s'", file);
1796
1797 return error;
1798 }
1799
repo_write_gitlink(const char * in_dir,const char * to_repo,bool use_relative_path)1800 static int repo_write_gitlink(
1801 const char *in_dir, const char *to_repo, bool use_relative_path)
1802 {
1803 int error;
1804 git_buf buf = GIT_BUF_INIT;
1805 git_buf path_to_repo = GIT_BUF_INIT;
1806 struct stat st;
1807
1808 git_path_dirname_r(&buf, to_repo);
1809 git_path_to_dir(&buf);
1810 if (git_buf_oom(&buf))
1811 return -1;
1812
1813 /* don't write gitlink to natural workdir */
1814 if (git__suffixcmp(to_repo, "/" DOT_GIT "/") == 0 &&
1815 strcmp(in_dir, buf.ptr) == 0)
1816 {
1817 error = GIT_PASSTHROUGH;
1818 goto cleanup;
1819 }
1820
1821 if ((error = git_buf_joinpath(&buf, in_dir, DOT_GIT)) < 0)
1822 goto cleanup;
1823
1824 if (!p_stat(buf.ptr, &st) && !S_ISREG(st.st_mode)) {
1825 git_error_set(GIT_ERROR_REPOSITORY,
1826 "cannot overwrite gitlink file into path '%s'", in_dir);
1827 error = GIT_EEXISTS;
1828 goto cleanup;
1829 }
1830
1831 git_buf_clear(&buf);
1832
1833 error = git_buf_sets(&path_to_repo, to_repo);
1834
1835 if (!error && use_relative_path)
1836 error = git_path_make_relative(&path_to_repo, in_dir);
1837
1838 if (!error)
1839 error = git_buf_join(&buf, ' ', GIT_FILE_CONTENT_PREFIX, path_to_repo.ptr);
1840
1841 if (!error)
1842 error = repo_write_template(in_dir, true, DOT_GIT, 0666, true, buf.ptr);
1843
1844 cleanup:
1845 git_buf_dispose(&buf);
1846 git_buf_dispose(&path_to_repo);
1847 return error;
1848 }
1849
pick_dir_mode(git_repository_init_options * opts)1850 static mode_t pick_dir_mode(git_repository_init_options *opts)
1851 {
1852 if (opts->mode == GIT_REPOSITORY_INIT_SHARED_UMASK)
1853 return 0777;
1854 if (opts->mode == GIT_REPOSITORY_INIT_SHARED_GROUP)
1855 return (0775 | S_ISGID);
1856 if (opts->mode == GIT_REPOSITORY_INIT_SHARED_ALL)
1857 return (0777 | S_ISGID);
1858 return opts->mode;
1859 }
1860
1861 #include "repo_template.h"
1862
repo_init_structure(const char * repo_dir,const char * work_dir,git_repository_init_options * opts)1863 static int repo_init_structure(
1864 const char *repo_dir,
1865 const char *work_dir,
1866 git_repository_init_options *opts)
1867 {
1868 int error = 0;
1869 repo_template_item *tpl;
1870 bool external_tpl =
1871 ((opts->flags & GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE) != 0);
1872 mode_t dmode = pick_dir_mode(opts);
1873 bool chmod = opts->mode != GIT_REPOSITORY_INIT_SHARED_UMASK;
1874
1875 /* Hide the ".git" directory */
1876 #ifdef GIT_WIN32
1877 if ((opts->flags & GIT_REPOSITORY_INIT__HAS_DOTGIT) != 0) {
1878 if (git_win32__set_hidden(repo_dir, true) < 0) {
1879 git_error_set(GIT_ERROR_OS,
1880 "failed to mark Git repository folder as hidden");
1881 return -1;
1882 }
1883 }
1884 #endif
1885
1886 /* Create the .git gitlink if appropriate */
1887 if ((opts->flags & GIT_REPOSITORY_INIT_BARE) == 0 &&
1888 (opts->flags & GIT_REPOSITORY_INIT__NATURAL_WD) == 0)
1889 {
1890 if (repo_write_gitlink(work_dir, repo_dir, opts->flags & GIT_REPOSITORY_INIT_RELATIVE_GITLINK) < 0)
1891 return -1;
1892 }
1893
1894 /* Copy external template if requested */
1895 if (external_tpl) {
1896 git_config *cfg = NULL;
1897 const char *tdir = NULL;
1898 bool default_template = false;
1899 git_buf template_buf = GIT_BUF_INIT;
1900
1901 if (opts->template_path)
1902 tdir = opts->template_path;
1903 else if ((error = git_config_open_default(&cfg)) >= 0) {
1904 if (!git_config_get_path(&template_buf, cfg, "init.templatedir"))
1905 tdir = template_buf.ptr;
1906 git_error_clear();
1907 }
1908
1909 if (!tdir) {
1910 if (!(error = git_sysdir_find_template_dir(&template_buf)))
1911 tdir = template_buf.ptr;
1912 default_template = true;
1913 }
1914
1915 /*
1916 * If tdir was the empty string, treat it like tdir was a path to an
1917 * empty directory (so, don't do any copying). This is the behavior
1918 * that git(1) exhibits, although it doesn't seem to be officially
1919 * documented.
1920 */
1921 if (tdir && git__strcmp(tdir, "") != 0) {
1922 uint32_t cpflags = GIT_CPDIR_COPY_SYMLINKS |
1923 GIT_CPDIR_SIMPLE_TO_MODE |
1924 GIT_CPDIR_COPY_DOTFILES;
1925 if (opts->mode != GIT_REPOSITORY_INIT_SHARED_UMASK)
1926 cpflags |= GIT_CPDIR_CHMOD_DIRS;
1927 error = git_futils_cp_r(tdir, repo_dir, cpflags, dmode);
1928 }
1929
1930 git_buf_dispose(&template_buf);
1931 git_config_free(cfg);
1932
1933 if (error < 0) {
1934 if (!default_template)
1935 return error;
1936
1937 /* if template was default, ignore error and use internal */
1938 git_error_clear();
1939 external_tpl = false;
1940 error = 0;
1941 }
1942 }
1943
1944 /* Copy internal template
1945 * - always ensure existence of dirs
1946 * - only create files if no external template was specified
1947 */
1948 for (tpl = repo_template; !error && tpl->path; ++tpl) {
1949 if (!tpl->content) {
1950 uint32_t mkdir_flags = GIT_MKDIR_PATH;
1951 if (chmod)
1952 mkdir_flags |= GIT_MKDIR_CHMOD;
1953
1954 error = git_futils_mkdir_relative(
1955 tpl->path, repo_dir, dmode, mkdir_flags, NULL);
1956 }
1957 else if (!external_tpl) {
1958 const char *content = tpl->content;
1959
1960 if (opts->description && strcmp(tpl->path, GIT_DESC_FILE) == 0)
1961 content = opts->description;
1962
1963 error = repo_write_template(
1964 repo_dir, false, tpl->path, tpl->mode, false, content);
1965 }
1966 }
1967
1968 return error;
1969 }
1970
mkdir_parent(git_buf * buf,uint32_t mode,bool skip2)1971 static int mkdir_parent(git_buf *buf, uint32_t mode, bool skip2)
1972 {
1973 /* When making parent directories during repository initialization
1974 * don't try to set gid or grant world write access
1975 */
1976 return git_futils_mkdir(
1977 buf->ptr, mode & ~(S_ISGID | 0002),
1978 GIT_MKDIR_PATH | GIT_MKDIR_VERIFY_DIR |
1979 (skip2 ? GIT_MKDIR_SKIP_LAST2 : GIT_MKDIR_SKIP_LAST));
1980 }
1981
repo_init_directories(git_buf * repo_path,git_buf * wd_path,const char * given_repo,git_repository_init_options * opts)1982 static int repo_init_directories(
1983 git_buf *repo_path,
1984 git_buf *wd_path,
1985 const char *given_repo,
1986 git_repository_init_options *opts)
1987 {
1988 int error = 0;
1989 bool is_bare, add_dotgit, has_dotgit, natural_wd;
1990 mode_t dirmode;
1991
1992 /* There are three possible rules for what we are allowed to create:
1993 * - MKPATH means anything we need
1994 * - MKDIR means just the .git directory and its parent and the workdir
1995 * - Neither means only the .git directory can be created
1996 *
1997 * There are 5 "segments" of path that we might need to deal with:
1998 * 1. The .git directory
1999 * 2. The parent of the .git directory
2000 * 3. Everything above the parent of the .git directory
2001 * 4. The working directory (often the same as #2)
2002 * 5. Everything above the working directory (often the same as #3)
2003 *
2004 * For all directories created, we start with the init_mode value for
2005 * permissions and then strip off bits in some cases:
2006 *
2007 * For MKPATH, we create #3 (and #5) paths without S_ISGID or S_IWOTH
2008 * For MKPATH and MKDIR, we create #2 (and #4) without S_ISGID
2009 * For all rules, we create #1 using the untouched init_mode
2010 */
2011
2012 /* set up repo path */
2013
2014 is_bare = ((opts->flags & GIT_REPOSITORY_INIT_BARE) != 0);
2015
2016 add_dotgit =
2017 (opts->flags & GIT_REPOSITORY_INIT_NO_DOTGIT_DIR) == 0 &&
2018 !is_bare &&
2019 git__suffixcmp(given_repo, "/" DOT_GIT) != 0 &&
2020 git__suffixcmp(given_repo, "/" GIT_DIR) != 0;
2021
2022 if (git_buf_joinpath(repo_path, given_repo, add_dotgit ? GIT_DIR : "") < 0)
2023 return -1;
2024
2025 has_dotgit = (git__suffixcmp(repo_path->ptr, "/" GIT_DIR) == 0);
2026 if (has_dotgit)
2027 opts->flags |= GIT_REPOSITORY_INIT__HAS_DOTGIT;
2028
2029 /* set up workdir path */
2030
2031 if (!is_bare) {
2032 if (opts->workdir_path) {
2033 if (git_path_join_unrooted(
2034 wd_path, opts->workdir_path, repo_path->ptr, NULL) < 0)
2035 return -1;
2036 } else if (has_dotgit) {
2037 if (git_path_dirname_r(wd_path, repo_path->ptr) < 0)
2038 return -1;
2039 } else {
2040 git_error_set(GIT_ERROR_REPOSITORY, "cannot pick working directory"
2041 " for non-bare repository that isn't a '.git' directory");
2042 return -1;
2043 }
2044
2045 if (git_path_to_dir(wd_path) < 0)
2046 return -1;
2047 } else {
2048 git_buf_clear(wd_path);
2049 }
2050
2051 natural_wd =
2052 has_dotgit &&
2053 wd_path->size > 0 &&
2054 wd_path->size + strlen(GIT_DIR) == repo_path->size &&
2055 memcmp(repo_path->ptr, wd_path->ptr, wd_path->size) == 0;
2056 if (natural_wd)
2057 opts->flags |= GIT_REPOSITORY_INIT__NATURAL_WD;
2058
2059 /* create directories as needed / requested */
2060
2061 dirmode = pick_dir_mode(opts);
2062
2063 if ((opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0) {
2064 /* create path #5 */
2065 if (wd_path->size > 0 &&
2066 (error = mkdir_parent(wd_path, dirmode, false)) < 0)
2067 return error;
2068
2069 /* create path #3 (if not the same as #5) */
2070 if (!natural_wd &&
2071 (error = mkdir_parent(repo_path, dirmode, has_dotgit)) < 0)
2072 return error;
2073 }
2074
2075 if ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0 ||
2076 (opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0)
2077 {
2078 /* create path #4 */
2079 if (wd_path->size > 0 &&
2080 (error = git_futils_mkdir(
2081 wd_path->ptr, dirmode & ~S_ISGID,
2082 GIT_MKDIR_VERIFY_DIR)) < 0)
2083 return error;
2084
2085 /* create path #2 (if not the same as #4) */
2086 if (!natural_wd &&
2087 (error = git_futils_mkdir(
2088 repo_path->ptr, dirmode & ~S_ISGID,
2089 GIT_MKDIR_VERIFY_DIR | GIT_MKDIR_SKIP_LAST)) < 0)
2090 return error;
2091 }
2092
2093 if ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0 ||
2094 (opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0 ||
2095 has_dotgit)
2096 {
2097 /* create path #1 */
2098 error = git_futils_mkdir(repo_path->ptr, dirmode,
2099 GIT_MKDIR_VERIFY_DIR | ((dirmode & S_ISGID) ? GIT_MKDIR_CHMOD : 0));
2100 }
2101
2102 /* prettify both directories now that they are created */
2103
2104 if (!error) {
2105 error = git_path_prettify_dir(repo_path, repo_path->ptr, NULL);
2106
2107 if (!error && wd_path->size > 0)
2108 error = git_path_prettify_dir(wd_path, wd_path->ptr, NULL);
2109 }
2110
2111 return error;
2112 }
2113
repo_init_head(const char * repo_dir,const char * given)2114 static int repo_init_head(const char *repo_dir, const char *given)
2115 {
2116 git_config *cfg = NULL;
2117 git_buf head_path = GIT_BUF_INIT, cfg_branch = GIT_BUF_INIT;
2118 const char *initial_head = NULL;
2119 int error;
2120
2121 if ((error = git_buf_joinpath(&head_path, repo_dir, GIT_HEAD_FILE)) < 0)
2122 goto out;
2123
2124 /*
2125 * A template may have set a HEAD; use that unless it's been
2126 * overridden by the caller's given initial head setting.
2127 */
2128 if (git_path_exists(head_path.ptr) && !given)
2129 goto out;
2130
2131 if (given) {
2132 initial_head = given;
2133 } else if ((error = git_config_open_default(&cfg)) >= 0 &&
2134 (error = git_config_get_string_buf(&cfg_branch, cfg, "init.defaultbranch")) >= 0 &&
2135 *cfg_branch.ptr) {
2136 initial_head = cfg_branch.ptr;
2137 }
2138
2139 if (!initial_head)
2140 initial_head = GIT_BRANCH_DEFAULT;
2141
2142 error = git_repository_create_head(repo_dir, initial_head);
2143
2144 out:
2145 git_config_free(cfg);
2146 git_buf_dispose(&head_path);
2147 git_buf_dispose(&cfg_branch);
2148
2149 return error;
2150 }
2151
repo_init_create_origin(git_repository * repo,const char * url)2152 static int repo_init_create_origin(git_repository *repo, const char *url)
2153 {
2154 int error;
2155 git_remote *remote;
2156
2157 if (!(error = git_remote_create(&remote, repo, GIT_REMOTE_ORIGIN, url))) {
2158 git_remote_free(remote);
2159 }
2160
2161 return error;
2162 }
2163
git_repository_init(git_repository ** repo_out,const char * path,unsigned is_bare)2164 int git_repository_init(
2165 git_repository **repo_out, const char *path, unsigned is_bare)
2166 {
2167 git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
2168
2169 opts.flags = GIT_REPOSITORY_INIT_MKPATH; /* don't love this default */
2170 if (is_bare)
2171 opts.flags |= GIT_REPOSITORY_INIT_BARE;
2172
2173 return git_repository_init_ext(repo_out, path, &opts);
2174 }
2175
git_repository_init_ext(git_repository ** out,const char * given_repo,git_repository_init_options * opts)2176 int git_repository_init_ext(
2177 git_repository **out,
2178 const char *given_repo,
2179 git_repository_init_options *opts)
2180 {
2181 git_buf repo_path = GIT_BUF_INIT, wd_path = GIT_BUF_INIT,
2182 common_path = GIT_BUF_INIT;
2183 const char *wd;
2184 bool is_valid;
2185 int error;
2186
2187 GIT_ASSERT_ARG(out);
2188 GIT_ASSERT_ARG(given_repo);
2189 GIT_ASSERT_ARG(opts);
2190
2191 GIT_ERROR_CHECK_VERSION(opts, GIT_REPOSITORY_INIT_OPTIONS_VERSION, "git_repository_init_options");
2192
2193 if ((error = repo_init_directories(&repo_path, &wd_path, given_repo, opts)) < 0)
2194 goto out;
2195
2196 wd = (opts->flags & GIT_REPOSITORY_INIT_BARE) ? NULL : git_buf_cstr(&wd_path);
2197
2198 if ((error = is_valid_repository_path(&is_valid, &repo_path, &common_path)) < 0)
2199 goto out;
2200
2201 if (is_valid) {
2202 if ((opts->flags & GIT_REPOSITORY_INIT_NO_REINIT) != 0) {
2203 git_error_set(GIT_ERROR_REPOSITORY,
2204 "attempt to reinitialize '%s'", given_repo);
2205 error = GIT_EEXISTS;
2206 goto out;
2207 }
2208
2209 opts->flags |= GIT_REPOSITORY_INIT__IS_REINIT;
2210
2211 if ((error = repo_init_config(repo_path.ptr, wd, opts->flags, opts->mode)) < 0)
2212 goto out;
2213
2214 /* TODO: reinitialize the templates */
2215 } else {
2216 if ((error = repo_init_structure(repo_path.ptr, wd, opts)) < 0 ||
2217 (error = repo_init_config(repo_path.ptr, wd, opts->flags, opts->mode)) < 0 ||
2218 (error = repo_init_head(repo_path.ptr, opts->initial_head)) < 0)
2219 goto out;
2220 }
2221
2222 if ((error = git_repository_open(out, repo_path.ptr)) < 0)
2223 goto out;
2224
2225 if (opts->origin_url &&
2226 (error = repo_init_create_origin(*out, opts->origin_url)) < 0)
2227 goto out;
2228
2229 out:
2230 git_buf_dispose(&common_path);
2231 git_buf_dispose(&repo_path);
2232 git_buf_dispose(&wd_path);
2233
2234 return error;
2235 }
2236
git_repository_head_detached(git_repository * repo)2237 int git_repository_head_detached(git_repository *repo)
2238 {
2239 git_reference *ref;
2240 git_odb *odb = NULL;
2241 int exists;
2242
2243 if (git_repository_odb__weakptr(&odb, repo) < 0)
2244 return -1;
2245
2246 if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0)
2247 return -1;
2248
2249 if (git_reference_type(ref) == GIT_REFERENCE_SYMBOLIC) {
2250 git_reference_free(ref);
2251 return 0;
2252 }
2253
2254 exists = git_odb_exists(odb, git_reference_target(ref));
2255
2256 git_reference_free(ref);
2257 return exists;
2258 }
2259
git_repository_head_detached_for_worktree(git_repository * repo,const char * name)2260 int git_repository_head_detached_for_worktree(git_repository *repo, const char *name)
2261 {
2262 git_reference *ref = NULL;
2263 int error;
2264
2265 GIT_ASSERT_ARG(repo);
2266 GIT_ASSERT_ARG(name);
2267
2268 if ((error = git_repository_head_for_worktree(&ref, repo, name)) < 0)
2269 goto out;
2270
2271 error = (git_reference_type(ref) != GIT_REFERENCE_SYMBOLIC);
2272 out:
2273 git_reference_free(ref);
2274
2275 return error;
2276 }
2277
git_repository_head(git_reference ** head_out,git_repository * repo)2278 int git_repository_head(git_reference **head_out, git_repository *repo)
2279 {
2280 git_reference *head;
2281 int error;
2282
2283 GIT_ASSERT_ARG(head_out);
2284
2285 if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0)
2286 return error;
2287
2288 if (git_reference_type(head) == GIT_REFERENCE_DIRECT) {
2289 *head_out = head;
2290 return 0;
2291 }
2292
2293 error = git_reference_lookup_resolved(head_out, repo, git_reference_symbolic_target(head), -1);
2294 git_reference_free(head);
2295
2296 return error == GIT_ENOTFOUND ? GIT_EUNBORNBRANCH : error;
2297 }
2298
git_repository_head_for_worktree(git_reference ** out,git_repository * repo,const char * name)2299 int git_repository_head_for_worktree(git_reference **out, git_repository *repo, const char *name)
2300 {
2301 git_repository *worktree_repo = NULL;
2302 git_worktree *worktree = NULL;
2303 git_reference *head = NULL;
2304 int error;
2305
2306 GIT_ASSERT_ARG(out);
2307 GIT_ASSERT_ARG(repo);
2308 GIT_ASSERT_ARG(name);
2309
2310 *out = NULL;
2311
2312 if ((error = git_worktree_lookup(&worktree, repo, name)) < 0 ||
2313 (error = git_repository_open_from_worktree(&worktree_repo, worktree)) < 0 ||
2314 (error = git_reference_lookup(&head, worktree_repo, GIT_HEAD_FILE)) < 0)
2315 goto out;
2316
2317 if (git_reference_type(head) != GIT_REFERENCE_DIRECT) {
2318 if ((error = git_reference_lookup_resolved(out, worktree_repo, git_reference_symbolic_target(head), -1)) < 0)
2319 goto out;
2320 } else {
2321 *out = head;
2322 head = NULL;
2323 }
2324
2325 out:
2326 git_reference_free(head);
2327 git_worktree_free(worktree);
2328 git_repository_free(worktree_repo);
2329 return error;
2330 }
2331
git_repository_foreach_worktree(git_repository * repo,git_repository_foreach_worktree_cb cb,void * payload)2332 int git_repository_foreach_worktree(git_repository *repo,
2333 git_repository_foreach_worktree_cb cb,
2334 void *payload)
2335 {
2336 git_strarray worktrees = {0};
2337 git_repository *worktree_repo = NULL;
2338 git_worktree *worktree = NULL;
2339 int error;
2340 size_t i;
2341
2342 if ((error = git_repository_open(&worktree_repo, repo->commondir)) < 0 ||
2343 (error = cb(worktree_repo, payload) != 0))
2344 goto out;
2345
2346 git_repository_free(worktree_repo);
2347 worktree_repo = NULL;
2348
2349 if ((error = git_worktree_list(&worktrees, repo)) < 0)
2350 goto out;
2351
2352 for (i = 0; i < worktrees.count; i++) {
2353 git_repository_free(worktree_repo);
2354 worktree_repo = NULL;
2355 git_worktree_free(worktree);
2356 worktree = NULL;
2357
2358 if ((error = git_worktree_lookup(&worktree, repo, worktrees.strings[i]) < 0) ||
2359 (error = git_repository_open_from_worktree(&worktree_repo, worktree)) < 0) {
2360 if (error != GIT_ENOTFOUND)
2361 goto out;
2362 error = 0;
2363 continue;
2364 }
2365
2366 if ((error = cb(worktree_repo, payload)) != 0)
2367 goto out;
2368 }
2369
2370 out:
2371 git_strarray_dispose(&worktrees);
2372 git_repository_free(worktree_repo);
2373 git_worktree_free(worktree);
2374 return error;
2375 }
2376
git_repository_head_unborn(git_repository * repo)2377 int git_repository_head_unborn(git_repository *repo)
2378 {
2379 git_reference *ref = NULL;
2380 int error;
2381
2382 error = git_repository_head(&ref, repo);
2383 git_reference_free(ref);
2384
2385 if (error == GIT_EUNBORNBRANCH) {
2386 git_error_clear();
2387 return 1;
2388 }
2389
2390 if (error < 0)
2391 return -1;
2392
2393 return 0;
2394 }
2395
repo_contains_no_reference(git_repository * repo)2396 static int repo_contains_no_reference(git_repository *repo)
2397 {
2398 git_reference_iterator *iter;
2399 const char *refname;
2400 int error;
2401
2402 if ((error = git_reference_iterator_new(&iter, repo)) < 0)
2403 return error;
2404
2405 error = git_reference_next_name(&refname, iter);
2406 git_reference_iterator_free(iter);
2407
2408 if (error == GIT_ITEROVER)
2409 return 1;
2410
2411 return error;
2412 }
2413
git_repository_initialbranch(git_buf * out,git_repository * repo)2414 int git_repository_initialbranch(git_buf *out, git_repository *repo)
2415 {
2416 git_config *config;
2417 git_config_entry *entry = NULL;
2418 const char *branch;
2419 int valid, error;
2420
2421 if ((error = git_repository_config__weakptr(&config, repo)) < 0)
2422 return error;
2423
2424 if ((error = git_config_get_entry(&entry, config, "init.defaultbranch")) == 0 &&
2425 *entry->value) {
2426 branch = entry->value;
2427 }
2428 else if (!error || error == GIT_ENOTFOUND) {
2429 branch = GIT_BRANCH_DEFAULT;
2430 }
2431 else {
2432 goto done;
2433 }
2434
2435 if ((error = git_buf_puts(out, GIT_REFS_HEADS_DIR)) < 0 ||
2436 (error = git_buf_puts(out, branch)) < 0 ||
2437 (error = git_reference_name_is_valid(&valid, out->ptr)) < 0)
2438 goto done;
2439
2440 if (!valid) {
2441 git_error_set(GIT_ERROR_INVALID, "the value of init.defaultBranch is not a valid branch name");
2442 error = -1;
2443 }
2444
2445 done:
2446 git_config_entry_free(entry);
2447 return error;
2448 }
2449
git_repository_is_empty(git_repository * repo)2450 int git_repository_is_empty(git_repository *repo)
2451 {
2452 git_reference *head = NULL;
2453 git_buf initialbranch = GIT_BUF_INIT;
2454 int result = 0;
2455
2456 if ((result = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0 ||
2457 (result = git_repository_initialbranch(&initialbranch, repo)) < 0)
2458 goto done;
2459
2460 result = (git_reference_type(head) == GIT_REFERENCE_SYMBOLIC &&
2461 strcmp(git_reference_symbolic_target(head), initialbranch.ptr) == 0 &&
2462 repo_contains_no_reference(repo));
2463
2464 done:
2465 git_reference_free(head);
2466 git_buf_dispose(&initialbranch);
2467
2468 return result;
2469 }
2470
resolved_parent_path(const git_repository * repo,git_repository_item_t item,git_repository_item_t fallback)2471 static const char *resolved_parent_path(const git_repository *repo, git_repository_item_t item, git_repository_item_t fallback)
2472 {
2473 const char *parent;
2474
2475 switch (item) {
2476 case GIT_REPOSITORY_ITEM_GITDIR:
2477 parent = git_repository_path(repo);
2478 break;
2479 case GIT_REPOSITORY_ITEM_WORKDIR:
2480 parent = git_repository_workdir(repo);
2481 break;
2482 case GIT_REPOSITORY_ITEM_COMMONDIR:
2483 parent = git_repository_commondir(repo);
2484 break;
2485 default:
2486 git_error_set(GIT_ERROR_INVALID, "invalid item directory");
2487 return NULL;
2488 }
2489 if (!parent && fallback != GIT_REPOSITORY_ITEM__LAST)
2490 return resolved_parent_path(repo, fallback, GIT_REPOSITORY_ITEM__LAST);
2491
2492 return parent;
2493 }
2494
git_repository_item_path(git_buf * out,const git_repository * repo,git_repository_item_t item)2495 int git_repository_item_path(git_buf *out, const git_repository *repo, git_repository_item_t item)
2496 {
2497 const char *parent = resolved_parent_path(repo, items[item].parent, items[item].fallback);
2498 if (parent == NULL) {
2499 git_error_set(GIT_ERROR_INVALID, "path cannot exist in repository");
2500 return GIT_ENOTFOUND;
2501 }
2502
2503 if (git_buf_sets(out, parent) < 0)
2504 return -1;
2505
2506 if (items[item].name) {
2507 if (git_buf_joinpath(out, parent, items[item].name) < 0)
2508 return -1;
2509 }
2510
2511 if (items[item].directory) {
2512 if (git_path_to_dir(out) < 0)
2513 return -1;
2514 }
2515
2516 return 0;
2517 }
2518
git_repository_path(const git_repository * repo)2519 const char *git_repository_path(const git_repository *repo)
2520 {
2521 GIT_ASSERT_ARG_WITH_RETVAL(repo, NULL);
2522 return repo->gitdir;
2523 }
2524
git_repository_workdir(const git_repository * repo)2525 const char *git_repository_workdir(const git_repository *repo)
2526 {
2527 GIT_ASSERT_ARG_WITH_RETVAL(repo, NULL);
2528
2529 if (repo->is_bare)
2530 return NULL;
2531
2532 return repo->workdir;
2533 }
2534
git_repository_workdir_path(git_buf * out,git_repository * repo,const char * path)2535 int git_repository_workdir_path(
2536 git_buf *out, git_repository *repo, const char *path)
2537 {
2538 int error;
2539
2540 if (!repo->workdir) {
2541 git_error_set(GIT_ERROR_REPOSITORY, "repository has no working directory");
2542 return GIT_EBAREREPO;
2543 }
2544
2545 if (!(error = git_buf_joinpath(out, repo->workdir, path)))
2546 error = git_path_validate_workdir_buf(repo, out);
2547
2548 return error;
2549 }
2550
git_repository_commondir(const git_repository * repo)2551 const char *git_repository_commondir(const git_repository *repo)
2552 {
2553 GIT_ASSERT_ARG_WITH_RETVAL(repo, NULL);
2554 return repo->commondir;
2555 }
2556
git_repository_set_workdir(git_repository * repo,const char * workdir,int update_gitlink)2557 int git_repository_set_workdir(
2558 git_repository *repo, const char *workdir, int update_gitlink)
2559 {
2560 int error = 0;
2561 git_buf path = GIT_BUF_INIT;
2562
2563 GIT_ASSERT_ARG(repo);
2564 GIT_ASSERT_ARG(workdir);
2565
2566 if (git_path_prettify_dir(&path, workdir, NULL) < 0)
2567 return -1;
2568
2569 if (repo->workdir && strcmp(repo->workdir, path.ptr) == 0)
2570 return 0;
2571
2572 if (update_gitlink) {
2573 git_config *config;
2574
2575 if (git_repository_config__weakptr(&config, repo) < 0)
2576 return -1;
2577
2578 error = repo_write_gitlink(path.ptr, git_repository_path(repo), false);
2579
2580 /* passthrough error means gitlink is unnecessary */
2581 if (error == GIT_PASSTHROUGH)
2582 error = git_config_delete_entry(config, "core.worktree");
2583 else if (!error)
2584 error = git_config_set_string(config, "core.worktree", path.ptr);
2585
2586 if (!error)
2587 error = git_config_set_bool(config, "core.bare", false);
2588 }
2589
2590 if (!error) {
2591 char *old_workdir = repo->workdir;
2592
2593 repo->workdir = git_buf_detach(&path);
2594 repo->is_bare = 0;
2595
2596 git__free(old_workdir);
2597 }
2598
2599 return error;
2600 }
2601
git_repository_is_bare(const git_repository * repo)2602 int git_repository_is_bare(const git_repository *repo)
2603 {
2604 GIT_ASSERT_ARG(repo);
2605 return repo->is_bare;
2606 }
2607
git_repository_is_worktree(const git_repository * repo)2608 int git_repository_is_worktree(const git_repository *repo)
2609 {
2610 GIT_ASSERT_ARG(repo);
2611 return repo->is_worktree;
2612 }
2613
git_repository_set_bare(git_repository * repo)2614 int git_repository_set_bare(git_repository *repo)
2615 {
2616 int error;
2617 git_config *config;
2618
2619 GIT_ASSERT_ARG(repo);
2620
2621 if (repo->is_bare)
2622 return 0;
2623
2624 if ((error = git_repository_config__weakptr(&config, repo)) < 0)
2625 return error;
2626
2627 if ((error = git_config_set_bool(config, "core.bare", true)) < 0)
2628 return error;
2629
2630 if ((error = git_config__update_entry(config, "core.worktree", NULL, true, true)) < 0)
2631 return error;
2632
2633 git__free(repo->workdir);
2634 repo->workdir = NULL;
2635 repo->is_bare = 1;
2636
2637 return 0;
2638 }
2639
git_repository_head_tree(git_tree ** tree,git_repository * repo)2640 int git_repository_head_tree(git_tree **tree, git_repository *repo)
2641 {
2642 git_reference *head;
2643 git_object *obj;
2644 int error;
2645
2646 if ((error = git_repository_head(&head, repo)) < 0)
2647 return error;
2648
2649 if ((error = git_reference_peel(&obj, head, GIT_OBJECT_TREE)) < 0)
2650 goto cleanup;
2651
2652 *tree = (git_tree *)obj;
2653
2654 cleanup:
2655 git_reference_free(head);
2656 return error;
2657 }
2658
git_repository__set_orig_head(git_repository * repo,const git_oid * orig_head)2659 int git_repository__set_orig_head(git_repository *repo, const git_oid *orig_head)
2660 {
2661 git_filebuf file = GIT_FILEBUF_INIT;
2662 git_buf file_path = GIT_BUF_INIT;
2663 char orig_head_str[GIT_OID_HEXSZ];
2664 int error = 0;
2665
2666 git_oid_fmt(orig_head_str, orig_head);
2667
2668 if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_ORIG_HEAD_FILE)) == 0 &&
2669 (error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_MERGE_FILE_MODE)) == 0 &&
2670 (error = git_filebuf_printf(&file, "%.*s\n", GIT_OID_HEXSZ, orig_head_str)) == 0)
2671 error = git_filebuf_commit(&file);
2672
2673 if (error < 0)
2674 git_filebuf_cleanup(&file);
2675
2676 git_buf_dispose(&file_path);
2677
2678 return error;
2679 }
2680
git_repository_message(git_buf * out,git_repository * repo)2681 int git_repository_message(git_buf *out, git_repository *repo)
2682 {
2683 git_buf path = GIT_BUF_INIT;
2684 struct stat st;
2685 int error;
2686
2687 if ((error = git_buf_sanitize(out)) < 0)
2688 return error;
2689
2690 if (git_buf_joinpath(&path, repo->gitdir, GIT_MERGE_MSG_FILE) < 0)
2691 return -1;
2692
2693 if ((error = p_stat(git_buf_cstr(&path), &st)) < 0) {
2694 if (errno == ENOENT)
2695 error = GIT_ENOTFOUND;
2696 git_error_set(GIT_ERROR_OS, "could not access message file");
2697 } else {
2698 error = git_futils_readbuffer(out, git_buf_cstr(&path));
2699 }
2700
2701 git_buf_dispose(&path);
2702
2703 return error;
2704 }
2705
git_repository_message_remove(git_repository * repo)2706 int git_repository_message_remove(git_repository *repo)
2707 {
2708 git_buf path = GIT_BUF_INIT;
2709 int error;
2710
2711 if (git_buf_joinpath(&path, repo->gitdir, GIT_MERGE_MSG_FILE) < 0)
2712 return -1;
2713
2714 error = p_unlink(git_buf_cstr(&path));
2715 git_buf_dispose(&path);
2716
2717 return error;
2718 }
2719
git_repository_hashfile(git_oid * out,git_repository * repo,const char * path,git_object_t type,const char * as_path)2720 int git_repository_hashfile(
2721 git_oid *out,
2722 git_repository *repo,
2723 const char *path,
2724 git_object_t type,
2725 const char *as_path)
2726 {
2727 int error;
2728 git_filter_list *fl = NULL;
2729 git_file fd = -1;
2730 uint64_t len;
2731 git_buf full_path = GIT_BUF_INIT;
2732
2733 /* as_path can be NULL */
2734 GIT_ASSERT_ARG(out);
2735 GIT_ASSERT_ARG(path);
2736 GIT_ASSERT_ARG(repo);
2737
2738 /* At some point, it would be nice if repo could be NULL to just
2739 * apply filter rules defined in system and global files, but for
2740 * now that is not possible because git_filters_load() needs it.
2741 */
2742
2743 if ((error = git_path_join_unrooted(
2744 &full_path, path, git_repository_workdir(repo), NULL)) < 0 ||
2745 (error = git_path_validate_workdir_buf(repo, &full_path)) < 0)
2746 return error;
2747
2748 if (!as_path)
2749 as_path = path;
2750
2751 /* passing empty string for "as_path" indicated --no-filters */
2752 if (strlen(as_path) > 0) {
2753 error = git_filter_list_load(
2754 &fl, repo, NULL, as_path,
2755 GIT_FILTER_TO_ODB, GIT_FILTER_DEFAULT);
2756 if (error < 0)
2757 return error;
2758 } else {
2759 error = 0;
2760 }
2761
2762 /* at this point, error is a count of the number of loaded filters */
2763
2764 fd = git_futils_open_ro(full_path.ptr);
2765 if (fd < 0) {
2766 error = fd;
2767 goto cleanup;
2768 }
2769
2770 if ((error = git_futils_filesize(&len, fd)) < 0)
2771 goto cleanup;
2772
2773 if (!git__is_sizet(len)) {
2774 git_error_set(GIT_ERROR_OS, "file size overflow for 32-bit systems");
2775 error = -1;
2776 goto cleanup;
2777 }
2778
2779 error = git_odb__hashfd_filtered(out, fd, (size_t)len, type, fl);
2780
2781 cleanup:
2782 if (fd >= 0)
2783 p_close(fd);
2784 git_filter_list_free(fl);
2785 git_buf_dispose(&full_path);
2786
2787 return error;
2788 }
2789
checkout_message(git_buf * out,git_reference * old,const char * new)2790 static int checkout_message(git_buf *out, git_reference *old, const char *new)
2791 {
2792 git_buf_puts(out, "checkout: moving from ");
2793
2794 if (git_reference_type(old) == GIT_REFERENCE_SYMBOLIC)
2795 git_buf_puts(out, git_reference__shorthand(git_reference_symbolic_target(old)));
2796 else
2797 git_buf_puts(out, git_oid_tostr_s(git_reference_target(old)));
2798
2799 git_buf_puts(out, " to ");
2800
2801 if (git_reference__is_branch(new) ||
2802 git_reference__is_tag(new) ||
2803 git_reference__is_remote(new))
2804 git_buf_puts(out, git_reference__shorthand(new));
2805 else
2806 git_buf_puts(out, new);
2807
2808 if (git_buf_oom(out))
2809 return -1;
2810
2811 return 0;
2812 }
2813
detach(git_repository * repo,const git_oid * id,const char * new)2814 static int detach(git_repository *repo, const git_oid *id, const char *new)
2815 {
2816 int error;
2817 git_buf log_message = GIT_BUF_INIT;
2818 git_object *object = NULL, *peeled = NULL;
2819 git_reference *new_head = NULL, *current = NULL;
2820
2821 GIT_ASSERT_ARG(repo);
2822 GIT_ASSERT_ARG(id);
2823
2824 if ((error = git_reference_lookup(¤t, repo, GIT_HEAD_FILE)) < 0)
2825 return error;
2826
2827 if ((error = git_object_lookup(&object, repo, id, GIT_OBJECT_ANY)) < 0)
2828 goto cleanup;
2829
2830 if ((error = git_object_peel(&peeled, object, GIT_OBJECT_COMMIT)) < 0)
2831 goto cleanup;
2832
2833 if (new == NULL)
2834 new = git_oid_tostr_s(git_object_id(peeled));
2835
2836 if ((error = checkout_message(&log_message, current, new)) < 0)
2837 goto cleanup;
2838
2839 error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_object_id(peeled), true, git_buf_cstr(&log_message));
2840
2841 cleanup:
2842 git_buf_dispose(&log_message);
2843 git_object_free(object);
2844 git_object_free(peeled);
2845 git_reference_free(current);
2846 git_reference_free(new_head);
2847 return error;
2848 }
2849
git_repository_set_head(git_repository * repo,const char * refname)2850 int git_repository_set_head(
2851 git_repository* repo,
2852 const char* refname)
2853 {
2854 git_reference *ref = NULL, *current = NULL, *new_head = NULL;
2855 git_buf log_message = GIT_BUF_INIT;
2856 int error;
2857
2858 GIT_ASSERT_ARG(repo);
2859 GIT_ASSERT_ARG(refname);
2860
2861 if ((error = git_reference_lookup(¤t, repo, GIT_HEAD_FILE)) < 0)
2862 return error;
2863
2864 if ((error = checkout_message(&log_message, current, refname)) < 0)
2865 goto cleanup;
2866
2867 error = git_reference_lookup(&ref, repo, refname);
2868 if (error < 0 && error != GIT_ENOTFOUND)
2869 goto cleanup;
2870
2871 if (ref && current->type == GIT_REFERENCE_SYMBOLIC && git__strcmp(current->target.symbolic, ref->name) &&
2872 git_reference_is_branch(ref) && git_branch_is_checked_out(ref)) {
2873 git_error_set(GIT_ERROR_REPOSITORY, "cannot set HEAD to reference '%s' as it is the current HEAD "
2874 "of a linked repository.", git_reference_name(ref));
2875 error = -1;
2876 goto cleanup;
2877 }
2878
2879 if (!error) {
2880 if (git_reference_is_branch(ref)) {
2881 error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE,
2882 git_reference_name(ref), true, git_buf_cstr(&log_message));
2883 } else {
2884 error = detach(repo, git_reference_target(ref),
2885 git_reference_is_tag(ref) || git_reference_is_remote(ref) ? refname : NULL);
2886 }
2887 } else if (git_reference__is_branch(refname)) {
2888 error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, refname,
2889 true, git_buf_cstr(&log_message));
2890 }
2891
2892 cleanup:
2893 git_buf_dispose(&log_message);
2894 git_reference_free(current);
2895 git_reference_free(ref);
2896 git_reference_free(new_head);
2897 return error;
2898 }
2899
git_repository_set_head_detached(git_repository * repo,const git_oid * commitish)2900 int git_repository_set_head_detached(
2901 git_repository* repo,
2902 const git_oid* commitish)
2903 {
2904 return detach(repo, commitish, NULL);
2905 }
2906
git_repository_set_head_detached_from_annotated(git_repository * repo,const git_annotated_commit * commitish)2907 int git_repository_set_head_detached_from_annotated(
2908 git_repository *repo,
2909 const git_annotated_commit *commitish)
2910 {
2911 GIT_ASSERT_ARG(repo);
2912 GIT_ASSERT_ARG(commitish);
2913
2914 return detach(repo, git_annotated_commit_id(commitish), commitish->description);
2915 }
2916
git_repository_detach_head(git_repository * repo)2917 int git_repository_detach_head(git_repository* repo)
2918 {
2919 git_reference *old_head = NULL, *new_head = NULL, *current = NULL;
2920 git_object *object = NULL;
2921 git_buf log_message = GIT_BUF_INIT;
2922 int error;
2923
2924 GIT_ASSERT_ARG(repo);
2925
2926 if ((error = git_reference_lookup(¤t, repo, GIT_HEAD_FILE)) < 0)
2927 return error;
2928
2929 if ((error = git_repository_head(&old_head, repo)) < 0)
2930 goto cleanup;
2931
2932 if ((error = git_object_lookup(&object, repo, git_reference_target(old_head), GIT_OBJECT_COMMIT)) < 0)
2933 goto cleanup;
2934
2935 if ((error = checkout_message(&log_message, current, git_oid_tostr_s(git_object_id(object)))) < 0)
2936 goto cleanup;
2937
2938 error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_reference_target(old_head),
2939 1, git_buf_cstr(&log_message));
2940
2941 cleanup:
2942 git_buf_dispose(&log_message);
2943 git_object_free(object);
2944 git_reference_free(old_head);
2945 git_reference_free(new_head);
2946 git_reference_free(current);
2947 return error;
2948 }
2949
2950 /**
2951 * Loosely ported from git.git
2952 * https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh#L198-289
2953 */
git_repository_state(git_repository * repo)2954 int git_repository_state(git_repository *repo)
2955 {
2956 git_buf repo_path = GIT_BUF_INIT;
2957 int state = GIT_REPOSITORY_STATE_NONE;
2958
2959 GIT_ASSERT_ARG(repo);
2960
2961 if (git_buf_puts(&repo_path, repo->gitdir) < 0)
2962 return -1;
2963
2964 if (git_path_contains_file(&repo_path, GIT_REBASE_MERGE_INTERACTIVE_FILE))
2965 state = GIT_REPOSITORY_STATE_REBASE_INTERACTIVE;
2966 else if (git_path_contains_dir(&repo_path, GIT_REBASE_MERGE_DIR))
2967 state = GIT_REPOSITORY_STATE_REBASE_MERGE;
2968 else if (git_path_contains_file(&repo_path, GIT_REBASE_APPLY_REBASING_FILE))
2969 state = GIT_REPOSITORY_STATE_REBASE;
2970 else if (git_path_contains_file(&repo_path, GIT_REBASE_APPLY_APPLYING_FILE))
2971 state = GIT_REPOSITORY_STATE_APPLY_MAILBOX;
2972 else if (git_path_contains_dir(&repo_path, GIT_REBASE_APPLY_DIR))
2973 state = GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE;
2974 else if (git_path_contains_file(&repo_path, GIT_MERGE_HEAD_FILE))
2975 state = GIT_REPOSITORY_STATE_MERGE;
2976 else if (git_path_contains_file(&repo_path, GIT_REVERT_HEAD_FILE)) {
2977 state = GIT_REPOSITORY_STATE_REVERT;
2978 if (git_path_contains_file(&repo_path, GIT_SEQUENCER_TODO_FILE)) {
2979 state = GIT_REPOSITORY_STATE_REVERT_SEQUENCE;
2980 }
2981 } else if (git_path_contains_file(&repo_path, GIT_CHERRYPICK_HEAD_FILE)) {
2982 state = GIT_REPOSITORY_STATE_CHERRYPICK;
2983 if (git_path_contains_file(&repo_path, GIT_SEQUENCER_TODO_FILE)) {
2984 state = GIT_REPOSITORY_STATE_CHERRYPICK_SEQUENCE;
2985 }
2986 } else if (git_path_contains_file(&repo_path, GIT_BISECT_LOG_FILE))
2987 state = GIT_REPOSITORY_STATE_BISECT;
2988
2989 git_buf_dispose(&repo_path);
2990 return state;
2991 }
2992
git_repository__cleanup_files(git_repository * repo,const char * files[],size_t files_len)2993 int git_repository__cleanup_files(
2994 git_repository *repo, const char *files[], size_t files_len)
2995 {
2996 git_buf buf = GIT_BUF_INIT;
2997 size_t i;
2998 int error;
2999
3000 for (error = 0, i = 0; !error && i < files_len; ++i) {
3001 const char *path;
3002
3003 if (git_buf_joinpath(&buf, repo->gitdir, files[i]) < 0)
3004 return -1;
3005
3006 path = git_buf_cstr(&buf);
3007
3008 if (git_path_isfile(path)) {
3009 error = p_unlink(path);
3010 } else if (git_path_isdir(path)) {
3011 error = git_futils_rmdir_r(path, NULL,
3012 GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS);
3013 }
3014
3015 git_buf_clear(&buf);
3016 }
3017
3018 git_buf_dispose(&buf);
3019 return error;
3020 }
3021
3022 static const char *state_files[] = {
3023 GIT_MERGE_HEAD_FILE,
3024 GIT_MERGE_MODE_FILE,
3025 GIT_MERGE_MSG_FILE,
3026 GIT_REVERT_HEAD_FILE,
3027 GIT_CHERRYPICK_HEAD_FILE,
3028 GIT_BISECT_LOG_FILE,
3029 GIT_REBASE_MERGE_DIR,
3030 GIT_REBASE_APPLY_DIR,
3031 GIT_SEQUENCER_DIR,
3032 };
3033
git_repository_state_cleanup(git_repository * repo)3034 int git_repository_state_cleanup(git_repository *repo)
3035 {
3036 GIT_ASSERT_ARG(repo);
3037
3038 return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files));
3039 }
3040
git_repository_is_shallow(git_repository * repo)3041 int git_repository_is_shallow(git_repository *repo)
3042 {
3043 git_buf path = GIT_BUF_INIT;
3044 struct stat st;
3045 int error;
3046
3047 if ((error = git_buf_joinpath(&path, repo->gitdir, "shallow")) < 0)
3048 return error;
3049
3050 error = git_path_lstat(path.ptr, &st);
3051 git_buf_dispose(&path);
3052
3053 if (error == GIT_ENOTFOUND) {
3054 git_error_clear();
3055 return 0;
3056 }
3057
3058 if (error < 0)
3059 return error;
3060 return st.st_size == 0 ? 0 : 1;
3061 }
3062
git_repository_init_options_init(git_repository_init_options * opts,unsigned int version)3063 int git_repository_init_options_init(
3064 git_repository_init_options *opts, unsigned int version)
3065 {
3066 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
3067 opts, version, git_repository_init_options,
3068 GIT_REPOSITORY_INIT_OPTIONS_INIT);
3069 return 0;
3070 }
3071
3072 #ifndef GIT_DEPRECATE_HARD
git_repository_init_init_options(git_repository_init_options * opts,unsigned int version)3073 int git_repository_init_init_options(
3074 git_repository_init_options *opts, unsigned int version)
3075 {
3076 return git_repository_init_options_init(opts, version);
3077 }
3078 #endif
3079
git_repository_ident(const char ** name,const char ** email,const git_repository * repo)3080 int git_repository_ident(const char **name, const char **email, const git_repository *repo)
3081 {
3082 *name = repo->ident_name;
3083 *email = repo->ident_email;
3084
3085 return 0;
3086 }
3087
git_repository_set_ident(git_repository * repo,const char * name,const char * email)3088 int git_repository_set_ident(git_repository *repo, const char *name, const char *email)
3089 {
3090 char *tmp_name = NULL, *tmp_email = NULL;
3091
3092 if (name) {
3093 tmp_name = git__strdup(name);
3094 GIT_ERROR_CHECK_ALLOC(tmp_name);
3095 }
3096
3097 if (email) {
3098 tmp_email = git__strdup(email);
3099 GIT_ERROR_CHECK_ALLOC(tmp_email);
3100 }
3101
3102 tmp_name = git_atomic_swap(repo->ident_name, tmp_name);
3103 tmp_email = git_atomic_swap(repo->ident_email, tmp_email);
3104
3105 git__free(tmp_name);
3106 git__free(tmp_email);
3107
3108 return 0;
3109 }
3110
git_repository_submodule_cache_all(git_repository * repo)3111 int git_repository_submodule_cache_all(git_repository *repo)
3112 {
3113 GIT_ASSERT_ARG(repo);
3114 return git_submodule_cache_init(&repo->submodule_cache, repo);
3115 }
3116
git_repository_submodule_cache_clear(git_repository * repo)3117 int git_repository_submodule_cache_clear(git_repository *repo)
3118 {
3119 int error = 0;
3120 GIT_ASSERT_ARG(repo);
3121
3122 error = git_submodule_cache_free(repo->submodule_cache);
3123 repo->submodule_cache = NULL;
3124 return error;
3125 }
3126