crlf.c 6.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright (C) 2009-2012 the libgit2 contributors
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include "common.h"
#include "fileops.h"
#include "hash.h"
#include "filter.h"
#include "repository.h"

#include "git2/attr.h"

struct crlf_attrs {
	int crlf_action;
	int eol;
};

struct crlf_filter {
	git_filter f;
	struct crlf_attrs attrs;
};

static int check_crlf(const char *value)
{
28
	if (GIT_ATTR_TRUE(value))
29 30
		return GIT_CRLF_TEXT;

31
	if (GIT_ATTR_FALSE(value))
32 33
		return GIT_CRLF_BINARY;

34
	if (GIT_ATTR_UNSPECIFIED(value))
35 36 37 38 39 40 41 42 43 44 45 46 47
		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)
{
48
	if (GIT_ATTR_UNSPECIFIED(value))
49 50 51 52 53 54 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 80 81 82 83 84
		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;
}

static int crlf_load_attributes(struct crlf_attrs *ca, git_repository *repo, const char *path)
{
#define NUM_CONV_ATTRS 3

	static const char *attr_names[NUM_CONV_ATTRS] = {
		"crlf", "eol", "text",
	};

	const char *attr_vals[NUM_CONV_ATTRS];
	int error;

85 86
	error = git_attr_get_many(attr_vals,
		repo, 0, path, NUM_CONV_ATTRS, attr_names);
87

88
	if (error == GIT_ENOTFOUND) {
89 90 91 92 93
		ca->crlf_action = GIT_CRLF_GUESS;
		ca->eol = GIT_EOL_UNSET;
		return 0;
	}

94
	if (error == 0) {
95 96 97 98 99 100 101 102
		ca->crlf_action = check_crlf(attr_vals[2]); /* text */
		if (ca->crlf_action == GIT_CRLF_GUESS)
			ca->crlf_action = check_crlf(attr_vals[0]); /* clrf */

		ca->eol = check_eol(attr_vals[1]); /* eol */
		return 0;
	}

103
	return -1;
104 105
}

106
static int drop_crlf(git_buf *dest, const git_buf *source)
107
{
108
	const char *scan = source->ptr, *next;
nulltoken committed
109
	const char *scan_end = git_buf_cstr(source) + git_buf_len(source);
110

111 112
	/* Main scan loop.  Find the next carriage return and copy the
	 * whole chunk up to that point to the destination buffer.
113
	 */
114 115 116 117 118 119 120
	while ((next = memchr(scan, '\r', scan_end - scan)) != NULL) {
		/* copy input up to \r */
		if (next > scan)
			git_buf_put(dest, scan, next - scan);

		/* Do not drop \r unless it is followed by \n */
		if (*(next + 1) != '\n')
121
			git_buf_putc(dest, '\r');
122 123

		scan = next + 1;
124 125
	}

126 127 128 129 130 131
	/* If there was no \r, then tell the library to skip this filter */
	if (scan == source->ptr)
		return -1;

	/* Copy remaining input into dest */
	git_buf_put(dest, scan, scan_end - scan);
132
	return 0;
133 134 135 136
}

static int crlf_apply_to_odb(git_filter *self, git_buf *dest, const git_buf *source)
{
137 138 139 140
	struct crlf_filter *filter = (struct crlf_filter *)self;

	assert(self && dest && source);

141
	/* Empty file? Nothing to do */
nulltoken committed
142
	if (git_buf_len(source) == 0)
143 144 145 146 147
		return 0;

	/* Heuristics to see if we can skip the conversion.
	 * Straight from Core Git.
	 */
148 149 150
	if (filter->attrs.crlf_action == GIT_CRLF_AUTO ||
		filter->attrs.crlf_action == GIT_CRLF_GUESS) {

151 152 153 154 155
		git_buf_text_stats stats;

		/* Check heuristics for binary vs text... */
		if (git_buf_text_gather_stats(&stats, source, false))
			return -1;
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

		/*
		 * 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)
			return -1;

#if 0
		if (crlf_action == CRLF_GUESS) {
			/*
			 * If the file in the index has any CR in it, do not convert.
			 * This is the new safer autocrlf handling.
			 */
			if (has_cr_in_index(path))
				return 0;
		}
#endif

		if (!stats.cr)
			return -1;
	}

