Commit 5269008cf632efcd6a16f6160ec44244ce442400

Russell Belfer 2014-05-06T16:01:49

Add filter options and ALLOW_UNSAFE Diff and status do not want core.safecrlf to actually raise an error regardless of the setting, so this extends the filter API with an additional options flags parameter and adds a flag so that filters can be applied with GIT_FILTER_OPT_ALLOW_UNSAFE, indicating that unsafe filter application should be downgraded from a failure to a warning.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
diff --git a/include/git2/filter.h b/include/git2/filter.h
index f96b676..7fd9b7e 100644
--- a/include/git2/filter.h
+++ b/include/git2/filter.h
@@ -35,6 +35,11 @@ typedef enum {
 	GIT_FILTER_CLEAN = GIT_FILTER_TO_ODB,
 } git_filter_mode_t;
 
+typedef enum {
+	GIT_FILTER_OPT_DEFAULT = 0u,
+	GIT_FILTER_OPT_ALLOW_UNSAFE = (1u << 0),
+} git_filter_opt_t;
+
 /**
  * A filter that can transform file data
  *
@@ -83,7 +88,8 @@ GIT_EXTERN(int) git_filter_list_load(
 	git_repository *repo,
 	git_blob *blob, /* can be NULL */
 	const char *path,
-	git_filter_mode_t mode);
+	git_filter_mode_t mode,
+	git_filter_opt_t options);
 
 /**
  * Apply filter list to a data buffer.
diff --git a/include/git2/repository.h b/include/git2/repository.h
index e3f687a..04df25f 100644
--- a/include/git2/repository.h
+++ b/include/git2/repository.h
@@ -546,6 +546,10 @@ GIT_EXTERN(int) git_repository_mergehead_foreach(
  * hash a file in the repository and you want to apply filtering rules (e.g.
  * crlf filters) before generating the SHA, then use this function.
  *
+ * Note: if the repository has `core.safecrlf` set to fail and the
+ * filtering triggers that failure, then this function will return an
+ * error and not calculate the hash of the file.
+ *
  * @param out Output value of calculated SHA
  * @param repo Repository pointer
  * @param path Path to file on disk whose contents should be hashed. If the
@@ -555,6 +559,7 @@ GIT_EXTERN(int) git_repository_mergehead_foreach(
  *             NULL, then the `path` parameter will be used instead. If
  *             this is passed as the empty string, then no filters will be
  *             applied when calculating the hash.
+ * @return 0 on success, or an error code
  */
 GIT_EXTERN(int) git_repository_hashfile(
 	git_oid *out,
diff --git a/include/git2/sys/filter.h b/include/git2/sys/filter.h
index 8fe21c9..1b21a9d 100644
--- a/include/git2/sys/filter.h
+++ b/include/git2/sys/filter.h
@@ -55,7 +55,10 @@ GIT_EXTERN(git_filter *) git_filter_lookup(const char *name);
  * your own chains of filters.
  */
 GIT_EXTERN(int) git_filter_list_new(
-	git_filter_list **out, git_repository *repo, git_filter_mode_t mode);
+	git_filter_list **out,
+	git_repository *repo,
+	git_filter_mode_t mode,
+	git_filter_opt_t options);
 
 /**
  * Add a filter to a filter list with the given payload.
@@ -115,10 +118,15 @@ GIT_EXTERN(uint16_t) git_filter_source_filemode(const git_filter_source *src);
 GIT_EXTERN(const git_oid *) git_filter_source_id(const git_filter_source *src);
 
 /**
- * Get the git_filter_mode_t to be applied
+ * Get the git_filter_mode_t to be used
  */
 GIT_EXTERN(git_filter_mode_t) git_filter_source_mode(const git_filter_source *src);
 
