Blame src/repository.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
#include <ctype.h>
Packit Service 20376f
Packit Service 20376f
#include "git2/object.h"
Packit Service 20376f
#include "git2/refdb.h"
Packit Service 20376f
#include "git2/sys/repository.h"
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "repository.h"
Packit Service 20376f
#include "commit.h"
Packit Service 20376f
#include "tag.h"
Packit Service 20376f
#include "blob.h"
Packit Service 20376f
#include "fileops.h"
Packit Service 20376f
#include "sysdir.h"
Packit Service 20376f
#include "filebuf.h"
Packit Service 20376f
#include "index.h"
Packit Service 20376f
#include "config.h"
Packit Service 20376f
#include "refs.h"
Packit Service 20376f
#include "filter.h"
Packit Service 20376f
#include "odb.h"
Packit Service 20376f
#include "remote.h"
Packit Service 20376f
#include "merge.h"
Packit Service 20376f
#include "diff_driver.h"
Packit Service 20376f
#include "annotated_commit.h"
Packit Service 20376f
#include "submodule.h"
Packit Service 20376f
#include "worktree.h"
Packit Service 20376f
Packit Service 20376f
#include "strmap.h"
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
# include "win32/w32_util.h"
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
bool git_repository__fsync_gitdir = false;
Packit Service 20376f
Packit Service 20376f
static const struct {
Packit Service 20376f
    git_repository_item_t parent;
Packit Service 20376f
    const char *name;
Packit Service 20376f
    bool directory;
Packit Service 20376f
} items[] = {
Packit Service 20376f
	{ GIT_REPOSITORY_ITEM_GITDIR, NULL, true },
Packit Service 20376f
	{ GIT_REPOSITORY_ITEM_WORKDIR, NULL, true },
Packit Service 20376f
	{ GIT_REPOSITORY_ITEM_COMMONDIR, NULL, true },
Packit Service 20376f
	{ GIT_REPOSITORY_ITEM_GITDIR, "index", false },
Packit Service 20376f
	{ GIT_REPOSITORY_ITEM_COMMONDIR, "objects", true },
Packit Service 20376f
	{ GIT_REPOSITORY_ITEM_COMMONDIR, "refs", true },
Packit Service 20376f
	{ GIT_REPOSITORY_ITEM_COMMONDIR, "packed-refs", false },
Packit Service 20376f
	{ GIT_REPOSITORY_ITEM_COMMONDIR, "remotes", true },
Packit Service 20376f
	{ GIT_REPOSITORY_ITEM_COMMONDIR, "config", false },
Packit Service 20376f
	{ GIT_REPOSITORY_ITEM_COMMONDIR, "info", true },
Packit Service 20376f
	{ GIT_REPOSITORY_ITEM_COMMONDIR, "hooks", true },
Packit Service 20376f
	{ GIT_REPOSITORY_ITEM_COMMONDIR, "logs", true },
Packit Service 20376f
	{ GIT_REPOSITORY_ITEM_GITDIR, "modules", true },
Packit Service 20376f
	{ GIT_REPOSITORY_ITEM_COMMONDIR, "worktrees", true }
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
static int check_repositoryformatversion(git_config *config);
Packit Service 20376f
Packit Service 20376f
#define GIT_COMMONDIR_FILE "commondir"
Packit Service 20376f
#define GIT_GITDIR_FILE "gitdir"
Packit Service 20376f
Packit Service 20376f
#define GIT_FILE_CONTENT_PREFIX "gitdir:"
Packit Service 20376f
Packit Service 20376f
#define GIT_BRANCH_MASTER "master"
Packit Service 20376f
Packit Service 20376f
#define GIT_REPO_VERSION 0
Packit Service 20376f
Packit Service 20376f
git_buf git_repository__reserved_names_win32[] = {
Packit Service 20376f
	{ DOT_GIT, 0, CONST_STRLEN(DOT_GIT) },
Packit Service 20376f
	{ GIT_DIR_SHORTNAME, 0, CONST_STRLEN(GIT_DIR_SHORTNAME) }
Packit Service 20376f
};
Packit Service 20376f
size_t git_repository__reserved_names_win32_len = 2;
Packit Service 20376f
Packit Service 20376f
git_buf git_repository__reserved_names_posix[] = {
Packit Service 20376f
	{ DOT_GIT, 0, CONST_STRLEN(DOT_GIT) },
Packit Service 20376f
};
Packit Service 20376f
size_t git_repository__reserved_names_posix_len = 1;
Packit Service 20376f
Packit Service 20376f
static void set_odb(git_repository *repo, git_odb *odb)
Packit Service 20376f
{
Packit Service 20376f
	if (odb) {
Packit Service 20376f
		GIT_REFCOUNT_OWN(odb, repo);
Packit Service 20376f
		GIT_REFCOUNT_INC(odb);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((odb = git__swap(repo->_odb, odb)) != NULL) {
Packit Service 20376f
		GIT_REFCOUNT_OWN(odb, NULL);
Packit Service 20376f
		git_odb_free(odb);
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void set_refdb(git_repository *repo, git_refdb *refdb)
Packit Service 20376f
{
Packit Service 20376f
	if (refdb) {
Packit Service 20376f
		GIT_REFCOUNT_OWN(refdb, repo);
Packit Service 20376f
		GIT_REFCOUNT_INC(refdb);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((refdb = git__swap(repo->_refdb, refdb)) != NULL) {
Packit Service 20376f
		GIT_REFCOUNT_OWN(refdb, NULL);
Packit Service 20376f
		git_refdb_free(refdb);
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void set_config(git_repository *repo, git_config *config)
Packit Service 20376f
{
Packit Service 20376f
	if (config) {
Packit Service 20376f
		GIT_REFCOUNT_OWN(config, repo);
Packit Service 20376f
		GIT_REFCOUNT_INC(config);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((config = git__swap(repo->_config, config)) != NULL) {
Packit Service 20376f
		GIT_REFCOUNT_OWN(config, NULL);
Packit Service 20376f
		git_config_free(config);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_repository__cvar_cache_clear(repo);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void set_index(git_repository *repo, git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	if (index) {
Packit Service 20376f
		GIT_REFCOUNT_OWN(index, repo);
Packit Service 20376f
		GIT_REFCOUNT_INC(index);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((index = git__swap(repo->_index, index)) != NULL) {
Packit Service 20376f
		GIT_REFCOUNT_OWN(index, NULL);
Packit Service 20376f
		git_index_free(index);
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_repository__cleanup(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	assert(repo);
Packit Service 20376f
Packit Service 20376f
	git_repository_submodule_cache_clear(repo);
Packit Service 20376f
	git_cache_clear(&repo->objects);
Packit Service 20376f
	git_attr_cache_flush(repo);
Packit Service 20376f
Packit Service 20376f
	set_config(repo, NULL);
Packit Service 20376f
	set_index(repo, NULL);
Packit Service 20376f
	set_odb(repo, NULL);
Packit Service 20376f
	set_refdb(repo, NULL);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_repository_free(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	size_t i;
Packit Service 20376f
Packit Service 20376f
	if (repo == NULL)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	git_repository__cleanup(repo);
Packit Service 20376f
Packit Service 20376f
	git_cache_free(&repo->objects);
Packit Service 20376f
Packit Service 20376f
	git_diff_driver_registry_free(repo->diff_drivers);
Packit Service 20376f
	repo->diff_drivers = NULL;
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < repo->reserved_names.size; i++)
Packit Service 20376f
		git_buf_free(git_array_get(repo->reserved_names, i));
Packit Service 20376f
	git_array_clear(repo->reserved_names);
Packit Service 20376f
Packit Service 20376f
	git__free(repo->gitlink);
Packit Service 20376f
	git__free(repo->gitdir);
Packit Service 20376f
	git__free(repo->commondir);
Packit Service 20376f
	git__free(repo->workdir);
Packit Service 20376f
	git__free(repo->namespace);
Packit Service 20376f
	git__free(repo->ident_name);
Packit Service 20376f
	git__free(repo->ident_email);
Packit Service 20376f
Packit Service 20376f
	git__memzero(repo, sizeof(*repo));
Packit Service 20376f
	git__free(repo);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Git repository open methods
Packit Service 20376f
 *
Packit Service 20376f
 * Open a repository object from its path
Packit Service 20376f
 */
Packit Service 20376f
static bool valid_repository_path(git_buf *repository_path, git_buf *common_path)
Packit Service 20376f
{
Packit Service 20376f
	/* Check if we have a separate commondir (e.g. we have a
Packit Service 20376f
	 * worktree) */
Packit Service 20376f
	if (git_path_contains_file(repository_path, GIT_COMMONDIR_FILE)) {
Packit Service 20376f
		git_buf common_link  = GIT_BUF_INIT;
Packit Service 20376f
		git_buf_joinpath(&common_link, repository_path->ptr, GIT_COMMONDIR_FILE);
Packit Service 20376f
Packit Service 20376f
		git_futils_readbuffer(&common_link, common_link.ptr);
Packit Service 20376f
		git_buf_rtrim(&common_link);
Packit Service 20376f
Packit Service 20376f
		if (git_path_is_relative(common_link.ptr)) {
Packit Service 20376f
			git_buf_joinpath(common_path, repository_path->ptr, common_link.ptr);
Packit Service 20376f
		} else {
Packit Service 20376f
			git_buf_swap(common_path, &common_link);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		git_buf_free(&common_link);
Packit Service 20376f
	}
Packit Service 20376f
	else {
Packit Service 20376f
		git_buf_set(common_path, repository_path->ptr, repository_path->size);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Make sure the commondir path always has a trailing * slash */
Packit Service 20376f
	if (git_buf_rfind(common_path, '/') != (ssize_t)common_path->size - 1)
Packit Service 20376f
		git_buf_putc(common_path, '/');
Packit Service 20376f
Packit Service 20376f
	/* Ensure HEAD file exists */
Packit Service 20376f
	if (git_path_contains_file(repository_path, GIT_HEAD_FILE) == false)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	/* Check files in common dir */
Packit Service 20376f
	if (git_path_contains_dir(common_path, GIT_OBJECTS_DIR) == false)
Packit Service 20376f
		return false;
Packit Service 20376f
	if (git_path_contains_dir(common_path, GIT_REFS_DIR) == false)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	return true;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static git_repository *repository_alloc(void)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *repo = git__calloc(1, sizeof(git_repository));
Packit Service 20376f
Packit Service 20376f
	if (repo == NULL ||
Packit Service 20376f
		git_cache_init(&repo->objects) < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	git_array_init_to_size(repo->reserved_names, 4);
Packit Service 20376f
	if (!repo->reserved_names.ptr)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	/* set all the entries in the cvar cache to `unset` */
Packit Service 20376f
	git_repository__cvar_cache_clear(repo);
Packit Service 20376f
Packit Service 20376f
	return repo;
Packit Service 20376f
Packit Service 20376f
on_error:
Packit Service 20376f
	if (repo)
Packit Service 20376f
		git_cache_free(&repo->objects);
Packit Service 20376f
Packit Service 20376f
	git__free(repo);
Packit Service 20376f
	return NULL;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_new(git_repository **out)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
Packit Service 20376f
	*out = repo = repository_alloc();
Packit Service 20376f
	GITERR_CHECK_ALLOC(repo);
Packit Service 20376f
Packit Service 20376f
	repo->is_bare = 1;
Packit Service 20376f
	repo->is_worktree = 0;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int load_config_data(git_repository *repo, const git_config *config)
Packit Service 20376f
{
Packit Service 20376f
	int is_bare;
Packit Service 20376f
Packit Service 20376f
	/* Try to figure out if it's bare, default to non-bare if it's not set */
Packit Service 20376f
	if (git_config_get_bool(&is_bare, config, "core.bare") < 0)
Packit Service 20376f
		repo->is_bare = 0;
Packit Service 20376f
	else
Packit Service 20376f
		repo->is_bare = is_bare;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int load_workdir(git_repository *repo, git_config *config, git_buf *parent_path)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_config_entry *ce;
Packit Service 20376f
	git_buf worktree = GIT_BUF_INIT;
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	if (repo->is_bare)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_config__lookup_entry(
Packit Service 20376f
			&ce, config, "core.worktree", false)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (repo->is_worktree) {
Packit Service 20376f
		char *gitlink = git_worktree__read_link(repo->gitdir, GIT_GITDIR_FILE);
Packit Service 20376f
		if (!gitlink) {
Packit Service 20376f
			error = -1;
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		git_buf_attach(&worktree, gitlink, 0);
Packit Service 20376f
Packit Service 20376f
		if ((git_path_dirname_r(&worktree, worktree.ptr)) < 0 ||
Packit Service 20376f
		    git_path_to_dir(&worktree) < 0) {
Packit Service 20376f
			error = -1;
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		repo->workdir = git_buf_detach(&worktree);
Packit Service 20376f
	}
Packit Service 20376f
	else if (ce && ce->value) {
Packit Service 20376f
		if ((error = git_path_prettify_dir(
Packit Service 20376f
				&worktree, ce->value, repo->gitdir)) < 0)
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
Packit Service 20376f
		repo->workdir = git_buf_detach(&worktree);
Packit Service 20376f
	}
Packit Service 20376f
	else if (parent_path && git_path_isdir(parent_path->ptr))
Packit Service 20376f
		repo->workdir = git_buf_detach(parent_path);
Packit Service 20376f
	else {
Packit Service 20376f
		if (git_path_dirname_r(&worktree, repo->gitdir) < 0 ||
Packit Service 20376f
		    git_path_to_dir(&worktree) < 0) {
Packit Service 20376f
			error = -1;
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		repo->workdir = git_buf_detach(&worktree);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC(repo->workdir);
Packit Service 20376f
cleanup:
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
	git_config_entry_free(ce);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * This function returns furthest offset into path where a ceiling dir
Packit Service 20376f
 * is found, so we can stop processing the path at that point.
Packit Service 20376f
 *
Packit Service 20376f
 * Note: converting this to use git_bufs instead of GIT_PATH_MAX buffers on
Packit Service 20376f
 * the stack could remove directories name limits, but at the cost of doing
Packit Service 20376f
 * repeated malloc/frees inside the loop below, so let's not do it now.
Packit Service 20376f
 */
Packit Service 20376f
static size_t find_ceiling_dir_offset(
Packit Service 20376f
	const char *path,
Packit Service 20376f
	const char *ceiling_directories)
Packit Service 20376f
{
Packit Service 20376f
	char buf[GIT_PATH_MAX + 1];
Packit Service 20376f
	char buf2[GIT_PATH_MAX + 1];
Packit Service 20376f
	const char *ceil, *sep;
Packit Service 20376f
	size_t len, max_len = 0, min_len;
Packit Service 20376f
Packit Service 20376f
	assert(path);
Packit Service 20376f
Packit Service 20376f
	min_len = (size_t)(git_path_root(path) + 1);
Packit Service 20376f
Packit Service 20376f
	if (ceiling_directories == NULL || min_len == 0)
Packit Service 20376f
		return min_len;
Packit Service 20376f
Packit Service 20376f
	for (sep = ceil = ceiling_directories; *sep; ceil = sep + 1) {
Packit Service 20376f
		for (sep = ceil; *sep && *sep != GIT_PATH_LIST_SEPARATOR; sep++);
Packit Service 20376f
		len = sep - ceil;
Packit Service 20376f
Packit Service 20376f
		if (len == 0 || len >= sizeof(buf) || git_path_root(ceil) == -1)
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		strncpy(buf, ceil, len);
Packit Service 20376f
		buf[len] = '\0';
Packit Service 20376f
Packit Service 20376f
		if (p_realpath(buf, buf2) == NULL)
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		len = strlen(buf2);
Packit Service 20376f
		if (len > 0 && buf2[len-1] == '/')
Packit Service 20376f
			buf[--len] = '\0';
Packit Service 20376f
Packit Service 20376f
		if (!strncmp(path, buf2, len) &&
Packit Service 20376f
			(path[len] == '/' || !path[len]) &&
Packit Service 20376f
			len > max_len)
Packit Service 20376f
		{
Packit Service 20376f
			max_len = len;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return (max_len <= min_len ? min_len : max_len);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Read the contents of `file_path` and set `path_out` to the repo dir that
Packit Service 20376f
 * it points to.  Before calling, set `path_out` to the base directory that
Packit Service 20376f
 * should be used if the contents of `file_path` are a relative path.
Packit Service 20376f
 */
Packit Service 20376f
static int read_gitfile(git_buf *path_out, const char *file_path)
Packit Service 20376f
{
Packit Service 20376f
	int     error = 0;
Packit Service 20376f
	git_buf file = GIT_BUF_INIT;
Packit Service 20376f
	size_t  prefix_len = strlen(GIT_FILE_CONTENT_PREFIX);
Packit Service 20376f
Packit Service 20376f
	assert(path_out && file_path);
Packit Service 20376f
Packit Service 20376f
	if (git_futils_readbuffer(&file, file_path) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	git_buf_rtrim(&file;;
Packit Service 20376f
	/* apparently on Windows, some people use backslashes in paths */
Packit Service 20376f
	git_path_mkposix(file.ptr);
Packit Service 20376f
Packit Service 20376f
	if (git_buf_len(&file) <= prefix_len ||
Packit Service 20376f
		memcmp(git_buf_cstr(&file), GIT_FILE_CONTENT_PREFIX, prefix_len) != 0)
Packit Service 20376f
	{
Packit Service 20376f
		giterr_set(GITERR_REPOSITORY,
Packit Service 20376f
			"the `.git` file at '%s' is malformed", file_path);
Packit Service 20376f
		error = -1;
Packit Service 20376f
	}
Packit Service 20376f
	else if ((error = git_path_dirname_r(path_out, file_path)) >= 0) {
Packit Service 20376f
		const char *gitlink = git_buf_cstr(&file) + prefix_len;
Packit Service 20376f
		while (*gitlink && git__isspace(*gitlink)) gitlink++;
Packit Service 20376f
Packit Service 20376f
		error = git_path_prettify_dir(
Packit Service 20376f
			path_out, gitlink, git_buf_cstr(path_out));
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&file;;
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int find_repo(
Packit Service 20376f
	git_buf *gitdir_path,
Packit Service 20376f
	git_buf *workdir_path,
Packit Service 20376f
	git_buf *gitlink_path,
Packit Service 20376f
	git_buf *commondir_path,
Packit Service 20376f
	const char *start_path,
Packit Service 20376f
	uint32_t flags,
Packit Service 20376f
	const char *ceiling_dirs)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	git_buf repo_link = GIT_BUF_INIT;
Packit Service 20376f
	git_buf common_link = GIT_BUF_INIT;
Packit Service 20376f
	struct stat st;
Packit Service 20376f
	dev_t initial_device = 0;
Packit Service 20376f
	int min_iterations;
Packit Service 20376f
	bool in_dot_git;
Packit Service 20376f
	size_t ceiling_offset = 0;
Packit Service 20376f
Packit Service 20376f
	git_buf_clear(gitdir_path);
Packit Service 20376f
Packit Service 20376f
	error = git_path_prettify(&path, start_path, NULL);
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	/* in_dot_git toggles each loop:
Packit Service 20376f
	 * /a/b/c/.git, /a/b/c, /a/b/.git, /a/b, /a/.git, /a
Packit Service 20376f
	 * With GIT_REPOSITORY_OPEN_BARE or GIT_REPOSITORY_OPEN_NO_DOTGIT, we
Packit Service 20376f
	 * assume we started with /a/b/c.git and don't append .git the first
Packit Service 20376f
	 * time through.
Packit Service 20376f
	 * min_iterations indicates the number of iterations left before going
Packit Service 20376f
	 * further counts as a search. */
Packit Service 20376f
	if (flags & (GIT_REPOSITORY_OPEN_BARE | GIT_REPOSITORY_OPEN_NO_DOTGIT)) {
Packit Service 20376f
		in_dot_git = true;
Packit Service 20376f
		min_iterations = 1;
Packit Service 20376f
	} else {
Packit Service 20376f
		in_dot_git = false;
Packit Service 20376f
		min_iterations = 2;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	for (;;) {
Packit Service 20376f
		if (!(flags & GIT_REPOSITORY_OPEN_NO_DOTGIT)) {
Packit Service 20376f
			if (!in_dot_git) {
Packit Service 20376f
				error = git_buf_joinpath(&path, path.ptr, DOT_GIT);
Packit Service 20376f
				if (error < 0)
Packit Service 20376f
					break;
Packit Service 20376f
			}
Packit Service 20376f
			in_dot_git = !in_dot_git;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (p_stat(path.ptr, &st) == 0) {
Packit Service 20376f
			/* check that we have not crossed device boundaries */
Packit Service 20376f
			if (initial_device == 0)
Packit Service 20376f
				initial_device = st.st_dev;
Packit Service 20376f
			else if (st.st_dev != initial_device &&
Packit Service 20376f
				 !(flags & GIT_REPOSITORY_OPEN_CROSS_FS))
Packit Service 20376f
				break;
Packit Service 20376f
Packit Service 20376f
			if (S_ISDIR(st.st_mode)) {
Packit Service 20376f
				if (valid_repository_path(&path, &common_link)) {
Packit Service 20376f
					git_path_to_dir(&path);
Packit Service 20376f
					git_buf_set(gitdir_path, path.ptr, path.size);
Packit Service 20376f
Packit Service 20376f
					if (gitlink_path)
Packit Service 20376f
						git_buf_attach(gitlink_path,
Packit Service 20376f
							git_worktree__read_link(path.ptr, GIT_GITDIR_FILE), 0);
Packit Service 20376f
					if (commondir_path)
Packit Service 20376f
						git_buf_swap(&common_link, commondir_path);
Packit Service 20376f
Packit Service 20376f
					break;
Packit Service 20376f
				}
Packit Service 20376f
			}
Packit Service 20376f
			else if (S_ISREG(st.st_mode) && git__suffixcmp(path.ptr, "/" DOT_GIT) == 0) {
Packit Service 20376f
				error = read_gitfile(&repo_link, path.ptr);
Packit Service 20376f
				if (error < 0)
Packit Service 20376f
					break;
Packit Service 20376f
				if (valid_repository_path(&repo_link, &common_link)) {
Packit Service 20376f
					git_buf_swap(gitdir_path, &repo_link);
Packit Service 20376f
Packit Service 20376f
					if (gitlink_path)
Packit Service 20376f
						error = git_buf_put(gitlink_path, path.ptr, path.size);
Packit Service 20376f
					if (commondir_path)
Packit Service 20376f
						git_buf_swap(&common_link, commondir_path);
Packit Service 20376f
				}
Packit Service 20376f
				break;
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* Move up one directory. If we're in_dot_git, we'll search the
Packit Service 20376f
		 * parent itself next. If we're !in_dot_git, we'll search .git
Packit Service 20376f
		 * in the parent directory next (added at the top of the loop). */
Packit Service 20376f
		if (git_path_dirname_r(&path, path.ptr) < 0) {
Packit Service 20376f
			error = -1;
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* Once we've checked the directory (and .git if applicable),
Packit Service 20376f
		 * find the ceiling for a search. */
Packit Service 20376f
		if (min_iterations && (--min_iterations == 0))
Packit Service 20376f
			ceiling_offset = find_ceiling_dir_offset(path.ptr, ceiling_dirs);
Packit Service 20376f
Packit Service 20376f
		/* Check if we should stop searching here. */
Packit Service 20376f
		if (min_iterations == 0
Packit Service 20376f
		    && (path.ptr[ceiling_offset] == 0
Packit Service 20376f
			|| (flags & GIT_REPOSITORY_OPEN_NO_SEARCH)))
Packit Service 20376f
			break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!error && workdir_path && !(flags & GIT_REPOSITORY_OPEN_BARE)) {
Packit Service 20376f
		if (!git_buf_len(gitdir_path))
Packit Service 20376f
			git_buf_clear(workdir_path);
Packit Service 20376f
		else {
Packit Service 20376f
			git_path_dirname_r(workdir_path, path.ptr);
Packit Service 20376f
			git_path_to_dir(workdir_path);
Packit Service 20376f
		}
Packit Service 20376f
		if (git_buf_oom(workdir_path))
Packit Service 20376f
			return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* If we didn't find the repository, and we don't have any other error
Packit Service 20376f
	 * to report, report that. */
Packit Service 20376f
	if (!git_buf_len(gitdir_path) && !error) {
Packit Service 20376f
		giterr_set(GITERR_REPOSITORY,
Packit Service 20376f
			"could not find repository from '%s'", start_path);
Packit Service 20376f
		error = GIT_ENOTFOUND;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
	git_buf_free(&repo_link);
Packit Service 20376f
	git_buf_free(&common_link);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_open_bare(
Packit Service 20376f
	git_repository **repo_ptr,
Packit Service 20376f
	const char *bare_path)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_buf path = GIT_BUF_INIT, common_path = GIT_BUF_INIT;
Packit Service 20376f
	git_repository *repo = NULL;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_path_prettify_dir(&path, bare_path, NULL)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (!valid_repository_path(&path, &common_path)) {
Packit Service 20376f
		git_buf_free(&path);
Packit Service 20376f
		git_buf_free(&common_path);
Packit Service 20376f
		giterr_set(GITERR_REPOSITORY, "path is not a repository: %s", bare_path);
Packit Service 20376f
		return GIT_ENOTFOUND;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	repo = repository_alloc();
Packit Service 20376f
	GITERR_CHECK_ALLOC(repo);
Packit Service 20376f
Packit Service 20376f
	repo->gitdir = git_buf_detach(&path);
Packit Service 20376f
	GITERR_CHECK_ALLOC(repo->gitdir);
Packit Service 20376f
	repo->commondir = git_buf_detach(&common_path);
Packit Service 20376f
	GITERR_CHECK_ALLOC(repo->commondir);
Packit Service 20376f
Packit Service 20376f
	/* of course we're bare! */
Packit Service 20376f
	repo->is_bare = 1;
Packit Service 20376f
	repo->is_worktree = 0;
Packit Service 20376f
	repo->workdir = NULL;
Packit Service 20376f
Packit Service 20376f
	*repo_ptr = repo;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int _git_repository_open_ext_from_env(
Packit Service 20376f
	git_repository **out,
Packit Service 20376f
	const char *start_path)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *repo = NULL;
Packit Service 20376f
	git_index *index = NULL;
Packit Service 20376f
	git_odb *odb = NULL;
Packit Service 20376f
	git_buf dir_buf = GIT_BUF_INIT;
Packit Service 20376f
	git_buf ceiling_dirs_buf = GIT_BUF_INIT;
Packit Service 20376f
	git_buf across_fs_buf = GIT_BUF_INIT;
Packit Service 20376f
	git_buf index_file_buf = GIT_BUF_INIT;
Packit Service 20376f
	git_buf namespace_buf = GIT_BUF_INIT;
Packit Service 20376f
	git_buf object_dir_buf = GIT_BUF_INIT;
Packit Service 20376f
	git_buf alts_buf = GIT_BUF_INIT;
Packit Service 20376f
	git_buf work_tree_buf = GIT_BUF_INIT;
Packit Service 20376f
	git_buf common_dir_buf = GIT_BUF_INIT;
Packit Service 20376f
	const char *ceiling_dirs = NULL;
Packit Service 20376f
	unsigned flags = 0;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if (!start_path) {
Packit Service 20376f
		error = git__getenv(&dir_buf, "GIT_DIR");
Packit Service 20376f
		if (error == GIT_ENOTFOUND) {
Packit Service 20376f
			giterr_clear();
Packit Service 20376f
			start_path = ".";
Packit Service 20376f
		} else if (error < 0)
Packit Service 20376f
			goto error;
Packit Service 20376f
		else {
Packit Service 20376f
			start_path = git_buf_cstr(&dir_buf);
Packit Service 20376f
			flags |= GIT_REPOSITORY_OPEN_NO_SEARCH;
Packit Service 20376f
			flags |= GIT_REPOSITORY_OPEN_NO_DOTGIT;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = git__getenv(&ceiling_dirs_buf, "GIT_CEILING_DIRECTORIES");
Packit Service 20376f
	if (error == GIT_ENOTFOUND)
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
	else if (error < 0)
Packit Service 20376f
		goto error;
Packit Service 20376f
	else
Packit Service 20376f
		ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf);
Packit Service 20376f
Packit Service 20376f
	error = git__getenv(&across_fs_buf, "GIT_DISCOVERY_ACROSS_FILESYSTEM");
Packit Service 20376f
	if (error == GIT_ENOTFOUND)
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
	else if (error < 0)
Packit Service 20376f
		goto error;
Packit Service 20376f
	else {
Packit Service 20376f
		int across_fs = 0;
Packit Service 20376f
		error = git_config_parse_bool(&across_fs, git_buf_cstr(&across_fs_buf));
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			goto error;
Packit Service 20376f
		if (across_fs)
Packit Service 20376f
			flags |= GIT_REPOSITORY_OPEN_CROSS_FS;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = git__getenv(&index_file_buf, "GIT_INDEX_FILE");
Packit Service 20376f
	if (error == GIT_ENOTFOUND)
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
	else if (error < 0)
Packit Service 20376f
		goto error;
Packit Service 20376f
	else {
Packit Service 20376f
		error = git_index_open(&index, git_buf_cstr(&index_file_buf));
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			goto error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = git__getenv(&namespace_buf, "GIT_NAMESPACE");
Packit Service 20376f
	if (error == GIT_ENOTFOUND)
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
	else if (error < 0)
Packit Service 20376f
		goto error;
Packit Service 20376f
Packit Service 20376f
	error = git__getenv(&object_dir_buf, "GIT_OBJECT_DIRECTORY");
Packit Service 20376f
	if (error == GIT_ENOTFOUND)
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
	else if (error < 0)
Packit Service 20376f
		goto error;
Packit Service 20376f
	else {
Packit Service 20376f
		error = git_odb_open(&odb, git_buf_cstr(&object_dir_buf));
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			goto error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = git__getenv(&work_tree_buf, "GIT_WORK_TREE");
Packit Service 20376f
	if (error == GIT_ENOTFOUND)
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
	else if (error < 0)
Packit Service 20376f
		goto error;
Packit Service 20376f
	else {
Packit Service 20376f
		giterr_set(GITERR_INVALID, "GIT_WORK_TREE unimplemented");
Packit Service 20376f
		error = GIT_ERROR;
Packit Service 20376f
		goto error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = git__getenv(&work_tree_buf, "GIT_COMMON_DIR");
Packit Service 20376f
	if (error == GIT_ENOTFOUND)
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
	else if (error < 0)
Packit Service 20376f
		goto error;
Packit Service 20376f
	else {
Packit Service 20376f
		giterr_set(GITERR_INVALID, "GIT_COMMON_DIR unimplemented");
Packit Service 20376f
		error = GIT_ERROR;
Packit Service 20376f
		goto error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = git_repository_open_ext(&repo, start_path, flags, ceiling_dirs);
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		goto error;
Packit Service 20376f
Packit Service 20376f
	if (odb)
Packit Service 20376f
		git_repository_set_odb(repo, odb);
Packit Service 20376f
Packit Service 20376f
	error = git__getenv(&alts_buf, "GIT_ALTERNATE_OBJECT_DIRECTORIES");
Packit Service 20376f
	if (error == GIT_ENOTFOUND) {
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
		error = 0;
Packit Service 20376f
	} else if (error < 0)
Packit Service 20376f
		goto error;
Packit Service 20376f
        else {
Packit Service 20376f
		const char *end;
Packit Service 20376f
		char *alt, *sep;
Packit Service 20376f
		if (!odb) {
Packit Service 20376f
			error = git_repository_odb(&odb, repo);
Packit Service 20376f
			if (error < 0)
Packit Service 20376f
				goto error;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		end = git_buf_cstr(&alts_buf) + git_buf_len(&alts_buf);
Packit Service 20376f
		for (sep = alt = alts_buf.ptr; sep != end; alt = sep+1) {
Packit Service 20376f
			for (sep = alt; *sep && *sep != GIT_PATH_LIST_SEPARATOR; sep++)
Packit Service 20376f
				;
Packit Service 20376f
			if (*sep)
Packit Service 20376f
				*sep = '\0';
Packit Service 20376f
			error = git_odb_add_disk_alternate(odb, alt);
Packit Service 20376f
			if (error < 0)
Packit Service 20376f
				goto error;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (git_buf_len(&namespace_buf)) {
Packit Service 20376f
		error = git_repository_set_namespace(repo, git_buf_cstr(&namespace_buf));
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			goto error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_repository_set_index(repo, index);
Packit Service 20376f
Packit Service 20376f
	if (out) {
Packit Service 20376f
		*out = repo;
Packit Service 20376f
		goto success;
Packit Service 20376f
	}
Packit Service 20376f
error:
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
success:
Packit Service 20376f
	git_odb_free(odb);
Packit Service 20376f
	git_index_free(index);
Packit Service 20376f
	git_buf_free(&common_dir_buf);
Packit Service 20376f
	git_buf_free(&work_tree_buf);
Packit Service 20376f
	git_buf_free(&alts_buf);
Packit Service 20376f
	git_buf_free(&object_dir_buf);
Packit Service 20376f
	git_buf_free(&namespace_buf);
Packit Service 20376f
	git_buf_free(&index_file_buf);
Packit Service 20376f
	git_buf_free(&across_fs_buf);
Packit Service 20376f
	git_buf_free(&ceiling_dirs_buf);
Packit Service 20376f
	git_buf_free(&dir_buf);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int repo_is_worktree(unsigned *out, const git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	git_buf gitdir_link = GIT_BUF_INIT;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	/* Worktrees cannot have the same commondir and gitdir */
Packit Service 20376f
	if (repo->commondir && repo->gitdir
Packit Service 20376f
	    && !strcmp(repo->commondir, repo->gitdir)) {
Packit Service 20376f
		*out = 0;
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_joinpath(&gitdir_link, repo->gitdir, "gitdir")) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* A 'gitdir' file inside a git directory is currently
Packit Service 20376f
	 * only used when the repository is a working tree. */
Packit Service 20376f
	*out = !!git_path_exists(gitdir_link.ptr);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&gitdir_link);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_open_ext(
Packit Service 20376f
	git_repository **repo_ptr,
Packit Service 20376f
	const char *start_path,
Packit Service 20376f
	unsigned int flags,
Packit Service 20376f
	const char *ceiling_dirs)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	unsigned is_worktree;
Packit Service 20376f
	git_buf gitdir = GIT_BUF_INIT, workdir = GIT_BUF_INIT,
Packit Service 20376f
		gitlink = GIT_BUF_INIT, commondir = GIT_BUF_INIT;
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
	git_config *config = NULL;
Packit Service 20376f
Packit Service 20376f
	if (flags & GIT_REPOSITORY_OPEN_FROM_ENV)
Packit Service 20376f
		return _git_repository_open_ext_from_env(repo_ptr, start_path);
Packit Service 20376f
Packit Service 20376f
	if (repo_ptr)
Packit Service 20376f
		*repo_ptr = NULL;
Packit Service 20376f
Packit Service 20376f
	error = find_repo(
Packit Service 20376f
		&gitdir, &workdir, &gitlink, &commondir, start_path, flags, ceiling_dirs);
Packit Service 20376f
Packit Service 20376f
	if (error < 0 || !repo_ptr)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	repo = repository_alloc();
Packit Service 20376f
	GITERR_CHECK_ALLOC(repo);
Packit Service 20376f
Packit Service 20376f
	repo->gitdir = git_buf_detach(&gitdir);
Packit Service 20376f
	GITERR_CHECK_ALLOC(repo->gitdir);
Packit Service 20376f
Packit Service 20376f
	if (gitlink.size) {
Packit Service 20376f
		repo->gitlink = git_buf_detach(&gitlink);
Packit Service 20376f
		GITERR_CHECK_ALLOC(repo->gitlink);
Packit Service 20376f
	}
Packit Service 20376f
	if (commondir.size) {
Packit Service 20376f
		repo->commondir = git_buf_detach(&commondir);
Packit Service 20376f
		GITERR_CHECK_ALLOC(repo->commondir);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = repo_is_worktree(&is_worktree, repo)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
	repo->is_worktree = is_worktree;
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * We'd like to have the config, but git doesn't particularly
Packit Service 20376f
	 * care if it's not there, so we need to deal with that.
Packit Service 20376f
	 */
Packit Service 20376f
Packit Service 20376f
	error = git_repository_config_snapshot(&config, repo);
Packit Service 20376f
	if (error < 0 && error != GIT_ENOTFOUND)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if (config && (error = check_repositoryformatversion(config)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if ((flags & GIT_REPOSITORY_OPEN_BARE) != 0)
Packit Service 20376f
		repo->is_bare = 1;
Packit Service 20376f
	else {
Packit Service 20376f
Packit Service 20376f
		if (config &&
Packit Service 20376f
		    ((error = load_config_data(repo, config)) < 0 ||
Packit Service 20376f
		     (error = load_workdir(repo, config, &workdir)) < 0))
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	git_buf_free(&gitdir);
Packit Service 20376f
	git_buf_free(&workdir);
Packit Service 20376f
	git_config_free(config);
Packit Service 20376f
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		git_repository_free(repo);
Packit Service 20376f
	else
Packit Service 20376f
		*repo_ptr = repo;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_open(git_repository **repo_out, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	return git_repository_open_ext(
Packit Service 20376f
		repo_out, path, GIT_REPOSITORY_OPEN_NO_SEARCH, NULL);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_open_from_worktree(git_repository **repo_out, git_worktree *wt)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	git_repository *repo = NULL;
Packit Service 20376f
	int len, err;
Packit Service 20376f
Packit Service 20376f
	assert(repo_out && wt);
Packit Service 20376f
Packit Service 20376f
	*repo_out = NULL;
Packit Service 20376f
	len = strlen(wt->gitlink_path);
Packit Service 20376f
Packit Service 20376f
	if (len <= 4 || strcasecmp(wt->gitlink_path + len - 4, ".git")) {
Packit Service 20376f
		err = -1;
Packit Service 20376f
		goto out;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((err = git_buf_set(&path, wt->gitlink_path, len - 4)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	if ((err = git_repository_open(&repo, path.ptr)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	*repo_out = repo;
Packit Service 20376f
Packit Service 20376f
out:
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	return err;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_wrap_odb(git_repository **repo_out, git_odb *odb)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
Packit Service 20376f
	repo = repository_alloc();
Packit Service 20376f
	GITERR_CHECK_ALLOC(repo);
Packit Service 20376f
Packit Service 20376f
	git_repository_set_odb(repo, odb);
Packit Service 20376f
	*repo_out = repo;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_discover(
Packit Service 20376f
	git_buf *out,
Packit Service 20376f
	const char *start_path,
Packit Service 20376f
	int across_fs,
Packit Service 20376f
	const char *ceiling_dirs)
Packit Service 20376f
{
Packit Service 20376f
	uint32_t flags = across_fs ? GIT_REPOSITORY_OPEN_CROSS_FS : 0;
Packit Service 20376f
Packit Service 20376f
	assert(start_path);
Packit Service 20376f
Packit Service 20376f
	git_buf_sanitize(out);
Packit Service 20376f
Packit Service 20376f
	return find_repo(out, NULL, NULL, NULL, start_path, flags, ceiling_dirs);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int load_config(
Packit Service 20376f
	git_config **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const char *global_config_path,
Packit Service 20376f
	const char *xdg_config_path,
Packit Service 20376f
	const char *system_config_path,
Packit Service 20376f
	const char *programdata_path)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_buf config_path = GIT_BUF_INIT;
Packit Service 20376f
	git_config *cfg = NULL;
Packit Service 20376f
Packit Service 20376f
	assert(repo && out);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_config_new(&cfg)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_repository_item_path(&config_path, repo, GIT_REPOSITORY_ITEM_CONFIG)) == 0)
Packit Service 20376f
		error = git_config_add_file_ondisk(cfg, config_path.ptr, GIT_CONFIG_LEVEL_LOCAL, 0);
Packit Service 20376f
Packit Service 20376f
	if (error && error != GIT_ENOTFOUND)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&config_path);
Packit Service 20376f
Packit Service 20376f
	if (global_config_path != NULL &&
Packit Service 20376f
		(error = git_config_add_file_ondisk(
Packit Service 20376f
			cfg, global_config_path, GIT_CONFIG_LEVEL_GLOBAL, 0)) < 0 &&
Packit Service 20376f
		error != GIT_ENOTFOUND)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	if (xdg_config_path != NULL &&
Packit Service 20376f
		(error = git_config_add_file_ondisk(
Packit Service 20376f
			cfg, xdg_config_path, GIT_CONFIG_LEVEL_XDG, 0)) < 0 &&
Packit Service 20376f
		error != GIT_ENOTFOUND)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	if (system_config_path != NULL &&
Packit Service 20376f
		(error = git_config_add_file_ondisk(
Packit Service 20376f
			cfg, system_config_path, GIT_CONFIG_LEVEL_SYSTEM, 0)) < 0 &&
Packit Service 20376f
		error != GIT_ENOTFOUND)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	if (programdata_path != NULL &&
Packit Service 20376f
		(error = git_config_add_file_ondisk(
Packit Service 20376f
			cfg, programdata_path, GIT_CONFIG_LEVEL_PROGRAMDATA, 0)) < 0 &&
Packit Service 20376f
		error != GIT_ENOTFOUND)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	giterr_clear(); /* clear any lingering ENOTFOUND errors */
Packit Service 20376f
Packit Service 20376f
	*out = cfg;
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
on_error:
Packit Service 20376f
	git_buf_free(&config_path);
Packit Service 20376f
	git_config_free(cfg);
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static const char *path_unless_empty(git_buf *buf)
Packit Service 20376f
{
Packit Service 20376f
	return git_buf_len(buf) > 0 ? git_buf_cstr(buf) : NULL;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_config__weakptr(git_config **out, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if (repo->_config == NULL) {
Packit Service 20376f
		git_buf global_buf = GIT_BUF_INIT;
Packit Service 20376f
		git_buf xdg_buf = GIT_BUF_INIT;
Packit Service 20376f
		git_buf system_buf = GIT_BUF_INIT;
Packit Service 20376f
		git_buf programdata_buf = GIT_BUF_INIT;
Packit Service 20376f
		git_config *config;
Packit Service 20376f
Packit Service 20376f
		git_config_find_global(&global_buf);
Packit Service 20376f
		git_config_find_xdg(&xdg_buf);
Packit Service 20376f
		git_config_find_system(&system_buf);
Packit Service 20376f
		git_config_find_programdata(&programdata_buf);
Packit Service 20376f
Packit Service 20376f
		/* If there is no global file, open a backend for it anyway */
Packit Service 20376f
		if (git_buf_len(&global_buf) == 0)
Packit Service 20376f
			git_config__global_location(&global_buf);
Packit Service 20376f
Packit Service 20376f
		error = load_config(
Packit Service 20376f
			&config, repo,
Packit Service 20376f
			path_unless_empty(&global_buf),
Packit Service 20376f
			path_unless_empty(&xdg_buf),
Packit Service 20376f
			path_unless_empty(&system_buf),
Packit Service 20376f
			path_unless_empty(&programdata_buf));
Packit Service 20376f
		if (!error) {
Packit Service 20376f
			GIT_REFCOUNT_OWN(config, repo);
Packit Service 20376f
Packit Service 20376f
			config = git__compare_and_swap(&repo->_config, NULL, config);
Packit Service 20376f
			if (config != NULL) {
Packit Service 20376f
				GIT_REFCOUNT_OWN(config, NULL);
Packit Service 20376f
				git_config_free(config);
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		git_buf_free(&global_buf);
Packit Service 20376f
		git_buf_free(&xdg_buf);
Packit Service 20376f
		git_buf_free(&system_buf);
Packit Service 20376f
		git_buf_free(&programdata_buf);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*out = repo->_config;
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_config(git_config **out, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	if (git_repository_config__weakptr(out, repo) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	GIT_REFCOUNT_INC(*out);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_config_snapshot(git_config **out, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_config *weak;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_repository_config__weakptr(&weak, repo)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	return git_config_snapshot(out, weak);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_repository_set_config(git_repository *repo, git_config *config)
Packit Service 20376f
{
Packit Service 20376f
	assert(repo && config);
Packit Service 20376f
	set_config(repo, config);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_odb__weakptr(git_odb **out, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(repo && out);
Packit Service 20376f
Packit Service 20376f
	if (repo->_odb == NULL) {
Packit Service 20376f
		git_buf odb_path = GIT_BUF_INIT;
Packit Service 20376f
		git_odb *odb;
Packit Service 20376f
Packit Service 20376f
		if ((error = git_repository_item_path(&odb_path, repo,
Packit Service 20376f
				GIT_REPOSITORY_ITEM_OBJECTS)) < 0 ||
Packit Service 20376f
			(error = git_odb_new(&odb)) < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		GIT_REFCOUNT_OWN(odb, repo);
Packit Service 20376f
Packit Service 20376f
		if ((error = git_odb__set_caps(odb, GIT_ODB_CAP_FROM_OWNER)) < 0 ||
Packit Service 20376f
			(error = git_odb__add_default_backends(odb, odb_path.ptr, 0, 0)) < 0) {
Packit Service 20376f
			git_odb_free(odb);
Packit Service 20376f
			return error;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		odb = git__compare_and_swap(&repo->_odb, NULL, odb);
Packit Service 20376f
		if (odb != NULL) {
Packit Service 20376f
			GIT_REFCOUNT_OWN(odb, NULL);
Packit Service 20376f
			git_odb_free(odb);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		git_buf_free(&odb_path);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*out = repo->_odb;
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_odb(git_odb **out, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	if (git_repository_odb__weakptr(out, repo) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	GIT_REFCOUNT_INC(*out);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_repository_set_odb(git_repository *repo, git_odb *odb)
Packit Service 20376f
{
Packit Service 20376f
	assert(repo && odb);
Packit Service 20376f
	set_odb(repo, odb);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(out && repo);
Packit Service 20376f
Packit Service 20376f
	if (repo->_refdb == NULL) {
Packit Service 20376f
		git_refdb *refdb;
Packit Service 20376f
Packit Service 20376f
		error = git_refdb_open(&refdb, repo);
Packit Service 20376f
		if (!error) {
Packit Service 20376f
			GIT_REFCOUNT_OWN(refdb, repo);
Packit Service 20376f
Packit Service 20376f
			refdb = git__compare_and_swap(&repo->_refdb, NULL, refdb);
Packit Service 20376f
			if (refdb != NULL) {
Packit Service 20376f
				GIT_REFCOUNT_OWN(refdb, NULL);
Packit Service 20376f
				git_refdb_free(refdb);
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*out = repo->_refdb;
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_refdb(git_refdb **out, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	if (git_repository_refdb__weakptr(out, repo) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	GIT_REFCOUNT_INC(*out);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_repository_set_refdb(git_repository *repo, git_refdb *refdb)
Packit Service 20376f
{
Packit Service 20376f
	assert(repo && refdb);
Packit Service 20376f
	set_refdb(repo, refdb);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_index__weakptr(git_index **out, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(out && repo);
Packit Service 20376f
Packit Service 20376f
	if (repo->_index == NULL) {
Packit Service 20376f
		git_buf index_path = GIT_BUF_INIT;
Packit Service 20376f
		git_index *index;
Packit Service 20376f
Packit Service 20376f
		if ((error = git_buf_joinpath(&index_path, repo->gitdir, GIT_INDEX_FILE)) < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		error = git_index_open(&index, index_path.ptr);
Packit Service 20376f
		if (!error) {
Packit Service 20376f
			GIT_REFCOUNT_OWN(index, repo);
Packit Service 20376f
Packit Service 20376f
			index = git__compare_and_swap(&repo->_index, NULL, index);
Packit Service 20376f
			if (index != NULL) {
Packit Service 20376f
				GIT_REFCOUNT_OWN(index, NULL);
Packit Service 20376f
				git_index_free(index);
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			error = git_index_set_caps(repo->_index, GIT_INDEXCAP_FROM_OWNER);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		git_buf_free(&index_path);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*out = repo->_index;
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_index(git_index **out, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	if (git_repository_index__weakptr(out, repo) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	GIT_REFCOUNT_INC(*out);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_repository_set_index(git_repository *repo, git_index *index)
Packit Service 20376f
{
Packit Service 20376f
	assert(repo);
Packit Service 20376f
	set_index(repo, index);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_set_namespace(git_repository *repo, const char *namespace)
Packit Service 20376f
{
Packit Service 20376f
	git__free(repo->namespace);
Packit Service 20376f
Packit Service 20376f
	if (namespace == NULL) {
Packit Service 20376f
		repo->namespace = NULL;
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return (repo->namespace = git__strdup(namespace)) ? 0 : -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const char *git_repository_get_namespace(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	return repo->namespace;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
static int reserved_names_add8dot3(git_repository *repo, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	char *name = git_win32_path_8dot3_name(path);
Packit Service 20376f
	const char *def = GIT_DIR_SHORTNAME;
Packit Service 20376f
	const char *def_dot_git = DOT_GIT;
Packit Service 20376f
	size_t name_len, def_len = CONST_STRLEN(GIT_DIR_SHORTNAME);
Packit Service 20376f
	size_t def_dot_git_len = CONST_STRLEN(DOT_GIT);
Packit Service 20376f
	git_buf *buf;
Packit Service 20376f
Packit Service 20376f
	if (!name)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	name_len = strlen(name);
Packit Service 20376f
Packit Service 20376f
	if ((name_len == def_len && memcmp(name, def, def_len) == 0) ||
Packit Service 20376f
		(name_len == def_dot_git_len && memcmp(name, def_dot_git, def_dot_git_len) == 0)) {
Packit Service 20376f
		git__free(name);
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((buf = git_array_alloc(repo->reserved_names)) == NULL)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	git_buf_attach(buf, name, name_len);
Packit Service 20376f
	return true;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
bool git_repository__reserved_names(
Packit Service 20376f
	git_buf **out, size_t *outlen, git_repository *repo, bool include_ntfs)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(include_ntfs);
Packit Service 20376f
Packit Service 20376f
	if (repo->reserved_names.size == 0) {
Packit Service 20376f
		git_buf *buf;
Packit Service 20376f
		size_t i;
Packit Service 20376f
Packit Service 20376f
		/* Add the static defaults */
Packit Service 20376f
		for (i = 0; i < git_repository__reserved_names_win32_len; i++) {
Packit Service 20376f
			if ((buf = git_array_alloc(repo->reserved_names)) == NULL)
Packit Service 20376f
				goto on_error;
Packit Service 20376f
Packit Service 20376f
			buf->ptr = git_repository__reserved_names_win32[i].ptr;
Packit Service 20376f
			buf->size = git_repository__reserved_names_win32[i].size;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* Try to add any repo-specific reserved names - the gitlink file
Packit Service 20376f
		 * within a submodule or the repository (if the repository directory
Packit Service 20376f
		 * is beneath the workdir).  These are typically `.git`, but should
Packit Service 20376f
		 * be protected in case they are not.  Note, repo and workdir paths
Packit Service 20376f
		 * are always prettified to end in `/`, so a prefixcmp is safe.
Packit Service 20376f
		 */
Packit Service 20376f
		if (!repo->is_bare) {
Packit Service 20376f
			int (*prefixcmp)(const char *, const char *);
Packit Service 20376f
			int error, ignorecase;
Packit Service 20376f
Packit Service 20376f
			error = git_repository__cvar(
Packit Service 20376f
				&ignorecase, repo, GIT_CVAR_IGNORECASE);
Packit Service 20376f
			prefixcmp = (error || ignorecase) ? git__prefixcmp_icase :
Packit Service 20376f
				git__prefixcmp;
Packit Service 20376f
Packit Service 20376f
			if (repo->gitlink &&
Packit Service 20376f
				reserved_names_add8dot3(repo, repo->gitlink) < 0)
Packit Service 20376f
				goto on_error;
Packit Service 20376f
Packit Service 20376f
			if (repo->gitdir &&
Packit Service 20376f
				prefixcmp(repo->gitdir, repo->workdir) == 0 &&
Packit Service 20376f
				reserved_names_add8dot3(repo, repo->gitdir) < 0)
Packit Service 20376f
				goto on_error;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*out = repo->reserved_names.ptr;
Packit Service 20376f
	*outlen = repo->reserved_names.size;
Packit Service 20376f
Packit Service 20376f
	return true;
Packit Service 20376f
Packit Service 20376f
	/* Always give good defaults, even on OOM */
Packit Service 20376f
on_error:
Packit Service 20376f
	*out = git_repository__reserved_names_win32;
Packit Service 20376f
	*outlen = git_repository__reserved_names_win32_len;
Packit Service 20376f
Packit Service 20376f
	return false;
Packit Service 20376f
}
Packit Service 20376f
#else
Packit Service 20376f
bool git_repository__reserved_names(
Packit Service 20376f
	git_buf **out, size_t *outlen, git_repository *repo, bool include_ntfs)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(repo);
Packit Service 20376f
Packit Service 20376f
	if (include_ntfs) {
Packit Service 20376f
		*out = git_repository__reserved_names_win32;
Packit Service 20376f
		*outlen = git_repository__reserved_names_win32_len;
Packit Service 20376f
	} else {
Packit Service 20376f
		*out = git_repository__reserved_names_posix;
Packit Service 20376f
		*outlen = git_repository__reserved_names_posix_len;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return true;
Packit Service 20376f
}
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
static int check_repositoryformatversion(git_config *config)
Packit Service 20376f
{
Packit Service 20376f
	int version, error;
Packit Service 20376f
Packit Service 20376f
	error = git_config_get_int32(&version, config, "core.repositoryformatversion");
Packit Service 20376f
	/* git ignores this if the config variable isn't there */
Packit Service 20376f
	if (error == GIT_ENOTFOUND)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (GIT_REPO_VERSION < version) {
Packit Service 20376f
		giterr_set(GITERR_REPOSITORY,
Packit Service 20376f
			"unsupported repository version %d. Only versions up to %d are supported.",
Packit Service 20376f
			version, GIT_REPO_VERSION);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_create_head(const char *git_dir, const char *ref_name)
Packit Service 20376f
{
Packit Service 20376f
	git_buf ref_path = GIT_BUF_INIT;
Packit Service 20376f
	git_filebuf ref = GIT_FILEBUF_INIT;
Packit Service 20376f
	const char *fmt;
Packit Service 20376f
Packit Service 20376f
	if (git_buf_joinpath(&ref_path, git_dir, GIT_HEAD_FILE) < 0 ||
Packit Service 20376f
		git_filebuf_open(&ref, ref_path.ptr, 0, GIT_REFS_FILE_MODE) < 0)
Packit Service 20376f
		goto fail;
Packit Service 20376f
Packit Service 20376f
	if (!ref_name)
Packit Service 20376f
		ref_name = GIT_BRANCH_MASTER;
Packit Service 20376f
Packit Service 20376f
	if (git__prefixcmp(ref_name, GIT_REFS_DIR) == 0)
Packit Service 20376f
		fmt = "ref: %s\n";
Packit Service 20376f
	else
Packit Service 20376f
		fmt = "ref: " GIT_REFS_HEADS_DIR "%s\n";
Packit Service 20376f
Packit Service 20376f
	if (git_filebuf_printf(&ref, fmt, ref_name) < 0 ||
Packit Service 20376f
		git_filebuf_commit(&ref) < 0)
Packit Service 20376f
		goto fail;
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&ref_path);
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
fail:
Packit Service 20376f
	git_buf_free(&ref_path);
Packit Service 20376f
	git_filebuf_cleanup(&ref;;
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static bool is_chmod_supported(const char *file_path)
Packit Service 20376f
{
Packit Service 20376f
	struct stat st1, st2;
Packit Service 20376f
Packit Service 20376f
	if (p_stat(file_path, &st1) < 0)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	if (p_chmod(file_path, st1.st_mode ^ S_IXUSR) < 0)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	if (p_stat(file_path, &st2) < 0)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	return (st1.st_mode != st2.st_mode);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static bool is_filesystem_case_insensitive(const char *gitdir_path)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	int is_insensitive = -1;
Packit Service 20376f
Packit Service 20376f
	if (!git_buf_joinpath(&path, gitdir_path, "CoNfIg"))
Packit Service 20376f
		is_insensitive = git_path_exists(git_buf_cstr(&path));
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
	return is_insensitive;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static bool are_symlinks_supported(const char *wd_path)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	int fd;
Packit Service 20376f
	struct stat st;
Packit Service 20376f
	int symlinks_supported = -1;
Packit Service 20376f
Packit Service 20376f
	if ((fd = git_futils_mktmp(&path, wd_path, 0666)) < 0 ||
Packit Service 20376f
		p_close(fd) < 0 ||
Packit Service 20376f
		p_unlink(path.ptr) < 0 ||
Packit Service 20376f
		p_symlink("testing", path.ptr) < 0 ||
Packit Service 20376f
		p_lstat(path.ptr, &st) < 0)
Packit Service 20376f
		symlinks_supported = false;
Packit Service 20376f
	else
Packit Service 20376f
		symlinks_supported = (S_ISLNK(st.st_mode) != 0);
Packit Service 20376f
Packit Service 20376f
	(void)p_unlink(path.ptr);
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	return symlinks_supported;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int create_empty_file(const char *path, mode_t mode)
Packit Service 20376f
{
Packit Service 20376f
	int fd;
Packit Service 20376f
Packit Service 20376f
	if ((fd = p_creat(path, mode)) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "error while creating '%s'", path);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (p_close(fd) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "error while closing '%s'", path);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int repo_local_config(
Packit Service 20376f
	git_config **out,
Packit Service 20376f
	git_buf *config_dir,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const char *repo_dir)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_config *parent;
Packit Service 20376f
	const char *cfg_path;
Packit Service 20376f
Packit Service 20376f
	if (git_buf_joinpath(config_dir, repo_dir, GIT_CONFIG_FILENAME_INREPO) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
	cfg_path = git_buf_cstr(config_dir);
Packit Service 20376f
Packit Service 20376f
	/* make LOCAL config if missing */
Packit Service 20376f
	if (!git_path_isfile(cfg_path) &&
Packit Service 20376f
		(error = create_empty_file(cfg_path, GIT_CONFIG_FILE_MODE)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	/* if no repo, just open that file directly */
Packit Service 20376f
	if (!repo)
Packit Service 20376f
		return git_config_open_ondisk(out, cfg_path);
Packit Service 20376f
Packit Service 20376f
	/* otherwise, open parent config and get that level */
Packit Service 20376f
	if ((error = git_repository_config__weakptr(&parent, repo)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (git_config_open_level(out, parent, GIT_CONFIG_LEVEL_LOCAL) < 0) {
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
Packit Service 20376f
		if (!(error = git_config_add_file_ondisk(
Packit Service 20376f
				parent, cfg_path, GIT_CONFIG_LEVEL_LOCAL, false)))
Packit Service 20376f
			error = git_config_open_level(out, parent, GIT_CONFIG_LEVEL_LOCAL);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_config_free(parent);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int repo_init_fs_configs(
Packit Service 20376f
	git_config *cfg,
Packit Service 20376f
	const char *cfg_path,
Packit Service 20376f
	const char *repo_dir,
Packit Service 20376f
	const char *work_dir,
Packit Service 20376f
	bool update_ignorecase)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if (!work_dir)
Packit Service 20376f
		work_dir = repo_dir;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_config_set_bool(
Packit Service 20376f
			cfg, "core.filemode", is_chmod_supported(cfg_path))) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (!are_symlinks_supported(work_dir)) {
Packit Service 20376f
		if ((error = git_config_set_bool(cfg, "core.symlinks", false)) < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
	} else if (git_config_delete_entry(cfg, "core.symlinks") < 0)
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
Packit Service 20376f
	if (update_ignorecase) {
Packit Service 20376f
		if (is_filesystem_case_insensitive(repo_dir)) {
Packit Service 20376f
			if ((error = git_config_set_bool(cfg, "core.ignorecase", true)) < 0)
Packit Service 20376f
				return error;
Packit Service 20376f
		} else if (git_config_delete_entry(cfg, "core.ignorecase") < 0)
Packit Service 20376f
			giterr_clear();
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_USE_ICONV
Packit Service 20376f
	if ((error = git_config_set_bool(
Packit Service 20376f
			cfg, "core.precomposeunicode",
Packit Service 20376f
			git_path_does_fs_decompose_unicode(work_dir))) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
	/* on non-iconv platforms, don't even set core.precomposeunicode */
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int repo_init_config(
Packit Service 20376f
	const char *repo_dir,
Packit Service 20376f
	const char *work_dir,
Packit Service 20376f
	uint32_t flags,
Packit Service 20376f
	uint32_t mode)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_buf cfg_path = GIT_BUF_INIT, worktree_path = GIT_BUF_INIT;
Packit Service 20376f
	git_config *config = NULL;
Packit Service 20376f
	bool is_bare = ((flags & GIT_REPOSITORY_INIT_BARE) != 0);
Packit Service 20376f
	bool is_reinit = ((flags & GIT_REPOSITORY_INIT__IS_REINIT) != 0);
Packit Service 20376f
Packit Service 20376f
	if ((error = repo_local_config(&config, &cfg_path, NULL, repo_dir)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if (is_reinit && (error = check_repositoryformatversion(config)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
#define SET_REPO_CONFIG(TYPE, NAME, VAL) do { \
Packit Service 20376f
	if ((error = git_config_set_##TYPE(config, NAME, VAL)) < 0) \
Packit Service 20376f
		goto cleanup; } while (0)
Packit Service 20376f
Packit Service 20376f
	SET_REPO_CONFIG(bool, "core.bare", is_bare);
Packit Service 20376f
	SET_REPO_CONFIG(int32, "core.repositoryformatversion", GIT_REPO_VERSION);
Packit Service 20376f
Packit Service 20376f
	if ((error = repo_init_fs_configs(
Packit Service 20376f
			config, cfg_path.ptr, repo_dir, work_dir, !is_reinit)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if (!is_bare) {
Packit Service 20376f
		SET_REPO_CONFIG(bool, "core.logallrefupdates", true);
Packit Service 20376f
Packit Service 20376f
		if (!(flags & GIT_REPOSITORY_INIT__NATURAL_WD)) {
Packit Service 20376f
			if ((error = git_buf_sets(&worktree_path, work_dir)) < 0)
Packit Service 20376f
				goto cleanup;
Packit Service 20376f
Packit Service 20376f
			if ((flags & GIT_REPOSITORY_INIT_RELATIVE_GITLINK))
Packit Service 20376f
				if ((error = git_path_make_relative(&worktree_path, repo_dir)) < 0)
Packit Service 20376f
					goto cleanup;
Packit Service 20376f
Packit Service 20376f
			SET_REPO_CONFIG(string, "core.worktree", worktree_path.ptr);
Packit Service 20376f
		} else if (is_reinit) {
Packit Service 20376f
			if (git_config_delete_entry(config, "core.worktree") < 0)
Packit Service 20376f
				giterr_clear();
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (mode == GIT_REPOSITORY_INIT_SHARED_GROUP) {
Packit Service 20376f
		SET_REPO_CONFIG(int32, "core.sharedrepository", 1);
Packit Service 20376f
		SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
Packit Service 20376f
	}
Packit Service 20376f
	else if (mode == GIT_REPOSITORY_INIT_SHARED_ALL) {
Packit Service 20376f
		SET_REPO_CONFIG(int32, "core.sharedrepository", 2);
Packit Service 20376f
		SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	git_buf_free(&cfg_path);
Packit Service 20376f
	git_buf_free(&worktree_path);
Packit Service 20376f
	git_config_free(config);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int repo_reinit_submodule_fs(git_submodule *sm, const char *n, void *p)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *smrepo = NULL;
Packit Service 20376f
	GIT_UNUSED(n); GIT_UNUSED(p);
Packit Service 20376f
Packit Service 20376f
	if (git_submodule_open(&smrepo, sm) < 0 ||
Packit Service 20376f
		git_repository_reinit_filesystem(smrepo, true) < 0)
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
	git_repository_free(smrepo);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_reinit_filesystem(git_repository *repo, int recurse)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	git_config *config = NULL;
Packit Service 20376f
	const char *repo_dir = git_repository_path(repo);
Packit Service 20376f
Packit Service 20376f
	if (!(error = repo_local_config(&config, &path, repo, repo_dir)))
Packit Service 20376f
		error = repo_init_fs_configs(
Packit Service 20376f
			config, path.ptr, repo_dir, git_repository_workdir(repo), true);
Packit Service 20376f
Packit Service 20376f
	git_config_free(config);
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	git_repository__cvar_cache_clear(repo);
Packit Service 20376f
Packit Service 20376f
	if (!repo->is_bare && recurse)
Packit Service 20376f
		(void)git_submodule_foreach(repo, repo_reinit_submodule_fs, NULL);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int repo_write_template(
Packit Service 20376f
	const char *git_dir,
Packit Service 20376f
	bool allow_overwrite,
Packit Service 20376f
	const char *file,
Packit Service 20376f
	mode_t mode,
Packit Service 20376f
	bool hidden,
Packit Service 20376f
	const char *content)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	int fd, error = 0, flags;
Packit Service 20376f
Packit Service 20376f
	if (git_buf_joinpath(&path, git_dir, file) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (allow_overwrite)
Packit Service 20376f
		flags = O_WRONLY | O_CREAT | O_TRUNC;
Packit Service 20376f
	else
Packit Service 20376f
		flags = O_WRONLY | O_CREAT | O_EXCL;
Packit Service 20376f
Packit Service 20376f
	fd = p_open(git_buf_cstr(&path), flags, mode);
Packit Service 20376f
Packit Service 20376f
	if (fd >= 0) {
Packit Service 20376f
		error = p_write(fd, content, strlen(content));
Packit Service 20376f
Packit Service 20376f
		p_close(fd);
Packit Service 20376f
	}
Packit Service 20376f
	else if (errno != EEXIST)
Packit Service 20376f
		error = fd;
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	if (!error && hidden) {
Packit Service 20376f
		if (git_win32__set_hidden(path.ptr, true) < 0)
Packit Service 20376f
			error = -1;
Packit Service 20376f
	}
Packit Service 20376f
#else
Packit Service 20376f
	GIT_UNUSED(hidden);
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	if (error)
Packit Service 20376f
		giterr_set(GITERR_OS,
Packit Service 20376f
			"failed to initialize repository with template '%s'", file);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int repo_write_gitlink(
Packit Service 20376f
	const char *in_dir, const char *to_repo, bool use_relative_path)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
	git_buf path_to_repo = GIT_BUF_INIT;
Packit Service 20376f
	struct stat st;
Packit Service 20376f
Packit Service 20376f
	git_path_dirname_r(&buf, to_repo);
Packit Service 20376f
	git_path_to_dir(&buf;;
Packit Service 20376f
	if (git_buf_oom(&buf))
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* don't write gitlink to natural workdir */
Packit Service 20376f
	if (git__suffixcmp(to_repo, "/" DOT_GIT "/") == 0 &&
Packit Service 20376f
		strcmp(in_dir, buf.ptr) == 0)
Packit Service 20376f
	{
Packit Service 20376f
		error = GIT_PASSTHROUGH;
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_joinpath(&buf, in_dir, DOT_GIT)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if (!p_stat(buf.ptr, &st) && !S_ISREG(st.st_mode)) {
Packit Service 20376f
		giterr_set(GITERR_REPOSITORY,
Packit Service 20376f
			"cannot overwrite gitlink file into path '%s'", in_dir);
Packit Service 20376f
		error = GIT_EEXISTS;
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_buf_clear(&buf;;
Packit Service 20376f
Packit Service 20376f
	error = git_buf_sets(&path_to_repo, to_repo);
Packit Service 20376f
Packit Service 20376f
	if (!error && use_relative_path)
Packit Service 20376f
		error = git_path_make_relative(&path_to_repo, in_dir);
Packit Service 20376f
Packit Service 20376f
	if (!error)
Packit Service 20376f
		error = git_buf_join(&buf, ' ', GIT_FILE_CONTENT_PREFIX, path_to_repo.ptr);
Packit Service 20376f
Packit Service 20376f
	if (!error)
Packit Service 20376f
		error = repo_write_template(in_dir, true, DOT_GIT, 0666, true, buf.ptr);
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
	git_buf_free(&path_to_repo);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static mode_t pick_dir_mode(git_repository_init_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	if (opts->mode == GIT_REPOSITORY_INIT_SHARED_UMASK)
Packit Service 20376f
		return 0777;
Packit Service 20376f
	if (opts->mode == GIT_REPOSITORY_INIT_SHARED_GROUP)
Packit Service 20376f
		return (0775 | S_ISGID);
Packit Service 20376f
	if (opts->mode == GIT_REPOSITORY_INIT_SHARED_ALL)
Packit Service 20376f
		return (0777 | S_ISGID);
Packit Service 20376f
	return opts->mode;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#include "repo_template.h"
Packit Service 20376f
Packit Service 20376f
static int repo_init_structure(
Packit Service 20376f
	const char *repo_dir,
Packit Service 20376f
	const char *work_dir,
Packit Service 20376f
	git_repository_init_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	repo_template_item *tpl;
Packit Service 20376f
	bool external_tpl =
Packit Service 20376f
		((opts->flags & GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE) != 0);
Packit Service 20376f
	mode_t dmode = pick_dir_mode(opts);
Packit Service 20376f
	bool chmod = opts->mode != GIT_REPOSITORY_INIT_SHARED_UMASK;
Packit Service 20376f
Packit Service 20376f
	/* Hide the ".git" directory */
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	if ((opts->flags & GIT_REPOSITORY_INIT__HAS_DOTGIT) != 0) {
Packit Service 20376f
		if (git_win32__set_hidden(repo_dir, true) < 0) {
Packit Service 20376f
			giterr_set(GITERR_OS,
Packit Service 20376f
				"failed to mark Git repository folder as hidden");
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	/* Create the .git gitlink if appropriate */
Packit Service 20376f
	if ((opts->flags & GIT_REPOSITORY_INIT_BARE) == 0 &&
Packit Service 20376f
		(opts->flags & GIT_REPOSITORY_INIT__NATURAL_WD) == 0)
Packit Service 20376f
	{
Packit Service 20376f
		if (repo_write_gitlink(work_dir, repo_dir, opts->flags & GIT_REPOSITORY_INIT_RELATIVE_GITLINK) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Copy external template if requested */
Packit Service 20376f
	if (external_tpl) {
Packit Service 20376f
		git_config *cfg = NULL;
Packit Service 20376f
		const char *tdir = NULL;
Packit Service 20376f
		bool default_template = false;
Packit Service 20376f
		git_buf template_buf = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
		if (opts->template_path)
Packit Service 20376f
			tdir = opts->template_path;
Packit Service 20376f
		else if ((error = git_config_open_default(&cfg)) >= 0) {
Packit Service 20376f
			if (!git_config_get_path(&template_buf, cfg, "init.templatedir"))
Packit Service 20376f
				tdir = template_buf.ptr;
Packit Service 20376f
			giterr_clear();
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (!tdir) {
Packit Service 20376f
			if (!(error = git_sysdir_find_template_dir(&template_buf)))
Packit Service 20376f
				tdir = template_buf.ptr;
Packit Service 20376f
			default_template = true;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/*
Packit Service 20376f
		 * If tdir was the empty string, treat it like tdir was a path to an
Packit Service 20376f
		 * empty directory (so, don't do any copying). This is the behavior
Packit Service 20376f
		 * that git(1) exhibits, although it doesn't seem to be officially
Packit Service 20376f
		 * documented.
Packit Service 20376f
		 */
Packit Service 20376f
		if (tdir && git__strcmp(tdir, "") != 0) {
Packit Service 20376f
			uint32_t cpflags = GIT_CPDIR_COPY_SYMLINKS |
Packit Service 20376f
				GIT_CPDIR_SIMPLE_TO_MODE |
Packit Service 20376f
				GIT_CPDIR_COPY_DOTFILES;
Packit Service 20376f
			if (opts->mode != GIT_REPOSITORY_INIT_SHARED_UMASK)
Packit Service 20376f
					cpflags |= GIT_CPDIR_CHMOD_DIRS;
Packit Service 20376f
			error = git_futils_cp_r(tdir, repo_dir, cpflags, dmode);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		git_buf_free(&template_buf);
Packit Service 20376f
		git_config_free(cfg);
Packit Service 20376f
Packit Service 20376f
		if (error < 0) {
Packit Service 20376f
			if (!default_template)
Packit Service 20376f
				return error;
Packit Service 20376f
Packit Service 20376f
			/* if template was default, ignore error and use internal */
Packit Service 20376f
			giterr_clear();
Packit Service 20376f
			external_tpl = false;
Packit Service 20376f
			error = 0;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Copy internal template
Packit Service 20376f
	 * - always ensure existence of dirs
Packit Service 20376f
	 * - only create files if no external template was specified
Packit Service 20376f
	 */
Packit Service 20376f
	for (tpl = repo_template; !error && tpl->path; ++tpl) {
Packit Service 20376f
		if (!tpl->content) {
Packit Service 20376f
			uint32_t mkdir_flags = GIT_MKDIR_PATH;
Packit Service 20376f
			if (chmod)
Packit Service 20376f
				mkdir_flags |= GIT_MKDIR_CHMOD;
Packit Service 20376f
Packit Service 20376f
			error = git_futils_mkdir_relative(
Packit Service 20376f
				tpl->path, repo_dir, dmode, mkdir_flags, NULL);
Packit Service 20376f
		}
Packit Service 20376f
		else if (!external_tpl) {
Packit Service 20376f
			const char *content = tpl->content;
Packit Service 20376f
Packit Service 20376f
			if (opts->description && strcmp(tpl->path, GIT_DESC_FILE) == 0)
Packit Service 20376f
				content = opts->description;
Packit Service 20376f
Packit Service 20376f
			error = repo_write_template(
Packit Service 20376f
				repo_dir, false, tpl->path, tpl->mode, false, content);
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int mkdir_parent(git_buf *buf, uint32_t mode, bool skip2)
Packit Service 20376f
{
Packit Service 20376f
	/* When making parent directories during repository initialization
Packit Service 20376f
	 * don't try to set gid or grant world write access
Packit Service 20376f
	 */
Packit Service 20376f
	return git_futils_mkdir(
Packit Service 20376f
		buf->ptr, mode & ~(S_ISGID | 0002),
Packit Service 20376f
		GIT_MKDIR_PATH | GIT_MKDIR_VERIFY_DIR |
Packit Service 20376f
		(skip2 ? GIT_MKDIR_SKIP_LAST2 : GIT_MKDIR_SKIP_LAST));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int repo_init_directories(
Packit Service 20376f
	git_buf *repo_path,
Packit Service 20376f
	git_buf *wd_path,
Packit Service 20376f
	const char *given_repo,
Packit Service 20376f
	git_repository_init_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	bool is_bare, add_dotgit, has_dotgit, natural_wd;
Packit Service 20376f
	mode_t dirmode;
Packit Service 20376f
Packit Service 20376f
	/* There are three possible rules for what we are allowed to create:
Packit Service 20376f
	 * - MKPATH means anything we need
Packit Service 20376f
	 * - MKDIR means just the .git directory and its parent and the workdir
Packit Service 20376f
	 * - Neither means only the .git directory can be created
Packit Service 20376f
	 *
Packit Service 20376f
	 * There are 5 "segments" of path that we might need to deal with:
Packit Service 20376f
	 * 1. The .git directory
Packit Service 20376f
	 * 2. The parent of the .git directory
Packit Service 20376f
	 * 3. Everything above the parent of the .git directory
Packit Service 20376f
	 * 4. The working directory (often the same as #2)
Packit Service 20376f
	 * 5. Everything above the working directory (often the same as #3)
Packit Service 20376f
	 *
Packit Service 20376f
	 * For all directories created, we start with the init_mode value for
Packit Service 20376f
	 * permissions and then strip off bits in some cases:
Packit Service 20376f
	 *
Packit Service 20376f
	 * For MKPATH, we create #3 (and #5) paths without S_ISGID or S_IWOTH
Packit Service 20376f
	 * For MKPATH and MKDIR, we create #2 (and #4) without S_ISGID
Packit Service 20376f
	 * For all rules, we create #1 using the untouched init_mode
Packit Service 20376f
	 */
Packit Service 20376f
Packit Service 20376f
	/* set up repo path */
Packit Service 20376f
Packit Service 20376f
	is_bare = ((opts->flags & GIT_REPOSITORY_INIT_BARE) != 0);
Packit Service 20376f
Packit Service 20376f
	add_dotgit =
Packit Service 20376f
		(opts->flags & GIT_REPOSITORY_INIT_NO_DOTGIT_DIR) == 0 &&
Packit Service 20376f
		!is_bare &&
Packit Service 20376f
		git__suffixcmp(given_repo, "/" DOT_GIT) != 0 &&
Packit Service 20376f
		git__suffixcmp(given_repo, "/" GIT_DIR) != 0;
Packit Service 20376f
Packit Service 20376f
	if (git_buf_joinpath(repo_path, given_repo, add_dotgit ? GIT_DIR : "") < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	has_dotgit = (git__suffixcmp(repo_path->ptr, "/" GIT_DIR) == 0);
Packit Service 20376f
	if (has_dotgit)
Packit Service 20376f
		opts->flags |= GIT_REPOSITORY_INIT__HAS_DOTGIT;
Packit Service 20376f
Packit Service 20376f
	/* set up workdir path */
Packit Service 20376f
Packit Service 20376f
	if (!is_bare) {
Packit Service 20376f
		if (opts->workdir_path) {
Packit Service 20376f
			if (git_path_join_unrooted(
Packit Service 20376f
					wd_path, opts->workdir_path, repo_path->ptr, NULL) < 0)
Packit Service 20376f
				return -1;
Packit Service 20376f
		} else if (has_dotgit) {
Packit Service 20376f
			if (git_path_dirname_r(wd_path, repo_path->ptr) < 0)
Packit Service 20376f
				return -1;
Packit Service 20376f
		} else {
Packit Service 20376f
			giterr_set(GITERR_REPOSITORY, "cannot pick working directory"
Packit Service 20376f
				" for non-bare repository that isn't a '.git' directory");
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (git_path_to_dir(wd_path) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
	} else {
Packit Service 20376f
		git_buf_clear(wd_path);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	natural_wd =
Packit Service 20376f
		has_dotgit &&
Packit Service 20376f
		wd_path->size > 0 &&
Packit Service 20376f
		wd_path->size + strlen(GIT_DIR) == repo_path->size &&
Packit Service 20376f
		memcmp(repo_path->ptr, wd_path->ptr, wd_path->size) == 0;
Packit Service 20376f
	if (natural_wd)
Packit Service 20376f
		opts->flags |= GIT_REPOSITORY_INIT__NATURAL_WD;
Packit Service 20376f
Packit Service 20376f
	/* create directories as needed / requested */
Packit Service 20376f
Packit Service 20376f
	dirmode = pick_dir_mode(opts);
Packit Service 20376f
Packit Service 20376f
	if ((opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0) {
Packit Service 20376f
		/* create path #5 */
Packit Service 20376f
		if (wd_path->size > 0 &&
Packit Service 20376f
			(error = mkdir_parent(wd_path, dirmode, false)) < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		/* create path #3 (if not the same as #5) */
Packit Service 20376f
		if (!natural_wd &&
Packit Service 20376f
			(error = mkdir_parent(repo_path, dirmode, has_dotgit)) < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0 ||
Packit Service 20376f
		(opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0)
Packit Service 20376f
	{
Packit Service 20376f
		/* create path #4 */
Packit Service 20376f
		if (wd_path->size > 0 &&
Packit Service 20376f
			(error = git_futils_mkdir(
Packit Service 20376f
				wd_path->ptr, dirmode & ~S_ISGID,
Packit Service 20376f
				GIT_MKDIR_VERIFY_DIR)) < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		/* create path #2 (if not the same as #4) */
Packit Service 20376f
		if (!natural_wd &&
Packit Service 20376f
			(error = git_futils_mkdir(
Packit Service 20376f
				repo_path->ptr, dirmode & ~S_ISGID,
Packit Service 20376f
				GIT_MKDIR_VERIFY_DIR | GIT_MKDIR_SKIP_LAST)) < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((opts->flags & GIT_REPOSITORY_INIT_MKDIR) != 0 ||
Packit Service 20376f
		(opts->flags & GIT_REPOSITORY_INIT_MKPATH) != 0 ||
Packit Service 20376f
		has_dotgit)
Packit Service 20376f
	{
Packit Service 20376f
		/* create path #1 */
Packit Service 20376f
		error = git_futils_mkdir(repo_path->ptr, dirmode,
Packit Service 20376f
			GIT_MKDIR_VERIFY_DIR | ((dirmode & S_ISGID) ? GIT_MKDIR_CHMOD : 0));
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* prettify both directories now that they are created */
Packit Service 20376f
Packit Service 20376f
	if (!error) {
Packit Service 20376f
		error = git_path_prettify_dir(repo_path, repo_path->ptr, NULL);
Packit Service 20376f
Packit Service 20376f
		if (!error && wd_path->size > 0)
Packit Service 20376f
			error = git_path_prettify_dir(wd_path, wd_path->ptr, NULL);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int repo_init_create_origin(git_repository *repo, const char *url)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_remote *remote;
Packit Service 20376f
Packit Service 20376f
	if (!(error = git_remote_create(&remote, repo, GIT_REMOTE_ORIGIN, url))) {
Packit Service 20376f
		git_remote_free(remote);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_init(
Packit Service 20376f
	git_repository **repo_out, const char *path, unsigned is_bare)
Packit Service 20376f
{
Packit Service 20376f
	git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
Packit Service 20376f
Packit Service 20376f
	opts.flags = GIT_REPOSITORY_INIT_MKPATH; /* don't love this default */
Packit Service 20376f
	if (is_bare)
Packit Service 20376f
		opts.flags |= GIT_REPOSITORY_INIT_BARE;
Packit Service 20376f
Packit Service 20376f
	return git_repository_init_ext(repo_out, path, &opts);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_init_ext(
Packit Service 20376f
	git_repository **out,
Packit Service 20376f
	const char *given_repo,
Packit Service 20376f
	git_repository_init_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_buf repo_path = GIT_BUF_INIT, wd_path = GIT_BUF_INIT,
Packit Service 20376f
		common_path = GIT_BUF_INIT;
Packit Service 20376f
	const char *wd;
Packit Service 20376f
Packit Service 20376f
	assert(out && given_repo && opts);
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_VERSION(opts, GIT_REPOSITORY_INIT_OPTIONS_VERSION, "git_repository_init_options");
Packit Service 20376f
Packit Service 20376f
	error = repo_init_directories(&repo_path, &wd_path, given_repo, opts);
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	wd = (opts->flags & GIT_REPOSITORY_INIT_BARE) ? NULL : git_buf_cstr(&wd_path);
Packit Service 20376f
	if (valid_repository_path(&repo_path, &common_path)) {
Packit Service 20376f
Packit Service 20376f
		if ((opts->flags & GIT_REPOSITORY_INIT_NO_REINIT) != 0) {
Packit Service 20376f
			giterr_set(GITERR_REPOSITORY,
Packit Service 20376f
				"attempt to reinitialize '%s'", given_repo);
Packit Service 20376f
			error = GIT_EEXISTS;
Packit Service 20376f
			goto cleanup;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		opts->flags |= GIT_REPOSITORY_INIT__IS_REINIT;
Packit Service 20376f
Packit Service 20376f
		error = repo_init_config(
Packit Service 20376f
			repo_path.ptr, wd, opts->flags, opts->mode);
Packit Service 20376f
Packit Service 20376f
		/* TODO: reinitialize the templates */
Packit Service 20376f
	}
Packit Service 20376f
	else {
Packit Service 20376f
		if (!(error = repo_init_structure(
Packit Service 20376f
				repo_path.ptr, wd, opts)) &&
Packit Service 20376f
			!(error = repo_init_config(
Packit Service 20376f
				repo_path.ptr, wd, opts->flags, opts->mode)))
Packit Service 20376f
			error = git_repository_create_head(
Packit Service 20376f
				repo_path.ptr, opts->initial_head);
Packit Service 20376f
	}
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	error = git_repository_open(out, repo_path.ptr);
Packit Service 20376f
Packit Service 20376f
	if (!error && opts->origin_url)
Packit Service 20376f
		error = repo_init_create_origin(*out, opts->origin_url);
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	git_buf_free(&common_path);
Packit Service 20376f
	git_buf_free(&repo_path);
Packit Service 20376f
	git_buf_free(&wd_path);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_head_detached(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *ref;
Packit Service 20376f
	git_odb *odb = NULL;
Packit Service 20376f
	int exists;
Packit Service 20376f
Packit Service 20376f
	if (git_repository_odb__weakptr(&odb, repo) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (git_reference_type(ref) == GIT_REF_SYMBOLIC) {
Packit Service 20376f
		git_reference_free(ref);
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	exists = git_odb_exists(odb, git_reference_target(ref));
Packit Service 20376f
Packit Service 20376f
	git_reference_free(ref);
Packit Service 20376f
	return exists;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int get_worktree_file_path(git_buf *out, git_repository *repo, const char *worktree, const char *file)
Packit Service 20376f
{
Packit Service 20376f
	git_buf_clear(out);
Packit Service 20376f
	return git_buf_printf(out, "%s/worktrees/%s/%s", repo->commondir, worktree, file);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_head_detached_for_worktree(git_repository *repo, const char *name)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *ref = NULL;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert(repo && name);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_repository_head_for_worktree(&ref, repo, name)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	error = (git_reference_type(ref) != GIT_REF_SYMBOLIC);
Packit Service 20376f
out:
Packit Service 20376f
	git_reference_free(ref);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_head(git_reference **head_out, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *head;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (git_reference_type(head) == GIT_REF_OID) {
Packit Service 20376f
		*head_out = head;
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = git_reference_lookup_resolved(head_out, repo, git_reference_symbolic_target(head), -1);
Packit Service 20376f
	git_reference_free(head);
Packit Service 20376f
Packit Service 20376f
	return error == GIT_ENOTFOUND ? GIT_EUNBORNBRANCH : error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_head_for_worktree(git_reference **out, git_repository *repo, const char *name)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	git_reference *head = NULL;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert(out && repo && name);
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	if ((error = get_worktree_file_path(&path, repo, name, GIT_HEAD_FILE)) < 0 ||
Packit Service 20376f
	    (error = git_reference__read_head(&head, repo, path.ptr)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	if (git_reference_type(head) != GIT_REF_OID) {
Packit Service 20376f
		git_reference *resolved;
Packit Service 20376f
Packit Service 20376f
		error = git_reference_lookup_resolved(&resolved, repo, git_reference_symbolic_target(head), -1);
Packit Service 20376f
		git_reference_free(head);
Packit Service 20376f
		head = resolved;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*out = head;
Packit Service 20376f
Packit Service 20376f
out:
Packit Service 20376f
	if (error)
Packit Service 20376f
		git_reference_free(head);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_foreach_head(git_repository *repo, git_repository_foreach_head_cb cb, void *payload)
Packit Service 20376f
{
Packit Service 20376f
	git_strarray worktrees = GIT_VECTOR_INIT;
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	int error;
Packit Service 20376f
	size_t i;
Packit Service 20376f
Packit Service 20376f
	/* Execute callback for HEAD of commondir */
Packit Service 20376f
	if ((error = git_buf_joinpath(&path, repo->commondir, GIT_HEAD_FILE)) < 0 ||
Packit Service 20376f
	    (error = cb(repo, path.ptr, payload) != 0))
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_worktree_list(&worktrees, repo)) < 0) {
Packit Service 20376f
		error = 0;
Packit Service 20376f
		goto out;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Execute callback for all worktree HEADs */
Packit Service 20376f
	for (i = 0; i < worktrees.count; i++) {
Packit Service 20376f
		if (get_worktree_file_path(&path, repo, worktrees.strings[i], GIT_HEAD_FILE) < 0)
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		if ((error = cb(repo, path.ptr, payload)) != 0)
Packit Service 20376f
			goto out;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
out:
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
	git_strarray_free(&worktrees);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_head_unborn(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *ref = NULL;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	error = git_repository_head(&ref, repo);
Packit Service 20376f
	git_reference_free(ref);
Packit Service 20376f
Packit Service 20376f
	if (error == GIT_EUNBORNBRANCH) {
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
		return 1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int at_least_one_cb(const char *refname, void *payload)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(refname);
Packit Service 20376f
	GIT_UNUSED(payload);
Packit Service 20376f
	return GIT_PASSTHROUGH;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int repo_contains_no_reference(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	int error = git_reference_foreach_name(repo, &at_least_one_cb, NULL);
Packit Service 20376f
Packit Service 20376f
	if (error == GIT_PASSTHROUGH)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (!error)
Packit Service 20376f
		return 1;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_is_empty(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *head = NULL;
Packit Service 20376f
	int is_empty = 0;
Packit Service 20376f
Packit Service 20376f
	if (git_reference_lookup(&head, repo, GIT_HEAD_FILE) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (git_reference_type(head) == GIT_REF_SYMBOLIC)
Packit Service 20376f
		is_empty =
Packit Service 20376f
			(strcmp(git_reference_symbolic_target(head),
Packit Service 20376f
					GIT_REFS_HEADS_DIR "master") == 0) &&
Packit Service 20376f
			repo_contains_no_reference(repo);
Packit Service 20376f
Packit Service 20376f
	git_reference_free(head);
Packit Service 20376f
Packit Service 20376f
	return is_empty;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_item_path(git_buf *out, git_repository *repo, git_repository_item_t item)
Packit Service 20376f
{
Packit Service 20376f
	const char *parent;
Packit Service 20376f
Packit Service 20376f
	switch (items[item].parent) {
Packit Service 20376f
		case GIT_REPOSITORY_ITEM_GITDIR:
Packit Service 20376f
			parent = git_repository_path(repo);
Packit Service 20376f
			break;
Packit Service 20376f
		case GIT_REPOSITORY_ITEM_WORKDIR:
Packit Service 20376f
			parent = git_repository_workdir(repo);
Packit Service 20376f
			break;
Packit Service 20376f
		case GIT_REPOSITORY_ITEM_COMMONDIR:
Packit Service 20376f
			parent = git_repository_commondir(repo);
Packit Service 20376f
			break;
Packit Service 20376f
		default:
Packit Service 20376f
			giterr_set(GITERR_INVALID, "invalid item directory");
Packit Service 20376f
			return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (parent == NULL) {
Packit Service 20376f
		giterr_set(GITERR_INVALID, "path cannot exist in repository");
Packit Service 20376f
		return GIT_ENOTFOUND;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (git_buf_sets(out, parent) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (items[item].name) {
Packit Service 20376f
		if (git_buf_joinpath(out, parent, items[item].name) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (items[item].directory) {
Packit Service 20376f
		if (git_path_to_dir(out) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const char *git_repository_path(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	assert(repo);
Packit Service 20376f
	return repo->gitdir;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const char *git_repository_workdir(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	assert(repo);
Packit Service 20376f
Packit Service 20376f
	if (repo->is_bare)
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	return repo->workdir;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const char *git_repository_commondir(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	assert(repo);
Packit Service 20376f
	return repo->commondir;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_set_workdir(
Packit Service 20376f
	git_repository *repo, const char *workdir, int update_gitlink)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	assert(repo && workdir);
Packit Service 20376f
Packit Service 20376f
	if (git_path_prettify_dir(&path, workdir, NULL) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (repo->workdir && strcmp(repo->workdir, path.ptr) == 0)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (update_gitlink) {
Packit Service 20376f
		git_config *config;
Packit Service 20376f
Packit Service 20376f
		if (git_repository_config__weakptr(&config, repo) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		error = repo_write_gitlink(path.ptr, git_repository_path(repo), false);
Packit Service 20376f
Packit Service 20376f
		/* passthrough error means gitlink is unnecessary */
Packit Service 20376f
		if (error == GIT_PASSTHROUGH)
Packit Service 20376f
			error = git_config_delete_entry(config, "core.worktree");
Packit Service 20376f
		else if (!error)
Packit Service 20376f
			error = git_config_set_string(config, "core.worktree", path.ptr);
Packit Service 20376f
Packit Service 20376f
		if (!error)
Packit Service 20376f
			error = git_config_set_bool(config, "core.bare", false);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!error) {
Packit Service 20376f
		char *old_workdir = repo->workdir;
Packit Service 20376f
Packit Service 20376f
		repo->workdir = git_buf_detach(&path);
Packit Service 20376f
		repo->is_bare = 0;
Packit Service 20376f
Packit Service 20376f
		git__free(old_workdir);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_is_bare(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	assert(repo);
Packit Service 20376f
	return repo->is_bare;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_is_worktree(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	assert(repo);
Packit Service 20376f
	return repo->is_worktree;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_set_bare(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_config *config;
Packit Service 20376f
Packit Service 20376f
	assert(repo);
Packit Service 20376f
Packit Service 20376f
	if (repo->is_bare)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_repository_config__weakptr(&config, repo)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_config_set_bool(config, "core.bare", true)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_config__update_entry(config, "core.worktree", NULL, true, true)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	git__free(repo->workdir);
Packit Service 20376f
	repo->workdir = NULL;
Packit Service 20376f
	repo->is_bare = 1;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_head_tree(git_tree **tree, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *head;
Packit Service 20376f
	git_object *obj;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_repository_head(&head, repo)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_reference_peel(&obj, head, GIT_OBJ_TREE)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	*tree = (git_tree *)obj;
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	git_reference_free(head);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository__set_orig_head(git_repository *repo, const git_oid *orig_head)
Packit Service 20376f
{
Packit Service 20376f
	git_filebuf file = GIT_FILEBUF_INIT;
Packit Service 20376f
	git_buf file_path = GIT_BUF_INIT;
Packit Service 20376f
	char orig_head_str[GIT_OID_HEXSZ];
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	git_oid_fmt(orig_head_str, orig_head);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_ORIG_HEAD_FILE)) == 0 &&
Packit Service 20376f
		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_MERGE_FILE_MODE)) == 0 &&
Packit Service 20376f
		(error = git_filebuf_printf(&file, "%.*s\n", GIT_OID_HEXSZ, orig_head_str)) == 0)
Packit Service 20376f
		error = git_filebuf_commit(&file;;
Packit Service 20376f
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		git_filebuf_cleanup(&file;;
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&file_path);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_message(git_buf *out, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	struct stat st;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	git_buf_sanitize(out);
Packit Service 20376f
Packit Service 20376f
	if (git_buf_joinpath(&path, repo->gitdir, GIT_MERGE_MSG_FILE) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if ((error = p_stat(git_buf_cstr(&path), &st)) < 0) {
Packit Service 20376f
		if (errno == ENOENT)
Packit Service 20376f
			error = GIT_ENOTFOUND;
Packit Service 20376f
		giterr_set(GITERR_OS, "could not access message file");
Packit Service 20376f
	} else {
Packit Service 20376f
		error = git_futils_readbuffer(out, git_buf_cstr(&path));
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_message_remove(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if (git_buf_joinpath(&path, repo->gitdir, GIT_MERGE_MSG_FILE) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	error = p_unlink(git_buf_cstr(&path));
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_hashfile(
Packit Service 20376f
	git_oid *out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	git_otype type,
Packit Service 20376f
	const char *as_path)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_filter_list *fl = NULL;
Packit Service 20376f
	git_file fd = -1;
Packit Service 20376f
	git_off_t len;
Packit Service 20376f
	git_buf full_path = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	assert(out && path && repo); /* as_path can be NULL */
Packit Service 20376f
Packit Service 20376f
	/* At some point, it would be nice if repo could be NULL to just
Packit Service 20376f
	 * apply filter rules defined in system and global files, but for
Packit Service 20376f
	 * now that is not possible because git_filters_load() needs it.
Packit Service 20376f
	 */
Packit Service 20376f
Packit Service 20376f
	error = git_path_join_unrooted(
Packit Service 20376f
		&full_path, path, git_repository_workdir(repo), NULL);
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (!as_path)
Packit Service 20376f
		as_path = path;
Packit Service 20376f
Packit Service 20376f
	/* passing empty string for "as_path" indicated --no-filters */
Packit Service 20376f
	if (strlen(as_path) > 0) {
Packit Service 20376f
		error = git_filter_list_load(
Packit Service 20376f
			&fl, repo, NULL, as_path,
Packit Service 20376f
			GIT_FILTER_TO_ODB, GIT_FILTER_DEFAULT);
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
	} else {
Packit Service 20376f
		error = 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* at this point, error is a count of the number of loaded filters */
Packit Service 20376f
Packit Service 20376f
	fd = git_futils_open_ro(full_path.ptr);
Packit Service 20376f
	if (fd < 0) {
Packit Service 20376f
		error = fd;
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	len = git_futils_filesize(fd);
Packit Service 20376f
	if (len < 0) {
Packit Service 20376f
		error = (int)len;
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!git__is_sizet(len)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "file size overflow for 32-bit systems");
Packit Service 20376f
		error = -1;
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = git_odb__hashfd_filtered(out, fd, (size_t)len, type, fl);
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	if (fd >= 0)
Packit Service 20376f
		p_close(fd);
Packit Service 20376f
	git_filter_list_free(fl);
Packit Service 20376f
	git_buf_free(&full_path);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int checkout_message(git_buf *out, git_reference *old, const char *new)
Packit Service 20376f
{
Packit Service 20376f
	git_buf_puts(out, "checkout: moving from ");
Packit Service 20376f
Packit Service 20376f
	if (git_reference_type(old) == GIT_REF_SYMBOLIC)
Packit Service 20376f
		git_buf_puts(out, git_reference__shorthand(git_reference_symbolic_target(old)));
Packit Service 20376f
	else
Packit Service 20376f
		git_buf_puts(out, git_oid_tostr_s(git_reference_target(old)));
Packit Service 20376f
Packit Service 20376f
	git_buf_puts(out, " to ");
Packit Service 20376f
Packit Service 20376f
	if (git_reference__is_branch(new) ||
Packit Service 20376f
		git_reference__is_tag(new) ||
Packit Service 20376f
		git_reference__is_remote(new))
Packit Service 20376f
		git_buf_puts(out, git_reference__shorthand(new));
Packit Service 20376f
	else
Packit Service 20376f
		git_buf_puts(out, new);
Packit Service 20376f
Packit Service 20376f
	if (git_buf_oom(out))
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int detach(git_repository *repo, const git_oid *id, const char *new)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_buf log_message = GIT_BUF_INIT;
Packit Service 20376f
	git_object *object = NULL, *peeled = NULL;
Packit Service 20376f
	git_reference *new_head = NULL, *current = NULL;
Packit Service 20376f
Packit Service 20376f
	assert(repo && id);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_reference_lookup(&current, repo, GIT_HEAD_FILE)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_object_lookup(&object, repo, id, GIT_OBJ_ANY)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_object_peel(&peeled, object, GIT_OBJ_COMMIT)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if (new == NULL)
Packit Service 20376f
		new = git_oid_tostr_s(git_object_id(peeled));
Packit Service 20376f
Packit Service 20376f
	if ((error = checkout_message(&log_message, current, new)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_object_id(peeled), true, git_buf_cstr(&log_message));
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	git_buf_free(&log_message);
Packit Service 20376f
	git_object_free(object);
Packit Service 20376f
	git_object_free(peeled);
Packit Service 20376f
	git_reference_free(current);
Packit Service 20376f
	git_reference_free(new_head);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_set_head(
Packit Service 20376f
	git_repository* repo,
Packit Service 20376f
	const char* refname)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *ref = NULL, *current = NULL, *new_head = NULL;
Packit Service 20376f
	git_buf log_message = GIT_BUF_INIT;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert(repo && refname);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_reference_lookup(&current, repo, GIT_HEAD_FILE)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if ((error = checkout_message(&log_message, current, refname)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	error = git_reference_lookup(&ref, repo, refname);
Packit Service 20376f
	if (error < 0 && error != GIT_ENOTFOUND)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if (ref && current->type == GIT_REF_SYMBOLIC && git__strcmp(current->target.symbolic, ref->name) &&
Packit Service 20376f
	    git_reference_is_branch(ref) && git_branch_is_checked_out(ref)) {
Packit Service 20376f
		giterr_set(GITERR_REPOSITORY, "cannot set HEAD to reference '%s' as it is the current HEAD "
Packit Service 20376f
			"of a linked repository.", git_reference_name(ref));
Packit Service 20376f
		error = -1;
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!error) {
Packit Service 20376f
		if (git_reference_is_branch(ref)) {
Packit Service 20376f
			error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE,
Packit Service 20376f
					git_reference_name(ref), true, git_buf_cstr(&log_message));
Packit Service 20376f
		} else {
Packit Service 20376f
			error = detach(repo, git_reference_target(ref),
Packit Service 20376f
				git_reference_is_tag(ref) || git_reference_is_remote(ref) ? refname : NULL);
Packit Service 20376f
		}
Packit Service 20376f
	} else if (git_reference__is_branch(refname)) {
Packit Service 20376f
		error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, refname,
Packit Service 20376f
				true, git_buf_cstr(&log_message));
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	git_buf_free(&log_message);
Packit Service 20376f
	git_reference_free(current);
Packit Service 20376f
	git_reference_free(ref);
Packit Service 20376f
	git_reference_free(new_head);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_set_head_detached(
Packit Service 20376f
	git_repository* repo,
Packit Service 20376f
	const git_oid* commitish)
Packit Service 20376f
{
Packit Service 20376f
	return detach(repo, commitish, NULL);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_set_head_detached_from_annotated(
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const git_annotated_commit *commitish)
Packit Service 20376f
{
Packit Service 20376f
	assert(repo && commitish);
Packit Service 20376f
Packit Service 20376f
	return detach(repo, git_annotated_commit_id(commitish), commitish->description);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_detach_head(git_repository* repo)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *old_head = NULL,	*new_head = NULL, *current = NULL;
Packit Service 20376f
	git_object *object = NULL;
Packit Service 20376f
	git_buf log_message = GIT_BUF_INIT;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert(repo);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_reference_lookup(&current, repo, GIT_HEAD_FILE)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_repository_head(&old_head, repo)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_object_lookup(&object, repo, git_reference_target(old_head), GIT_OBJ_COMMIT)) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	if ((error = checkout_message(&log_message, current, git_oid_tostr_s(git_object_id(object)))) < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_reference_target(old_head),
Packit Service 20376f
			1, git_buf_cstr(&log_message));
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	git_buf_free(&log_message);
Packit Service 20376f
	git_object_free(object);
Packit Service 20376f
	git_reference_free(old_head);
Packit Service 20376f
	git_reference_free(new_head);
Packit Service 20376f
	git_reference_free(current);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Loosely ported from git.git
Packit Service 20376f
 * https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh#L198-289
Packit Service 20376f
 */
Packit Service 20376f
int git_repository_state(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	git_buf repo_path = GIT_BUF_INIT;
Packit Service 20376f
	int state = GIT_REPOSITORY_STATE_NONE;
Packit Service 20376f
Packit Service 20376f
	assert(repo);
Packit Service 20376f
Packit Service 20376f
	if (git_buf_puts(&repo_path, repo->gitdir) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (git_path_contains_file(&repo_path, GIT_REBASE_MERGE_INTERACTIVE_FILE))
Packit Service 20376f
		state = GIT_REPOSITORY_STATE_REBASE_INTERACTIVE;
Packit Service 20376f
	else if (git_path_contains_dir(&repo_path, GIT_REBASE_MERGE_DIR))
Packit Service 20376f
		state = GIT_REPOSITORY_STATE_REBASE_MERGE;
Packit Service 20376f
	else if (git_path_contains_file(&repo_path, GIT_REBASE_APPLY_REBASING_FILE))
Packit Service 20376f
		state = GIT_REPOSITORY_STATE_REBASE;
Packit Service 20376f
	else if (git_path_contains_file(&repo_path, GIT_REBASE_APPLY_APPLYING_FILE))
Packit Service 20376f
		state = GIT_REPOSITORY_STATE_APPLY_MAILBOX;
Packit Service 20376f
	else if (git_path_contains_dir(&repo_path, GIT_REBASE_APPLY_DIR))
Packit Service 20376f
		state = GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE;
Packit Service 20376f
	else if (git_path_contains_file(&repo_path, GIT_MERGE_HEAD_FILE))
Packit Service 20376f
		state = GIT_REPOSITORY_STATE_MERGE;
Packit Service 20376f
	else if (git_path_contains_file(&repo_path, GIT_REVERT_HEAD_FILE)) {
Packit Service 20376f
		state = GIT_REPOSITORY_STATE_REVERT;
Packit Service 20376f
		if (git_path_contains_file(&repo_path, GIT_SEQUENCER_TODO_FILE)) {
Packit Service 20376f
			state = GIT_REPOSITORY_STATE_REVERT_SEQUENCE;
Packit Service 20376f
		}
Packit Service 20376f
	} else if (git_path_contains_file(&repo_path, GIT_CHERRYPICK_HEAD_FILE)) {
Packit Service 20376f
		state = GIT_REPOSITORY_STATE_CHERRYPICK;
Packit Service 20376f
		if (git_path_contains_file(&repo_path, GIT_SEQUENCER_TODO_FILE)) {
Packit Service 20376f
			state = GIT_REPOSITORY_STATE_CHERRYPICK_SEQUENCE;
Packit Service 20376f
		}
Packit Service 20376f
	} else if (git_path_contains_file(&repo_path, GIT_BISECT_LOG_FILE))
Packit Service 20376f
		state = GIT_REPOSITORY_STATE_BISECT;
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&repo_path);
Packit Service 20376f
	return state;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository__cleanup_files(
Packit Service 20376f
	git_repository *repo, const char *files[], size_t files_len)
Packit Service 20376f
{
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
	size_t i;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	for (error = 0, i = 0; !error && i < files_len; ++i) {
Packit Service 20376f
		const char *path;
Packit Service 20376f
Packit Service 20376f
		if (git_buf_joinpath(&buf, repo->gitdir, files[i]) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		path = git_buf_cstr(&buf;;
Packit Service 20376f
Packit Service 20376f
		if (git_path_isfile(path)) {
Packit Service 20376f
			error = p_unlink(path);
Packit Service 20376f
		} else if (git_path_isdir(path)) {
Packit Service 20376f
			error = git_futils_rmdir_r(path, NULL,
Packit Service 20376f
				GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		git_buf_clear(&buf;;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static const char *state_files[] = {
Packit Service 20376f
	GIT_MERGE_HEAD_FILE,
Packit Service 20376f
	GIT_MERGE_MODE_FILE,
Packit Service 20376f
	GIT_MERGE_MSG_FILE,
Packit Service 20376f
	GIT_REVERT_HEAD_FILE,
Packit Service 20376f
	GIT_CHERRYPICK_HEAD_FILE,
Packit Service 20376f
	GIT_BISECT_LOG_FILE,
Packit Service 20376f
	GIT_REBASE_MERGE_DIR,
Packit Service 20376f
	GIT_REBASE_APPLY_DIR,
Packit Service 20376f
	GIT_SEQUENCER_DIR,
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
int git_repository_state_cleanup(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	assert(repo);
Packit Service 20376f
Packit Service 20376f
	return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_is_shallow(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	struct stat st;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_joinpath(&path, repo->gitdir, "shallow")) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	error = git_path_lstat(path.ptr, &st);
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	if (error == GIT_ENOTFOUND) {
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
	return st.st_size == 0 ? 0 : 1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_init_init_options(
Packit Service 20376f
	git_repository_init_options *opts, unsigned int version)
Packit Service 20376f
{
Packit Service 20376f
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
Packit Service 20376f
		opts, version, git_repository_init_options,
Packit Service 20376f
		GIT_REPOSITORY_INIT_OPTIONS_INIT);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_ident(const char **name, const char **email, const git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	*name = repo->ident_name;
Packit Service 20376f
	*email = repo->ident_email;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_set_ident(git_repository *repo, const char *name, const char *email)
Packit Service 20376f
{
Packit Service 20376f
	char *tmp_name = NULL, *tmp_email = NULL;
Packit Service 20376f
Packit Service 20376f
	if (name) {
Packit Service 20376f
		tmp_name = git__strdup(name);
Packit Service 20376f
		GITERR_CHECK_ALLOC(tmp_name);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (email) {
Packit Service 20376f
		tmp_email = git__strdup(email);
Packit Service 20376f
		GITERR_CHECK_ALLOC(tmp_email);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	tmp_name = git__swap(repo->ident_name, tmp_name);
Packit Service 20376f
	tmp_email = git__swap(repo->ident_email, tmp_email);
Packit Service 20376f
Packit Service 20376f
	git__free(tmp_name);
Packit Service 20376f
	git__free(tmp_email);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_submodule_cache_all(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert(repo);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_strmap_alloc(&repo->submodule_cache)))
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	error = git_submodule__map(repo, repo->submodule_cache);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_repository_submodule_cache_clear(git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	git_submodule *sm;
Packit Service 20376f
	assert(repo);
Packit Service 20376f
	if (repo->submodule_cache == NULL) {
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
	git_strmap_foreach_value(repo->submodule_cache, sm, {
Packit Service 20376f
		git_submodule_free(sm);
Packit Service 20376f
	});
Packit Service 20376f
	git_strmap_free(repo->submodule_cache);
Packit Service 20376f
	repo->submodule_cache = 0;
Packit Service 20376f
	return 0;
Packit Service 20376f
}