]> git.proxmox.com Git - libgit2.git/blob - src/patch_generate.c
Merge pull request #3882 from pks-t/pks/fix-fetch-refspec-dst-parsing
[libgit2.git] / src / patch_generate.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7 #include "common.h"
8 #include "git2/blob.h"
9 #include "diff.h"
10 #include "diff_generate.h"
11 #include "diff_file.h"
12 #include "diff_driver.h"
13 #include "patch_generate.h"
14 #include "diff_xdiff.h"
15 #include "delta.h"
16 #include "zstream.h"
17 #include "fileops.h"
18
19 static void diff_output_init(
20 git_patch_generated_output *, const git_diff_options *, git_diff_file_cb,
21 git_diff_binary_cb, git_diff_hunk_cb, git_diff_line_cb, void*);
22
23 static void diff_output_to_patch(
24 git_patch_generated_output *, git_patch_generated *);
25
26 static void patch_generated_free(git_patch *p)
27 {
28 git_patch_generated *patch = (git_patch_generated *)p;
29
30 git_array_clear(patch->base.lines);
31 git_array_clear(patch->base.hunks);
32
33 git__free((char *)patch->base.binary.old_file.data);
34 git__free((char *)patch->base.binary.new_file.data);
35
36 git_diff_file_content__clear(&patch->ofile);
37 git_diff_file_content__clear(&patch->nfile);
38
39 git_diff_free(patch->diff); /* decrements refcount */
40 patch->diff = NULL;
41
42 git_pool_clear(&patch->flattened);
43
44 git__free((char *)patch->base.diff_opts.old_prefix);
45 git__free((char *)patch->base.diff_opts.new_prefix);
46
47 if (patch->flags & GIT_PATCH_GENERATED_ALLOCATED)
48 git__free(patch);
49 }
50
51 static void patch_generated_update_binary(git_patch_generated *patch)
52 {
53 if ((patch->base.delta->flags & DIFF_FLAGS_KNOWN_BINARY) != 0)
54 return;
55
56 if ((patch->ofile.file->flags & GIT_DIFF_FLAG_BINARY) != 0 ||
57 (patch->nfile.file->flags & GIT_DIFF_FLAG_BINARY) != 0)
58 patch->base.delta->flags |= GIT_DIFF_FLAG_BINARY;
59
60 else if (patch->ofile.file->size > GIT_XDIFF_MAX_SIZE ||
61 patch->nfile.file->size > GIT_XDIFF_MAX_SIZE)
62 patch->base.delta->flags |= GIT_DIFF_FLAG_BINARY;
63
64 else if ((patch->ofile.file->flags & DIFF_FLAGS_NOT_BINARY) != 0 &&
65 (patch->nfile.file->flags & DIFF_FLAGS_NOT_BINARY) != 0)
66 patch->base.delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;
67 }
68
69 static void patch_generated_init_common(git_patch_generated *patch)
70 {
71 patch->base.free_fn = patch_generated_free;
72
73 patch_generated_update_binary(patch);
74
75 patch->flags |= GIT_PATCH_GENERATED_INITIALIZED;
76
77 if (patch->diff)
78 git_diff_addref(patch->diff);
79 }
80
81 static int patch_generated_normalize_options(
82 git_diff_options *out,
83 const git_diff_options *opts)
84 {
85 if (opts) {
86 GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options");
87 memcpy(out, opts, sizeof(git_diff_options));
88 } else {
89 git_diff_options default_opts = GIT_DIFF_OPTIONS_INIT;
90 memcpy(out, &default_opts, sizeof(git_diff_options));
91 }
92
93 out->old_prefix = opts && opts->old_prefix ?
94 git__strdup(opts->old_prefix) :
95 git__strdup(DIFF_OLD_PREFIX_DEFAULT);
96
97 out->new_prefix = opts && opts->new_prefix ?
98 git__strdup(opts->new_prefix) :
99 git__strdup(DIFF_NEW_PREFIX_DEFAULT);
100
101 GITERR_CHECK_ALLOC(out->old_prefix);
102 GITERR_CHECK_ALLOC(out->new_prefix);
103
104 return 0;
105 }
106
107 static int patch_generated_init(
108 git_patch_generated *patch, git_diff *diff, size_t delta_index)
109 {
110 int error = 0;
111
112 memset(patch, 0, sizeof(*patch));
113
114 patch->diff = diff;
115 patch->base.repo = diff->repo;
116 patch->base.delta = git_vector_get(&diff->deltas, delta_index);
117 patch->delta_index = delta_index;
118
119 if ((error = patch_generated_normalize_options(
120 &patch->base.diff_opts, &diff->opts)) < 0 ||
121 (error = git_diff_file_content__init_from_diff(
122 &patch->ofile, diff, patch->base.delta, true)) < 0 ||
123 (error = git_diff_file_content__init_from_diff(
124 &patch->nfile, diff, patch->base.delta, false)) < 0)
125 return error;
126
127 patch_generated_init_common(patch);
128
129 return 0;
130 }
131
132 static int patch_generated_alloc_from_diff(
133 git_patch_generated **out, git_diff *diff, size_t delta_index)
134 {
135 int error;
136 git_patch_generated *patch = git__calloc(1, sizeof(git_patch_generated));
137 GITERR_CHECK_ALLOC(patch);
138
139 if (!(error = patch_generated_init(patch, diff, delta_index))) {
140 patch->flags |= GIT_PATCH_GENERATED_ALLOCATED;
141 GIT_REFCOUNT_INC(patch);
142 } else {
143 git__free(patch);
144 patch = NULL;
145 }
146
147 *out = patch;
148 return error;
149 }
150
151 GIT_INLINE(bool) should_skip_binary(git_patch_generated *patch, git_diff_file *file)
152 {
153 if ((patch->base.diff_opts.flags & GIT_DIFF_SHOW_BINARY) != 0)
154 return false;
155
156 return (file->flags & GIT_DIFF_FLAG_BINARY) != 0;
157 }
158
159 static bool patch_generated_diffable(git_patch_generated *patch)
160 {
161 size_t olen, nlen;
162
163 if (patch->base.delta->status == GIT_DELTA_UNMODIFIED)
164 return false;
165
166 /* if we've determined this to be binary (and we are not showing binary
167 * data) then we have skipped loading the map data. instead, query the
168 * file data itself.
169 */
170 if ((patch->base.delta->flags & GIT_DIFF_FLAG_BINARY) != 0 &&
171 (patch->base.diff_opts.flags & GIT_DIFF_SHOW_BINARY) == 0) {
172 olen = (size_t)patch->ofile.file->size;
173 nlen = (size_t)patch->nfile.file->size;
174 } else {
175 olen = patch->ofile.map.len;
176 nlen = patch->nfile.map.len;
177 }
178
179 /* if both sides are empty, files are identical */
180 if (!olen && !nlen)
181 return false;
182
183 /* otherwise, check the file sizes and the oid */
184 return (olen != nlen ||
185 !git_oid_equal(&patch->ofile.file->id, &patch->nfile.file->id));
186 }
187
188 static int patch_generated_load(git_patch_generated *patch, git_patch_generated_output *output)
189 {
190 int error = 0;
191 bool incomplete_data;
192
193 if ((patch->flags & GIT_PATCH_GENERATED_LOADED) != 0)
194 return 0;
195
196 /* if no hunk and data callbacks and user doesn't care if data looks
197 * binary, then there is no need to actually load the data
198 */
199 if ((patch->ofile.opts_flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0 &&
200 output && !output->binary_cb && !output->hunk_cb && !output->data_cb)
201 return 0;
202
203 incomplete_data =
204 (((patch->ofile.flags & GIT_DIFF_FLAG__NO_DATA) != 0 ||
205 (patch->ofile.file->flags & GIT_DIFF_FLAG_VALID_ID) != 0) &&
206 ((patch->nfile.flags & GIT_DIFF_FLAG__NO_DATA) != 0 ||
207 (patch->nfile.file->flags & GIT_DIFF_FLAG_VALID_ID) != 0));
208
209 /* always try to load workdir content first because filtering may
210 * need 2x data size and this minimizes peak memory footprint
211 */
212 if (patch->ofile.src == GIT_ITERATOR_TYPE_WORKDIR) {
213 if ((error = git_diff_file_content__load(
214 &patch->ofile, &patch->base.diff_opts)) < 0 ||
215 should_skip_binary(patch, patch->ofile.file))
216 goto cleanup;
217 }
218 if (patch->nfile.src == GIT_ITERATOR_TYPE_WORKDIR) {
219 if ((error = git_diff_file_content__load(
220 &patch->nfile, &patch->base.diff_opts)) < 0 ||
221 should_skip_binary(patch, patch->nfile.file))
222 goto cleanup;
223 }
224
225 /* once workdir has been tried, load other data as needed */
226 if (patch->ofile.src != GIT_ITERATOR_TYPE_WORKDIR) {
227 if ((error = git_diff_file_content__load(
228 &patch->ofile, &patch->base.diff_opts)) < 0 ||
229 should_skip_binary(patch, patch->ofile.file))
230 goto cleanup;
231 }
232 if (patch->nfile.src != GIT_ITERATOR_TYPE_WORKDIR) {
233 if ((error = git_diff_file_content__load(
234 &patch->nfile, &patch->base.diff_opts)) < 0 ||
235 should_skip_binary(patch, patch->nfile.file))
236 goto cleanup;
237 }
238
239 /* if previously missing an oid, and now that we have it the two sides
240 * are the same (and not submodules), update MODIFIED -> UNMODIFIED
241 */
242 if (incomplete_data &&
243 patch->ofile.file->mode == patch->nfile.file->mode &&
244 patch->ofile.file->mode != GIT_FILEMODE_COMMIT &&
245 git_oid_equal(&patch->ofile.file->id, &patch->nfile.file->id) &&
246 patch->base.delta->status == GIT_DELTA_MODIFIED) /* not RENAMED/COPIED! */
247 patch->base.delta->status = GIT_DELTA_UNMODIFIED;
248
249 cleanup:
250 patch_generated_update_binary(patch);
251
252 if (!error) {
253 if (patch_generated_diffable(patch))
254 patch->flags |= GIT_PATCH_GENERATED_DIFFABLE;
255
256 patch->flags |= GIT_PATCH_GENERATED_LOADED;
257 }
258
259 return error;
260 }
261
262 static int patch_generated_invoke_file_callback(
263 git_patch_generated *patch, git_patch_generated_output *output)
264 {
265 float progress = patch->diff ?
266 ((float)patch->delta_index / patch->diff->deltas.length) : 1.0f;
267
268 if (!output->file_cb)
269 return 0;
270
271 return giterr_set_after_callback_function(
272 output->file_cb(patch->base.delta, progress, output->payload),
273 "git_patch");
274 }
275
276 static int create_binary(
277 git_diff_binary_t *out_type,
278 char **out_data,
279 size_t *out_datalen,
280 size_t *out_inflatedlen,
281 const char *a_data,
282 size_t a_datalen,
283 const char *b_data,
284 size_t b_datalen)
285 {
286 git_buf deflate = GIT_BUF_INIT, delta = GIT_BUF_INIT;
287 size_t delta_data_len;
288 int error;
289
290 /* The git_delta function accepts unsigned long only */
291 if (!git__is_ulong(a_datalen) || !git__is_ulong(b_datalen))
292 return GIT_EBUFS;
293
294 if ((error = git_zstream_deflatebuf(&deflate, b_data, b_datalen)) < 0)
295 goto done;
296
297 /* The git_delta function accepts unsigned long only */
298 if (!git__is_ulong(deflate.size)) {
299 error = GIT_EBUFS;
300 goto done;
301 }
302
303 if (a_datalen && b_datalen) {
304 void *delta_data;
305
306 error = git_delta(&delta_data, &delta_data_len,
307 a_data, a_datalen,
308 b_data, b_datalen,
309 deflate.size);
310
311 if (error == 0) {
312 error = git_zstream_deflatebuf(
313 &delta, delta_data, delta_data_len);
314
315 git__free(delta_data);
316 } else if (error == GIT_EBUFS) {
317 error = 0;
318 }
319
320 if (error < 0)
321 goto done;
322 }
323
324 if (delta.size && delta.size < deflate.size) {
325 *out_type = GIT_DIFF_BINARY_DELTA;
326 *out_datalen = delta.size;
327 *out_data = git_buf_detach(&delta);
328 *out_inflatedlen = delta_data_len;
329 } else {
330 *out_type = GIT_DIFF_BINARY_LITERAL;
331 *out_datalen = deflate.size;
332 *out_data = git_buf_detach(&deflate);
333 *out_inflatedlen = b_datalen;
334 }
335
336 done:
337 git_buf_free(&deflate);
338 git_buf_free(&delta);
339
340 return error;
341 }
342
343 static int diff_binary(git_patch_generated_output *output, git_patch_generated *patch)
344 {
345 git_diff_binary binary = {{0}};
346 const char *old_data = patch->ofile.map.data;
347 const char *new_data = patch->nfile.map.data;
348 size_t old_len = patch->ofile.map.len,
349 new_len = patch->nfile.map.len;
350 int error;
351
352 /* Only load contents if the user actually wants to diff
353 * binary files. */
354 if (patch->base.diff_opts.flags & GIT_DIFF_SHOW_BINARY) {
355 /* Create the old->new delta (as the "new" side of the patch),
356 * and the new->old delta (as the "old" side)
357 */
358 if ((error = create_binary(&binary.old_file.type,
359 (char **)&binary.old_file.data,
360 &binary.old_file.datalen,
361 &binary.old_file.inflatedlen,
362 new_data, new_len, old_data, old_len)) < 0 ||
363 (error = create_binary(&binary.new_file.type,
364 (char **)&binary.new_file.data,
365 &binary.new_file.datalen,
366 &binary.new_file.inflatedlen,
367 old_data, old_len, new_data, new_len)) < 0)
368 return error;
369 }
370
371 error = giterr_set_after_callback_function(
372 output->binary_cb(patch->base.delta, &binary, output->payload),
373 "git_patch");
374
375 git__free((char *) binary.old_file.data);
376 git__free((char *) binary.new_file.data);
377
378 return error;
379 }
380
381 static int patch_generated_create(
382 git_patch_generated *patch,
383 git_patch_generated_output *output)
384 {
385 int error = 0;
386
387 if ((patch->flags & GIT_PATCH_GENERATED_DIFFED) != 0)
388 return 0;
389
390 /* if we are not looking at the binary or text data, don't do the diff */
391 if (!output->binary_cb && !output->hunk_cb && !output->data_cb)
392 return 0;
393
394 if ((patch->flags & GIT_PATCH_GENERATED_LOADED) == 0 &&
395 (error = patch_generated_load(patch, output)) < 0)
396 return error;
397
398 if ((patch->flags & GIT_PATCH_GENERATED_DIFFABLE) == 0)
399 return 0;
400
401 if ((patch->base.delta->flags & GIT_DIFF_FLAG_BINARY) != 0) {
402 if (output->binary_cb)
403 error = diff_binary(output, patch);
404 }
405 else {
406 if (output->diff_cb)
407 error = output->diff_cb(output, patch);
408 }
409
410 patch->flags |= GIT_PATCH_GENERATED_DIFFED;
411 return error;
412 }
413
414 static int diff_required(git_diff *diff, const char *action)
415 {
416 if (diff)
417 return 0;
418 giterr_set(GITERR_INVALID, "Must provide valid diff to %s", action);
419 return -1;
420 }
421
422 int git_diff_foreach(
423 git_diff *diff,
424 git_diff_file_cb file_cb,
425 git_diff_binary_cb binary_cb,
426 git_diff_hunk_cb hunk_cb,
427 git_diff_line_cb data_cb,
428 void *payload)
429 {
430 int error = 0;
431 git_xdiff_output xo;
432 size_t idx;
433 git_patch_generated patch;
434
435 if ((error = diff_required(diff, "git_diff_foreach")) < 0)
436 return error;
437
438 memset(&xo, 0, sizeof(xo));
439 memset(&patch, 0, sizeof(patch));
440 diff_output_init(
441 &xo.output, &diff->opts, file_cb, binary_cb, hunk_cb, data_cb, payload);
442 git_xdiff_init(&xo, &diff->opts);
443
444 git_vector_foreach(&diff->deltas, idx, patch.base.delta) {
445
446 /* check flags against patch status */
447 if (git_diff_delta__should_skip(&diff->opts, patch.base.delta))
448 continue;
449
450 if (binary_cb || hunk_cb || data_cb) {
451 if ((error = patch_generated_init(&patch, diff, idx)) != 0 ||
452 (error = patch_generated_load(&patch, &xo.output)) != 0)
453 return error;
454 }
455
456 if ((error = patch_generated_invoke_file_callback(&patch, &xo.output)) == 0) {
457 if (binary_cb || hunk_cb || data_cb)
458 error = patch_generated_create(&patch, &xo.output);
459 }
460
461 git_patch_free(&patch.base);
462
463 if (error)
464 break;
465 }
466
467 return error;
468 }
469
470 typedef struct {
471 git_patch_generated patch;
472 git_diff_delta delta;
473 char paths[GIT_FLEX_ARRAY];
474 } patch_generated_with_delta;
475
476 static int diff_single_generate(patch_generated_with_delta *pd, git_xdiff_output *xo)
477 {
478 int error = 0;
479 git_patch_generated *patch = &pd->patch;
480 bool has_old = ((patch->ofile.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
481 bool has_new = ((patch->nfile.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
482
483 pd->delta.status = has_new ?
484 (has_old ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) :
485 (has_old ? GIT_DELTA_DELETED : GIT_DELTA_UNTRACKED);
486
487 if (git_oid_equal(&patch->nfile.file->id, &patch->ofile.file->id))
488 pd->delta.status = GIT_DELTA_UNMODIFIED;
489
490 patch->base.delta = &pd->delta;
491
492 patch_generated_init_common(patch);
493
494 if (pd->delta.status == GIT_DELTA_UNMODIFIED &&
495 !(patch->ofile.opts_flags & GIT_DIFF_INCLUDE_UNMODIFIED))
496 return error;
497
498 error = patch_generated_invoke_file_callback(patch, (git_patch_generated_output *)xo);
499
500 if (!error)
501 error = patch_generated_create(patch, (git_patch_generated_output *)xo);
502
503 return error;
504 }
505
506 static int patch_generated_from_sources(
507 patch_generated_with_delta *pd,
508 git_xdiff_output *xo,
509 git_diff_file_content_src *oldsrc,
510 git_diff_file_content_src *newsrc,
511 const git_diff_options *opts)
512 {
513 int error = 0;
514 git_repository *repo =
515 oldsrc->blob ? git_blob_owner(oldsrc->blob) :
516 newsrc->blob ? git_blob_owner(newsrc->blob) : NULL;
517 git_diff_file *lfile = &pd->delta.old_file, *rfile = &pd->delta.new_file;
518 git_diff_file_content *ldata = &pd->patch.ofile, *rdata = &pd->patch.nfile;
519
520 if ((error = patch_generated_normalize_options(&pd->patch.base.diff_opts, opts)) < 0)
521 return error;
522
523 if (opts && (opts->flags & GIT_DIFF_REVERSE) != 0) {
524 void *tmp = lfile; lfile = rfile; rfile = tmp;
525 tmp = ldata; ldata = rdata; rdata = tmp;
526 }
527
528 pd->patch.base.delta = &pd->delta;
529
530 if (!oldsrc->as_path) {
531 if (newsrc->as_path)
532 oldsrc->as_path = newsrc->as_path;
533 else
534 oldsrc->as_path = newsrc->as_path = "file";
535 }
536 else if (!newsrc->as_path)
537 newsrc->as_path = oldsrc->as_path;
538
539 lfile->path = oldsrc->as_path;
540 rfile->path = newsrc->as_path;
541
542 if ((error = git_diff_file_content__init_from_src(
543 ldata, repo, opts, oldsrc, lfile)) < 0 ||
544 (error = git_diff_file_content__init_from_src(
545 rdata, repo, opts, newsrc, rfile)) < 0)
546 return error;
547
548 return diff_single_generate(pd, xo);
549 }
550
551 static int patch_generated_with_delta_alloc(
552 patch_generated_with_delta **out,
553 const char **old_path,
554 const char **new_path)
555 {
556 patch_generated_with_delta *pd;
557 size_t old_len = *old_path ? strlen(*old_path) : 0;
558 size_t new_len = *new_path ? strlen(*new_path) : 0;
559 size_t alloc_len;
560
561 GITERR_CHECK_ALLOC_ADD(&alloc_len, sizeof(*pd), old_len);
562 GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, new_len);
563 GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2);
564
565 *out = pd = git__calloc(1, alloc_len);
566 GITERR_CHECK_ALLOC(pd);
567
568 pd->patch.flags = GIT_PATCH_GENERATED_ALLOCATED;
569
570 if (*old_path) {
571 memcpy(&pd->paths[0], *old_path, old_len);
572 *old_path = &pd->paths[0];
573 } else if (*new_path)
574 *old_path = &pd->paths[old_len + 1];
575
576 if (*new_path) {
577 memcpy(&pd->paths[old_len + 1], *new_path, new_len);
578 *new_path = &pd->paths[old_len + 1];
579 } else if (*old_path)
580 *new_path = &pd->paths[0];
581
582 return 0;
583 }
584
585 static int diff_from_sources(
586 git_diff_file_content_src *oldsrc,
587 git_diff_file_content_src *newsrc,
588 const git_diff_options *opts,
589 git_diff_file_cb file_cb,
590 git_diff_binary_cb binary_cb,
591 git_diff_hunk_cb hunk_cb,
592 git_diff_line_cb data_cb,
593 void *payload)
594 {
595 int error = 0;
596 patch_generated_with_delta pd;
597 git_xdiff_output xo;
598
599 memset(&xo, 0, sizeof(xo));
600 diff_output_init(
601 &xo.output, opts, file_cb, binary_cb, hunk_cb, data_cb, payload);
602 git_xdiff_init(&xo, opts);
603
604 memset(&pd, 0, sizeof(pd));
605
606 error = patch_generated_from_sources(&pd, &xo, oldsrc, newsrc, opts);
607
608 git_patch_free(&pd.patch.base);
609
610 return error;
611 }
612
613 static int patch_from_sources(
614 git_patch **out,
615 git_diff_file_content_src *oldsrc,
616 git_diff_file_content_src *newsrc,
617 const git_diff_options *opts)
618 {
619 int error = 0;
620 patch_generated_with_delta *pd;
621 git_xdiff_output xo;
622
623 assert(out);
624 *out = NULL;
625
626 if ((error = patch_generated_with_delta_alloc(
627 &pd, &oldsrc->as_path, &newsrc->as_path)) < 0)
628 return error;
629
630 memset(&xo, 0, sizeof(xo));
631 diff_output_to_patch(&xo.output, &pd->patch);
632 git_xdiff_init(&xo, opts);
633
634 if (!(error = patch_generated_from_sources(pd, &xo, oldsrc, newsrc, opts)))
635 *out = (git_patch *)pd;
636 else
637 git_patch_free((git_patch *)pd);
638
639 return error;
640 }
641
642 int git_diff_blobs(
643 const git_blob *old_blob,
644 const char *old_path,
645 const git_blob *new_blob,
646 const char *new_path,
647 const git_diff_options *opts,
648 git_diff_file_cb file_cb,
649 git_diff_binary_cb binary_cb,
650 git_diff_hunk_cb hunk_cb,
651 git_diff_line_cb data_cb,
652 void *payload)
653 {
654 git_diff_file_content_src osrc =
655 GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
656 git_diff_file_content_src nsrc =
657 GIT_DIFF_FILE_CONTENT_SRC__BLOB(new_blob, new_path);
658 return diff_from_sources(
659 &osrc, &nsrc, opts, file_cb, binary_cb, hunk_cb, data_cb, payload);
660 }
661
662 int git_patch_from_blobs(
663 git_patch **out,
664 const git_blob *old_blob,
665 const char *old_path,
666 const git_blob *new_blob,
667 const char *new_path,
668 const git_diff_options *opts)
669 {
670 git_diff_file_content_src osrc =
671 GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
672 git_diff_file_content_src nsrc =
673 GIT_DIFF_FILE_CONTENT_SRC__BLOB(new_blob, new_path);
674 return patch_from_sources(out, &osrc, &nsrc, opts);
675 }
676
677 int git_diff_blob_to_buffer(
678 const git_blob *old_blob,
679 const char *old_path,
680 const char *buf,
681 size_t buflen,
682 const char *buf_path,
683 const git_diff_options *opts,
684 git_diff_file_cb file_cb,
685 git_diff_binary_cb binary_cb,
686 git_diff_hunk_cb hunk_cb,
687 git_diff_line_cb data_cb,
688 void *payload)
689 {
690 git_diff_file_content_src osrc =
691 GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
692 git_diff_file_content_src nsrc =
693 GIT_DIFF_FILE_CONTENT_SRC__BUF(buf, buflen, buf_path);
694 return diff_from_sources(
695 &osrc, &nsrc, opts, file_cb, binary_cb, hunk_cb, data_cb, payload);
696 }
697
698 int git_patch_from_blob_and_buffer(
699 git_patch **out,
700 const git_blob *old_blob,
701 const char *old_path,
702 const char *buf,
703 size_t buflen,
704 const char *buf_path,
705 const git_diff_options *opts)
706 {
707 git_diff_file_content_src osrc =
708 GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
709 git_diff_file_content_src nsrc =
710 GIT_DIFF_FILE_CONTENT_SRC__BUF(buf, buflen, buf_path);
711 return patch_from_sources(out, &osrc, &nsrc, opts);
712 }
713
714 int git_diff_buffers(
715 const void *old_buf,
716 size_t old_len,
717 const char *old_path,
718 const void *new_buf,
719 size_t new_len,
720 const char *new_path,
721 const git_diff_options *opts,
722 git_diff_file_cb file_cb,
723 git_diff_binary_cb binary_cb,
724 git_diff_hunk_cb hunk_cb,
725 git_diff_line_cb data_cb,
726 void *payload)
727 {
728 git_diff_file_content_src osrc =
729 GIT_DIFF_FILE_CONTENT_SRC__BUF(old_buf, old_len, old_path);
730 git_diff_file_content_src nsrc =
731 GIT_DIFF_FILE_CONTENT_SRC__BUF(new_buf, new_len, new_path);
732 return diff_from_sources(
733 &osrc, &nsrc, opts, file_cb, binary_cb, hunk_cb, data_cb, payload);
734 }
735
736 int git_patch_from_buffers(
737 git_patch **out,
738 const void *old_buf,
739 size_t old_len,
740 const char *old_path,
741 const char *new_buf,
742 size_t new_len,
743 const char *new_path,
744 const git_diff_options *opts)
745 {
746 git_diff_file_content_src osrc =
747 GIT_DIFF_FILE_CONTENT_SRC__BUF(old_buf, old_len, old_path);
748 git_diff_file_content_src nsrc =
749 GIT_DIFF_FILE_CONTENT_SRC__BUF(new_buf, new_len, new_path);
750 return patch_from_sources(out, &osrc, &nsrc, opts);
751 }
752
753 int git_patch_generated_from_diff(
754 git_patch **patch_ptr, git_diff *diff, size_t idx)
755 {
756 int error = 0;
757 git_xdiff_output xo;
758 git_diff_delta *delta = NULL;
759 git_patch_generated *patch = NULL;
760
761 if (patch_ptr) *patch_ptr = NULL;
762
763 if (diff_required(diff, "git_patch_from_diff") < 0)
764 return -1;
765
766 delta = git_vector_get(&diff->deltas, idx);
767 if (!delta) {
768 giterr_set(GITERR_INVALID, "Index out of range for delta in diff");
769 return GIT_ENOTFOUND;
770 }
771
772 if (git_diff_delta__should_skip(&diff->opts, delta))
773 return 0;
774
775 /* don't load the patch data unless we need it for binary check */
776 if (!patch_ptr &&
777 ((delta->flags & DIFF_FLAGS_KNOWN_BINARY) != 0 ||
778 (diff->opts.flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0))
779 return 0;
780
781 if ((error = patch_generated_alloc_from_diff(&patch, diff, idx)) < 0)
782 return error;
783
784 memset(&xo, 0, sizeof(xo));
785 diff_output_to_patch(&xo.output, patch);
786 git_xdiff_init(&xo, &diff->opts);
787
788 error = patch_generated_invoke_file_callback(patch, &xo.output);
789
790 if (!error)
791 error = patch_generated_create(patch, &xo.output);
792
793 if (!error) {
794 /* TODO: if cumulative diff size is < 0.5 total size, flatten patch */
795 /* TODO: and unload the file content */
796 }
797
798 if (error || !patch_ptr)
799 git_patch_free(&patch->base);
800 else
801 *patch_ptr = &patch->base;
802
803 return error;
804 }
805
806 git_diff_driver *git_patch_generated_driver(git_patch_generated *patch)
807 {
808 /* ofile driver is representative for whole patch */
809 return patch->ofile.driver;
810 }
811
812 void git_patch_generated_old_data(
813 char **ptr, size_t *len, git_patch_generated *patch)
814 {
815 *ptr = patch->ofile.map.data;
816 *len = patch->ofile.map.len;
817 }
818
819 void git_patch_generated_new_data(
820 char **ptr, size_t *len, git_patch_generated *patch)
821 {
822 *ptr = patch->nfile.map.data;
823 *len = patch->nfile.map.len;
824 }
825
826 static int patch_generated_file_cb(
827 const git_diff_delta *delta,
828 float progress,
829 void *payload)
830 {
831 GIT_UNUSED(delta); GIT_UNUSED(progress); GIT_UNUSED(payload);
832 return 0;
833 }
834
835 static int patch_generated_binary_cb(
836 const git_diff_delta *delta,
837 const git_diff_binary *binary,
838 void *payload)
839 {
840 git_patch *patch = payload;
841
842 GIT_UNUSED(delta);
843
844 memcpy(&patch->binary, binary, sizeof(git_diff_binary));
845
846 if (binary->old_file.data) {
847 patch->binary.old_file.data = git__malloc(binary->old_file.datalen);
848 GITERR_CHECK_ALLOC(patch->binary.old_file.data);
849
850 memcpy((char *)patch->binary.old_file.data,
851 binary->old_file.data, binary->old_file.datalen);
852 }
853
854 if (binary->new_file.data) {
855 patch->binary.new_file.data = git__malloc(binary->new_file.datalen);
856 GITERR_CHECK_ALLOC(patch->binary.new_file.data);
857
858 memcpy((char *)patch->binary.new_file.data,
859 binary->new_file.data, binary->new_file.datalen);
860 }
861
862 return 0;
863 }
864
865 static int git_patch_hunk_cb(
866 const git_diff_delta *delta,
867 const git_diff_hunk *hunk_,
868 void *payload)
869 {
870 git_patch_generated *patch = payload;
871 git_patch_hunk *hunk;
872
873 GIT_UNUSED(delta);
874
875 hunk = git_array_alloc(patch->base.hunks);
876 GITERR_CHECK_ALLOC(hunk);
877
878 memcpy(&hunk->hunk, hunk_, sizeof(hunk->hunk));
879
880 patch->base.header_size += hunk_->header_len;
881
882 hunk->line_start = git_array_size(patch->base.lines);
883 hunk->line_count = 0;
884
885 return 0;
886 }
887
888 static int patch_generated_line_cb(
889 const git_diff_delta *delta,
890 const git_diff_hunk *hunk_,
891 const git_diff_line *line_,
892 void *payload)
893 {
894 git_patch_generated *patch = payload;
895 git_patch_hunk *hunk;
896 git_diff_line *line;
897
898 GIT_UNUSED(delta);
899 GIT_UNUSED(hunk_);
900
901 hunk = git_array_last(patch->base.hunks);
902 assert(hunk); /* programmer error if no hunk is available */
903
904 line = git_array_alloc(patch->base.lines);
905 GITERR_CHECK_ALLOC(line);
906
907 memcpy(line, line_, sizeof(*line));
908
909 /* do some bookkeeping so we can provide old/new line numbers */
910
911 patch->base.content_size += line->content_len;
912
913 if (line->origin == GIT_DIFF_LINE_ADDITION ||
914 line->origin == GIT_DIFF_LINE_DELETION)
915 patch->base.content_size += 1;
916 else if (line->origin == GIT_DIFF_LINE_CONTEXT) {
917 patch->base.content_size += 1;
918 patch->base.context_size += line->content_len + 1;
919 } else if (line->origin == GIT_DIFF_LINE_CONTEXT_EOFNL)
920 patch->base.context_size += line->content_len;
921
922 hunk->line_count++;
923
924 return 0;
925 }
926
927 static void diff_output_init(
928 git_patch_generated_output *out,
929 const git_diff_options *opts,
930 git_diff_file_cb file_cb,
931 git_diff_binary_cb binary_cb,
932 git_diff_hunk_cb hunk_cb,
933 git_diff_line_cb data_cb,
934 void *payload)
935 {
936 GIT_UNUSED(opts);
937
938 memset(out, 0, sizeof(*out));
939
940 out->file_cb = file_cb;
941 out->binary_cb = binary_cb;
942 out->hunk_cb = hunk_cb;
943 out->data_cb = data_cb;
944 out->payload = payload;
945 }
946
947 static void diff_output_to_patch(
948 git_patch_generated_output *out, git_patch_generated *patch)
949 {
950 diff_output_init(
951 out,
952 NULL,
953 patch_generated_file_cb,
954 patch_generated_binary_cb,
955 git_patch_hunk_cb,
956 patch_generated_line_cb,
957 patch);
958 }