]> git.proxmox.com Git - libgit2.git/blob - src/patch_generate.c
Merge pull request #4027 from pks-t/pks/pack-deref-cache-on-error
[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 = 0;
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 binary.contains_data = 1;
356
357 /* Create the old->new delta (as the "new" side of the patch),
358 * and the new->old delta (as the "old" side)
359 */
360 if ((error = create_binary(&binary.old_file.type,
361 (char **)&binary.old_file.data,
362 &binary.old_file.datalen,
363 &binary.old_file.inflatedlen,
364 new_data, new_len, old_data, old_len)) < 0 ||
365 (error = create_binary(&binary.new_file.type,
366 (char **)&binary.new_file.data,
367 &binary.new_file.datalen,
368 &binary.new_file.inflatedlen,
369 old_data, old_len, new_data, new_len)) < 0)
370 return error;
371 }
372
373 error = giterr_set_after_callback_function(
374 output->binary_cb(patch->base.delta, &binary, output->payload),
375 "git_patch");
376
377 git__free((char *) binary.old_file.data);
378 git__free((char *) binary.new_file.data);
379
380 return error;
381 }
382
383 static int patch_generated_create(
384 git_patch_generated *patch,
385 git_patch_generated_output *output)
386 {
387 int error = 0;
388
389 if ((patch->flags & GIT_PATCH_GENERATED_DIFFED) != 0)
390 return 0;
391
392 /* if we are not looking at the binary or text data, don't do the diff */
393 if (!output->binary_cb && !output->hunk_cb && !output->data_cb)
394 return 0;
395
396 if ((patch->flags & GIT_PATCH_GENERATED_LOADED) == 0 &&
397 (error = patch_generated_load(patch, output)) < 0)
398 return error;
399
400 if ((patch->flags & GIT_PATCH_GENERATED_DIFFABLE) == 0)
401 return 0;
402
403 if ((patch->base.delta->flags & GIT_DIFF_FLAG_BINARY) != 0) {
404 if (output->binary_cb)
405 error = diff_binary(output, patch);
406 }
407 else {
408 if (output->diff_cb)
409 error = output->diff_cb(output, patch);
410 }
411
412 patch->flags |= GIT_PATCH_GENERATED_DIFFED;
413 return error;
414 }
415
416 static int diff_required(git_diff *diff, const char *action)
417 {
418 if (diff)
419 return 0;
420 giterr_set(GITERR_INVALID, "Must provide valid diff to %s", action);
421 return -1;
422 }
423
424 int git_diff_foreach(
425 git_diff *diff,
426 git_diff_file_cb file_cb,
427 git_diff_binary_cb binary_cb,
428 git_diff_hunk_cb hunk_cb,
429 git_diff_line_cb data_cb,
430 void *payload)
431 {
432 int error = 0;
433 git_xdiff_output xo;
434 size_t idx;
435 git_patch_generated patch;
436
437 if ((error = diff_required(diff, "git_diff_foreach")) < 0)
438 return error;
439
440 memset(&xo, 0, sizeof(xo));
441 memset(&patch, 0, sizeof(patch));
442 diff_output_init(
443 &xo.output, &diff->opts, file_cb, binary_cb, hunk_cb, data_cb, payload);
444 git_xdiff_init(&xo, &diff->opts);
445
446 git_vector_foreach(&diff->deltas, idx, patch.base.delta) {
447
448 /* check flags against patch status */
449 if (git_diff_delta__should_skip(&diff->opts, patch.base.delta))
450 continue;
451
452 if (binary_cb || hunk_cb || data_cb) {
453 if ((error = patch_generated_init(&patch, diff, idx)) != 0 ||
454 (error = patch_generated_load(&patch, &xo.output)) != 0)
455 return error;
456 }
457
458 if ((error = patch_generated_invoke_file_callback(&patch, &xo.output)) == 0) {
459 if (binary_cb || hunk_cb || data_cb)
460 error = patch_generated_create(&patch, &xo.output);
461 }
462
463 git_patch_free(&patch.base);
464
465 if (error)
466 break;
467 }
468
469 return error;
470 }
471
472 typedef struct {
473 git_patch_generated patch;
474 git_diff_delta delta;
475 char paths[GIT_FLEX_ARRAY];
476 } patch_generated_with_delta;
477
478 static int diff_single_generate(patch_generated_with_delta *pd, git_xdiff_output *xo)
479 {
480 int error = 0;
481 git_patch_generated *patch = &pd->patch;
482 bool has_old = ((patch->ofile.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
483 bool has_new = ((patch->nfile.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
484
485 pd->delta.status = has_new ?
486 (has_old ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) :
487 (has_old ? GIT_DELTA_DELETED : GIT_DELTA_UNTRACKED);
488
489 if (git_oid_equal(&patch->nfile.file->id, &patch->ofile.file->id))
490 pd->delta.status = GIT_DELTA_UNMODIFIED;
491
492 patch->base.delta = &pd->delta;
493
494 patch_generated_init_common(patch);
495
496 if (pd->delta.status == GIT_DELTA_UNMODIFIED &&
497 !(patch->ofile.opts_flags & GIT_DIFF_INCLUDE_UNMODIFIED)) {
498
499 /* Even empty patches are flagged as binary, and even though
500 * there's no difference, we flag this as "containing data"
501 * (the data is known to be empty, as opposed to wholly unknown).
502 */
503 if (patch->base.diff_opts.flags & GIT_DIFF_SHOW_BINARY)
504 patch->base.binary.contains_data = 1;
505
506 return error;
507 }
508
509 error = patch_generated_invoke_file_callback(patch, (git_patch_generated_output *)xo);
510
511 if (!error)
512 error = patch_generated_create(patch, (git_patch_generated_output *)xo);
513
514 return error;
515 }
516
517 static int patch_generated_from_sources(
518 patch_generated_with_delta *pd,
519 git_xdiff_output *xo,
520 git_diff_file_content_src *oldsrc,
521 git_diff_file_content_src *newsrc,
522 const git_diff_options *opts)
523 {
524 int error = 0;
525 git_repository *repo =
526 oldsrc->blob ? git_blob_owner(oldsrc->blob) :
527 newsrc->blob ? git_blob_owner(newsrc->blob) : NULL;
528 git_diff_file *lfile = &pd->delta.old_file, *rfile = &pd->delta.new_file;
529 git_diff_file_content *ldata = &pd->patch.ofile, *rdata = &pd->patch.nfile;
530
531 if ((error = patch_generated_normalize_options(&pd->patch.base.diff_opts, opts)) < 0)
532 return error;
533
534 if (opts && (opts->flags & GIT_DIFF_REVERSE) != 0) {
535 void *tmp = lfile; lfile = rfile; rfile = tmp;
536 tmp = ldata; ldata = rdata; rdata = tmp;
537 }
538
539 pd->patch.base.delta = &pd->delta;
540
541 if (!oldsrc->as_path) {
542 if (newsrc->as_path)
543 oldsrc->as_path = newsrc->as_path;
544 else
545 oldsrc->as_path = newsrc->as_path = "file";
546 }
547 else if (!newsrc->as_path)
548 newsrc->as_path = oldsrc->as_path;
549
550 lfile->path = oldsrc->as_path;
551 rfile->path = newsrc->as_path;
552
553 if ((error = git_diff_file_content__init_from_src(
554 ldata, repo, opts, oldsrc, lfile)) < 0 ||
555 (error = git_diff_file_content__init_from_src(
556 rdata, repo, opts, newsrc, rfile)) < 0)
557 return error;
558
559 return diff_single_generate(pd, xo);
560 }
561
562 static int patch_generated_with_delta_alloc(
563 patch_generated_with_delta **out,
564 const char **old_path,
565 const char **new_path)
566 {
567 patch_generated_with_delta *pd;
568 size_t old_len = *old_path ? strlen(*old_path) : 0;
569 size_t new_len = *new_path ? strlen(*new_path) : 0;
570 size_t alloc_len;
571
572 GITERR_CHECK_ALLOC_ADD(&alloc_len, sizeof(*pd), old_len);
573 GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, new_len);
574 GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2);
575
576 *out = pd = git__calloc(1, alloc_len);
577 GITERR_CHECK_ALLOC(pd);
578
579 pd->patch.flags = GIT_PATCH_GENERATED_ALLOCATED;
580
581 if (*old_path) {
582 memcpy(&pd->paths[0], *old_path, old_len);
583 *old_path = &pd->paths[0];
584 } else if (*new_path)
585 *old_path = &pd->paths[old_len + 1];
586
587 if (*new_path) {
588 memcpy(&pd->paths[old_len + 1], *new_path, new_len);
589 *new_path = &pd->paths[old_len + 1];
590 } else if (*old_path)
591 *new_path = &pd->paths[0];
592
593 return 0;
594 }
595
596 static int diff_from_sources(
597 git_diff_file_content_src *oldsrc,
598 git_diff_file_content_src *newsrc,
599 const git_diff_options *opts,
600 git_diff_file_cb file_cb,
601 git_diff_binary_cb binary_cb,
602 git_diff_hunk_cb hunk_cb,
603 git_diff_line_cb data_cb,
604 void *payload)
605 {
606 int error = 0;
607 patch_generated_with_delta pd;
608 git_xdiff_output xo;
609
610 memset(&xo, 0, sizeof(xo));
611 diff_output_init(
612 &xo.output, opts, file_cb, binary_cb, hunk_cb, data_cb, payload);
613 git_xdiff_init(&xo, opts);
614
615 memset(&pd, 0, sizeof(pd));
616
617 error = patch_generated_from_sources(&pd, &xo, oldsrc, newsrc, opts);
618
619 git_patch_free(&pd.patch.base);
620
621 return error;
622 }
623
624 static int patch_from_sources(
625 git_patch **out,
626 git_diff_file_content_src *oldsrc,
627 git_diff_file_content_src *newsrc,
628 const git_diff_options *opts)
629 {
630 int error = 0;
631 patch_generated_with_delta *pd;
632 git_xdiff_output xo;
633
634 assert(out);
635 *out = NULL;
636
637 if ((error = patch_generated_with_delta_alloc(
638 &pd, &oldsrc->as_path, &newsrc->as_path)) < 0)
639 return error;
640
641 memset(&xo, 0, sizeof(xo));
642 diff_output_to_patch(&xo.output, &pd->patch);
643 git_xdiff_init(&xo, opts);
644
645 if (!(error = patch_generated_from_sources(pd, &xo, oldsrc, newsrc, opts)))
646 *out = (git_patch *)pd;
647 else
648 git_patch_free((git_patch *)pd);
649
650 return error;
651 }
652
653 int git_diff_blobs(
654 const git_blob *old_blob,
655 const char *old_path,
656 const git_blob *new_blob,
657 const char *new_path,
658 const git_diff_options *opts,
659 git_diff_file_cb file_cb,
660 git_diff_binary_cb binary_cb,
661 git_diff_hunk_cb hunk_cb,
662 git_diff_line_cb data_cb,
663 void *payload)
664 {
665 git_diff_file_content_src osrc =
666 GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
667 git_diff_file_content_src nsrc =
668 GIT_DIFF_FILE_CONTENT_SRC__BLOB(new_blob, new_path);
669 return diff_from_sources(
670 &osrc, &nsrc, opts, file_cb, binary_cb, hunk_cb, data_cb, payload);
671 }
672
673 int git_patch_from_blobs(
674 git_patch **out,
675 const git_blob *old_blob,
676 const char *old_path,
677 const git_blob *new_blob,
678 const char *new_path,
679 const git_diff_options *opts)
680 {
681 git_diff_file_content_src osrc =
682 GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
683 git_diff_file_content_src nsrc =
684 GIT_DIFF_FILE_CONTENT_SRC__BLOB(new_blob, new_path);
685 return patch_from_sources(out, &osrc, &nsrc, opts);
686 }
687
688 int git_diff_blob_to_buffer(
689 const git_blob *old_blob,
690 const char *old_path,
691 const char *buf,
692 size_t buflen,
693 const char *buf_path,
694 const git_diff_options *opts,
695 git_diff_file_cb file_cb,
696 git_diff_binary_cb binary_cb,
697 git_diff_hunk_cb hunk_cb,
698 git_diff_line_cb data_cb,
699 void *payload)
700 {
701 git_diff_file_content_src osrc =
702 GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
703 git_diff_file_content_src nsrc =
704 GIT_DIFF_FILE_CONTENT_SRC__BUF(buf, buflen, buf_path);
705 return diff_from_sources(
706 &osrc, &nsrc, opts, file_cb, binary_cb, hunk_cb, data_cb, payload);
707 }
708
709 int git_patch_from_blob_and_buffer(
710 git_patch **out,
711 const git_blob *old_blob,
712 const char *old_path,
713 const char *buf,
714 size_t buflen,
715 const char *buf_path,
716 const git_diff_options *opts)
717 {
718 git_diff_file_content_src osrc =
719 GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
720 git_diff_file_content_src nsrc =
721 GIT_DIFF_FILE_CONTENT_SRC__BUF(buf, buflen, buf_path);
722 return patch_from_sources(out, &osrc, &nsrc, opts);
723 }
724
725 int git_diff_buffers(
726 const void *old_buf,
727 size_t old_len,
728 const char *old_path,
729 const void *new_buf,
730 size_t new_len,
731 const char *new_path,
732 const git_diff_options *opts,
733 git_diff_file_cb file_cb,
734 git_diff_binary_cb binary_cb,
735 git_diff_hunk_cb hunk_cb,
736 git_diff_line_cb data_cb,
737 void *payload)
738 {
739 git_diff_file_content_src osrc =
740 GIT_DIFF_FILE_CONTENT_SRC__BUF(old_buf, old_len, old_path);
741 git_diff_file_content_src nsrc =
742 GIT_DIFF_FILE_CONTENT_SRC__BUF(new_buf, new_len, new_path);
743 return diff_from_sources(
744 &osrc, &nsrc, opts, file_cb, binary_cb, hunk_cb, data_cb, payload);
745 }
746
747 int git_patch_from_buffers(
748 git_patch **out,
749 const void *old_buf,
750 size_t old_len,
751 const char *old_path,
752 const char *new_buf,
753 size_t new_len,
754 const char *new_path,
755 const git_diff_options *opts)
756 {
757 git_diff_file_content_src osrc =
758 GIT_DIFF_FILE_CONTENT_SRC__BUF(old_buf, old_len, old_path);
759 git_diff_file_content_src nsrc =
760 GIT_DIFF_FILE_CONTENT_SRC__BUF(new_buf, new_len, new_path);
761 return patch_from_sources(out, &osrc, &nsrc, opts);
762 }
763
764 int git_patch_generated_from_diff(
765 git_patch **patch_ptr, git_diff *diff, size_t idx)
766 {
767 int error = 0;
768 git_xdiff_output xo;
769 git_diff_delta *delta = NULL;
770 git_patch_generated *patch = NULL;
771
772 if (patch_ptr) *patch_ptr = NULL;
773
774 if (diff_required(diff, "git_patch_from_diff") < 0)
775 return -1;
776
777 delta = git_vector_get(&diff->deltas, idx);
778 if (!delta) {
779 giterr_set(GITERR_INVALID, "Index out of range for delta in diff");
780 return GIT_ENOTFOUND;
781 }
782
783 if (git_diff_delta__should_skip(&diff->opts, delta))
784 return 0;
785
786 /* don't load the patch data unless we need it for binary check */
787 if (!patch_ptr &&
788 ((delta->flags & DIFF_FLAGS_KNOWN_BINARY) != 0 ||
789 (diff->opts.flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0))
790 return 0;
791
792 if ((error = patch_generated_alloc_from_diff(&patch, diff, idx)) < 0)
793 return error;
794
795 memset(&xo, 0, sizeof(xo));
796 diff_output_to_patch(&xo.output, patch);
797 git_xdiff_init(&xo, &diff->opts);
798
799 error = patch_generated_invoke_file_callback(patch, &xo.output);
800
801 if (!error)
802 error = patch_generated_create(patch, &xo.output);
803
804 if (!error) {
805 /* TODO: if cumulative diff size is < 0.5 total size, flatten patch */
806 /* TODO: and unload the file content */
807 }
808
809 if (error || !patch_ptr)
810 git_patch_free(&patch->base);
811 else
812 *patch_ptr = &patch->base;
813
814 return error;
815 }
816
817 git_diff_driver *git_patch_generated_driver(git_patch_generated *patch)
818 {
819 /* ofile driver is representative for whole patch */
820 return patch->ofile.driver;
821 }
822
823 void git_patch_generated_old_data(
824 char **ptr, size_t *len, git_patch_generated *patch)
825 {
826 *ptr = patch->ofile.map.data;
827 *len = patch->ofile.map.len;
828 }
829
830 void git_patch_generated_new_data(
831 char **ptr, size_t *len, git_patch_generated *patch)
832 {
833 *ptr = patch->nfile.map.data;
834 *len = patch->nfile.map.len;
835 }
836
837 static int patch_generated_file_cb(
838 const git_diff_delta *delta,
839 float progress,
840 void *payload)
841 {
842 GIT_UNUSED(delta); GIT_UNUSED(progress); GIT_UNUSED(payload);
843 return 0;
844 }
845
846 static int patch_generated_binary_cb(
847 const git_diff_delta *delta,
848 const git_diff_binary *binary,
849 void *payload)
850 {
851 git_patch *patch = payload;
852
853 GIT_UNUSED(delta);
854
855 memcpy(&patch->binary, binary, sizeof(git_diff_binary));
856
857 if (binary->old_file.data) {
858 patch->binary.old_file.data = git__malloc(binary->old_file.datalen);
859 GITERR_CHECK_ALLOC(patch->binary.old_file.data);
860
861 memcpy((char *)patch->binary.old_file.data,
862 binary->old_file.data, binary->old_file.datalen);
863 }
864
865 if (binary->new_file.data) {
866 patch->binary.new_file.data = git__malloc(binary->new_file.datalen);
867 GITERR_CHECK_ALLOC(patch->binary.new_file.data);
868
869 memcpy((char *)patch->binary.new_file.data,
870 binary->new_file.data, binary->new_file.datalen);
871 }
872
873 return 0;
874 }
875
876 static int git_patch_hunk_cb(
877 const git_diff_delta *delta,
878 const git_diff_hunk *hunk_,
879 void *payload)
880 {
881 git_patch_generated *patch = payload;
882 git_patch_hunk *hunk;
883
884 GIT_UNUSED(delta);
885
886 hunk = git_array_alloc(patch->base.hunks);
887 GITERR_CHECK_ALLOC(hunk);
888
889 memcpy(&hunk->hunk, hunk_, sizeof(hunk->hunk));
890
891 patch->base.header_size += hunk_->header_len;
892
893 hunk->line_start = git_array_size(patch->base.lines);
894 hunk->line_count = 0;
895
896 return 0;
897 }
898
899 static int patch_generated_line_cb(
900 const git_diff_delta *delta,
901 const git_diff_hunk *hunk_,
902 const git_diff_line *line_,
903 void *payload)
904 {
905 git_patch_generated *patch = payload;
906 git_patch_hunk *hunk;
907 git_diff_line *line;
908
909 GIT_UNUSED(delta);
910 GIT_UNUSED(hunk_);
911
912 hunk = git_array_last(patch->base.hunks);
913 assert(hunk); /* programmer error if no hunk is available */
914
915 line = git_array_alloc(patch->base.lines);
916 GITERR_CHECK_ALLOC(line);
917
918 memcpy(line, line_, sizeof(*line));
919
920 /* do some bookkeeping so we can provide old/new line numbers */
921
922 patch->base.content_size += line->content_len;
923
924 if (line->origin == GIT_DIFF_LINE_ADDITION ||
925 line->origin == GIT_DIFF_LINE_DELETION)
926 patch->base.content_size += 1;
927 else if (line->origin == GIT_DIFF_LINE_CONTEXT) {
928 patch->base.content_size += 1;
929 patch->base.context_size += line->content_len + 1;
930 } else if (line->origin == GIT_DIFF_LINE_CONTEXT_EOFNL)
931 patch->base.context_size += line->content_len;
932
933 hunk->line_count++;
934
935 return 0;
936 }
937
938 static void diff_output_init(
939 git_patch_generated_output *out,
940 const git_diff_options *opts,
941 git_diff_file_cb file_cb,
942 git_diff_binary_cb binary_cb,
943 git_diff_hunk_cb hunk_cb,
944 git_diff_line_cb data_cb,
945 void *payload)
946 {
947 GIT_UNUSED(opts);
948
949 memset(out, 0, sizeof(*out));
950
951 out->file_cb = file_cb;
952 out->binary_cb = binary_cb;
953 out->hunk_cb = hunk_cb;
954 out->data_cb = data_cb;
955 out->payload = payload;
956 }
957
958 static void diff_output_to_patch(
959 git_patch_generated_output *out, git_patch_generated *patch)
960 {
961 diff_output_init(
962 out,
963 NULL,
964 patch_generated_file_cb,
965 patch_generated_binary_cb,
966 git_patch_hunk_cb,
967 patch_generated_line_cb,
968 patch);
969 }