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