]> git.proxmox.com Git - libgit2.git/blob - src/patch_generate.c
Merge pull request #4169 from csware/absolute-symlink
[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 if ((error = git_diff_file_content__load(
210 &patch->ofile, &patch->base.diff_opts)) < 0 ||
211 should_skip_binary(patch, patch->ofile.file))
212 goto cleanup;
213 if ((error = git_diff_file_content__load(
214 &patch->nfile, &patch->base.diff_opts)) < 0 ||
215 should_skip_binary(patch, patch->nfile.file))
216 goto cleanup;
217
218 /* if previously missing an oid, and now that we have it the two sides
219 * are the same (and not submodules), update MODIFIED -> UNMODIFIED
220 */
221 if (incomplete_data &&
222 patch->ofile.file->mode == patch->nfile.file->mode &&
223 patch->ofile.file->mode != GIT_FILEMODE_COMMIT &&
224 git_oid_equal(&patch->ofile.file->id, &patch->nfile.file->id) &&
225 patch->base.delta->status == GIT_DELTA_MODIFIED) /* not RENAMED/COPIED! */
226 patch->base.delta->status = GIT_DELTA_UNMODIFIED;
227
228 cleanup:
229 patch_generated_update_binary(patch);
230
231 if (!error) {
232 if (patch_generated_diffable(patch))
233 patch->flags |= GIT_PATCH_GENERATED_DIFFABLE;
234
235 patch->flags |= GIT_PATCH_GENERATED_LOADED;
236 }
237
238 return error;
239 }
240
241 static int patch_generated_invoke_file_callback(
242 git_patch_generated *patch, git_patch_generated_output *output)
243 {
244 float progress = patch->diff ?
245 ((float)patch->delta_index / patch->diff->deltas.length) : 1.0f;
246
247 if (!output->file_cb)
248 return 0;
249
250 return giterr_set_after_callback_function(
251 output->file_cb(patch->base.delta, progress, output->payload),
252 "git_patch");
253 }
254
255 static int create_binary(
256 git_diff_binary_t *out_type,
257 char **out_data,
258 size_t *out_datalen,
259 size_t *out_inflatedlen,
260 const char *a_data,
261 size_t a_datalen,
262 const char *b_data,
263 size_t b_datalen)
264 {
265 git_buf deflate = GIT_BUF_INIT, delta = GIT_BUF_INIT;
266 size_t delta_data_len = 0;
267 int error;
268
269 /* The git_delta function accepts unsigned long only */
270 if (!git__is_ulong(a_datalen) || !git__is_ulong(b_datalen))
271 return GIT_EBUFS;
272
273 if ((error = git_zstream_deflatebuf(&deflate, b_data, b_datalen)) < 0)
274 goto done;
275
276 /* The git_delta function accepts unsigned long only */
277 if (!git__is_ulong(deflate.size)) {
278 error = GIT_EBUFS;
279 goto done;
280 }
281
282 if (a_datalen && b_datalen) {
283 void *delta_data;
284
285 error = git_delta(&delta_data, &delta_data_len,
286 a_data, a_datalen,
287 b_data, b_datalen,
288 deflate.size);
289
290 if (error == 0) {
291 error = git_zstream_deflatebuf(
292 &delta, delta_data, delta_data_len);
293
294 git__free(delta_data);
295 } else if (error == GIT_EBUFS) {
296 error = 0;
297 }
298
299 if (error < 0)
300 goto done;
301 }
302
303 if (delta.size && delta.size < deflate.size) {
304 *out_type = GIT_DIFF_BINARY_DELTA;
305 *out_datalen = delta.size;
306 *out_data = git_buf_detach(&delta);
307 *out_inflatedlen = delta_data_len;
308 } else {
309 *out_type = GIT_DIFF_BINARY_LITERAL;
310 *out_datalen = deflate.size;
311 *out_data = git_buf_detach(&deflate);
312 *out_inflatedlen = b_datalen;
313 }
314
315 done:
316 git_buf_free(&deflate);
317 git_buf_free(&delta);
318
319 return error;
320 }
321
322 static int diff_binary(git_patch_generated_output *output, git_patch_generated *patch)
323 {
324 git_diff_binary binary = {0};
325 const char *old_data = patch->ofile.map.data;
326 const char *new_data = patch->nfile.map.data;
327 size_t old_len = patch->ofile.map.len,
328 new_len = patch->nfile.map.len;
329 int error;
330
331 /* Only load contents if the user actually wants to diff
332 * binary files. */
333 if (patch->base.diff_opts.flags & GIT_DIFF_SHOW_BINARY) {
334 binary.contains_data = 1;
335
336 /* Create the old->new delta (as the "new" side of the patch),
337 * and the new->old delta (as the "old" side)
338 */
339 if ((error = create_binary(&binary.old_file.type,
340 (char **)&binary.old_file.data,
341 &binary.old_file.datalen,
342 &binary.old_file.inflatedlen,
343 new_data, new_len, old_data, old_len)) < 0 ||
344 (error = create_binary(&binary.new_file.type,
345 (char **)&binary.new_file.data,
346 &binary.new_file.datalen,
347 &binary.new_file.inflatedlen,
348 old_data, old_len, new_data, new_len)) < 0)
349 return error;
350 }
351
352 error = giterr_set_after_callback_function(
353 output->binary_cb(patch->base.delta, &binary, output->payload),
354 "git_patch");
355
356 git__free((char *) binary.old_file.data);
357 git__free((char *) binary.new_file.data);
358
359 return error;
360 }
361
362 static int patch_generated_create(
363 git_patch_generated *patch,
364 git_patch_generated_output *output)
365 {
366 int error = 0;
367
368 if ((patch->flags & GIT_PATCH_GENERATED_DIFFED) != 0)
369 return 0;
370
371 /* if we are not looking at the binary or text data, don't do the diff */
372 if (!output->binary_cb && !output->hunk_cb && !output->data_cb)
373 return 0;
374
375 if ((patch->flags & GIT_PATCH_GENERATED_LOADED) == 0 &&
376 (error = patch_generated_load(patch, output)) < 0)
377 return error;
378
379 if ((patch->flags & GIT_PATCH_GENERATED_DIFFABLE) == 0)
380 return 0;
381
382 if ((patch->base.delta->flags & GIT_DIFF_FLAG_BINARY) != 0) {
383 if (output->binary_cb)
384 error = diff_binary(output, patch);
385 }
386 else {
387 if (output->diff_cb)
388 error = output->diff_cb(output, patch);
389 }
390
391 patch->flags |= GIT_PATCH_GENERATED_DIFFED;
392 return error;
393 }
394
395 static int diff_required(git_diff *diff, const char *action)
396 {
397 if (diff)
398 return 0;
399 giterr_set(GITERR_INVALID, "must provide valid diff to %s", action);
400 return -1;
401 }
402
403 typedef struct {
404 git_patch_generated patch;
405 git_diff_delta delta;
406 char paths[GIT_FLEX_ARRAY];
407 } patch_generated_with_delta;
408
409 static int diff_single_generate(patch_generated_with_delta *pd, git_xdiff_output *xo)
410 {
411 int error = 0;
412 git_patch_generated *patch = &pd->patch;
413 bool has_old = ((patch->ofile.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
414 bool has_new = ((patch->nfile.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
415
416 pd->delta.status = has_new ?
417 (has_old ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) :
418 (has_old ? GIT_DELTA_DELETED : GIT_DELTA_UNTRACKED);
419
420 if (git_oid_equal(&patch->nfile.file->id, &patch->ofile.file->id))
421 pd->delta.status = GIT_DELTA_UNMODIFIED;
422
423 patch->base.delta = &pd->delta;
424
425 patch_generated_init_common(patch);
426
427 if (pd->delta.status == GIT_DELTA_UNMODIFIED &&
428 !(patch->ofile.opts_flags & GIT_DIFF_INCLUDE_UNMODIFIED)) {
429
430 /* Even empty patches are flagged as binary, and even though
431 * there's no difference, we flag this as "containing data"
432 * (the data is known to be empty, as opposed to wholly unknown).
433 */
434 if (patch->base.diff_opts.flags & GIT_DIFF_SHOW_BINARY)
435 patch->base.binary.contains_data = 1;
436
437 return error;
438 }
439
440 error = patch_generated_invoke_file_callback(patch, (git_patch_generated_output *)xo);
441
442 if (!error)
443 error = patch_generated_create(patch, (git_patch_generated_output *)xo);
444
445 return error;
446 }
447
448 static int patch_generated_from_sources(
449 patch_generated_with_delta *pd,
450 git_xdiff_output *xo,
451 git_diff_file_content_src *oldsrc,
452 git_diff_file_content_src *newsrc,
453 const git_diff_options *opts)
454 {
455 int error = 0;
456 git_repository *repo =
457 oldsrc->blob ? git_blob_owner(oldsrc->blob) :
458 newsrc->blob ? git_blob_owner(newsrc->blob) : NULL;
459 git_diff_file *lfile = &pd->delta.old_file, *rfile = &pd->delta.new_file;
460 git_diff_file_content *ldata = &pd->patch.ofile, *rdata = &pd->patch.nfile;
461
462 if ((error = patch_generated_normalize_options(&pd->patch.base.diff_opts, opts)) < 0)
463 return error;
464
465 if (opts && (opts->flags & GIT_DIFF_REVERSE) != 0) {
466 void *tmp = lfile; lfile = rfile; rfile = tmp;
467 tmp = ldata; ldata = rdata; rdata = tmp;
468 }
469
470 pd->patch.base.delta = &pd->delta;
471
472 if (!oldsrc->as_path) {
473 if (newsrc->as_path)
474 oldsrc->as_path = newsrc->as_path;
475 else
476 oldsrc->as_path = newsrc->as_path = "file";
477 }
478 else if (!newsrc->as_path)
479 newsrc->as_path = oldsrc->as_path;
480
481 lfile->path = oldsrc->as_path;
482 rfile->path = newsrc->as_path;
483
484 if ((error = git_diff_file_content__init_from_src(
485 ldata, repo, opts, oldsrc, lfile)) < 0 ||
486 (error = git_diff_file_content__init_from_src(
487 rdata, repo, opts, newsrc, rfile)) < 0)
488 return error;
489
490 return diff_single_generate(pd, xo);
491 }
492
493 static int patch_generated_with_delta_alloc(
494 patch_generated_with_delta **out,
495 const char **old_path,
496 const char **new_path)
497 {
498 patch_generated_with_delta *pd;
499 size_t old_len = *old_path ? strlen(*old_path) : 0;
500 size_t new_len = *new_path ? strlen(*new_path) : 0;
501 size_t alloc_len;
502
503 GITERR_CHECK_ALLOC_ADD(&alloc_len, sizeof(*pd), old_len);
504 GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, new_len);
505 GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2);
506
507 *out = pd = git__calloc(1, alloc_len);
508 GITERR_CHECK_ALLOC(pd);
509
510 pd->patch.flags = GIT_PATCH_GENERATED_ALLOCATED;
511
512 if (*old_path) {
513 memcpy(&pd->paths[0], *old_path, old_len);
514 *old_path = &pd->paths[0];
515 } else if (*new_path)
516 *old_path = &pd->paths[old_len + 1];
517
518 if (*new_path) {
519 memcpy(&pd->paths[old_len + 1], *new_path, new_len);
520 *new_path = &pd->paths[old_len + 1];
521 } else if (*old_path)
522 *new_path = &pd->paths[0];
523
524 return 0;
525 }
526
527 static int diff_from_sources(
528 git_diff_file_content_src *oldsrc,
529 git_diff_file_content_src *newsrc,
530 const git_diff_options *opts,
531 git_diff_file_cb file_cb,
532 git_diff_binary_cb binary_cb,
533 git_diff_hunk_cb hunk_cb,
534 git_diff_line_cb data_cb,
535 void *payload)
536 {
537 int error = 0;
538 patch_generated_with_delta pd;
539 git_xdiff_output xo;
540
541 memset(&xo, 0, sizeof(xo));
542 diff_output_init(
543 &xo.output, opts, file_cb, binary_cb, hunk_cb, data_cb, payload);
544 git_xdiff_init(&xo, opts);
545
546 memset(&pd, 0, sizeof(pd));
547
548 error = patch_generated_from_sources(&pd, &xo, oldsrc, newsrc, opts);
549
550 git_patch_free(&pd.patch.base);
551
552 return error;
553 }
554
555 static int patch_from_sources(
556 git_patch **out,
557 git_diff_file_content_src *oldsrc,
558 git_diff_file_content_src *newsrc,
559 const git_diff_options *opts)
560 {
561 int error = 0;
562 patch_generated_with_delta *pd;
563 git_xdiff_output xo;
564
565 assert(out);
566 *out = NULL;
567
568 if ((error = patch_generated_with_delta_alloc(
569 &pd, &oldsrc->as_path, &newsrc->as_path)) < 0)
570 return error;
571
572 memset(&xo, 0, sizeof(xo));
573 diff_output_to_patch(&xo.output, &pd->patch);
574 git_xdiff_init(&xo, opts);
575
576 if (!(error = patch_generated_from_sources(pd, &xo, oldsrc, newsrc, opts)))
577 *out = (git_patch *)pd;
578 else
579 git_patch_free((git_patch *)pd);
580
581 return error;
582 }
583
584 int git_diff_blobs(
585 const git_blob *old_blob,
586 const char *old_path,
587 const git_blob *new_blob,
588 const char *new_path,
589 const git_diff_options *opts,
590 git_diff_file_cb file_cb,
591 git_diff_binary_cb binary_cb,
592 git_diff_hunk_cb hunk_cb,
593 git_diff_line_cb data_cb,
594 void *payload)
595 {
596 git_diff_file_content_src osrc =
597 GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
598 git_diff_file_content_src nsrc =
599 GIT_DIFF_FILE_CONTENT_SRC__BLOB(new_blob, new_path);
600 return diff_from_sources(
601 &osrc, &nsrc, opts, file_cb, binary_cb, hunk_cb, data_cb, payload);
602 }
603
604 int git_patch_from_blobs(
605 git_patch **out,
606 const git_blob *old_blob,
607 const char *old_path,
608 const git_blob *new_blob,
609 const char *new_path,
610 const git_diff_options *opts)
611 {
612 git_diff_file_content_src osrc =
613 GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
614 git_diff_file_content_src nsrc =
615 GIT_DIFF_FILE_CONTENT_SRC__BLOB(new_blob, new_path);
616 return patch_from_sources(out, &osrc, &nsrc, opts);
617 }
618
619 int git_diff_blob_to_buffer(
620 const git_blob *old_blob,
621 const char *old_path,
622 const char *buf,
623 size_t buflen,
624 const char *buf_path,
625 const git_diff_options *opts,
626 git_diff_file_cb file_cb,
627 git_diff_binary_cb binary_cb,
628 git_diff_hunk_cb hunk_cb,
629 git_diff_line_cb data_cb,
630 void *payload)
631 {
632 git_diff_file_content_src osrc =
633 GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
634 git_diff_file_content_src nsrc =
635 GIT_DIFF_FILE_CONTENT_SRC__BUF(buf, buflen, buf_path);
636 return diff_from_sources(
637 &osrc, &nsrc, opts, file_cb, binary_cb, hunk_cb, data_cb, payload);
638 }
639
640 int git_patch_from_blob_and_buffer(
641 git_patch **out,
642 const git_blob *old_blob,
643 const char *old_path,
644 const char *buf,
645 size_t buflen,
646 const char *buf_path,
647 const git_diff_options *opts)
648 {
649 git_diff_file_content_src osrc =
650 GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
651 git_diff_file_content_src nsrc =
652 GIT_DIFF_FILE_CONTENT_SRC__BUF(buf, buflen, buf_path);
653 return patch_from_sources(out, &osrc, &nsrc, opts);
654 }
655
656 int git_diff_buffers(
657 const void *old_buf,
658 size_t old_len,
659 const char *old_path,
660 const void *new_buf,
661 size_t new_len,
662 const char *new_path,
663 const git_diff_options *opts,
664 git_diff_file_cb file_cb,
665 git_diff_binary_cb binary_cb,
666 git_diff_hunk_cb hunk_cb,
667 git_diff_line_cb data_cb,
668 void *payload)
669 {
670 git_diff_file_content_src osrc =
671 GIT_DIFF_FILE_CONTENT_SRC__BUF(old_buf, old_len, old_path);
672 git_diff_file_content_src nsrc =
673 GIT_DIFF_FILE_CONTENT_SRC__BUF(new_buf, new_len, new_path);
674 return diff_from_sources(
675 &osrc, &nsrc, opts, file_cb, binary_cb, hunk_cb, data_cb, payload);
676 }
677
678 int git_patch_from_buffers(
679 git_patch **out,
680 const void *old_buf,
681 size_t old_len,
682 const char *old_path,
683 const char *new_buf,
684 size_t new_len,
685 const char *new_path,
686 const git_diff_options *opts)
687 {
688 git_diff_file_content_src osrc =
689 GIT_DIFF_FILE_CONTENT_SRC__BUF(old_buf, old_len, old_path);
690 git_diff_file_content_src nsrc =
691 GIT_DIFF_FILE_CONTENT_SRC__BUF(new_buf, new_len, new_path);
692 return patch_from_sources(out, &osrc, &nsrc, opts);
693 }
694
695 int git_patch_generated_from_diff(
696 git_patch **patch_ptr, git_diff *diff, size_t idx)
697 {
698 int error = 0;
699 git_xdiff_output xo;
700 git_diff_delta *delta = NULL;
701 git_patch_generated *patch = NULL;
702
703 if (patch_ptr) *patch_ptr = NULL;
704
705 if (diff_required(diff, "git_patch_from_diff") < 0)
706 return -1;
707
708 delta = git_vector_get(&diff->deltas, idx);
709 if (!delta) {
710 giterr_set(GITERR_INVALID, "index out of range for delta in diff");
711 return GIT_ENOTFOUND;
712 }
713
714 if (git_diff_delta__should_skip(&diff->opts, delta))
715 return 0;
716
717 /* don't load the patch data unless we need it for binary check */
718 if (!patch_ptr &&
719 ((delta->flags & DIFF_FLAGS_KNOWN_BINARY) != 0 ||
720 (diff->opts.flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0))
721 return 0;
722
723 if ((error = patch_generated_alloc_from_diff(&patch, diff, idx)) < 0)
724 return error;
725
726 memset(&xo, 0, sizeof(xo));
727 diff_output_to_patch(&xo.output, patch);
728 git_xdiff_init(&xo, &diff->opts);
729
730 error = patch_generated_invoke_file_callback(patch, &xo.output);
731
732 if (!error)
733 error = patch_generated_create(patch, &xo.output);
734
735 if (!error) {
736 /* TODO: if cumulative diff size is < 0.5 total size, flatten patch */
737 /* TODO: and unload the file content */
738 }
739
740 if (error || !patch_ptr)
741 git_patch_free(&patch->base);
742 else
743 *patch_ptr = &patch->base;
744
745 return error;
746 }
747
748 git_diff_driver *git_patch_generated_driver(git_patch_generated *patch)
749 {
750 /* ofile driver is representative for whole patch */
751 return patch->ofile.driver;
752 }
753
754 void git_patch_generated_old_data(
755 char **ptr, size_t *len, git_patch_generated *patch)
756 {
757 *ptr = patch->ofile.map.data;
758 *len = patch->ofile.map.len;
759 }
760
761 void git_patch_generated_new_data(
762 char **ptr, size_t *len, git_patch_generated *patch)
763 {
764 *ptr = patch->nfile.map.data;
765 *len = patch->nfile.map.len;
766 }
767
768 static int patch_generated_file_cb(
769 const git_diff_delta *delta,
770 float progress,
771 void *payload)
772 {
773 GIT_UNUSED(delta); GIT_UNUSED(progress); GIT_UNUSED(payload);
774 return 0;
775 }
776
777 static int patch_generated_binary_cb(
778 const git_diff_delta *delta,
779 const git_diff_binary *binary,
780 void *payload)
781 {
782 git_patch *patch = payload;
783
784 GIT_UNUSED(delta);
785
786 memcpy(&patch->binary, binary, sizeof(git_diff_binary));
787
788 if (binary->old_file.data) {
789 patch->binary.old_file.data = git__malloc(binary->old_file.datalen);
790 GITERR_CHECK_ALLOC(patch->binary.old_file.data);
791
792 memcpy((char *)patch->binary.old_file.data,
793 binary->old_file.data, binary->old_file.datalen);
794 }
795
796 if (binary->new_file.data) {
797 patch->binary.new_file.data = git__malloc(binary->new_file.datalen);
798 GITERR_CHECK_ALLOC(patch->binary.new_file.data);
799
800 memcpy((char *)patch->binary.new_file.data,
801 binary->new_file.data, binary->new_file.datalen);
802 }
803
804 return 0;
805 }
806
807 static int git_patch_hunk_cb(
808 const git_diff_delta *delta,
809 const git_diff_hunk *hunk_,
810 void *payload)
811 {
812 git_patch_generated *patch = payload;
813 git_patch_hunk *hunk;
814
815 GIT_UNUSED(delta);
816
817 hunk = git_array_alloc(patch->base.hunks);
818 GITERR_CHECK_ALLOC(hunk);
819
820 memcpy(&hunk->hunk, hunk_, sizeof(hunk->hunk));
821
822 patch->base.header_size += hunk_->header_len;
823
824 hunk->line_start = git_array_size(patch->base.lines);
825 hunk->line_count = 0;
826
827 return 0;
828 }
829
830 static int patch_generated_line_cb(
831 const git_diff_delta *delta,
832 const git_diff_hunk *hunk_,
833 const git_diff_line *line_,
834 void *payload)
835 {
836 git_patch_generated *patch = payload;
837 git_patch_hunk *hunk;
838 git_diff_line *line;
839
840 GIT_UNUSED(delta);
841 GIT_UNUSED(hunk_);
842
843 hunk = git_array_last(patch->base.hunks);
844 assert(hunk); /* programmer error if no hunk is available */
845
846 line = git_array_alloc(patch->base.lines);
847 GITERR_CHECK_ALLOC(line);
848
849 memcpy(line, line_, sizeof(*line));
850
851 /* do some bookkeeping so we can provide old/new line numbers */
852
853 patch->base.content_size += line->content_len;
854
855 if (line->origin == GIT_DIFF_LINE_ADDITION ||
856 line->origin == GIT_DIFF_LINE_DELETION)
857 patch->base.content_size += 1;
858 else if (line->origin == GIT_DIFF_LINE_CONTEXT) {
859 patch->base.content_size += 1;
860 patch->base.context_size += line->content_len + 1;
861 } else if (line->origin == GIT_DIFF_LINE_CONTEXT_EOFNL)
862 patch->base.context_size += line->content_len;
863
864 hunk->line_count++;
865
866 return 0;
867 }
868
869 static void diff_output_init(
870 git_patch_generated_output *out,
871 const git_diff_options *opts,
872 git_diff_file_cb file_cb,
873 git_diff_binary_cb binary_cb,
874 git_diff_hunk_cb hunk_cb,
875 git_diff_line_cb data_cb,
876 void *payload)
877 {
878 GIT_UNUSED(opts);
879
880 memset(out, 0, sizeof(*out));
881
882 out->file_cb = file_cb;
883 out->binary_cb = binary_cb;
884 out->hunk_cb = hunk_cb;
885 out->data_cb = data_cb;
886 out->payload = payload;
887 }
888
889 static void diff_output_to_patch(
890 git_patch_generated_output *out, git_patch_generated *patch)
891 {
892 diff_output_init(
893 out,
894 NULL,
895 patch_generated_file_cb,
896 patch_generated_binary_cb,
897 git_patch_hunk_cb,
898 patch_generated_line_cb,
899 patch);
900 }