180 181
	/* Actually drop the carriage returns */
	return drop_crlf(dest, source);
182 183
}

Ben Straub committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
static int convert_line_endings(git_buf *dest, const git_buf *source, const char *ending)
{
	const char *scan = git_buf_cstr(source),
				  *next,
				  *scan_end = git_buf_cstr(source) + git_buf_len(source);

	while ((next = memchr(scan, '\n', scan_end - scan)) != NULL) {
		if (next > scan)
			git_buf_put(dest, scan, next-scan);
		git_buf_puts(dest, ending);
		scan = next + 1;
	}

	git_buf_put(dest, scan, scan_end - scan);
	return 0;
}

static const char *line_ending(struct crlf_filter *filter)
{
	switch (filter->attrs.crlf_action) {
	case GIT_CRLF_BINARY:
	case GIT_CRLF_INPUT:
		return "\n";

	case GIT_CRLF_CRLF:
		return "\r\n";

	case GIT_CRLF_AUTO:
	case GIT_CRLF_TEXT:
	case GIT_CRLF_GUESS:
		break;

	default:
		goto line_ending_error;
	}

	switch (filter->attrs.eol) {
	case GIT_EOL_UNSET:
		return GIT_EOL_NATIVE == GIT_EOL_CRLF
			? "\r\n"
			: "\n";

	case GIT_EOL_CRLF:
		return "\r\n";

	case GIT_EOL_LF:
		return "\n";

	default:
		goto line_ending_error;
	}

line_ending_error:
	giterr_set(GITERR_INVALID, "Invalid input to line ending filter");
	return NULL;
}

static int crlf_apply_to_workdir(git_filter *self, git_buf *dest, const git_buf *source)
{
	struct crlf_filter *filter = (struct crlf_filter *)self;
	const char *workdir_ending = NULL;

	assert (self && dest && source);

	/* Empty file? Nothing to do. */
	if (git_buf_len(source) == 0)
		return 0;

	/* Determine proper line ending */
	workdir_ending = line_ending(filter);
	if (!workdir_ending) return -1;

	/* If the line ending is '\n', just copy the input */
	if (!strcmp(workdir_ending, "\n"))
		return git_buf_puts(dest, git_buf_cstr(source));

	return convert_line_endings(dest, source, workdir_ending);
}

263 264 265
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))
266
{
267 268
	struct crlf_attrs ca;
	struct crlf_filter *filter;
269 270
	int error;

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

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

281
	if (ca.crlf_action == GIT_CRLF_BINARY)
282 283
		return 0;

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

Ben Straub committed
287
		if ((error = git_repository__cvar(&auto_crlf, repo, GIT_CVAR_AUTO_CRLF)) < 0)
288 289 290 291 292
			return error;

		if (auto_crlf == GIT_AUTO_CRLF_FALSE)
			return 0;
	}
293

294 295 296
	/* If we're good, we create a new filter object and push it
	 * into the filters array */
	filter = git__malloc(sizeof(struct crlf_filter));
297
	GITERR_CHECK_ALLOC(filter);
298

299
	filter->f.apply = apply;
300 301 302 303
	filter->f.do_free = NULL;
	memcpy(&filter->attrs, &ca, sizeof(struct crlf_attrs));

	return git_vector_insert(filters, filter);
304 305
}

306 307 308 309 310 311 312 313 314
int git_filter_add__crlf_to_odb(git_vector *filters, git_repository *repo, const char *path)
{
	return find_and_add_filter(filters, repo, path, &crlf_apply_to_odb);
}

int git_filter_add__crlf_to_workdir(git_vector *filters, git_repository *repo, const char *path)
{
	return find_and_add_filter(filters, repo, path, &crlf_apply_to_workdir);
}