]> git.proxmox.com Git - libgit2.git/blob - src/patch_parse.c
patch: `git_patch_from_patchfile` -> `git_patch_from_buffer`
[libgit2.git] / src / patch_parse.c
1 #include "git2/patch.h"
2 #include "patch.h"
3 #include "path.h"
4
5 #define parse_err(...) \
6 ( giterr_set(GITERR_PATCH, __VA_ARGS__), -1 )
7
8 typedef struct {
9 git_patch base;
10
11 git_patch_options opts;
12
13 /* the patch contents, which lines will point into. */
14 /* TODO: allow us to point somewhere refcounted. */
15 char *content;
16
17 /* the paths from the `diff --git` header, these will be used if this is not
18 * a rename (and rename paths are specified) or if no `+++`/`---` line specify
19 * the paths.
20 */
21 char *header_old_path, *header_new_path;
22
23 /* renamed paths are precise and are not prefixed */
24 char *rename_old_path, *rename_new_path;
25
26 /* the paths given in `---` and `+++` lines */
27 char *old_path, *new_path;
28
29 /* the prefixes from the old/new paths */
30 char *old_prefix, *new_prefix;
31 } git_patch_parsed;
32
33 typedef struct {
34 const char *content;
35 size_t content_len;
36
37 const char *line;
38 size_t line_len;
39 size_t line_num;
40
41 size_t remain;
42 } patch_parse_ctx;
43
44
45 static void parse_advance_line(patch_parse_ctx *ctx)
46 {
47 ctx->line += ctx->line_len;
48 ctx->remain -= ctx->line_len;
49 ctx->line_len = git__linenlen(ctx->line, ctx->remain);
50 ctx->line_num++;
51 }
52
53 static void parse_advance_chars(patch_parse_ctx *ctx, size_t char_cnt)
54 {
55 ctx->line += char_cnt;
56 ctx->remain -= char_cnt;
57 ctx->line_len -= char_cnt;
58 }
59
60 static int parse_advance_expected(
61 patch_parse_ctx *ctx,
62 const char *expected,
63 size_t expected_len)
64 {
65 if (ctx->line_len < expected_len)
66 return -1;
67
68 if (memcmp(ctx->line, expected, expected_len) != 0)
69 return -1;
70
71 parse_advance_chars(ctx, expected_len);
72 return 0;
73 }
74
75 #define parse_advance_expected_s(ctx, str) \
76 parse_advance_expected(ctx, str, sizeof(str) - 1)
77
78 static int parse_advance_ws(patch_parse_ctx *ctx)
79 {
80 int ret = -1;
81
82 while (ctx->line_len > 0 &&
83 ctx->line[0] != '\n' &&
84 git__isspace(ctx->line[0])) {
85 ctx->line++;
86 ctx->line_len--;
87 ctx->remain--;
88 ret = 0;
89 }
90
91 return ret;
92 }
93
94 static int parse_advance_nl(patch_parse_ctx *ctx)
95 {
96 if (ctx->line_len != 1 || ctx->line[0] != '\n')
97 return -1;
98
99 parse_advance_line(ctx);
100 return 0;
101 }
102
103 static int header_path_len(patch_parse_ctx *ctx)
104 {
105 bool inquote = 0;
106 bool quoted = (ctx->line_len > 0 && ctx->line[0] == '"');
107 size_t len;
108
109 for (len = quoted; len < ctx->line_len; len++) {
110 if (!quoted && git__isspace(ctx->line[len]))
111 break;
112 else if (quoted && !inquote && ctx->line[len] == '"') {
113 len++;
114 break;
115 }
116
117 inquote = (!inquote && ctx->line[len] == '\\');
118 }
119
120 return len;
121 }
122
123 static int parse_header_path_buf(git_buf *path, patch_parse_ctx *ctx)
124 {
125 int path_len, error = 0;
126
127 path_len = header_path_len(ctx);
128
129 if ((error = git_buf_put(path, ctx->line, path_len)) < 0)
130 goto done;
131
132 parse_advance_chars(ctx, path_len);
133
134 git_buf_rtrim(path);
135
136 if (path->size > 0 && path->ptr[0] == '"')
137 error = git_buf_unquote(path);
138
139 if (error < 0)
140 goto done;
141
142 git_path_squash_slashes(path);
143
144 done:
145 return error;
146 }
147
148 static int parse_header_path(char **out, patch_parse_ctx *ctx)
149 {
150 git_buf path = GIT_BUF_INIT;
151 int error = parse_header_path_buf(&path, ctx);
152
153 *out = git_buf_detach(&path);
154
155 return error;
156 }
157
158 static int parse_header_git_oldpath(
159 git_patch_parsed *patch, patch_parse_ctx *ctx)
160 {
161 return parse_header_path(&patch->old_path, ctx);
162 }
163
164 static int parse_header_git_newpath(
165 git_patch_parsed *patch, patch_parse_ctx *ctx)
166 {
167 return parse_header_path(&patch->new_path, ctx);
168 }
169
170 static int parse_header_mode(uint16_t *mode, patch_parse_ctx *ctx)
171 {
172 const char *end;
173 int32_t m;
174 int ret;
175
176 if (ctx->line_len < 1 || !git__isdigit(ctx->line[0]))
177 return parse_err("invalid file mode at line %d", ctx->line_num);
178
179 if ((ret = git__strntol32(&m, ctx->line, ctx->line_len, &end, 8)) < 0)
180 return ret;
181
182 if (m > UINT16_MAX)
183 return -1;
184
185 *mode = (uint16_t)m;
186
187 parse_advance_chars(ctx, (end - ctx->line));
188
189 return ret;
190 }
191
192 static int parse_header_oid(
193 git_oid *oid,
194 int *oid_len,
195 patch_parse_ctx *ctx)
196 {
197 size_t len;
198
199 for (len = 0; len < ctx->line_len && len < GIT_OID_HEXSZ; len++) {
200 if (!git__isxdigit(ctx->line[len]))
201 break;
202 }
203
204 if (len < GIT_OID_MINPREFIXLEN ||
205 git_oid_fromstrn(oid, ctx->line, len) < 0)
206 return parse_err("invalid hex formatted object id at line %d",
207 ctx->line_num);
208
209 parse_advance_chars(ctx, len);
210
211 *oid_len = (int)len;
212
213 return 0;
214 }
215
216 static int parse_header_git_index(
217 git_patch_parsed *patch, patch_parse_ctx *ctx)
218 {
219 if (parse_header_oid(&patch->base.delta->old_file.id,
220 &patch->base.delta->old_file.id_abbrev, ctx) < 0 ||
221 parse_advance_expected_s(ctx, "..") < 0 ||
222 parse_header_oid(&patch->base.delta->new_file.id,
223 &patch->base.delta->new_file.id_abbrev, ctx) < 0)
224 return -1;
225
226 if (ctx->line_len > 0 && ctx->line[0] == ' ') {
227 uint16_t mode;
228
229 parse_advance_chars(ctx, 1);
230
231 if (parse_header_mode(&mode, ctx) < 0)
232 return -1;
233
234 if (!patch->base.delta->new_file.mode)
235 patch->base.delta->new_file.mode = mode;
236
237 if (!patch->base.delta->old_file.mode)
238 patch->base.delta->old_file.mode = mode;
239 }
240
241 return 0;
242 }
243
244 static int parse_header_git_oldmode(
245 git_patch_parsed *patch, patch_parse_ctx *ctx)
246 {
247 return parse_header_mode(&patch->base.delta->old_file.mode, ctx);
248 }
249
250 static int parse_header_git_newmode(
251 git_patch_parsed *patch, patch_parse_ctx *ctx)
252 {
253 return parse_header_mode(&patch->base.delta->new_file.mode, ctx);
254 }
255
256 static int parse_header_git_deletedfilemode(
257 git_patch_parsed *patch,
258 patch_parse_ctx *ctx)
259 {
260 git__free((char *)patch->base.delta->old_file.path);
261
262 patch->base.delta->old_file.path = NULL;
263 patch->base.delta->status = GIT_DELTA_DELETED;
264 patch->base.delta->nfiles = 1;
265
266 return parse_header_mode(&patch->base.delta->old_file.mode, ctx);
267 }
268
269 static int parse_header_git_newfilemode(
270 git_patch_parsed *patch,
271 patch_parse_ctx *ctx)
272 {
273 git__free((char *)patch->base.delta->new_file.path);
274
275 patch->base.delta->new_file.path = NULL;
276 patch->base.delta->status = GIT_DELTA_ADDED;
277 patch->base.delta->nfiles = 1;
278
279 return parse_header_mode(&patch->base.delta->new_file.mode, ctx);
280 }
281
282 static int parse_header_rename(
283 char **out,
284 patch_parse_ctx *ctx)
285 {
286 git_buf path = GIT_BUF_INIT;
287
288 if (parse_header_path_buf(&path, ctx) < 0)
289 return -1;
290
291 /* Note: the `rename from` and `rename to` lines include the literal
292 * filename. They do *not* include the prefix. (Who needs consistency?)
293 */
294 *out = git_buf_detach(&path);
295 return 0;
296 }
297
298 static int parse_header_renamefrom(
299 git_patch_parsed *patch, patch_parse_ctx *ctx)
300 {
301 patch->base.delta->status = GIT_DELTA_RENAMED;
302 return parse_header_rename(&patch->rename_old_path, ctx);
303 }
304
305 static int parse_header_renameto(
306 git_patch_parsed *patch, patch_parse_ctx *ctx)
307 {
308 patch->base.delta->status = GIT_DELTA_RENAMED;
309 return parse_header_rename(&patch->rename_new_path, ctx);
310 }
311
312 static int parse_header_percent(uint16_t *out, patch_parse_ctx *ctx)
313 {
314 int32_t val;
315 const char *end;
316
317 if (ctx->line_len < 1 || !git__isdigit(ctx->line[0]) ||
318 git__strntol32(&val, ctx->line, ctx->line_len, &end, 10) < 0)
319 return -1;
320
321 parse_advance_chars(ctx, (end - ctx->line));
322
323 if (parse_advance_expected_s(ctx, "%") < 0)
324 return -1;
325
326 if (val > 100)
327 return -1;
328
329 *out = val;
330 return 0;
331 }
332
333 static int parse_header_similarity(
334 git_patch_parsed *patch, patch_parse_ctx *ctx)
335 {
336 if (parse_header_percent(&patch->base.delta->similarity, ctx) < 0)
337 return parse_err("invalid similarity percentage at line %d",
338 ctx->line_num);
339
340 return 0;
341 }
342
343 static int parse_header_dissimilarity(
344 git_patch_parsed *patch, patch_parse_ctx *ctx)
345 {
346 uint16_t dissimilarity;
347
348 if (parse_header_percent(&dissimilarity, ctx) < 0)
349 return parse_err("invalid similarity percentage at line %d",
350 ctx->line_num);
351
352 patch->base.delta->similarity = 100 - dissimilarity;
353
354 return 0;
355 }
356
357 typedef struct {
358 const char *str;
359 int(*fn)(git_patch_parsed *, patch_parse_ctx *);
360 } header_git_op;
361
362 static const header_git_op header_git_ops[] = {
363 { "@@ -", NULL },
364 { "GIT binary patch", NULL },
365 { "--- ", parse_header_git_oldpath },
366 { "+++ ", parse_header_git_newpath },
367 { "index ", parse_header_git_index },
368 { "old mode ", parse_header_git_oldmode },
369 { "new mode ", parse_header_git_newmode },
370 { "deleted file mode ", parse_header_git_deletedfilemode },
371 { "new file mode ", parse_header_git_newfilemode },
372 { "rename from ", parse_header_renamefrom },
373 { "rename to ", parse_header_renameto },
374 { "rename old ", parse_header_renamefrom },
375 { "rename new ", parse_header_renameto },
376 { "similarity index ", parse_header_similarity },
377 { "dissimilarity index ", parse_header_dissimilarity },
378 };
379
380 static int parse_header_git(
381 git_patch_parsed *patch,
382 patch_parse_ctx *ctx)
383 {
384 size_t i;
385 int error = 0;
386
387 /* Parse the diff --git line */
388 if (parse_advance_expected_s(ctx, "diff --git ") < 0)
389 return parse_err("corrupt git diff header at line %d", ctx->line_num);
390
391 if (parse_header_path(&patch->header_old_path, ctx) < 0)
392 return parse_err("corrupt old path in git diff header at line %d",
393 ctx->line_num);
394
395 if (parse_advance_ws(ctx) < 0 ||
396 parse_header_path(&patch->header_new_path, ctx) < 0)
397 return parse_err("corrupt new path in git diff header at line %d",
398 ctx->line_num);
399
400 /* Parse remaining header lines */
401 for (parse_advance_line(ctx); ctx->remain > 0; parse_advance_line(ctx)) {
402 if (ctx->line_len == 0 || ctx->line[ctx->line_len - 1] != '\n')
403 break;
404
405 for (i = 0; i < ARRAY_SIZE(header_git_ops); i++) {
406 const header_git_op *op = &header_git_ops[i];
407 size_t op_len = strlen(op->str);
408
409 if (memcmp(ctx->line, op->str, min(op_len, ctx->line_len)) != 0)
410 continue;
411
412 /* Do not advance if this is the patch separator */
413 if (op->fn == NULL)
414 goto done;
415
416 parse_advance_chars(ctx, op_len);
417
418 if ((error = op->fn(patch, ctx)) < 0)
419 goto done;
420
421 parse_advance_ws(ctx);
422 parse_advance_expected_s(ctx, "\n");
423
424 if (ctx->line_len > 0) {
425 error = parse_err("trailing data at line %d", ctx->line_num);
426 goto done;
427 }
428
429 break;
430 }
431 }
432
433 done:
434 return error;
435 }
436
437 static int parse_number(git_off_t *out, patch_parse_ctx *ctx)
438 {
439 const char *end;
440 int64_t num;
441
442 if (!git__isdigit(ctx->line[0]))
443 return -1;
444
445 if (git__strntol64(&num, ctx->line, ctx->line_len, &end, 10) < 0)
446 return -1;
447
448 if (num < 0)
449 return -1;
450
451 *out = num;
452 parse_advance_chars(ctx, (end - ctx->line));
453
454 return 0;
455 }
456
457 static int parse_int(int *out, patch_parse_ctx *ctx)
458 {
459 git_off_t num;
460
461 if (parse_number(&num, ctx) < 0 || !git__is_int(num))
462 return -1;
463
464 *out = (int)num;
465 return 0;
466 }
467
468 static int parse_hunk_header(
469 git_patch_hunk *hunk,
470 patch_parse_ctx *ctx)
471 {
472 const char *header_start = ctx->line;
473
474 hunk->hunk.old_lines = 1;
475 hunk->hunk.new_lines = 1;
476
477 if (parse_advance_expected_s(ctx, "@@ -") < 0 ||
478 parse_int(&hunk->hunk.old_start, ctx) < 0)
479 goto fail;
480
481 if (ctx->line_len > 0 && ctx->line[0] == ',') {
482 if (parse_advance_expected_s(ctx, ",") < 0 ||
483 parse_int(&hunk->hunk.old_lines, ctx) < 0)
484 goto fail;
485 }
486
487 if (parse_advance_expected_s(ctx, " +") < 0 ||
488 parse_int(&hunk->hunk.new_start, ctx) < 0)
489 goto fail;
490
491 if (ctx->line_len > 0 && ctx->line[0] == ',') {
492 if (parse_advance_expected_s(ctx, ",") < 0 ||
493 parse_int(&hunk->hunk.new_lines, ctx) < 0)
494 goto fail;
495 }
496
497 if (parse_advance_expected_s(ctx, " @@") < 0)
498 goto fail;
499
500 parse_advance_line(ctx);
501
502 if (!hunk->hunk.old_lines && !hunk->hunk.new_lines)
503 goto fail;
504
505 hunk->hunk.header_len = ctx->line - header_start;
506 if (hunk->hunk.header_len > (GIT_DIFF_HUNK_HEADER_SIZE - 1))
507 return parse_err("oversized patch hunk header at line %d",
508 ctx->line_num);
509
510 memcpy(hunk->hunk.header, header_start, hunk->hunk.header_len);
511 hunk->hunk.header[hunk->hunk.header_len] = '\0';
512
513 return 0;
514
515 fail:
516 giterr_set(GITERR_PATCH, "invalid patch hunk header at line %d",
517 ctx->line_num);
518 return -1;
519 }
520
521 static int parse_hunk_body(
522 git_patch_parsed *patch,
523 git_patch_hunk *hunk,
524 patch_parse_ctx *ctx)
525 {
526 git_diff_line *line;
527 int error = 0;
528
529 int oldlines = hunk->hunk.old_lines;
530 int newlines = hunk->hunk.new_lines;
531
532 for (;
533 ctx->remain > 4 && (oldlines || newlines) &&
534 memcmp(ctx->line, "@@ -", 4) != 0;
535 parse_advance_line(ctx)) {
536
537 int origin;
538 int prefix = 1;
539
540 if (ctx->line_len == 0 || ctx->line[ctx->line_len - 1] != '\n') {
541 error = parse_err("invalid patch instruction at line %d",
542 ctx->line_num);
543 goto done;
544 }
545
546 switch (ctx->line[0]) {
547 case '\n':
548 prefix = 0;
549
550 case ' ':
551 origin = GIT_DIFF_LINE_CONTEXT;
552 oldlines--;
553 newlines--;
554 break;
555
556 case '-':
557 origin = GIT_DIFF_LINE_DELETION;
558 oldlines--;
559 break;
560
561 case '+':
562 origin = GIT_DIFF_LINE_ADDITION;
563 newlines--;
564 break;
565
566 default:
567 error = parse_err("invalid patch hunk at line %d", ctx->line_num);
568 goto done;
569 }
570
571 line = git_array_alloc(patch->base.lines);
572 GITERR_CHECK_ALLOC(line);
573
574 memset(line, 0x0, sizeof(git_diff_line));
575
576 line->content = ctx->line + prefix;
577 line->content_len = ctx->line_len - prefix;
578 line->content_offset = ctx->content_len - ctx->remain;
579 line->origin = origin;
580
581 hunk->line_count++;
582 }
583
584 if (oldlines || newlines) {
585 error = parse_err(
586 "invalid patch hunk, expected %d old lines and %d new lines",
587 hunk->hunk.old_lines, hunk->hunk.new_lines);
588 goto done;
589 }
590
591 /* Handle "\ No newline at end of file". Only expect the leading
592 * backslash, though, because the rest of the string could be
593 * localized. Because `diff` optimizes for the case where you
594 * want to apply the patch by hand.
595 */
596 if (ctx->line_len >= 2 && memcmp(ctx->line, "\\ ", 2) == 0 &&
597 git_array_size(patch->base.lines) > 0) {
598
599 line = git_array_get(patch->base.lines, git_array_size(patch->base.lines) - 1);
600
601 if (line->content_len < 1) {
602 error = parse_err("cannot trim trailing newline of empty line");
603 goto done;
604 }
605
606 line->content_len--;
607
608 parse_advance_line(ctx);
609 }
610
611 done:
612 return error;
613 }
614
615 static int parsed_patch_header(
616 git_patch_parsed *patch,
617 patch_parse_ctx *ctx)
618 {
619 int error = 0;
620
621 for (ctx->line = ctx->content; ctx->remain > 0; parse_advance_line(ctx)) {
622 /* This line is too short to be a patch header. */
623 if (ctx->line_len < 6)
624 continue;
625
626 /* This might be a hunk header without a patch header, provide a
627 * sensible error message. */
628 if (memcmp(ctx->line, "@@ -", 4) == 0) {
629 size_t line_num = ctx->line_num;
630 git_patch_hunk hunk;
631
632 /* If this cannot be parsed as a hunk header, it's just leading
633 * noise, continue.
634 */
635 if (parse_hunk_header(&hunk, ctx) < 0) {
636 giterr_clear();
637 continue;
638 }
639
640 error = parse_err("invalid hunk header outside patch at line %d",
641 line_num);
642 goto done;
643 }
644
645 /* This buffer is too short to contain a patch. */
646 if (ctx->remain < ctx->line_len + 6)
647 break;
648
649 /* A proper git patch */
650 if (ctx->line_len >= 11 && memcmp(ctx->line, "diff --git ", 11) == 0) {
651 error = parse_header_git(patch, ctx);
652 goto done;
653 }
654
655 error = 0;
656 continue;
657 }
658
659 error = parse_err("no header in patch file");
660
661 done:
662 return error;
663 }
664
665 static int parsed_patch_binary_side(
666 git_diff_binary_file *binary,
667 patch_parse_ctx *ctx)
668 {
669 git_diff_binary_t type = GIT_DIFF_BINARY_NONE;
670 git_buf base85 = GIT_BUF_INIT, decoded = GIT_BUF_INIT;
671 git_off_t len;
672 int error = 0;
673
674 if (ctx->line_len >= 8 && memcmp(ctx->line, "literal ", 8) == 0) {
675 type = GIT_DIFF_BINARY_LITERAL;
676 parse_advance_chars(ctx, 8);
677 }
678 else if (ctx->line_len >= 6 && memcmp(ctx->line, "delta ", 6) == 0) {
679 type = GIT_DIFF_BINARY_DELTA;
680 parse_advance_chars(ctx, 6);
681 }
682 else {
683 error = parse_err("unknown binary delta type at line %d", ctx->line_num);
684 goto done;
685 }
686
687 if (parse_number(&len, ctx) < 0 || parse_advance_nl(ctx) < 0 || len < 0) {
688 error = parse_err("invalid binary size at line %d", ctx->line_num);
689 goto done;
690 }
691
692 while (ctx->line_len) {
693 char c = ctx->line[0];
694 size_t encoded_len, decoded_len = 0, decoded_orig = decoded.size;
695
696 if (c == '\n')
697 break;
698 else if (c >= 'A' && c <= 'Z')
699 decoded_len = c - 'A' + 1;
700 else if (c >= 'a' && c <= 'z')
701 decoded_len = c - 'a' + (('z' - 'a') + 1) + 1;
702
703 if (!decoded_len) {
704 error = parse_err("invalid binary length at line %d", ctx->line_num);
705 goto done;
706 }
707
708 parse_advance_chars(ctx, 1);
709
710 encoded_len = ((decoded_len / 4) + !!(decoded_len % 4)) * 5;
711
712 if (encoded_len > ctx->line_len - 1) {
713 error = parse_err("truncated binary data at line %d", ctx->line_num);
714 goto done;
715 }
716
717 if ((error = git_buf_decode_base85(
718 &decoded, ctx->line, encoded_len, decoded_len)) < 0)
719 goto done;
720
721 if (decoded.size - decoded_orig != decoded_len) {
722 error = parse_err("truncated binary data at line %d", ctx->line_num);
723 goto done;
724 }
725
726 parse_advance_chars(ctx, encoded_len);
727
728 if (parse_advance_nl(ctx) < 0) {
729 error = parse_err("trailing data at line %d", ctx->line_num);
730 goto done;
731 }
732 }
733
734 binary->type = type;
735 binary->inflatedlen = (size_t)len;
736 binary->datalen = decoded.size;
737 binary->data = git_buf_detach(&decoded);
738
739 done:
740 git_buf_free(&base85);
741 git_buf_free(&decoded);
742 return error;
743 }
744
745 static int parsed_patch_binary(
746 git_patch_parsed *patch,
747 patch_parse_ctx *ctx)
748 {
749 int error;
750
751 if (parse_advance_expected_s(ctx, "GIT binary patch") < 0 ||
752 parse_advance_nl(ctx) < 0)
753 return parse_err("corrupt git binary header at line %d", ctx->line_num);
754
755 /* parse old->new binary diff */
756 if ((error = parsed_patch_binary_side(
757 &patch->base.binary.new_file, ctx)) < 0)
758 return error;
759
760 if (parse_advance_nl(ctx) < 0)
761 return parse_err("corrupt git binary separator at line %d",
762 ctx->line_num);
763
764 /* parse new->old binary diff */
765 if ((error = parsed_patch_binary_side(
766 &patch->base.binary.old_file, ctx)) < 0)
767 return error;
768
769 patch->base.delta->flags |= GIT_DIFF_FLAG_BINARY;
770 return 0;
771 }
772
773 static int parsed_patch_hunks(
774 git_patch_parsed *patch,
775 patch_parse_ctx *ctx)
776 {
777 git_patch_hunk *hunk;
778 int error = 0;
779
780 for (; ctx->line_len > 4 && memcmp(ctx->line, "@@ -", 4) == 0; ) {
781
782 hunk = git_array_alloc(patch->base.hunks);
783 GITERR_CHECK_ALLOC(hunk);
784
785 memset(hunk, 0, sizeof(git_patch_hunk));
786
787 hunk->line_start = git_array_size(patch->base.lines);
788 hunk->line_count = 0;
789
790 if ((error = parse_hunk_header(hunk, ctx)) < 0 ||
791 (error = parse_hunk_body(patch, hunk, ctx)) < 0)
792 goto done;
793 }
794
795 done:
796 return error;
797 }
798
799 static int parsed_patch_body(
800 git_patch_parsed *patch, patch_parse_ctx *ctx)
801 {
802 if (ctx->line_len >= 16 && memcmp(ctx->line, "GIT binary patch", 16) == 0)
803 return parsed_patch_binary(patch, ctx);
804
805 else if (ctx->line_len >= 4 && memcmp(ctx->line, "@@ -", 4) == 0)
806 return parsed_patch_hunks(patch, ctx);
807
808 return 0;
809 }
810
811 int check_header_names(
812 const char *one,
813 const char *two,
814 const char *old_or_new,
815 bool two_null)
816 {
817 if (!one || !two)
818 return 0;
819
820 if (two_null && strcmp(two, "/dev/null") != 0)
821 return parse_err("expected %s path of '/dev/null'", old_or_new);
822
823 else if (!two_null && strcmp(one, two) != 0)
824 return parse_err("mismatched %s path names", old_or_new);
825
826 return 0;
827 }
828
829 static int check_prefix(
830 char **out,
831 size_t *out_len,
832 git_patch_parsed *patch,
833 const char *path_start)
834 {
835 const char *path = path_start;
836 uint32_t remain = patch->opts.prefix_len;
837
838 *out = NULL;
839 *out_len = 0;
840
841 if (patch->opts.prefix_len == 0)
842 goto done;
843
844 /* leading slashes do not count as part of the prefix in git apply */
845 while (*path == '/')
846 path++;
847
848 while (*path && remain) {
849 if (*path == '/')
850 remain--;
851
852 path++;
853 }
854
855 if (remain || !*path)
856 return parse_err("header filename does not contain %d path components",
857 patch->opts.prefix_len);
858
859 done:
860 *out_len = (path - path_start);
861 *out = git__strndup(path_start, *out_len);
862
863 return (out == NULL) ? -1 : 0;
864 }
865
866 static int check_filenames(git_patch_parsed *patch)
867 {
868 const char *prefixed_new, *prefixed_old;
869 size_t old_prefixlen = 0, new_prefixlen = 0;
870 bool added = (patch->base.delta->status == GIT_DELTA_ADDED);
871 bool deleted = (patch->base.delta->status == GIT_DELTA_DELETED);
872
873 if (patch->old_path && !patch->new_path)
874 return parse_err("missing new path");
875
876 if (!patch->old_path && patch->new_path)
877 return parse_err("missing old path");
878
879 /* Ensure (non-renamed) paths match */
880 if (check_header_names(
881 patch->header_old_path, patch->old_path, "old", added) < 0 ||
882 check_header_names(
883 patch->header_new_path, patch->new_path, "new", deleted) < 0)
884 return -1;
885
886 prefixed_old = (!added && patch->old_path) ? patch->old_path :
887 patch->header_old_path;
888 prefixed_new = (!deleted && patch->new_path) ? patch->new_path :
889 patch->header_new_path;
890
891 if (check_prefix(
892 &patch->old_prefix, &old_prefixlen, patch, prefixed_old) < 0 ||
893 check_prefix(
894 &patch->new_prefix, &new_prefixlen, patch, prefixed_new) < 0)
895 return -1;
896
897 /* Prefer the rename filenames as they are unambiguous and unprefixed */
898 if (patch->rename_old_path)
899 patch->base.delta->old_file.path = patch->rename_old_path;
900 else
901 patch->base.delta->old_file.path = prefixed_old + old_prefixlen;
902
903 if (patch->rename_new_path)
904 patch->base.delta->new_file.path = patch->rename_new_path;
905 else
906 patch->base.delta->new_file.path = prefixed_new + new_prefixlen;
907
908 if (!patch->base.delta->old_file.path &&
909 !patch->base.delta->new_file.path)
910 return parse_err("git diff header lacks old / new paths");
911
912 return 0;
913 }
914
915 static int check_patch(git_patch_parsed *patch)
916 {
917 if (check_filenames(patch) < 0)
918 return -1;
919
920 if (patch->base.delta->old_file.path &&
921 patch->base.delta->status != GIT_DELTA_DELETED &&
922 !patch->base.delta->new_file.mode)
923 patch->base.delta->new_file.mode = patch->base.delta->old_file.mode;
924
925 if (patch->base.delta->status == GIT_DELTA_MODIFIED &&
926 !(patch->base.delta->flags & GIT_DIFF_FLAG_BINARY) &&
927 patch->base.delta->new_file.mode == patch->base.delta->old_file.mode &&
928 git_array_size(patch->base.hunks) == 0)
929 return parse_err("patch with no hunks");
930
931 return 0;
932 }
933
934 static void patch_parsed__free(git_patch *p)
935 {
936 git_patch_parsed *patch = (git_patch_parsed *)p;
937
938 if (!patch)
939 return;
940
941 git__free((char *)patch->base.binary.old_file.data);
942 git__free((char *)patch->base.binary.new_file.data);
943 git_array_clear(patch->base.hunks);
944 git_array_clear(patch->base.lines);
945 git__free(patch->base.delta);
946
947 git__free(patch->old_prefix);
948 git__free(patch->new_prefix);
949 git__free(patch->header_old_path);
950 git__free(patch->header_new_path);
951 git__free(patch->rename_old_path);
952 git__free(patch->rename_new_path);
953 git__free(patch->old_path);
954 git__free(patch->new_path);
955 git__free(patch->content);
956 git__free(patch);
957 }
958
959 int git_patch_from_buffer(
960 git_patch **out,
961 const char *content,
962 size_t content_len,
963 git_patch_options *opts)
964 {
965 patch_parse_ctx ctx = { 0 };
966 git_patch_parsed *patch;
967 git_patch_options default_opts = GIT_PATCH_OPTIONS_INIT;
968 int error = 0;
969
970 *out = NULL;
971
972 patch = git__calloc(1, sizeof(git_patch_parsed));
973 GITERR_CHECK_ALLOC(patch);
974
975 if (opts)
976 memcpy(&patch->opts, opts, sizeof(git_patch_options));
977 else
978 memcpy(&patch->opts, &default_opts, sizeof(git_patch_options));
979
980 patch->base.free_fn = patch_parsed__free;
981
982 patch->base.delta = git__calloc(1, sizeof(git_diff_delta));
983 GITERR_CHECK_ALLOC(patch->base.delta);
984
985 patch->base.delta->status = GIT_DELTA_MODIFIED;
986 patch->base.delta->nfiles = 2;
987
988 if (content_len) {
989 patch->content = git__malloc(content_len);
990 GITERR_CHECK_ALLOC(patch->content);
991
992 memcpy(patch->content, content, content_len);
993 }
994
995 ctx.content = patch->content;
996 ctx.content_len = content_len;
997 ctx.remain = content_len;
998
999 if ((error = parsed_patch_header(patch, &ctx)) < 0 ||
1000 (error = parsed_patch_body(patch, &ctx)) < 0 ||
1001 (error = check_patch(patch)) < 0)
1002 goto done;
1003
1004 patch->base.diff_opts.old_prefix = patch->old_prefix;
1005 patch->base.diff_opts.new_prefix = patch->new_prefix;
1006 patch->base.diff_opts.flags |= GIT_DIFF_SHOW_BINARY;
1007
1008 GIT_REFCOUNT_INC(patch);
1009 *out = &patch->base;
1010
1011 done:
1012 if (error < 0)
1013 patch_parsed__free(&patch->base);
1014
1015 return error;
1016 }