crlf.c 8.76 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6 7
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

8 9
#include "common.h"

10 11 12
#include "git2/attr.h"
#include "git2/blob.h"
#include "git2/index.h"
13
#include "git2/sys/filter.h"
14

15 16 17
#include "fileops.h"
#include "hash.h"
#include "filter.h"
18
#include "buf_text.h"
19 20 21 22 23
#include "repository.h"

struct crlf_attrs {
	int crlf_action;
	int eol;
24
	int auto_crlf;
25
	int safe_crlf;
26 27 28 29 30 31 32 33
};

struct crlf_filter {
	git_filter f;
};

static int check_crlf(const char *value)
{
34
	if (GIT_ATTR_TRUE(value))
35 36
		return GIT_CRLF_TEXT;

37
	if (GIT_ATTR_FALSE(value))
38 39
		return GIT_CRLF_BINARY;

40
	if (GIT_ATTR_UNSPECIFIED(value))
41 42 43 44 45 46 47 48 49 50 51 52 53
		return GIT_CRLF_GUESS;

	if (strcmp(value, "input") == 0)
		return GIT_CRLF_INPUT;

	if (strcmp(value, "auto") == 0)
		return GIT_CRLF_AUTO;

	return GIT_CRLF_GUESS;
}

static int check_eol(const char *value)
{
54
	if (GIT_ATTR_UNSPECIFIED(value))
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
		return GIT_EOL_UNSET;

	if (strcmp(value, "lf") == 0)
		return GIT_EOL_LF;

	if (strcmp(value, "crlf") == 0)
		return GIT_EOL_CRLF;

	return GIT_EOL_UNSET;
}

static int crlf_input_action(struct crlf_attrs *ca)
{
	if (ca->crlf_action == GIT_CRLF_BINARY)
		return GIT_CRLF_BINARY;

	if (ca->eol == GIT_EOL_LF)
		return GIT_CRLF_INPUT;

	if (ca->eol == GIT_EOL_CRLF)
		return GIT_CRLF_CRLF;

	return ca->crlf_action;
}

80
static int has_cr_in_index(const git_filter_source *src)
81
{
82 83
	git_repository *repo = git_filter_source_repo(src);
	const char *path = git_filter_source_path(src);
84 85 86 87 88 89 90
	git_index *index;
	const git_index_entry *entry;
	git_blob *blob;
	const void *blobcontent;
	git_off_t blobsize;
	bool found_cr;

91 92 93
	if (!path)
		return false;

94
	if (git_repository_index__weakptr(&index, repo) < 0) {
95 96 97 98
		giterr_clear();
		return false;
	}

99 100
	if (!(entry = git_index_get_bypath(index, path, 0)) &&
		!(entry = git_index_get_bypath(index, path, 1)))
101 102 103 104 105
		return false;

	if (!S_ISREG(entry->mode)) /* don't crlf filter non-blobs */
		return true;

106
	if (git_blob_lookup(&blob, repo, &entry->id) < 0)
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
		return false;

	blobcontent = git_blob_rawcontent(blob);
	blobsize    = git_blob_rawsize(blob);
	if (!git__is_sizet(blobsize))
		blobsize = (size_t)-1;

	found_cr = (blobcontent != NULL &&
		blobsize > 0 &&
		memchr(blobcontent, '\r', (size_t)blobsize) != NULL);

	git_blob_free(blob);
	return found_cr;
}

static int crlf_apply_to_odb(
123
	struct crlf_attrs *ca,
124 125
	git_buf *to,
	const git_buf *from,
126
	const git_filter_source *src)
