2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
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"
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*);
23 static void diff_output_to_patch(
24 git_patch_generated_output
*, git_patch_generated
*);
26 static void patch_generated_free(git_patch
*p
)
28 git_patch_generated
*patch
= (git_patch_generated
*)p
;
30 git_array_clear(patch
->base
.lines
);
31 git_array_clear(patch
->base
.hunks
);
33 git__free((char *)patch
->base
.binary
.old_file
.data
);
34 git__free((char *)patch
->base
.binary
.new_file
.data
);
36 git_diff_file_content__clear(&patch
->ofile
);
37 git_diff_file_content__clear(&patch
->nfile
);
39 git_diff_free(patch
->diff
); /* decrements refcount */
42 git_pool_clear(&patch
->flattened
);
44 git__free((char *)patch
->base
.diff_opts
.old_prefix
);
45 git__free((char *)patch
->base
.diff_opts
.new_prefix
);
47 if (patch
->flags
& GIT_PATCH_GENERATED_ALLOCATED
)
51 static void patch_generated_update_binary(git_patch_generated
*patch
)
53 if ((patch
->base
.delta
->flags
& DIFF_FLAGS_KNOWN_BINARY
) != 0)
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
;
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
;
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
;
69 static void patch_generated_init_common(git_patch_generated
*patch
)
71 patch
->base
.free_fn
= patch_generated_free
;
73 patch_generated_update_binary(patch
);
75 patch
->flags
|= GIT_PATCH_GENERATED_INITIALIZED
;
78 git_diff_addref(patch
->diff
);
81 static int patch_generated_normalize_options(
82 git_diff_options
*out
,
83 const git_diff_options
*opts
)
86 GITERR_CHECK_VERSION(opts
, GIT_DIFF_OPTIONS_VERSION
, "git_diff_options");
87 memcpy(out
, opts
, sizeof(git_diff_options
));
89 git_diff_options default_opts
= GIT_DIFF_OPTIONS_INIT
;
90 memcpy(out
, &default_opts
, sizeof(git_diff_options
));
93 out
->old_prefix
= opts
&& opts
->old_prefix
?
94 git__strdup(opts
->old_prefix
) :
95 git__strdup(DIFF_OLD_PREFIX_DEFAULT
);
97 out
->new_prefix
= opts
&& opts
->new_prefix
?
98 git__strdup(opts
->new_prefix
) :
99 git__strdup(DIFF_NEW_PREFIX_DEFAULT
);
101 GITERR_CHECK_ALLOC(out
->old_prefix
);
102 GITERR_CHECK_ALLOC(out
->new_prefix
);
107 static int patch_generated_init(
108 git_patch_generated
*patch
, git_diff
*diff
, size_t delta_index
)
112 memset(patch
, 0, sizeof(*patch
));
115 patch
->base
.repo
= diff
->repo
;
116 patch
->base
.delta
= git_vector_get(&diff
->deltas
, delta_index
);
117 patch
->delta_index
= delta_index
;
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)
127 patch_generated_init_common(patch
);
132 static int patch_generated_alloc_from_diff(
133 git_patch_generated
**out
, git_diff
*diff
, size_t delta_index
)
136 git_patch_generated
*patch
= git__calloc(1, sizeof(git_patch_generated
));
137 GITERR_CHECK_ALLOC(patch
);
139 if (!(error
= patch_generated_init(patch
, diff
, delta_index
))) {
140 patch
->flags
|= GIT_PATCH_GENERATED_ALLOCATED
;
141 GIT_REFCOUNT_INC(patch
);
151 GIT_INLINE(bool) should_skip_binary(git_patch_generated
*patch
, git_diff_file
*file
)
153 if ((patch
->base
.diff_opts
.flags
& GIT_DIFF_SHOW_BINARY
) != 0)
156 return (file
->flags
& GIT_DIFF_FLAG_BINARY
) != 0;
159 static bool patch_generated_diffable(git_patch_generated
*patch
)
163 if (patch
->base
.delta
->status
== GIT_DELTA_UNMODIFIED
)
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
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
;
175 olen
= patch
->ofile
.map
.len
;
176 nlen
= patch
->nfile
.map
.len
;
179 /* if both sides are empty, files are identical */
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
));
188 static int patch_generated_load(git_patch_generated
*patch
, git_patch_generated_output
*output
)
191 bool incomplete_data
;
193 if ((patch
->flags
& GIT_PATCH_GENERATED_LOADED
) != 0)
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
199 if ((patch
->ofile
.opts_flags
& GIT_DIFF_SKIP_BINARY_CHECK
) != 0 &&
200 output
&& !output
->binary_cb
&& !output
->hunk_cb
&& !output
->data_cb
)
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));
209 if ((error
= git_diff_file_content__load(
210 &patch
->ofile
, &patch
->base
.diff_opts
)) < 0 ||
211 should_skip_binary(patch
, patch
->ofile
.file
))
213 if ((error
= git_diff_file_content__load(
214 &patch
->nfile
, &patch
->base
.diff_opts
)) < 0 ||
215 should_skip_binary(patch
, patch
->nfile
.file
))
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
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
;
229 patch_generated_update_binary(patch
);
232 if (patch_generated_diffable(patch
))
233 patch
->flags
|= GIT_PATCH_GENERATED_DIFFABLE
;
235 patch
->flags
|= GIT_PATCH_GENERATED_LOADED
;
241 static int patch_generated_invoke_file_callback(
242 git_patch_generated
*patch
, git_patch_generated_output
*output
)
244 float progress
= patch
->diff
?
245 ((float)patch
->delta_index
/ patch
->diff
->deltas
.length
) : 1.0f
;
247 if (!output
->file_cb
)
250 return giterr_set_after_callback_function(
251 output
->file_cb(patch
->base
.delta
, progress
, output
->payload
),
255 static int create_binary(
256 git_diff_binary_t
*out_type
,
259 size_t *out_inflatedlen
,
265 git_buf deflate
= GIT_BUF_INIT
, delta
= GIT_BUF_INIT
;
266 size_t delta_data_len
= 0;
269 /* The git_delta function accepts unsigned long only */
270 if (!git__is_ulong(a_datalen
) || !git__is_ulong(b_datalen
))
273 if ((error
= git_zstream_deflatebuf(&deflate
, b_data
, b_datalen
)) < 0)
276 /* The git_delta function accepts unsigned long only */
277 if (!git__is_ulong(deflate
.size
)) {
282 if (a_datalen
&& b_datalen
) {
285 error
= git_delta(&delta_data
, &delta_data_len
,
291 error
= git_zstream_deflatebuf(
292 &delta
, delta_data
, delta_data_len
);
294 git__free(delta_data
);
295 } else if (error
== GIT_EBUFS
) {
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
;
309 *out_type
= GIT_DIFF_BINARY_LITERAL
;
310 *out_datalen
= deflate
.size
;
311 *out_data
= git_buf_detach(&deflate
);
312 *out_inflatedlen
= b_datalen
;
316 git_buf_free(&deflate
);
317 git_buf_free(&delta
);
322 static int diff_binary(git_patch_generated_output
*output
, git_patch_generated
*patch
)
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
;
331 /* Only load contents if the user actually wants to diff
333 if (patch
->base
.diff_opts
.flags
& GIT_DIFF_SHOW_BINARY
) {
334 binary
.contains_data
= 1;
336 /* Create the old->new delta (as the "new" side of the patch),
337 * and the new->old delta (as the "old" side)
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)
352 error
= giterr_set_after_callback_function(
353 output
->binary_cb(patch
->base
.delta
, &binary
, output
->payload
),
356 git__free((char *) binary
.old_file
.data
);
357 git__free((char *) binary
.new_file
.data
);
362 static int patch_generated_create(
363 git_patch_generated
*patch
,
364 git_patch_generated_output
*output
)
368 if ((patch
->flags
& GIT_PATCH_GENERATED_DIFFED
) != 0)
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
)
375 if ((patch
->flags
& GIT_PATCH_GENERATED_LOADED
) == 0 &&
376 (error
= patch_generated_load(patch
, output
)) < 0)
379 if ((patch
->flags
& GIT_PATCH_GENERATED_DIFFABLE
) == 0)
382 if ((patch
->base
.delta
->flags
& GIT_DIFF_FLAG_BINARY
) != 0) {
383 if (output
->binary_cb
)
384 error
= diff_binary(output
, patch
);
388 error
= output
->diff_cb(output
, patch
);
391 patch
->flags
|= GIT_PATCH_GENERATED_DIFFED
;
395 static int diff_required(git_diff
*diff
, const char *action
)
399 giterr_set(GITERR_INVALID
, "must provide valid diff to %s", action
);
404 git_patch_generated patch
;
405 git_diff_delta delta
;
406 char paths
[GIT_FLEX_ARRAY
];
407 } patch_generated_with_delta
;
409 static int diff_single_generate(patch_generated_with_delta
*pd
, git_xdiff_output
*xo
)
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);
416 pd
->delta
.status
= has_new
?
417 (has_old
? GIT_DELTA_MODIFIED
: GIT_DELTA_ADDED
) :
418 (has_old
? GIT_DELTA_DELETED
: GIT_DELTA_UNTRACKED
);
420 if (git_oid_equal(&patch
->nfile
.file
->id
, &patch
->ofile
.file
->id
))
421 pd
->delta
.status
= GIT_DELTA_UNMODIFIED
;
423 patch
->base
.delta
= &pd
->delta
;
425 patch_generated_init_common(patch
);
427 if (pd
->delta
.status
== GIT_DELTA_UNMODIFIED
&&
428 !(patch
->ofile
.opts_flags
& GIT_DIFF_INCLUDE_UNMODIFIED
)) {
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).
434 if (patch
->base
.diff_opts
.flags
& GIT_DIFF_SHOW_BINARY
)
435 patch
->base
.binary
.contains_data
= 1;
440 error
= patch_generated_invoke_file_callback(patch
, (git_patch_generated_output
*)xo
);
443 error
= patch_generated_create(patch
, (git_patch_generated_output
*)xo
);
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
)
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
;
462 if ((error
= patch_generated_normalize_options(&pd
->patch
.base
.diff_opts
, opts
)) < 0)
465 if (opts
&& (opts
->flags
& GIT_DIFF_REVERSE
) != 0) {
466 void *tmp
= lfile
; lfile
= rfile
; rfile
= tmp
;
467 tmp
= ldata
; ldata
= rdata
; rdata
= tmp
;
470 pd
->patch
.base
.delta
= &pd
->delta
;
472 if (!oldsrc
->as_path
) {
474 oldsrc
->as_path
= newsrc
->as_path
;
476 oldsrc
->as_path
= newsrc
->as_path
= "file";
478 else if (!newsrc
->as_path
)
479 newsrc
->as_path
= oldsrc
->as_path
;
481 lfile
->path
= oldsrc
->as_path
;
482 rfile
->path
= newsrc
->as_path
;
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)
490 return diff_single_generate(pd
, xo
);
493 static int patch_generated_with_delta_alloc(
494 patch_generated_with_delta
**out
,
495 const char **old_path
,
496 const char **new_path
)
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;
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);
507 *out
= pd
= git__calloc(1, alloc_len
);
508 GITERR_CHECK_ALLOC(pd
);
510 pd
->patch
.flags
= GIT_PATCH_GENERATED_ALLOCATED
;
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];
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];
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
,
538 patch_generated_with_delta pd
;
541 memset(&xo
, 0, sizeof(xo
));
543 &xo
.output
, opts
, file_cb
, binary_cb
, hunk_cb
, data_cb
, payload
);
544 git_xdiff_init(&xo
, opts
);
546 memset(&pd
, 0, sizeof(pd
));
548 error
= patch_generated_from_sources(&pd
, &xo
, oldsrc
, newsrc
, opts
);
550 git_patch_free(&pd
.patch
.base
);
555 static int patch_from_sources(
557 git_diff_file_content_src
*oldsrc
,
558 git_diff_file_content_src
*newsrc
,
559 const git_diff_options
*opts
)
562 patch_generated_with_delta
*pd
;
568 if ((error
= patch_generated_with_delta_alloc(
569 &pd
, &oldsrc
->as_path
, &newsrc
->as_path
)) < 0)
572 memset(&xo
, 0, sizeof(xo
));
573 diff_output_to_patch(&xo
.output
, &pd
->patch
);
574 git_xdiff_init(&xo
, opts
);
576 if (!(error
= patch_generated_from_sources(pd
, &xo
, oldsrc
, newsrc
, opts
)))
577 *out
= (git_patch
*)pd
;
579 git_patch_free((git_patch
*)pd
);
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
,
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
);
604 int git_patch_from_blobs(
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
)
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
);
619 int git_diff_blob_to_buffer(
620 const git_blob
*old_blob
,
621 const char *old_path
,
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
,
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
);
640 int git_patch_from_blob_and_buffer(
642 const git_blob
*old_blob
,
643 const char *old_path
,
646 const char *buf_path
,
647 const git_diff_options
*opts
)
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
);
656 int git_diff_buffers(
659 const char *old_path
,
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
,
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
);
678 int git_patch_from_buffers(
682 const char *old_path
,
685 const char *new_path
,
686 const git_diff_options
*opts
)
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
);
695 int git_patch_generated_from_diff(
696 git_patch
**patch_ptr
, git_diff
*diff
, size_t idx
)
700 git_diff_delta
*delta
= NULL
;
701 git_patch_generated
*patch
= NULL
;
703 if (patch_ptr
) *patch_ptr
= NULL
;
705 if (diff_required(diff
, "git_patch_from_diff") < 0)
708 delta
= git_vector_get(&diff
->deltas
, idx
);
710 giterr_set(GITERR_INVALID
, "index out of range for delta in diff");
711 return GIT_ENOTFOUND
;
714 if (git_diff_delta__should_skip(&diff
->opts
, delta
))
717 /* don't load the patch data unless we need it for binary check */
719 ((delta
->flags
& DIFF_FLAGS_KNOWN_BINARY
) != 0 ||
720 (diff
->opts
.flags
& GIT_DIFF_SKIP_BINARY_CHECK
) != 0))
723 if ((error
= patch_generated_alloc_from_diff(&patch
, diff
, idx
)) < 0)
726 memset(&xo
, 0, sizeof(xo
));
727 diff_output_to_patch(&xo
.output
, patch
);
728 git_xdiff_init(&xo
, &diff
->opts
);
730 error
= patch_generated_invoke_file_callback(patch
, &xo
.output
);
733 error
= patch_generated_create(patch
, &xo
.output
);
736 /* TODO: if cumulative diff size is < 0.5 total size, flatten patch */
737 /* TODO: and unload the file content */
740 if (error
|| !patch_ptr
)
741 git_patch_free(&patch
->base
);
743 *patch_ptr
= &patch
->base
;
748 git_diff_driver
*git_patch_generated_driver(git_patch_generated
*patch
)
750 /* ofile driver is representative for whole patch */
751 return patch
->ofile
.driver
;
754 void git_patch_generated_old_data(
755 char **ptr
, size_t *len
, git_patch_generated
*patch
)
757 *ptr
= patch
->ofile
.map
.data
;
758 *len
= patch
->ofile
.map
.len
;
761 void git_patch_generated_new_data(
762 char **ptr
, size_t *len
, git_patch_generated
*patch
)
764 *ptr
= patch
->nfile
.map
.data
;
765 *len
= patch
->nfile
.map
.len
;
768 static int patch_generated_file_cb(
769 const git_diff_delta
*delta
,
773 GIT_UNUSED(delta
); GIT_UNUSED(progress
); GIT_UNUSED(payload
);
777 static int patch_generated_binary_cb(
778 const git_diff_delta
*delta
,
779 const git_diff_binary
*binary
,
782 git_patch
*patch
= payload
;
786 memcpy(&patch
->binary
, binary
, sizeof(git_diff_binary
));
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
);
792 memcpy((char *)patch
->binary
.old_file
.data
,
793 binary
->old_file
.data
, binary
->old_file
.datalen
);
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
);
800 memcpy((char *)patch
->binary
.new_file
.data
,
801 binary
->new_file
.data
, binary
->new_file
.datalen
);
807 static int git_patch_hunk_cb(
808 const git_diff_delta
*delta
,
809 const git_diff_hunk
*hunk_
,
812 git_patch_generated
*patch
= payload
;
813 git_patch_hunk
*hunk
;
817 hunk
= git_array_alloc(patch
->base
.hunks
);
818 GITERR_CHECK_ALLOC(hunk
);
820 memcpy(&hunk
->hunk
, hunk_
, sizeof(hunk
->hunk
));
822 patch
->base
.header_size
+= hunk_
->header_len
;
824 hunk
->line_start
= git_array_size(patch
->base
.lines
);
825 hunk
->line_count
= 0;
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_
,
836 git_patch_generated
*patch
= payload
;
837 git_patch_hunk
*hunk
;
843 hunk
= git_array_last(patch
->base
.hunks
);
844 assert(hunk
); /* programmer error if no hunk is available */
846 line
= git_array_alloc(patch
->base
.lines
);
847 GITERR_CHECK_ALLOC(line
);
849 memcpy(line
, line_
, sizeof(*line
));
851 /* do some bookkeeping so we can provide old/new line numbers */
853 patch
->base
.content_size
+= line
->content_len
;
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
;
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
,
880 memset(out
, 0, sizeof(*out
));
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
;
889 static void diff_output_to_patch(
890 git_patch_generated_output
*out
, git_patch_generated
*patch
)
895 patch_generated_file_cb
,
896 patch_generated_binary_cb
,
898 patch_generated_line_cb
,