+/**
+ * Get the git_filter_opt_t options to be applied
+ */
+GIT_EXTERN(git_filter_opt_t) git_filter_source_options(const git_filter_source *src);
+
 /*
  * struct git_filter
  *
diff --git a/src/blob.c b/src/blob.c
index 0aa2516..ab7dec6 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -198,7 +198,8 @@ int git_blob__create_from_paths(
 		if (try_load_filters)
 			/* Load the filters for writing this file to the ODB */
 			error = git_filter_list_load(
-				&fl, repo, NULL, hint_path, GIT_FILTER_TO_ODB);
+				&fl, repo, NULL, hint_path,
+				GIT_FILTER_TO_ODB, GIT_FILTER_OPT_DEFAULT);
 
 		if (error < 0)
 			/* well, that didn't work */;
@@ -356,7 +357,8 @@ int git_blob_filtered_content(
 		return 0;
 
 	if (!(error = git_filter_list_load(
-			&fl, git_blob_owner(blob), blob, path, GIT_FILTER_TO_WORKTREE))) {
+			&fl, git_blob_owner(blob), blob, path,
+			GIT_FILTER_TO_WORKTREE, GIT_FILTER_OPT_DEFAULT))) {
 
 		error = git_filter_list_apply_to_blob(out, fl, blob);
 
diff --git a/src/checkout.c b/src/checkout.c
index b869efe..20763fd 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -1212,7 +1212,8 @@ static int blob_content_to_file(
 
 	if (!opts->disable_filters)
 		error = git_filter_list_load(
-			&fl, git_blob_owner(blob), blob, hint_path, GIT_FILTER_TO_WORKTREE);
+			&fl, git_blob_owner(blob), blob, hint_path,
+			GIT_FILTER_TO_WORKTREE, GIT_FILTER_OPT_DEFAULT);
 
 	if (!error)
 		error = git_filter_list_apply_to_blob(&out, fl, blob);
diff --git a/src/crlf.c b/src/crlf.c
index 8be1b9a..dad3ece 100644
--- a/src/crlf.c
+++ b/src/crlf.c
@@ -139,10 +139,19 @@ static int crlf_apply_to_odb(
 			return GIT_PASSTHROUGH;
 
 		/* If safecrlf is enabled, sanity-check the result. */
-		if (ca->safe_crlf && (stats.cr != stats.crlf || stats.lf != stats.crlf)) {
-			giterr_set(GITERR_FILTER, "LF would be replaced by CRLF in '%s'",
-				git_filter_source_path(src));
-			return -1;
+		if (stats.cr != stats.crlf || stats.lf != stats.crlf) {
+			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;
+			}
 		}
 
 		/*
@@ -267,6 +276,7 @@ static int crlf_check(
 	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)
@@ -285,6 +295,11 @@ static int crlf_check(
 			&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));
diff --git a/src/diff.c b/src/diff.c
index 781f23e..bc23e6b 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -589,7 +589,8 @@ int git_diff__oid_for_entry(
 			entry.path);
 		error = -1;
 	} else if (!(error = git_filter_list_load(
-			&fl, diff->repo, NULL, entry.path, GIT_FILTER_TO_ODB)))
+		&fl, diff->repo, NULL, entry.path,
+		GIT_FILTER_TO_ODB, GIT_FILTER_OPT_ALLOW_UNSAFE)))
 	{
 		int fd = git_futils_open_ro(full_path.ptr);
 		if (fd < 0)
diff --git a/src/diff_file.c b/src/diff_file.c
index b9f92df..f2a1d50 100644
--- a/src/diff_file.c
+++ b/src/diff_file.c
@@ -300,7 +300,8 @@ static int diff_file_content_load_workdir_file(
 		goto cleanup;
 
 	if ((error = git_filter_list_load(
-			&fl, fc->repo, NULL, fc->file->path, GIT_FILTER_TO_ODB)) < 0)
+			&fl, fc->repo, NULL, fc->file->path,
+			GIT_FILTER_TO_ODB, GIT_FILTER_OPT_ALLOW_UNSAFE)) < 0)
 		goto cleanup;
 
 	/* if there are no filters, try to mmap the file */
diff --git a/src/filter.c b/src/filter.c
index b2f5796..b0e2b8b 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -23,6 +23,7 @@ struct git_filter_source {
 	git_oid         oid;  /* zero if unknown (which is likely) */
 	uint16_t        filemode; /* zero if unknown */
 	git_filter_mode_t mode;
+	git_filter_opt_t  options;
 };
 
 typedef struct {
@@ -358,6 +359,11 @@ git_filter_mode_t git_filter_source_mode(const git_filter_source *src)
 	return src->mode;
 }
 
+git_filter_opt_t git_filter_source_options(const git_filter_source *src)
+{
+	return src->options;
+}
+
 static int filter_list_new(
 	git_filter_list **out, const git_filter_source *src)
 {
@@ -372,6 +378,7 @@ static int filter_list_new(
 	fl->source.repo = src->repo;
 	fl->source.path = fl->path;
 	fl->source.mode = src->mode;
+	fl->source.options = src->options;
 
 	*out = fl;
 	return 0;
@@ -419,12 +426,16 @@ static int filter_list_check_attributes(
 }
 
 int git_filter_list_new(
-	git_filter_list **out, git_repository *repo, git_filter_mode_t mode)
+	git_filter_list **out,
+	git_repository *repo,
+	git_filter_mode_t mode,
+	git_filter_opt_t options)
 {
 	git_filter_source src = { 0 };
 	src.repo = repo;
 	src.path = NULL;
 	src.mode = mode;
+	src.options = options;
 	return filter_list_new(out, &src);
 }
 
@@ -433,7 +444,8 @@ int git_filter_list_load(
 	git_repository *repo,
 	git_blob *blob, /* can be NULL */
 	const char *path,
-	git_filter_mode_t mode)
+	git_filter_mode_t mode,
+	git_filter_opt_t options)
 {
 	int error = 0;
 	git_filter_list *fl = NULL;
@@ -448,6 +460,7 @@ int git_filter_list_load(
 	src.repo = repo;
 	src.path = path;
 	src.mode = mode;
+	src.options = options;
 	if (blob)
 		git_oid_cpy(&src.oid, git_blob_id(blob));
 
diff --git a/src/repository.c b/src/repository.c
index ac7af76..df5f322 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -1789,7 +1789,8 @@ int git_repository_hashfile(
 	/* passing empty string for "as_path" indicated --no-filters */
 	if (strlen(as_path) > 0) {
 		error = git_filter_list_load(
-			&fl, repo, NULL, as_path, GIT_FILTER_TO_ODB);
+			&fl, repo, NULL, as_path,
+			GIT_FILTER_TO_ODB, GIT_FILTER_OPT_DEFAULT);
 		if (error < 0)
 			return error;
 	} else {
diff --git a/tests/filter/crlf.c b/tests/filter/crlf.c
index 75320ef..66c267e 100644
--- a/tests/filter/crlf.c
+++ b/tests/filter/crlf.c
@@ -25,7 +25,8 @@ void test_filter_crlf__to_worktree(void)
 	git_filter *crlf;
 	git_buf in = { 0 }, out = { 0 };
 
-	cl_git_pass(git_filter_list_new(&fl, g_repo, GIT_FILTER_TO_WORKTREE));
+	cl_git_pass(git_filter_list_new(
+		&fl, g_repo, GIT_FILTER_TO_WORKTREE, 0));
 
 	crlf = git_filter_lookup(GIT_FILTER_CRLF);
 	cl_assert(crlf != NULL);
@@ -53,7 +54,8 @@ void test_filter_crlf__to_odb(void)
 	git_filter *crlf;
 	git_buf in = { 0 }, out = { 0 };
 
-	cl_git_pass(git_filter_list_new(&fl, g_repo, GIT_FILTER_TO_ODB));
+	cl_git_pass(git_filter_list_new(
+		&fl, g_repo, GIT_FILTER_TO_ODB, 0));
 
 	crlf = git_filter_lookup(GIT_FILTER_CRLF);
 	cl_assert(crlf != NULL);
@@ -79,7 +81,8 @@ void test_filter_crlf__with_safecrlf(void)
 
 	cl_repo_set_bool(g_repo, "core.safecrlf", true);
 
-	cl_git_pass(git_filter_list_new(&fl, g_repo, GIT_FILTER_TO_ODB));
+	cl_git_pass(git_filter_list_new(
+		&fl, g_repo, GIT_FILTER_TO_ODB, 0));
 
 	crlf = git_filter_lookup(GIT_FILTER_CRLF);
 	cl_assert(crlf != NULL);
@@ -111,13 +114,57 @@ void test_filter_crlf__with_safecrlf(void)
 	git_buf_free(&out);
 }
 
+void test_filter_crlf__with_safecrlf_and_unsafe_allowed(void)
+{
+	git_filter_list *fl;
+	git_filter *crlf;
+	git_buf in = {0}, out = GIT_BUF_INIT;
+
+	cl_repo_set_bool(g_repo, "core.safecrlf", true);
+
+	cl_git_pass(git_filter_list_new(
+		&fl, g_repo, GIT_FILTER_TO_ODB, GIT_FILTER_OPT_ALLOW_UNSAFE));
+
+	crlf = git_filter_lookup(GIT_FILTER_CRLF);
+	cl_assert(crlf != NULL);
+
+	cl_git_pass(git_filter_list_push(fl, crlf, NULL));
+
+	/* Normalized \r\n succeeds with safecrlf */
+	in.ptr = "Normal\r\nCRLF\r\nline-endings.\r\n";
+	in.size = strlen(in.ptr);
+
+	cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
+	cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr);
+
+	/* Mix of line endings fails with safecrlf, but allowed to pass */
+	in.ptr = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
+	in.size = strlen(in.ptr);
+
+	cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
+	/* TODO: check for warning */
+	cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr);
+
+	/* Normalized \n fails with safecrlf, but allowed to pass */
+	in.ptr = "Normal\nLF\nonly\nline-endings.\n";
+	in.size = strlen(in.ptr);
+
+	cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
+	/* TODO: check for warning */
+	cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out.ptr);
+
+	git_filter_list_free(fl);
+	git_buf_free(&out);
+}
+
 void test_filter_crlf__no_safecrlf(void)
 {
 	git_filter_list *fl;
 	git_filter *crlf;
 	git_buf in = {0}, out = GIT_BUF_INIT;
 
-	cl_git_pass(git_filter_list_new(&fl, g_repo, GIT_FILTER_TO_ODB));
+	cl_git_pass(git_filter_list_new(
+		&fl, g_repo, GIT_FILTER_TO_ODB, 0));
 
 	crlf = git_filter_lookup(GIT_FILTER_CRLF);
 	cl_assert(crlf != NULL);
diff --git a/tests/filter/custom.c b/tests/filter/custom.c
index 7052401..0fd7c37 100644
--- a/tests/filter/custom.c
+++ b/tests/filter/custom.c
@@ -194,7 +194,7 @@ void test_filter_custom__to_odb(void)
 	git_buf in = GIT_BUF_INIT_CONST(workdir_data, strlen(workdir_data));
 
 	cl_git_pass(git_filter_list_load(
-		&fl, g_repo, NULL, "herofile", GIT_FILTER_TO_ODB));
+		&fl, g_repo, NULL, "herofile", GIT_FILTER_TO_ODB, 0));
 
 	cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
 
