Blame src/crlf.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
Packit Service 20376f
#include "git2/attr.h"
Packit Service 20376f
#include "git2/blob.h"
Packit Service 20376f
#include "git2/index.h"
Packit Service 20376f
#include "git2/sys/filter.h"
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "fileops.h"
Packit Service 20376f
#include "hash.h"
Packit Service 20376f
#include "filter.h"
Packit Service 20376f
#include "buf_text.h"
Packit Service 20376f
#include "repository.h"
Packit Service 20376f
Packit Service 20376f
struct crlf_attrs {
Packit Service 20376f
	int crlf_action;
Packit Service 20376f
	int eol;
Packit Service 20376f
	int auto_crlf;
Packit Service 20376f
	int safe_crlf;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
struct crlf_filter {
Packit Service 20376f
	git_filter f;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
static int check_crlf(const char *value)
Packit Service 20376f
{
Packit Service 20376f
	if (GIT_ATTR_TRUE(value))
Packit Service 20376f
		return GIT_CRLF_TEXT;
Packit Service 20376f
Packit Service 20376f
	if (GIT_ATTR_FALSE(value))
Packit Service 20376f
		return GIT_CRLF_BINARY;
Packit Service 20376f
Packit Service 20376f
	if (GIT_ATTR_UNSPECIFIED(value))
Packit Service 20376f
		return GIT_CRLF_GUESS;
Packit Service 20376f
Packit Service 20376f
	if (strcmp(value, "input") == 0)
Packit Service 20376f
		return GIT_CRLF_INPUT;
Packit Service 20376f
Packit Service 20376f
	if (strcmp(value, "auto") == 0)
Packit Service 20376f
		return GIT_CRLF_AUTO;
Packit Service 20376f
Packit Service 20376f
	return GIT_CRLF_GUESS;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int check_eol(const char *value)
Packit Service 20376f
{
Packit Service 20376f
	if (GIT_ATTR_UNSPECIFIED(value))
Packit Service 20376f
		return GIT_EOL_UNSET;
Packit Service 20376f
Packit Service 20376f
	if (strcmp(value, "lf") == 0)
Packit Service 20376f
		return GIT_EOL_LF;
Packit Service 20376f
Packit Service 20376f
	if (strcmp(value, "crlf") == 0)
Packit Service 20376f
		return GIT_EOL_CRLF;
Packit Service 20376f
Packit Service 20376f
	return GIT_EOL_UNSET;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int crlf_input_action(struct crlf_attrs *ca)
Packit Service 20376f
{
Packit Service 20376f
	if (ca->crlf_action == GIT_CRLF_BINARY)
Packit Service 20376f
		return GIT_CRLF_BINARY;
Packit Service 20376f
Packit Service 20376f
	if (ca->eol == GIT_EOL_LF)
Packit Service 20376f
		return GIT_CRLF_INPUT;
Packit Service 20376f
Packit Service 20376f
	if (ca->eol == GIT_EOL_CRLF)
Packit Service 20376f
		return GIT_CRLF_CRLF;
Packit Service 20376f
Packit Service 20376f
	return ca->crlf_action;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int has_cr_in_index(const git_filter_source *src)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *repo = git_filter_source_repo(src);
Packit Service 20376f
	const char *path = git_filter_source_path(src);
Packit Service 20376f
	git_index *index;
Packit Service 20376f
	const git_index_entry *entry;
Packit Service 20376f
	git_blob *blob;
Packit Service 20376f
	const void *blobcontent;
Packit Service 20376f
	git_off_t blobsize;
Packit Service 20376f
	bool found_cr;
Packit Service 20376f
Packit Service 20376f
	if (!path)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	if (git_repository_index__weakptr(&index, repo) < 0) {
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
		return false;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!(entry = git_index_get_bypath(index, path, 0)) &&
Packit Service 20376f
		!(entry = git_index_get_bypath(index, path, 1)))
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	if (!S_ISREG(entry->mode)) /* don't crlf filter non-blobs */
Packit Service 20376f
		return true;
Packit Service 20376f
Packit Service 20376f
	if (git_blob_lookup(&blob, repo, &entry->id) < 0)
Packit Service 20376f
		return false;
Packit Service 20376f
Packit Service 20376f
	blobcontent = git_blob_rawcontent(blob);
Packit Service 20376f
	blobsize    = git_blob_rawsize(blob);
Packit Service 20376f
	if (!git__is_sizet(blobsize))
Packit Service 20376f
		blobsize = (size_t)-1;
Packit Service 20376f
Packit Service 20376f
	found_cr = (blobcontent != NULL &&
Packit Service 20376f
		blobsize > 0 &&
Packit Service 20376f
		memchr(blobcontent, '\r', (size_t)blobsize) != NULL);
Packit Service 20376f
Packit Service 20376f
	git_blob_free(blob);
Packit Service 20376f
	return found_cr;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int crlf_apply_to_odb(
Packit Service 20376f
	struct crlf_attrs *ca,
Packit Service 20376f
	git_buf *to,
Packit Service 20376f
	const git_buf *from,
Packit Service 20376f
	const git_filter_source *src)
Packit Service 20376f
{
Packit Service 20376f
	/* Empty file? Nothing to do */
Packit Service 20376f
	if (!git_buf_len(from))
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	/* Heuristics to see if we can skip the conversion.
Packit Service 20376f
	 * Straight from Core Git.
Packit Service 20376f
	 */
Packit Service 20376f
	if (ca->crlf_action == GIT_CRLF_AUTO || ca->crlf_action == GIT_CRLF_GUESS) {
Packit Service 20376f
		git_buf_text_stats stats;
Packit Service 20376f
Packit Service 20376f
		/* Check heuristics for binary vs text - returns true if binary */
Packit Service 20376f
		if (git_buf_text_gather_stats(&stats, from, false))
Packit Service 20376f
			return GIT_PASSTHROUGH;
Packit Service 20376f
Packit Service 20376f
		/* If there are no CR characters to filter out, then just pass */
Packit Service 20376f
		if (!stats.cr)
Packit Service 20376f
			return GIT_PASSTHROUGH;
Packit Service 20376f
Packit Service 20376f
		/* If safecrlf is enabled, sanity-check the result. */
Packit Service 20376f
		if (stats.cr != stats.crlf || stats.lf != stats.crlf) {
Packit Service 20376f
			switch (ca->safe_crlf) {
Packit Service 20376f
			case GIT_SAFE_CRLF_FAIL:
Packit Service 20376f
				giterr_set(
Packit Service 20376f
					GITERR_FILTER, "LF would be replaced by CRLF in '%s'",
Packit Service 20376f
					git_filter_source_path(src));
Packit Service 20376f
				return -1;
Packit Service 20376f
			case GIT_SAFE_CRLF_WARN:
Packit Service 20376f
				/* TODO: issue warning when warning API is available */;
Packit Service 20376f
				break;
Packit Service 20376f
			default:
Packit Service 20376f
				break;
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/*
Packit Service 20376f
		 * We're currently not going to even try to convert stuff
Packit Service 20376f
		 * that has bare CR characters. Does anybody do that crazy
Packit Service 20376f
		 * stuff?
Packit Service 20376f
		 */
Packit Service 20376f
		if (stats.cr != stats.crlf)
Packit Service 20376f
			return GIT_PASSTHROUGH;
Packit Service 20376f
Packit Service 20376f
		if (ca->crlf_action == GIT_CRLF_GUESS) {
Packit Service 20376f
			/*
Packit Service 20376f
			 * If the file in the index has any CR in it, do not convert.
Packit Service 20376f
			 * This is the new safer autocrlf handling.
Packit Service 20376f
			 */
Packit Service 20376f
			if (has_cr_in_index(src))
Packit Service 20376f
				return GIT_PASSTHROUGH;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (!stats.cr)
Packit Service 20376f
			return GIT_PASSTHROUGH;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Actually drop the carriage returns */
Packit Service 20376f
	return git_buf_text_crlf_to_lf(to, from);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static const char *line_ending(struct crlf_attrs *ca)
Packit Service 20376f
{
Packit Service 20376f
	switch (ca->crlf_action) {
Packit Service 20376f
	case GIT_CRLF_BINARY:
Packit Service 20376f
	case GIT_CRLF_INPUT:
Packit Service 20376f
		return "\n";
Packit Service 20376f
Packit Service 20376f
	case GIT_CRLF_CRLF:
Packit Service 20376f
		return "\r\n";
Packit Service 20376f
Packit Service 20376f
	case GIT_CRLF_GUESS:
Packit Service 20376f
		if (ca->auto_crlf == GIT_AUTO_CRLF_FALSE)
Packit Service 20376f
			return "\n";
Packit Service 20376f
		break;
Packit Service 20376f
Packit Service 20376f
	case GIT_CRLF_AUTO:
Packit Service 20376f
	case GIT_CRLF_TEXT:
Packit Service 20376f
		break;
Packit Service 20376f
Packit Service 20376f
	default:
Packit Service 20376f
		goto line_ending_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (ca->auto_crlf == GIT_AUTO_CRLF_TRUE)
Packit Service 20376f
		return "\r\n";
Packit Service 20376f
	else if (ca->auto_crlf == GIT_AUTO_CRLF_INPUT)
Packit Service 20376f
		return "\n";
Packit Service 20376f
	else if (ca->eol == GIT_EOL_UNSET)
Packit Service 20376f
		return GIT_EOL_NATIVE == GIT_EOL_CRLF ? "\r\n" : "\n";
Packit Service 20376f
	else if (ca->eol == GIT_EOL_LF)
Packit Service 20376f
		return "\n";
Packit Service 20376f
	else if (ca->eol == GIT_EOL_CRLF)
Packit Service 20376f
		return "\r\n";
Packit Service 20376f
Packit Service 20376f
line_ending_error:
Packit Service 20376f
	giterr_set(GITERR_INVALID, "invalid input to line ending filter");
Packit Service 20376f
	return NULL;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int crlf_apply_to_workdir(
Packit Service 20376f
	struct crlf_attrs *ca, git_buf *to, const git_buf *from)
Packit Service 20376f
{
Packit Service 20376f
	git_buf_text_stats stats;
Packit Service 20376f
	const char *workdir_ending = NULL;
Packit Service 20376f
	bool is_binary;
Packit Service 20376f
Packit Service 20376f
	/* Empty file? Nothing to do. */
Packit Service 20376f
	if (git_buf_len(from) == 0)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	/* Determine proper line ending */
Packit Service 20376f
	workdir_ending = line_ending(ca);
Packit Service 20376f
	if (!workdir_ending)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* only LF->CRLF conversion is supported, do nothing on LF platforms */
Packit Service 20376f
	if (strcmp(workdir_ending, "\r\n") != 0)
Packit Service 20376f
		return GIT_PASSTHROUGH;
Packit Service 20376f
Packit Service 20376f
	/* If there are no LFs, or all LFs are part of a CRLF, nothing to do */
Packit Service 20376f
	is_binary = git_buf_text_gather_stats(&stats, from, false);
Packit Service 20376f
Packit Service 20376f
	if (stats.lf == 0 || stats.lf == stats.crlf)
Packit Service 20376f
		return GIT_PASSTHROUGH;
Packit Service 20376f
Packit Service 20376f
	if (ca->crlf_action == GIT_CRLF_AUTO ||
Packit Service 20376f
		ca->crlf_action == GIT_CRLF_GUESS) {
Packit Service 20376f
Packit Service 20376f
		/* If we have any existing CR or CRLF line endings, do nothing */
Packit Service 20376f
		if (ca->crlf_action == GIT_CRLF_GUESS &&
Packit Service 20376f
			stats.cr > 0 && stats.crlf > 0)
Packit Service 20376f
			return GIT_PASSTHROUGH;
Packit Service 20376f
Packit Service 20376f
		/* If we have bare CR characters, do nothing */
Packit Service 20376f
		if (stats.cr != stats.crlf)
Packit Service 20376f
			return GIT_PASSTHROUGH;
Packit Service 20376f
Packit Service 20376f
		/* Don't filter binary files */
Packit Service 20376f
		if (is_binary)
Packit Service 20376f
			return GIT_PASSTHROUGH;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return git_buf_text_lf_to_crlf(to, from);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int crlf_check(
Packit Service 20376f
	git_filter        *self,
Packit Service 20376f
	void              **payload, /* points to NULL ptr on entry, may be set */
Packit Service 20376f
	const git_filter_source *src,
Packit Service 20376f
	const char **attr_values)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	struct crlf_attrs ca;
Packit Service 20376f
Packit Service 20376f
	GIT_UNUSED(self);
Packit Service 20376f
Packit Service 20376f
	if (!attr_values) {
Packit Service 20376f
		ca.crlf_action = GIT_CRLF_GUESS;
Packit Service 20376f
		ca.eol = GIT_EOL_UNSET;
Packit Service 20376f
	} else {
Packit Service 20376f
		ca.crlf_action = check_crlf(attr_values[2]); /* text */
Packit Service 20376f
		if (ca.crlf_action == GIT_CRLF_GUESS)
Packit Service 20376f
			ca.crlf_action = check_crlf(attr_values[0]); /* clrf */
Packit Service 20376f
		ca.eol = check_eol(attr_values[1]); /* eol */
Packit Service 20376f
	}
Packit Service 20376f
	ca.auto_crlf = GIT_AUTO_CRLF_DEFAULT;
Packit Service 20376f
	ca.safe_crlf = GIT_SAFE_CRLF_DEFAULT;
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * Use the core Git logic to see if we should perform CRLF for this file
Packit Service 20376f
	 * based on its attributes & the value of `core.autocrlf`
Packit Service 20376f
	 */
Packit Service 20376f
	ca.crlf_action = crlf_input_action(&ca);
Packit Service 20376f
Packit Service 20376f
	if (ca.crlf_action == GIT_CRLF_BINARY)
Packit Service 20376f
		return GIT_PASSTHROUGH;
Packit Service 20376f
Packit Service 20376f
	if (ca.crlf_action == GIT_CRLF_GUESS ||
Packit Service 20376f
		((ca.crlf_action == GIT_CRLF_AUTO || ca.crlf_action == GIT_CRLF_TEXT) &&
Packit Service 20376f
		git_filter_source_mode(src) == GIT_FILTER_SMUDGE)) {
Packit Service 20376f
Packit Service 20376f
		error = git_repository__cvar(
Packit Service 20376f
			&ca.auto_crlf, git_filter_source_repo(src), GIT_CVAR_AUTO_CRLF);
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		if (ca.crlf_action == GIT_CRLF_GUESS &&
Packit Service 20376f
			ca.auto_crlf == GIT_AUTO_CRLF_FALSE)
Packit Service 20376f
			return GIT_PASSTHROUGH;
Packit Service 20376f
Packit Service 20376f
		if (ca.auto_crlf == GIT_AUTO_CRLF_INPUT &&
Packit Service 20376f
			git_filter_source_mode(src) == GIT_FILTER_SMUDGE)
Packit Service 20376f
			return GIT_PASSTHROUGH;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (git_filter_source_mode(src) == GIT_FILTER_CLEAN) {
Packit Service 20376f
		error = git_repository__cvar(
Packit Service 20376f
			&ca.safe_crlf, git_filter_source_repo(src), GIT_CVAR_SAFE_CRLF);
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		/* downgrade FAIL to WARN if ALLOW_UNSAFE option is used */
Packit Service 20376f
		if ((git_filter_source_flags(src) & GIT_FILTER_ALLOW_UNSAFE) &&
Packit Service 20376f
			ca.safe_crlf == GIT_SAFE_CRLF_FAIL)
Packit Service 20376f
			ca.safe_crlf = GIT_SAFE_CRLF_WARN;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*payload = git__malloc(sizeof(ca));
Packit Service 20376f
	GITERR_CHECK_ALLOC(*payload);
Packit Service 20376f
	memcpy(*payload, &ca, sizeof(ca));
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int crlf_apply(
Packit Service 20376f
	git_filter    *self,
Packit Service 20376f
	void         **payload, /* may be read and/or set */
Packit Service 20376f
	git_buf       *to,
Packit Service 20376f
	const git_buf *from,
Packit Service 20376f
	const git_filter_source *src)
Packit Service 20376f
{
Packit Service 20376f
	/* initialize payload in case `check` was bypassed */
Packit Service 20376f
	if (!*payload) {
Packit Service 20376f
		int error = crlf_check(self, payload, src, NULL);
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (git_filter_source_mode(src) == GIT_FILTER_SMUDGE)
Packit Service 20376f
		return crlf_apply_to_workdir(*payload, to, from);
Packit Service 20376f
	else
Packit Service 20376f
		return crlf_apply_to_odb(*payload, to, from, src);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void crlf_cleanup(
Packit Service 20376f
	git_filter *self,
Packit Service 20376f
	void       *payload)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(self);
Packit Service 20376f
	git__free(payload);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
git_filter *git_crlf_filter_new(void)
Packit Service 20376f
{
Packit Service 20376f
	struct crlf_filter *f = git__calloc(1, sizeof(struct crlf_filter));
Packit Service 20376f
	if (f == NULL)
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	f->f.version = GIT_FILTER_VERSION;
Packit Service 20376f
	f->f.attributes = "crlf eol text";
Packit Service 20376f
	f->f.initialize = NULL;
Packit Service 20376f
	f->f.shutdown = git_filter_free;
Packit Service 20376f
	f->f.check    = crlf_check;
Packit Service 20376f
	f->f.apply    = crlf_apply;
Packit Service 20376f
	f->f.cleanup  = crlf_cleanup;
Packit Service 20376f
Packit Service 20376f
	return (git_filter *)f;
Packit Service 20376f
}