Commit 17572f67ed9a3eb57b981d97468bd216d571bf10

Edward Thomson 2016-04-21T00:04:14

git_patch_parse_ctx: refcount the context

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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
diff --git a/src/patch.h b/src/patch.h
index 28fe766..525ae70 100644
--- a/src/patch.h
+++ b/src/patch.h
@@ -61,21 +61,6 @@ typedef struct {
 
 #define GIT_PATCH_OPTIONS_INIT { 1 }
 
-/**
- * Create a patch for a single file from the contents of a patch buffer.
- *
- * @param out The patch to be created
- * @param contents The contents of a patch file
- * @param contents_len The length of the patch file
- * @param opts The git_patch_options
- * @return 0 on success, <0 on failure.
- */
-extern int git_patch_from_buffer(
-	git_patch **out,
-	const char *contents,
-	size_t contents_len,
-	git_patch_options *opts);
-
 extern void git_patch_free(git_patch *patch);
 
 #endif
diff --git a/src/patch_parse.c b/src/patch_parse.c
index 5c771d9..70acdbc 100644
--- a/src/patch_parse.c
+++ b/src/patch_parse.c
@@ -1,3 +1,9 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * 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 "git2/patch.h"
 #include "patch.h"
 #include "path.h"
@@ -6,13 +12,24 @@
 	( giterr_set(GITERR_PATCH, __VA_ARGS__), -1 )
 
 typedef struct {
-	git_patch base;
+	git_refcount rc;
+
+	const char *content;
+	size_t content_len;
 
 	git_patch_options opts;
 
-	/* the patch contents, which lines will point into. */
-	/* TODO: allow us to point somewhere refcounted. */
-	char *content;
+	const char *line;
+	size_t line_len;
+	size_t line_num;
+
+	size_t remain;
+} git_patch_parse_ctx;
+
+typedef struct {
+	git_patch base;
+
+	git_patch_parse_ctx *ctx;
 
 	/* the paths from the `diff --git` header, these will be used if this is not
 	 * a rename (and rename paths are specified) or if no `+++`/`---` line specify
@@ -30,20 +47,9 @@ typedef struct {
 	char *old_prefix, *new_prefix;
 } git_patch_parsed;
 
-typedef struct {
-	const char *content;
-	size_t content_len;
-
-	const char *line;
-	size_t line_len;
-	size_t line_num;
-
-	size_t remain;
-} patch_parse_ctx;
-
 
 GIT_INLINE(bool) parse_ctx_contains(
-	patch_parse_ctx *ctx, const char *str, size_t len)
+	git_patch_parse_ctx *ctx, const char *str, size_t len)
 {
 	return (ctx->line_len >= len && memcmp(ctx->line, str, len) == 0);
 }
@@ -51,7 +57,7 @@ GIT_INLINE(bool) parse_ctx_contains(
 #define parse_ctx_contains_s(ctx, str) \
 	parse_ctx_contains(ctx, str, sizeof(str) - 1)
 
-static void parse_advance_line(patch_parse_ctx *ctx)
+static void parse_advance_line(git_patch_parse_ctx *ctx)
 {
 	ctx->line += ctx->line_len;
 	ctx->remain -= ctx->line_len;
@@ -59,7 +65,7 @@ static void parse_advance_line(patch_parse_ctx *ctx)
 	ctx->line_num++;
 }
 
-static void parse_advance_chars(patch_parse_ctx *ctx, size_t char_cnt)
+static void parse_advance_chars(git_patch_parse_ctx *ctx, size_t char_cnt)
 {
 	ctx->line += char_cnt;
 	ctx->remain -= char_cnt;
@@ -67,7 +73,7 @@ static void parse_advance_chars(patch_parse_ctx *ctx, size_t char_cnt)
 }
 
 static int parse_advance_expected(
-	patch_parse_ctx *ctx,
+	git_patch_parse_ctx *ctx,
 	const char *expected,
 	size_t expected_len)
 {
@@ -84,7 +90,7 @@ static int parse_advance_expected(
 #define parse_advance_expected_s(ctx, str) \
 	parse_advance_expected(ctx, str, sizeof(str) - 1)
 
-static int parse_advance_ws(patch_parse_ctx *ctx)
+static int parse_advance_ws(git_patch_parse_ctx *ctx)
 {
 	int ret = -1;
 
@@ -100,7 +106,7 @@ static int parse_advance_ws(patch_parse_ctx *ctx)
 	return ret;
 }
 
-static int parse_advance_nl(patch_parse_ctx *ctx)
+static int parse_advance_nl(git_patch_parse_ctx *ctx)
 {
 	if (ctx->line_len != 1 || ctx->line[0] != '\n')
 		return -1;
@@ -109,7 +115,7 @@ static int parse_advance_nl(patch_parse_ctx *ctx)
 	return 0;
 }
 
-static int header_path_len(patch_parse_ctx *ctx)
+static int header_path_len(git_patch_parse_ctx *ctx)
 {
 	bool inquote = 0;
 	bool quoted = (ctx->line_len > 0 && ctx->line[0] == '"');
@@ -129,7 +135,7 @@ static int header_path_len(patch_parse_ctx *ctx)
 	return len;
 }
 
-static int parse_header_path_buf(git_buf *path, patch_parse_ctx *ctx)
+static int parse_header_path_buf(git_buf *path, git_patch_parse_ctx *ctx)
 {
 	int path_len, error = 0;
 
@@ -154,7 +160,7 @@ done:
 	return error;
 }
 
-static int parse_header_path(char **out, patch_parse_ctx *ctx)
+static int parse_header_path(char **out, git_patch_parse_ctx *ctx)
 {
 	git_buf path = GIT_BUF_INIT;
 	int error = parse_header_path_buf(&path, ctx);
@@ -165,18 +171,18 @@ static int parse_header_path(char **out, patch_parse_ctx *ctx)
 }
 
 static int parse_header_git_oldpath(
-	git_patch_parsed *patch, patch_parse_ctx *ctx)
+	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
 {
 	return parse_header_path(&patch->old_path, ctx);
 }
 
 static int parse_header_git_newpath(
-	git_patch_parsed *patch, patch_parse_ctx *ctx)
+	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
 {
 	return parse_header_path(&patch->new_path, ctx);
 }
 
-static int parse_header_mode(uint16_t *mode, patch_parse_ctx *ctx)
+static int parse_header_mode(uint16_t *mode, git_patch_parse_ctx *ctx)
 {
 	const char *end;
 	int32_t m;
@@ -201,7 +207,7 @@ static int parse_header_mode(uint16_t *mode, patch_parse_ctx *ctx)
 static int parse_header_oid(
 	git_oid *oid,
 	int *oid_len,
-	patch_parse_ctx *ctx)
+	git_patch_parse_ctx *ctx)
 {
 	size_t len;
 
@@ -223,7 +229,7 @@ static int parse_header_oid(
 }
 
 static int parse_header_git_index(
-	git_patch_parsed *patch, patch_parse_ctx *ctx)
+	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
 {
 	if (parse_header_oid(&patch->base.delta->old_file.id,
 			&patch->base.delta->old_file.id_abbrev, ctx) < 0 ||
@@ -251,20 +257,20 @@ static int parse_header_git_index(
 }
 
 static int parse_header_git_oldmode(
-	git_patch_parsed *patch, patch_parse_ctx *ctx)
+	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
 {
 	return parse_header_mode(&patch->base.delta->old_file.mode, ctx);
 }
 
 static int parse_header_git_newmode(
-	git_patch_parsed *patch, patch_parse_ctx *ctx)
+	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
 {
 	return parse_header_mode(&patch->base.delta->new_file.mode, ctx);
 }
 
 static int parse_header_git_deletedfilemode(
 	git_patch_parsed *patch,
-	patch_parse_ctx *ctx)
+	git_patch_parse_ctx *ctx)
 {
 	git__free((char *)patch->base.delta->old_file.path);
 
@@ -277,7 +283,7 @@ static int parse_header_git_deletedfilemode(
 
 static int parse_header_git_newfilemode(
 	git_patch_parsed *patch,
-	patch_parse_ctx *ctx)
+	git_patch_parse_ctx *ctx)
 {
 	git__free((char *)patch->base.delta->new_file.path);
 
@@ -290,7 +296,7 @@ static int parse_header_git_newfilemode(
 
 static int parse_header_rename(
 	char **out,
-	patch_parse_ctx *ctx)
+	git_patch_parse_ctx *ctx)
 {
 	git_buf path = GIT_BUF_INIT;
 
@@ -305,20 +311,20 @@ static int parse_header_rename(
 }
 
 static int parse_header_renamefrom(
-	git_patch_parsed *patch, patch_parse_ctx *ctx)
+	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
 {
 	patch->base.delta->status = GIT_DELTA_RENAMED;
 	return parse_header_rename(&patch->rename_old_path, ctx);
 }
 
 static int parse_header_renameto(
-	git_patch_parsed *patch, patch_parse_ctx *ctx)
+	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
 {
 	patch->base.delta->status = GIT_DELTA_RENAMED;
 	return parse_header_rename(&patch->rename_new_path, ctx);
 }
 
-static int parse_header_percent(uint16_t *out, patch_parse_ctx *ctx)
+static int parse_header_percent(uint16_t *out, git_patch_parse_ctx *ctx)
 {
 	int32_t val;
 	const char *end;
@@ -340,7 +346,7 @@ static int parse_header_percent(uint16_t *out, patch_parse_ctx *ctx)
 }
 
 static int parse_header_similarity(
-	git_patch_parsed *patch, patch_parse_ctx *ctx)
+	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
 {
 	if (parse_header_percent(&patch->base.delta->similarity, ctx) < 0)
 		return parse_err("invalid similarity percentage at line %d",
@@ -350,7 +356,7 @@ static int parse_header_similarity(
 }
 
 static int parse_header_dissimilarity(
-	git_patch_parsed *patch, patch_parse_ctx *ctx)
+	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
 {
 	uint16_t dissimilarity;
 
@@ -365,7 +371,7 @@ static int parse_header_dissimilarity(
 
 typedef struct {
 	const char *str;
-	int(*fn)(git_patch_parsed *, patch_parse_ctx *);
+	int(*fn)(git_patch_parsed *, git_patch_parse_ctx *);
 } header_git_op;
 
 static const header_git_op header_git_ops[] = {
@@ -388,7 +394,7 @@ static const header_git_op header_git_ops[] = {
 
 static int parse_header_git(
 	git_patch_parsed *patch,
-	patch_parse_ctx *ctx)
+	git_patch_parse_ctx *ctx)
 {
 	size_t i;
 	int error = 0;
@@ -443,7 +449,7 @@ done:
 	return error;
 }
 
-static int parse_number(git_off_t *out, patch_parse_ctx *ctx)
+static int parse_number(git_off_t *out, git_patch_parse_ctx *ctx)
 {
 	const char *end;
 	int64_t num;
@@ -463,7 +469,7 @@ static int parse_number(git_off_t *out, patch_parse_ctx *ctx)
 	return 0;
 }
 
-static int parse_int(int *out, patch_parse_ctx *ctx)
+static int parse_int(int *out, git_patch_parse_ctx *ctx)
 {
 	git_off_t num;
 
@@ -476,7 +482,7 @@ static int parse_int(int *out, patch_parse_ctx *ctx)
 
 static int parse_hunk_header(
 	git_patch_hunk *hunk,
-	patch_parse_ctx *ctx)
+	git_patch_parse_ctx *ctx)
 {
 	const char *header_start = ctx->line;
 
@@ -530,7 +536,7 @@ fail:
 static int parse_hunk_body(
 	git_patch_parsed *patch,
 	git_patch_hunk *hunk,
-	patch_parse_ctx *ctx)
+	git_patch_parse_ctx *ctx)
 {
 	git_diff_line *line;
 	int error = 0;
@@ -621,9 +627,9 @@ done:
 	return error;
 }
 
-static int parsed_patch_header(
+static int parse_patch_header(
 	git_patch_parsed *patch,
-	patch_parse_ctx *ctx)
+	git_patch_parse_ctx *ctx)
 {
 	int error = 0;
 
@@ -671,9 +677,9 @@ done:
 	return error;
 }
 
-static int parsed_patch_binary_side(
+static int parse_patch_binary_side(
 	git_diff_binary_file *binary,
-	patch_parse_ctx *ctx)
+	git_patch_parse_ctx *ctx)
 {
 	git_diff_binary_t type = GIT_DIFF_BINARY_NONE;
 	git_buf base85 = GIT_BUF_INIT, decoded = GIT_BUF_INIT;
@@ -750,9 +756,9 @@ done:
 	return error;
 }
 
-static int parsed_patch_binary(
+static int parse_patch_binary(
 	git_patch_parsed *patch,
-	patch_parse_ctx *ctx)
+	git_patch_parse_ctx *ctx)
 {
 	int error;
 
@@ -761,7 +767,7 @@ static int parsed_patch_binary(
 		return parse_err("corrupt git binary header at line %d", ctx->line_num);
 
 	/* parse old->new binary diff */
-	if ((error = parsed_patch_binary_side(
+	if ((error = parse_patch_binary_side(
 			&patch->base.binary.new_file, ctx)) < 0)
 		return error;
 
@@ -770,7 +776,7 @@ static int parsed_patch_binary(
 			ctx->line_num);
 
 	/* parse new->old binary diff */
-	if ((error = parsed_patch_binary_side(
+	if ((error = parse_patch_binary_side(
 			&patch->base.binary.old_file, ctx)) < 0)
 		return error;
 
@@ -778,9 +784,9 @@ static int parsed_patch_binary(
 	return 0;
 }
 
-static int parsed_patch_hunks(
+static int parse_patch_hunks(
 	git_patch_parsed *patch,
-	patch_parse_ctx *ctx)
+	git_patch_parse_ctx *ctx)
 {
 	git_patch_hunk *hunk;
 	int error = 0;
@@ -803,14 +809,14 @@ done:
 	return error;
 }
 
-static int parsed_patch_body(
-	git_patch_parsed *patch, patch_parse_ctx *ctx)
+static int parse_patch_body(
+	git_patch_parsed *patch, git_patch_parse_ctx *ctx)
 {
 	if (parse_ctx_contains_s(ctx, "GIT binary patch"))
-		return parsed_patch_binary(patch, ctx);
+		return parse_patch_binary(patch, ctx);
 
 	else if (parse_ctx_contains_s(ctx, "@@ -"))
-		return parsed_patch_hunks(patch, ctx);
+		return parse_patch_hunks(patch, ctx);
 
 	return 0;
 }
@@ -840,12 +846,13 @@ static int check_prefix(
 	const char *path_start)
 {
 	const char *path = path_start;
-	uint32_t remain = patch->opts.prefix_len;
+	size_t prefix_len = patch->ctx->opts.prefix_len;
+	size_t remain = prefix_len;
 
 	*out = NULL;
 	*out_len = 0;
 
-	if (patch->opts.prefix_len == 0)
+	if (prefix_len == 0)
 		goto done;
 
 	/* leading slashes do not count as part of the prefix in git apply */
@@ -860,8 +867,9 @@ static int check_prefix(
 	}
 
 	if (remain || !*path)
-		return parse_err("header filename does not contain %d path components",
-			patch->opts.prefix_len);
+		return parse_err(
+			"header filename does not contain %d path components",
+			prefix_len);
 
 done:
 	*out_len = (path - path_start);
@@ -938,6 +946,50 @@ static int check_patch(git_patch_parsed *patch)
 	return 0;
 }
 
+static git_patch_parse_ctx *git_patch_parse_ctx_init(
+	const char *content,
+	size_t content_len,
+	const git_patch_options *opts)
+{
+	git_patch_parse_ctx *ctx;
+	git_patch_options default_opts = GIT_PATCH_OPTIONS_INIT;
+
+	if ((ctx = git__calloc(1, sizeof(git_patch_parse_ctx))) == NULL)
+		return NULL;
+
+	if (content_len) {
+		if ((ctx->content = git__malloc(content_len)) == NULL)
+			return NULL;
+
+		memcpy((char *)ctx->content, content, content_len);
+	}
+
+	ctx->content_len = content_len;
+	ctx->remain = content_len;
+
+	if (opts)
+		memcpy(&ctx->opts, opts, sizeof(git_patch_options));
+	else
+		memcpy(&ctx->opts, &default_opts, sizeof(git_patch_options));
+
+	GIT_REFCOUNT_INC(ctx);
+	return ctx;
+}
+
+static void patch_parse_ctx_free(git_patch_parse_ctx *ctx)
+{
+	if (!ctx)
+		return;
+
+	git__free((char *)ctx->content);
+	git__free(ctx);
+}
+
+static void git_patch_parse_ctx_free(git_patch_parse_ctx *ctx)
+{
+	GIT_REFCOUNT_DEC(ctx, patch_parse_ctx_free);
+}
+
 static void patch_parsed__free(git_patch *p)
 {
 	git_patch_parsed *patch = (git_patch_parsed *)p;
@@ -945,6 +997,8 @@ static void patch_parsed__free(git_patch *p)
 	if (!patch)
 		return;
 
+	git_patch_parse_ctx_free(patch->ctx);
+
 	git__free((char *)patch->base.binary.old_file.data);
 	git__free((char *)patch->base.binary.new_file.data);
 	git_array_clear(patch->base.hunks);
@@ -959,30 +1013,25 @@ static void patch_parsed__free(git_patch *p)
 	git__free(patch->rename_new_path);
 	git__free(patch->old_path);
 	git__free(patch->new_path);
-	git__free(patch->content);
 	git__free(patch);
 }
 
-int git_patch_from_buffer(
+static int git_patch_parse(
 	git_patch **out,
-	const char *content,
-	size_t content_len,
-	git_patch_options *opts)
+	git_patch_parse_ctx *ctx)
 {
-	patch_parse_ctx ctx = { 0 };
 	git_patch_parsed *patch;
-	git_patch_options default_opts = GIT_PATCH_OPTIONS_INIT;
 	int error = 0;
 
+	assert(out && ctx);
+
 	*out = NULL;
 
 	patch = git__calloc(1, sizeof(git_patch_parsed));
 	GITERR_CHECK_ALLOC(patch);
 
-	if (opts)
-		memcpy(&patch->opts, opts, sizeof(git_patch_options));
-	else
-		memcpy(&patch->opts, &default_opts, sizeof(git_patch_options));
+	patch->ctx = ctx;
+	GIT_REFCOUNT_INC(patch->ctx);
 
 	patch->base.free_fn = patch_parsed__free;
 
@@ -992,19 +1041,8 @@ int git_patch_from_buffer(
 	patch->base.delta->status = GIT_DELTA_MODIFIED;
 	patch->base.delta->nfiles = 2;
 
-	if (content_len) {
-		patch->content = git__malloc(content_len);
-		GITERR_CHECK_ALLOC(patch->content);
-
-		memcpy(patch->content, content, content_len);
-	}
-
-	ctx.content = patch->content;
-	ctx.content_len = content_len;
-	ctx.remain = content_len;
-
-	if ((error = parsed_patch_header(patch, &ctx)) < 0 ||
-		(error = parsed_patch_body(patch, &ctx)) < 0 ||
+	if ((error = parse_patch_header(patch, ctx)) < 0 ||
+		(error = parse_patch_body(patch, ctx)) < 0 ||
 		(error = check_patch(patch)) < 0)
 		goto done;
 
@@ -1021,3 +1059,22 @@ done:
 
 	return error;
 }
+
+int git_patch_from_buffer(
+	git_patch **out,
+	const char *content,
+	size_t content_len,
+	const git_patch_options *opts)
+{
+	git_patch_parse_ctx *ctx;
+	int error;
+
+	ctx = git_patch_parse_ctx_init(content, content_len, opts);
+	GITERR_CHECK_ALLOC(ctx);
+
+	error = git_patch_parse(out, ctx);
+
+	git_patch_parse_ctx_free(ctx);
+	return error;
+}
+
diff --git a/src/patch_parse.h b/src/patch_parse.h
new file mode 100644
index 0000000..d5e8607
--- /dev/null
+++ b/src/patch_parse.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_patch_parse_h__
+#define INCLUDE_patch_parse_h__
+
+/**
+ * Create a patch for a single file from the contents of a patch buffer.
+ *
+ * @param out The patch to be created
+ * @param contents The contents of a patch file
+ * @param contents_len The length of the patch file
+ * @param opts The git_patch_options
+ * @return 0 on success, <0 on failure.
+ */
+extern int git_patch_from_buffer(
+	git_patch **out,
+	const char *contents,
+	size_t contents_len,
+	const git_patch_options *opts);
+
+#endif
diff --git a/tests/apply/fromfile.c b/tests/apply/fromfile.c
index 7eb1af9..31fffa1 100644
--- a/tests/apply/fromfile.c
+++ b/tests/apply/fromfile.c
@@ -3,6 +3,7 @@
 
 #include "apply.h"
 #include "patch.h"
+#include "patch_parse.h"
 #include "repository.h"
 #include "buf_text.h"
 
diff --git a/tests/patch/parse.c b/tests/patch/parse.c
index 88cdbf6..9243482 100644
--- a/tests/patch/parse.c
+++ b/tests/patch/parse.c
@@ -1,5 +1,6 @@
 #include "clar_libgit2.h"
 #include "patch.h"
+#include "patch_parse.h"
 
 #include "patch_common.h"
 
diff --git a/tests/patch/print.c b/tests/patch/print.c
index 047b48e..5a86573 100644
--- a/tests/patch/print.c
+++ b/tests/patch/print.c
@@ -1,5 +1,6 @@
 #include "clar_libgit2.h"
 #include "patch.h"
+#include "patch_parse.h"
 
 #include "patch_common.h"