@@ -215,7 +215,7 @@ void test_filter_custom__to_workdir(void)
 		bitflipped_and_reversed_data, BITFLIPPED_AND_REVERSED_DATA_LEN);
 
 	cl_git_pass(git_filter_list_load(
-		&fl, g_repo, NULL, "herofile", GIT_FILTER_TO_WORKTREE));
+		&fl, g_repo, NULL, "herofile", GIT_FILTER_TO_WORKTREE, 0));
 
 	cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
 
@@ -233,13 +233,13 @@ void test_filter_custom__can_register_a_custom_filter_in_the_repository(void)
 	git_filter_list *fl;
 
 	cl_git_pass(git_filter_list_load(
-		&fl, g_repo, NULL, "herofile", GIT_FILTER_TO_WORKTREE));
+		&fl, g_repo, NULL, "herofile", GIT_FILTER_TO_WORKTREE, 0));
 	/* expect: bitflip, reverse, crlf */
 	cl_assert_equal_sz(3, git_filter_list_length(fl));
 	git_filter_list_free(fl);
 
 	cl_git_pass(git_filter_list_load(
-		&fl, g_repo, NULL, "herocorp", GIT_FILTER_TO_WORKTREE));
+		&fl, g_repo, NULL, "herocorp", GIT_FILTER_TO_WORKTREE, 0));
 	/* expect: bitflip, reverse - possibly crlf depending on global config */
 	{
 		size_t flen = git_filter_list_length(fl);
@@ -248,19 +248,20 @@ void test_filter_custom__can_register_a_custom_filter_in_the_repository(void)
 	git_filter_list_free(fl);
 
 	cl_git_pass(git_filter_list_load(
-		&fl, g_repo, NULL, "hero.bin", GIT_FILTER_TO_WORKTREE));
+		&fl, g_repo, NULL, "hero.bin", GIT_FILTER_TO_WORKTREE, 0));
 	/* expect: bitflip, reverse */
 	cl_assert_equal_sz(2, git_filter_list_length(fl));
 	git_filter_list_free(fl);
 
 	cl_git_pass(git_filter_list_load(
-		&fl, g_repo, NULL, "heroflip", GIT_FILTER_TO_WORKTREE));
+		&fl, g_repo, NULL, "heroflip", GIT_FILTER_TO_WORKTREE, 0));
 	/* expect: bitflip (because of -reverse) */
 	cl_assert_equal_sz(1, git_filter_list_length(fl));
 	git_filter_list_free(fl);
 
 	cl_git_pass(git_filter_list_load(
-		&fl, g_repo, NULL, "doesntapplytome.bin", GIT_FILTER_TO_WORKTREE));
+		&fl, g_repo, NULL, "doesntapplytome.bin",
+		GIT_FILTER_TO_WORKTREE, 0));
 	/* expect: none */
 	cl_assert_equal_sz(0, git_filter_list_length(fl));
 	git_filter_list_free(fl);