127 128
{
	/* Empty file? Nothing to do */
129
	if (!git_buf_len(from))
130 131 132 133 134
		return 0;

	/* Heuristics to see if we can skip the conversion.
	 * Straight from Core Git.
	 */
135
	if (ca->crlf_action == GIT_CRLF_AUTO || ca->crlf_action == GIT_CRLF_GUESS) {
136 137
		git_buf_text_stats stats;

138
		/* Check heuristics for binary vs text - returns true if binary */
139
		if (git_buf_text_gather_stats(&stats, from, false))
140
			return GIT_PASSTHROUGH;
141

142 143 144
		/* If there are no CR characters to filter out, then just pass */
		if (!stats.cr)
			return GIT_PASSTHROUGH;
145

146 147
		/* If safecrlf is enabled, sanity-check the result. */
		if (stats.cr != stats.crlf || stats.lf != stats.crlf) {
148 149 150 151 152 153 154 155 156 157 158 159
			switch (ca->safe_crlf) {
			case GIT_SAFE_CRLF_FAIL:
				giterr_set(
					GITERR_FILTER, "LF would be replaced by CRLF in '%s'",
					git_filter_source_path(src));
				return -1;
			case GIT_SAFE_CRLF_WARN:
				/* TODO: issue warning when warning API is available */;
				break;
			default:
				break;
			}
160 161
		}

162 163 164 165 166 167
		/*
		 * We're currently not going to even try to convert stuff
		 * that has bare CR characters. Does anybody do that crazy
		 * stuff?
		 */
		if (stats.cr != stats.crlf)
Russell Belfer committed
168
			return GIT_PASSTHROUGH;
169

170
		if (ca->crlf_action == GIT_CRLF_GUESS) {
171 172 173 174
			/*
			 * If the file in the index has any CR in it, do not convert.
			 * This is the new safer autocrlf handling.
			 */
175
			if (has_cr_in_index(src))
Russell Belfer committed
176
				return GIT_PASSTHROUGH;
177 178
		}

179
		if (!stats.cr)
Russell Belfer committed
180
			return GIT_PASSTHROUGH;
181 182
	}

183
	/* Actually drop the carriage returns */
184
	return git_buf_text_crlf_to_lf(to, from);
Ben Straub committed
185 186
}

187
static const char *line_ending(struct crlf_attrs *ca)
Ben Straub committed
188
{
189
	switch (ca->crlf_action) {
Ben Straub committed
190 191 192 193 194 195 196
	case GIT_CRLF_BINARY:
	case GIT_CRLF_INPUT:
		return "\n";

	case GIT_CRLF_CRLF:
		return "\r\n";

197 198 199 200 201
	case GIT_CRLF_GUESS:
		if (ca->auto_crlf == GIT_AUTO_CRLF_FALSE)
			return "\n";
		break;

Ben Straub committed
202 203 204 205 206 207 208 209
	case GIT_CRLF_AUTO:
	case GIT_CRLF_TEXT:
		break;

	default:
		goto line_ending_error;
	}

210
	if (ca->auto_crlf == GIT_AUTO_CRLF_TRUE)
Ben Straub committed
211
		return "\r\n";
212
	else if (ca->auto_crlf == GIT_AUTO_CRLF_INPUT)
Ben Straub committed
213
		return "\n";
214 215 216 217 218 219
	else if (ca->eol == GIT_EOL_UNSET)
		return GIT_EOL_NATIVE == GIT_EOL_CRLF ? "\r\n" : "\n";
	else if (ca->eol == GIT_EOL_LF)
		return "\n";
	else if (ca->eol == GIT_EOL_CRLF)
		return "\r\n";
Ben Straub committed
220 221

line_ending_error:
222
	giterr_set(GITERR_INVALID, "invalid input to line ending filter");
Ben Straub committed
223 224 225
	return NULL;
}

226
static int crlf_apply_to_workdir(
227
	struct crlf_attrs *ca, git_buf *to, const git_buf *from)
Ben Straub committed
228
{
229
	git_buf_text_stats stats;
Ben Straub committed
230
	const char *workdir_ending = NULL;
231
	bool is_binary;
Ben Straub committed
232 233

	/* Empty file? Nothing to do. */
234
	if (git_buf_len(from) == 0)
235
		return 0;
Ben Straub committed
236 237

	/* Determine proper line ending */
238
	workdir_ending = line_ending(ca);
239 240
	if (!workdir_ending)
		return -1;
Ben Straub committed
241

Edward Thomson committed
242 243 244
	/* only LF->CRLF conversion is supported, do nothing on LF platforms */
	if (strcmp(workdir_ending, "\r\n") != 0)
		return GIT_PASSTHROUGH;
245

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
	/* If there are no LFs, or all LFs are part of a CRLF, nothing to do */
	is_binary = git_buf_text_gather_stats(&stats, from, false);

	if (stats.lf == 0 || stats.lf == stats.crlf)
		return GIT_PASSTHROUGH;

	if (ca->crlf_action == GIT_CRLF_AUTO ||
		ca->crlf_action == GIT_CRLF_GUESS) {

		/* If we have any existing CR or CRLF line endings, do nothing */
		if (ca->crlf_action == GIT_CRLF_GUESS &&
			stats.cr > 0 && stats.crlf > 0)
			return GIT_PASSTHROUGH;

		/* If we have bare CR characters, do nothing */
		if (stats.cr != stats.crlf)
			return GIT_PASSTHROUGH;

		/* Don't filter binary files */
		if (is_binary)
			return GIT_PASSTHROUGH;
	}

Edward Thomson committed
269
	return git_buf_text_lf_to_crlf(to, from);
Ben Straub committed
270 271
}

