Example #1
0
GIT_INLINE(unsigned int) dotgit_flags(
	git_repository *repo,
	unsigned int flags)
{
	int protectHFS = 0, protectNTFS = 0;

#ifdef __APPLE__
	protectHFS = 1;
#endif

#ifdef GIT_WIN32
	protectNTFS = 1;
#endif

	if (repo && !protectHFS)
		git_repository__cvar(&protectHFS, repo, GIT_CVAR_PROTECTHFS);
	if (protectHFS)
		flags |= GIT_PATH_REJECT_DOT_GIT_HFS;

	if (repo && !protectNTFS)
		git_repository__cvar(&protectNTFS, repo, GIT_CVAR_PROTECTNTFS);
	if (protectNTFS)
		flags |= GIT_PATH_REJECT_DOT_GIT_NTFS;

	return flags;
}
Example #2
0
static int diff_file_content_load_workdir_symlink(
	git_diff_file_content *fc, git_buf *path)
{
	ssize_t alloc_len, read_len;
	int symlink_supported, error;

	if ((error = git_repository__cvar(
		&symlink_supported, fc->repo, GIT_CVAR_SYMLINKS)) < 0)
		return -1;

	if (!symlink_supported)
		return diff_file_content_load_workdir_symlink_fake(fc, path);

	/* link path on disk could be UTF-16, so prepare a buffer that is
	 * big enough to handle some UTF-8 data expansion
	 */
	alloc_len = (ssize_t)(fc->file->size * 2) + 1;

	fc->map.data = git__calloc(alloc_len, sizeof(char));
	GITERR_CHECK_ALLOC(fc->map.data);

	fc->flags |= GIT_DIFF_FLAG__FREE_DATA;

	read_len = p_readlink(git_buf_cstr(path), fc->map.data, alloc_len);
	if (read_len < 0) {
		giterr_set(GITERR_OS, "failed to read symlink '%s'", fc->file->path);
		return -1;
	}

	fc->map.len = read_len;
	return 0;
}
static int diff_print_info_init__common(
	diff_print_info *pi,
	git_buf *out,
	git_repository *repo,
	git_diff_format_t format,
	git_diff_line_cb cb,
	void *payload)
{
	pi->format = format;
	pi->print_cb = cb;
	pi->payload = payload;
	pi->buf = out;

	if (!pi->oid_strlen) {
		if (!repo)
			pi->oid_strlen = GIT_ABBREV_DEFAULT;
		else if (git_repository__cvar(&pi->oid_strlen, repo, GIT_CVAR_ABBREV) < 0)
			return -1;
	}

	pi->oid_strlen += 1; /* for NUL byte */

	if (pi->oid_strlen > GIT_OID_HEXSZ + 1)
		pi->oid_strlen = GIT_OID_HEXSZ + 1;

	memset(&pi->line, 0, sizeof(pi->line));
	pi->line.old_lineno = -1;
	pi->line.new_lineno = -1;
	pi->line.num_lines = 1;

	return 0;
}
Example #4
0
static int symlink_or_fake(git_repository *repo, const char *a, const char *b)
{
	int symlinks;

	cl_git_pass(git_repository__cvar(&symlinks, repo, GIT_CVAR_SYMLINKS));

	if (symlinks)
		return p_symlink(a, b);
	else
		return git_futils_fake_symlink(a, b);
}
Example #5
0
static int reference_normalize_for_repo(
	git_refname_t out,
	git_repository *repo,
	const char *name)
{
	int precompose;
	unsigned int flags = GIT_REF_FORMAT_ALLOW_ONELEVEL;

	if (!git_repository__cvar(&precompose, repo, GIT_CVAR_PRECOMPOSE) &&
		precompose)
		flags |= GIT_REF_FORMAT__PRECOMPOSE_UNICODE;

	return git_reference_normalize_name(out, GIT_REFNAME_MAX, name, flags);
}
Example #6
0
File: crlf.c Project: 0CV0/libgit2
static int find_and_add_filter(
	git_vector *filters, git_repository *repo, const char *path,
	int (*apply)(struct git_filter *self, git_buf *dest, const git_buf *source))
{
	struct crlf_attrs ca;
	struct crlf_filter *filter;
	size_t pathlen;
	int error;

	/* Load gitattributes for the path */
	if ((error = crlf_load_attributes(&ca, repo, path)) < 0)
		return error;

	/*
	 * Use the core Git logic to see if we should perform CRLF for this file
	 * based on its attributes & the value of `core.autocrlf`
	 */
	ca.crlf_action = crlf_input_action(&ca);

	if (ca.crlf_action == GIT_CRLF_BINARY)
		return 0;

	if (ca.crlf_action == GIT_CRLF_GUESS) {
		int auto_crlf;

		if ((error = git_repository__cvar(&auto_crlf, repo, GIT_CVAR_AUTO_CRLF)) < 0)
			return error;

		if (auto_crlf == GIT_AUTO_CRLF_FALSE)
			return 0;
	}

	/* If we're good, we create a new filter object and push it
	 * into the filters array */
	pathlen = strlen(path);
	filter = git__malloc(sizeof(struct crlf_filter) + pathlen + 1);
	GITERR_CHECK_ALLOC(filter);

	filter->f.apply = apply;
	filter->f.do_free = NULL;
	memcpy(&filter->attrs, &ca, sizeof(struct crlf_attrs));
	filter->repo = repo;
	memcpy(filter->path, path, pathlen + 1);

	return git_vector_insert(filters, filter);
}
Example #7
0
int git_object_short_id(git_buf *out, const git_object *obj)
{
	git_repository *repo;
	int len = GIT_ABBREV_DEFAULT, error;
	git_oid id = {{0}};
	git_odb *odb;

	assert(out && obj);

	git_buf_sanitize(out);
	repo = git_object_owner(obj);

	if ((error = git_repository__cvar(&len, repo, GIT_CVAR_ABBREV)) < 0)
		return error;

	if ((error = git_repository_odb(&odb, repo)) < 0)
		return error;

	while (len < GIT_OID_HEXSZ) {
		/* set up short oid */
		memcpy(&id.id, &obj->cached.oid.id, (len + 1) / 2);
		if (len & 1)
			id.id[len / 2] &= 0xf0;

		error = git_odb_exists_prefix(NULL, odb, &id, len);
		if (error != GIT_EAMBIGUOUS)
			break;

		giterr_clear();
		len++;
	}

	if (!error && !(error = git_buf_grow(out, len + 1))) {
		git_oid_tostr(out->ptr, len + 1, &id);
		out->size = len;
	}

	git_odb_free(odb);

	return error;
}
Example #8
0
static int diff_print_info_init(
	diff_print_info *pi,
	git_buf *out,
	git_diff *diff,
	git_diff_format_t format,
	git_diff_line_cb cb,
	void *payload)
{
	pi->diff     = diff;
	pi->format   = format;
	pi->print_cb = cb;
	pi->payload  = payload;
	pi->buf      = out;

	if (diff)
		pi->flags = diff->opts.flags;
	else
		pi->flags = 0;

	if (diff && diff->opts.id_abbrev != 0)
		pi->oid_strlen = diff->opts.id_abbrev;
	else if (!diff || !diff->repo)
		pi->oid_strlen = GIT_ABBREV_DEFAULT;
	else if (git_repository__cvar(
		&pi->oid_strlen, diff->repo, GIT_CVAR_ABBREV) < 0)
		return -1;

	pi->oid_strlen += 1; /* for NUL byte */

	if (pi->oid_strlen < 2)
		pi->oid_strlen = 2;
	else if (pi->oid_strlen > GIT_OID_HEXSZ + 1)
		pi->oid_strlen = GIT_OID_HEXSZ + 1;

	memset(&pi->line, 0, sizeof(pi->line));
	pi->line.old_lineno = -1;
	pi->line.new_lineno = -1;
	pi->line.num_lines  = 1;

	return 0;
}
Example #9
0
static int diff_print_info_init(
	diff_print_info *pi,
	git_buf *out, git_diff_list *diff, git_diff_data_cb cb, void *payload)
{
	pi->diff     = diff;
	pi->print_cb = cb;
	pi->payload  = payload;
	pi->buf      = out;

	if (!diff || !diff->repo)
		pi->oid_strlen = GIT_ABBREV_DEFAULT;
	else if (git_repository__cvar(
		&pi->oid_strlen, diff->repo, GIT_CVAR_ABBREV) < 0)
		return -1;

	pi->oid_strlen += 1; /* for NUL byte */

	if (pi->oid_strlen < 2)
		pi->oid_strlen = 2;
	else if (pi->oid_strlen > GIT_OID_HEXSZ + 1)
		pi->oid_strlen = GIT_OID_HEXSZ + 1;

	return 0;
}
Example #10
0
static int crlf_check(
	git_filter        *self,
	void              **payload, /* points to NULL ptr on entry, may be set */
	const git_filter_source *src,
	const char **attr_values)
{
	int error;
	struct crlf_attrs ca;

	GIT_UNUSED(self);

	if (!attr_values) {
		ca.crlf_action = GIT_CRLF_GUESS;
		ca.eol = GIT_EOL_UNSET;
	} else {
		ca.crlf_action = check_crlf(attr_values[2]); /* text */
		if (ca.crlf_action == GIT_CRLF_GUESS)
			ca.crlf_action = check_crlf(attr_values[0]); /* clrf */
		ca.eol = check_eol(attr_values[1]); /* eol */
	}
	ca.auto_crlf = GIT_AUTO_CRLF_DEFAULT;

	/*
	 * Use the core Git logic to see if we should perform CRLF for this file
	 * based on its attributes & the value of `core.autocrlf`
	 */
	ca.crlf_action = crlf_input_action(&ca);

	if (ca.crlf_action == GIT_CRLF_BINARY)
		return GIT_PASSTHROUGH;

	if (ca.crlf_action == GIT_CRLF_GUESS ||
		(ca.crlf_action == GIT_CRLF_AUTO &&
		git_filter_source_mode(src) == GIT_FILTER_SMUDGE)) {

		error = git_repository__cvar(
			&ca.auto_crlf, git_filter_source_repo(src), GIT_CVAR_AUTO_CRLF);
		if (error < 0)
			return error;

		if (ca.auto_crlf == GIT_AUTO_CRLF_FALSE)
			return GIT_PASSTHROUGH;

		if (ca.auto_crlf == GIT_AUTO_CRLF_INPUT &&
			git_filter_source_mode(src) == GIT_FILTER_SMUDGE)
			return GIT_PASSTHROUGH;
	}

	if (git_filter_source_mode(src) == GIT_FILTER_CLEAN) {
		error = git_repository__cvar(
			&ca.safe_crlf, git_filter_source_repo(src), GIT_CVAR_SAFE_CRLF);
		if (error < 0)
			return error;

		/* downgrade FAIL to WARN if ALLOW_UNSAFE option is used */
		if ((git_filter_source_options(src) & GIT_FILTER_OPT_ALLOW_UNSAFE) &&
			ca.safe_crlf == GIT_SAFE_CRLF_FAIL)
			ca.safe_crlf = GIT_SAFE_CRLF_WARN;
	}

	*payload = git__malloc(sizeof(ca));
	GITERR_CHECK_ALLOC(*payload);
	memcpy(*payload, &ca, sizeof(ca));

	return 0;
}
Example #11
0
void test_repo_config__read_with_no_configs_at_all(void)
{
	git_repository *repo;
	int val;

	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));

	/* with none */

	cl_must_pass(p_unlink("empty_standard_repo/.git/config"));
	cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));

	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
	git_repository__cvar_cache_clear(repo);
	val = -1;
	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
	cl_assert_equal_i(GIT_ABBREV_DEFAULT, val);
	git_repository_free(repo);

	/* with no local config, just system */

	cl_sandbox_set_search_path_defaults();

	cl_must_pass(p_mkdir("alternate/1", 0777));
	cl_git_pass(git_buf_joinpath(&path, path.ptr, "1"));
	cl_git_rewritefile("alternate/1/gitconfig", "[core]\n\tabbrev = 10\n");
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));

	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
	git_repository__cvar_cache_clear(repo);
	val = -1;
	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
	cl_assert_equal_i(10, val);
	git_repository_free(repo);

	/* with just xdg + system */

	cl_must_pass(p_mkdir("alternate/2", 0777));
	path.ptr[path.size - 1] = '2';
	cl_git_rewritefile("alternate/2/config", "[core]\n\tabbrev = 20\n");
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));

	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
	git_repository__cvar_cache_clear(repo);
	val = -1;
	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
	cl_assert_equal_i(20, val);
	git_repository_free(repo);

	/* with global + xdg + system */

	cl_must_pass(p_mkdir("alternate/3", 0777));
	path.ptr[path.size - 1] = '3';
	cl_git_rewritefile("alternate/3/.gitconfig", "[core]\n\tabbrev = 30\n");
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));

	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
	git_repository__cvar_cache_clear(repo);
	val = -1;
	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
	cl_assert_equal_i(30, val);
	git_repository_free(repo);

	/* with all configs */

	cl_git_rewritefile("empty_standard_repo/.git/config", "[core]\n\tabbrev = 40\n");

	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
	git_repository__cvar_cache_clear(repo);
	val = -1;
	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
	cl_assert_equal_i(40, val);
	git_repository_free(repo);

	/* with all configs but delete the files ? */

	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
	git_repository__cvar_cache_clear(repo);
	val = -1;
	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
	cl_assert_equal_i(40, val);

	cl_must_pass(p_unlink("empty_standard_repo/.git/config"));
	cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));

	cl_must_pass(p_unlink("alternate/1/gitconfig"));
	cl_assert(!git_path_isfile("alternate/1/gitconfig"));

	cl_must_pass(p_unlink("alternate/2/config"));
	cl_assert(!git_path_isfile("alternate/2/config"));

	cl_must_pass(p_unlink("alternate/3/.gitconfig"));
	cl_assert(!git_path_isfile("alternate/3/.gitconfig"));

	git_repository__cvar_cache_clear(repo);
	val = -1;
	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
	cl_assert_equal_i(40, val);
	git_repository_free(repo);

	/* reopen */

	cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));
	cl_assert(!git_path_isfile("alternate/3/.gitconfig"));

	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
	git_repository__cvar_cache_clear(repo);
	val = -1;
	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
	cl_assert_equal_i(7, val);
	git_repository_free(repo);

	cl_assert(!git_path_exists("empty_standard_repo/.git/config"));
	cl_assert(!git_path_exists("alternate/3/.gitconfig"));
}