diff --git a/tests/filter/ident.c b/tests/filter/ident.c
index 2c8e6ab..2c9a3eb 100644
--- a/tests/filter/ident.c
+++ b/tests/filter/ident.c
@@ -39,7 +39,8 @@ void test_filter_ident__to_worktree(void)
 	git_filter_list *fl;
 	git_filter *ident;
 
-	cl_git_pass(git_filter_list_new(&fl, g_repo, GIT_FILTER_TO_WORKTREE));
+	cl_git_pass(git_filter_list_new(
+		&fl, g_repo, GIT_FILTER_TO_WORKTREE, 0));
 
 	ident = git_filter_lookup(GIT_FILTER_IDENT);
 	cl_assert(ident != NULL);
@@ -78,7 +79,8 @@ void test_filter_ident__to_odb(void)
 	git_filter_list *fl;
 	git_filter *ident;
 
-	cl_git_pass(git_filter_list_new(&fl, g_repo, GIT_FILTER_TO_ODB));
+	cl_git_pass(git_filter_list_new(
+		&fl, g_repo, GIT_FILTER_TO_ODB, 0));
 
 	ident = git_filter_lookup(GIT_FILTER_IDENT);
 	cl_assert(ident != NULL);
diff --git a/tests/object/blob/filter.c b/tests/object/blob/filter.c
index 0b2d6bf..9798055 100644
--- a/tests/object/blob/filter.c
+++ b/tests/object/blob/filter.c
@@ -121,7 +121,7 @@ void test_object_blob_filter__to_odb(void)
 	cl_git_append2file("empty_standard_repo/.gitattributes", "*.txt text\n");
 
 	cl_git_pass(git_filter_list_load(
-		&fl, g_repo, NULL, "filename.txt", GIT_FILTER_TO_ODB));
+		&fl, g_repo, NULL, "filename.txt", GIT_FILTER_TO_ODB, 0));
 	cl_assert(fl != NULL);
 
 	for (i = 0; i < CRLF_NUM_TEST_OBJECTS; i++) {