272 273 274
static int crlf_check(
	git_filter        *self,
	void              **payload, /* points to NULL ptr on entry, may be set */
275 276
	const git_filter_source *src,
	const char **attr_values)
277 278
{
	int error;
279 280 281
	struct crlf_attrs ca;

	GIT_UNUSED(self);
282

283 284 285 286 287 288 289 290 291 292
	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;
293
	ca.safe_crlf = GIT_SAFE_CRLF_DEFAULT;
294

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

301
	if (ca.crlf_action == GIT_CRLF_BINARY)
Russell Belfer committed
302
		return GIT_PASSTHROUGH;
303

304
	if (ca.crlf_action == GIT_CRLF_GUESS ||
305
		((ca.crlf_action == GIT_CRLF_AUTO || ca.crlf_action == GIT_CRLF_TEXT) &&
306
		git_filter_source_mode(src) == GIT_FILTER_SMUDGE)) {
307

308 309 310
		error = git_repository__cvar(
			&ca.auto_crlf, git_filter_source_repo(src), GIT_CVAR_AUTO_CRLF);
		if (error < 0)
311 312
			return error;

313 314
		if (ca.crlf_action == GIT_CRLF_GUESS &&
			ca.auto_crlf == GIT_AUTO_CRLF_FALSE)
Russell Belfer committed
315
			return GIT_PASSTHROUGH;
316 317 318 319

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

322 323 324 325 326
	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;
327 328

		/* downgrade FAIL to WARN if ALLOW_UNSAFE option is used */
329
		if ((git_filter_source_flags(src) & GIT_FILTER_ALLOW_UNSAFE) &&
330 331
			ca.safe_crlf == GIT_SAFE_CRLF_FAIL)
			ca.safe_crlf = GIT_SAFE_CRLF_WARN;
332 333
	}

334 335 336
	*payload = git__malloc(sizeof(ca));
	GITERR_CHECK_ALLOC(*payload);
	memcpy(*payload, &ca, sizeof(ca));
337

338 339 340 341
	return 0;
}

static int crlf_apply(
342 343 344 345
	git_filter    *self,
	void         **payload, /* may be read and/or set */
	git_buf       *to,
	const git_buf *from,
346 347
	const git_filter_source *src)
{
348 349 350
	/* initialize payload in case `check` was bypassed */
	if (!*payload) {
		int error = crlf_check(self, payload, src, NULL);
351
		if (error < 0)
352 353
			return error;
	}
354

355
	if (git_filter_source_mode(src) == GIT_FILTER_SMUDGE)
356 357 358
		return crlf_apply_to_workdir(*payload, to, from);
	else
		return crlf_apply_to_odb(*payload, to, from, src);
359 360
}

361 362 363
static void crlf_cleanup(
	git_filter *self,
	void       *payload)
364
{
365 366
	GIT_UNUSED(self);
	git__free(payload);
367 368
}

369
git_filter *git_crlf_filter_new(void)
370
{
371
	struct crlf_filter *f = git__calloc(1, sizeof(struct crlf_filter));
Jacques Germishuys committed
372 373
	if (f == NULL)
		return NULL;
374

375
	f->f.version = GIT_FILTER_VERSION;
376 377
	f->f.attributes = "crlf eol text";
	f->f.initialize = NULL;
Russell Belfer committed
378
	f->f.shutdown = git_filter_free;
379 380 381
	f->f.check    = crlf_check;
	f->f.apply    = crlf_apply;
	f->f.cleanup  = crlf_cleanup;
382

383
	return (git_filter *)f;
384
}