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 /* always try to load workdir content first because filtering may
210 * need 2x data size and this minimizes peak memory footprint
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
))
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
))
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
))
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
))
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
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
;
250 patch_generated_update_binary(patch
);
253 if (patch_generated_diffable(patch
))
254 patch
->flags
|= GIT_PATCH_GENERATED_DIFFABLE
;
256 patch
->flags
|= GIT_PATCH_GENERATED_LOADED
;
262 static int patch_generated_invoke_file_callback(
263 git_patch_generated
*patch
, git_patch_generated_output
*output
)
265 float progress
= patch
->diff
?
266 ((float)patch
->delta_index
/ patch
->diff
->deltas
.length
) : 1.0f
;
268 if (!output
->file_cb
)
271 return giterr_set_after_callback_function(
272 output
->file_cb(patch
->base
.delta
, progress
, output
->payload
),
276 static int create_binary(
277 git_diff_binary_t
*out_type
,
280 size_t *out_inflatedlen
,
286 git_buf deflate
= GIT_BUF_INIT
, delta
= GIT_BUF_INIT
;
287 size_t delta_data_len
= 0;
290 /* The git_delta function accepts unsigned long only */
291 if (!git__is_ulong(a_datalen
) || !git__is_ulong(b_datalen
))
294 if ((error
= git_zstream_deflatebuf(&deflate
, b_data
, b_datalen
)) < 0)
297 /* The git_delta function accepts unsigned long only */
298 if (!git__is_ulong(deflate
.size
)) {
303 if (a_datalen
&& b_datalen
) {
306 error
= git_delta(&delta_data
, &delta_data_len
,
312 error
= git_zstream_deflatebuf(
313 &delta
, delta_data
, delta_data_len
);
315 git__free(delta_data
);
316 } else if (error
== GIT_EBUFS
) {
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
;
330 *out_type
= GIT_DIFF_BINARY_LITERAL
;
331 *out_datalen
= deflate
.size
;
332 *out_data
= git_buf_detach(&deflate
);
333 *out_inflatedlen
= b_datalen
;
337 git_buf_free(&deflate
);
338 git_buf_free(&delta
);
343 static int diff_binary(git_patch_generated_output
*output
, git_patch_generated
*patch
)
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
;
352 /* Only load contents if the user actually wants to diff
354 if (patch
->base
.diff_opts
.flags
& GIT_DIFF_SHOW_BINARY
) {
355 binary
.contains_data
= 1;
357 /* Create the old->new delta (as the "new" side of the patch),
358 * and the new->old delta (as the "old" side)
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)
373 error
= giterr_set_after_callback_function(
374 output
->binary_cb(patch
->base
.delta
, &binary
, output
->payload
),
377 git__free((char *) binary
.old_file
.data
);
378 git__free((char *) binary
.new_file
.data
);
383 static int patch_generated_create(
384 git_patch_generated
*patch
,
385 git_patch_generated_output
*output
)
389 if ((patch
->flags
& GIT_PATCH_GENERATED_DIFFED
) != 0)
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
)
396 if ((patch
->flags
& GIT_PATCH_GENERATED_LOADED
) == 0 &&
397 (error
= patch_generated_load(patch
, output
)) < 0)
400 if ((patch
->flags
& GIT_PATCH_GENERATED_DIFFABLE
) == 0)
403 if ((patch
->base
.delta
->flags
& GIT_DIFF_FLAG_BINARY
) != 0) {
404 if (output
->binary_cb
)
405 error
= diff_binary(output
, patch
);
409 error
= output
->diff_cb(output
, patch
);
412 patch
->flags
|= GIT_PATCH_GENERATED_DIFFED
;
416 static int diff_required(git_diff
*diff
, const char *action
)
420 giterr_set(GITERR_INVALID
, "must provide valid diff to %s", action
);
424 int git_diff_foreach(
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
,
435 git_patch_generated patch
;
437 if ((error
= diff_required(diff
, "git_diff_foreach")) < 0)
440 memset(&xo
, 0, sizeof(xo
));
441 memset(&patch
, 0, sizeof(patch
));
443 &xo
.output
, &diff
->opts
, file_cb
, binary_cb
, hunk_cb
, data_cb
, payload
);
444 git_xdiff_init(&xo
, &diff
->opts
);
446 git_vector_foreach(&diff
->deltas
, idx
, patch
.base
.delta
) {
448 /* check flags against patch status */
449 if (git_diff_delta__should_skip(&diff
->opts
, patch
.base
.delta
))
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 git_patch_free(&patch
.base
);
460 if ((error
= patch_generated_invoke_file_callback(&patch
, &xo
.output
)) == 0) {
461 if (binary_cb
|| hunk_cb
|| data_cb
)
462 error
= patch_generated_create(&patch
, &xo
.output
);
465 git_patch_free(&patch
.base
);
475 git_patch_generated patch
;
476 git_diff_delta delta
;
477 char paths
[GIT_FLEX_ARRAY
];
478 } patch_generated_with_delta
;
480 static int diff_single_generate(patch_generated_with_delta
*pd
, git_xdiff_output
*xo
)
483 git_patch_generated
*patch
= &pd
->patch
;
484 bool has_old
= ((patch
->ofile
.flags
& GIT_DIFF_FLAG__NO_DATA
) == 0);
485 bool has_new
= ((patch
->nfile
.flags
& GIT_DIFF_FLAG__NO_DATA
) == 0);
487 pd
->delta
.status
= has_new
?
488 (has_old
? GIT_DELTA_MODIFIED
: GIT_DELTA_ADDED
) :
489 (has_old
? GIT_DELTA_DELETED
: GIT_DELTA_UNTRACKED
);
491 if (git_oid_equal(&patch
->nfile
.file
->id
, &patch
->ofile
.file
->id
))
492 pd
->delta
.status
= GIT_DELTA_UNMODIFIED
;
494 patch
->base
.delta
= &pd
->delta
;
496 patch_generated_init_common(patch
);
498 if (pd
->delta
.status
== GIT_DELTA_UNMODIFIED
&&
499 !(patch
->ofile
.opts_flags
& GIT_DIFF_INCLUDE_UNMODIFIED
)) {
501 /* Even empty patches are flagged as binary, and even though
502 * there's no difference, we flag this as "containing data"
503 * (the data is known to be empty, as opposed to wholly unknown).
505 if (patch
->base
.diff_opts
.flags
& GIT_DIFF_SHOW_BINARY
)
506 patch
->base
.binary
.contains_data
= 1;
511 error
= patch_generated_invoke_file_callback(patch
, (git_patch_generated_output
*)xo
);
514 error
= patch_generated_create(patch
, (git_patch_generated_output
*)xo
);
519 static int patch_generated_from_sources(
520 patch_generated_with_delta
*pd
,
521 git_xdiff_output
*xo
,
522 git_diff_file_content_src
*oldsrc
,
523 git_diff_file_content_src
*newsrc
,
524 const git_diff_options
*opts
)
527 git_repository
*repo
=
528 oldsrc
->blob
? git_blob_owner(oldsrc
->blob
) :
529 newsrc
->blob
? git_blob_owner(newsrc
->blob
) : NULL
;
530 git_diff_file
*lfile
= &pd
->delta
.old_file
, *rfile
= &pd
->delta
.new_file
;
531 git_diff_file_content
*ldata
= &pd
->patch
.ofile
, *rdata
= &pd
->patch
.nfile
;
533 if ((error
= patch_generated_normalize_options(&pd
->patch
.base
.diff_opts
, opts
)) < 0)
536 if (opts
&& (opts
->flags
& GIT_DIFF_REVERSE
) != 0) {
537 void *tmp
= lfile
; lfile
= rfile
; rfile
= tmp
;
538 tmp
= ldata
; ldata
= rdata
; rdata
= tmp
;
541 pd
->patch
.base
.delta
= &pd
->delta
;
543 if (!oldsrc
->as_path
) {
545 oldsrc
->as_path
= newsrc
->as_path
;
547 oldsrc
->as_path
= newsrc
->as_path
= "file";
549 else if (!newsrc
->as_path
)
550 newsrc
->as_path
= oldsrc
->as_path
;
552 lfile
->path
= oldsrc
->as_path
;
553 rfile
->path
= newsrc
->as_path
;
555 if ((error
= git_diff_file_content__init_from_src(
556 ldata
, repo
, opts
, oldsrc
, lfile
)) < 0 ||
557 (error
= git_diff_file_content__init_from_src(
558 rdata
, repo
, opts
, newsrc
, rfile
)) < 0)
561 return diff_single_generate(pd
, xo
);
564 static int patch_generated_with_delta_alloc(
565 patch_generated_with_delta
**out
,
566 const char **old_path
,
567 const char **new_path
)
569 patch_generated_with_delta
*pd
;
570 size_t old_len
= *old_path
? strlen(*old_path
) : 0;
571 size_t new_len
= *new_path
? strlen(*new_path
) : 0;
574 GITERR_CHECK_ALLOC_ADD(&alloc_len
, sizeof(*pd
), old_len
);
575 GITERR_CHECK_ALLOC_ADD(&alloc_len
, alloc_len
, new_len
);
576 GITERR_CHECK_ALLOC_ADD(&alloc_len
, alloc_len
, 2);
578 *out
= pd
= git__calloc(1, alloc_len
);
579 GITERR_CHECK_ALLOC(pd
);
581 pd
->patch
.flags
= GIT_PATCH_GENERATED_ALLOCATED
;
584 memcpy(&pd
->paths
[0], *old_path
, old_len
);
585 *old_path
= &pd
->paths
[0];
586 } else if (*new_path
)
587 *old_path
= &pd
->paths
[old_len
+ 1];
590 memcpy(&pd
->paths
[old_len
+ 1], *new_path
, new_len
);
591 *new_path
= &pd
->paths
[old_len
+ 1];
592 } else if (*old_path
)
593 *new_path
= &pd
->paths
[0];
598 static int diff_from_sources(
599 git_diff_file_content_src
*oldsrc
,
600 git_diff_file_content_src
*newsrc
,
601 const git_diff_options
*opts
,
602 git_diff_file_cb file_cb
,
603 git_diff_binary_cb binary_cb
,
604 git_diff_hunk_cb hunk_cb
,
605 git_diff_line_cb data_cb
,
609 patch_generated_with_delta pd
;
612 memset(&xo
, 0, sizeof(xo
));
614 &xo
.output
, opts
, file_cb
, binary_cb
, hunk_cb
, data_cb
, payload
);
615 git_xdiff_init(&xo
, opts
);
617 memset(&pd
, 0, sizeof(pd
));
619 error
= patch_generated_from_sources(&pd
, &xo
, oldsrc
, newsrc
, opts
);
621 git_patch_free(&pd
.patch
.base
);
626 static int patch_from_sources(
628 git_diff_file_content_src
*oldsrc
,
629 git_diff_file_content_src
*newsrc
,
630 const git_diff_options
*opts
)
633 patch_generated_with_delta
*pd
;
639 if ((error
= patch_generated_with_delta_alloc(
640 &pd
, &oldsrc
->as_path
, &newsrc
->as_path
)) < 0)
643 memset(&xo
, 0, sizeof(xo
));
644 diff_output_to_patch(&xo
.output
, &pd
->patch
);
645 git_xdiff_init(&xo
, opts
);
647 if (!(error
= patch_generated_from_sources(pd
, &xo
, oldsrc
, newsrc
, opts
)))
648 *out
= (git_patch
*)pd
;
650 git_patch_free((git_patch
*)pd
);
656 const git_blob
*old_blob
,
657 const char *old_path
,
658 const git_blob
*new_blob
,
659 const char *new_path
,
660 const git_diff_options
*opts
,
661 git_diff_file_cb file_cb
,
662 git_diff_binary_cb binary_cb
,
663 git_diff_hunk_cb hunk_cb
,
664 git_diff_line_cb data_cb
,
667 git_diff_file_content_src osrc
=
668 GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob
, old_path
);
669 git_diff_file_content_src nsrc
=
670 GIT_DIFF_FILE_CONTENT_SRC__BLOB(new_blob
, new_path
);
671 return diff_from_sources(
672 &osrc
, &nsrc
, opts
, file_cb
, binary_cb
, hunk_cb
, data_cb
, payload
);
675 int git_patch_from_blobs(
677 const git_blob
*old_blob
,
678 const char *old_path
,
679 const git_blob
*new_blob
,
680 const char *new_path
,
681 const git_diff_options
*opts
)
683 git_diff_file_content_src osrc
=
684 GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob
, old_path
);
685 git_diff_file_content_src nsrc
=
686 GIT_DIFF_FILE_CONTENT_SRC__BLOB(new_blob
, new_path
);
687 return patch_from_sources(out
, &osrc
, &nsrc
, opts
);
690 int git_diff_blob_to_buffer(
691 const git_blob
*old_blob
,
692 const char *old_path
,
695 const char *buf_path
,
696 const git_diff_options
*opts
,
697 git_diff_file_cb file_cb
,
698 git_diff_binary_cb binary_cb
,
699 git_diff_hunk_cb hunk_cb
,
700 git_diff_line_cb data_cb
,
703 git_diff_file_content_src osrc
=
704 GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob
, old_path
);
705 git_diff_file_content_src nsrc
=
706 GIT_DIFF_FILE_CONTENT_SRC__BUF(buf
, buflen
, buf_path
);
707 return diff_from_sources(
708 &osrc
, &nsrc
, opts
, file_cb
, binary_cb
, hunk_cb
, data_cb
, payload
);
711 int git_patch_from_blob_and_buffer(
713 const git_blob
*old_blob
,
714 const char *old_path
,
717 const char *buf_path
,
718 const git_diff_options
*opts
)
720 git_diff_file_content_src osrc
=
721 GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob
, old_path
);
722 git_diff_file_content_src nsrc
=
723 GIT_DIFF_FILE_CONTENT_SRC__BUF(buf
, buflen
, buf_path
);
724 return patch_from_sources(out
, &osrc
, &nsrc
, opts
);
727 int git_diff_buffers(
730 const char *old_path
,
733 const char *new_path
,
734 const git_diff_options
*opts
,
735 git_diff_file_cb file_cb
,
736 git_diff_binary_cb binary_cb
,
737 git_diff_hunk_cb hunk_cb
,
738 git_diff_line_cb data_cb
,
741 git_diff_file_content_src osrc
=
742 GIT_DIFF_FILE_CONTENT_SRC__BUF(old_buf
, old_len
, old_path
);
743 git_diff_file_content_src nsrc
=
744 GIT_DIFF_FILE_CONTENT_SRC__BUF(new_buf
, new_len
, new_path
);
745 return diff_from_sources(
746 &osrc
, &nsrc
, opts
, file_cb
, binary_cb
, hunk_cb
, data_cb
, payload
);
749 int git_patch_from_buffers(
753 const char *old_path
,
756 const char *new_path
,
757 const git_diff_options
*opts
)
759 git_diff_file_content_src osrc
=
760 GIT_DIFF_FILE_CONTENT_SRC__BUF(old_buf
, old_len
, old_path
);
761 git_diff_file_content_src nsrc
=
762 GIT_DIFF_FILE_CONTENT_SRC__BUF(new_buf
, new_len
, new_path
);
763 return patch_from_sources(out
, &osrc
, &nsrc
, opts
);
766 int git_patch_generated_from_diff(
767 git_patch
**patch_ptr
, git_diff
*diff
, size_t idx
)
771 git_diff_delta
*delta
= NULL
;
772 git_patch_generated
*patch
= NULL
;
774 if (patch_ptr
) *patch_ptr
= NULL
;
776 if (diff_required(diff
, "git_patch_from_diff") < 0)
779 delta
= git_vector_get(&diff
->deltas
, idx
);
781 giterr_set(GITERR_INVALID
, "index out of range for delta in diff");
782 return GIT_ENOTFOUND
;
785 if (git_diff_delta__should_skip(&diff
->opts
, delta
))
788 /* don't load the patch data unless we need it for binary check */
790 ((delta
->flags
& DIFF_FLAGS_KNOWN_BINARY
) != 0 ||
791 (diff
->opts
.flags
& GIT_DIFF_SKIP_BINARY_CHECK
) != 0))
794 if ((error
= patch_generated_alloc_from_diff(&patch
, diff
, idx
)) < 0)
797 memset(&xo
, 0, sizeof(xo
));
798 diff_output_to_patch(&xo
.output
, patch
);
799 git_xdiff_init(&xo
, &diff
->opts
);
801 error
= patch_generated_invoke_file_callback(patch
, &xo
.output
);
804 error
= patch_generated_create(patch
, &xo
.output
);
807 /* TODO: if cumulative diff size is < 0.5 total size, flatten patch */
808 /* TODO: and unload the file content */
811 if (error
|| !patch_ptr
)
812 git_patch_free(&patch
->base
);
814 *patch_ptr
= &patch
->base
;
819 git_diff_driver
*git_patch_generated_driver(git_patch_generated
*patch
)
821 /* ofile driver is representative for whole patch */
822 return patch
->ofile
.driver
;
825 void git_patch_generated_old_data(
826 char **ptr
, size_t *len
, git_patch_generated
*patch
)
828 *ptr
= patch
->ofile
.map
.data
;
829 *len
= patch
->ofile
.map
.len
;
832 void git_patch_generated_new_data(
833 char **ptr
, size_t *len
, git_patch_generated
*patch
)
835 *ptr
= patch
->nfile
.map
.data
;
836 *len
= patch
->nfile
.map
.len
;
839 static int patch_generated_file_cb(
840 const git_diff_delta
*delta
,
844 GIT_UNUSED(delta
); GIT_UNUSED(progress
); GIT_UNUSED(payload
);
848 static int patch_generated_binary_cb(
849 const git_diff_delta
*delta
,
850 const git_diff_binary
*binary
,
853 git_patch
*patch
= payload
;
857 memcpy(&patch
->binary
, binary
, sizeof(git_diff_binary
));
859 if (binary
->old_file
.data
) {
860 patch
->binary
.old_file
.data
= git__malloc(binary
->old_file
.datalen
);
861 GITERR_CHECK_ALLOC(patch
->binary
.old_file
.data
);
863 memcpy((char *)patch
->binary
.old_file
.data
,
864 binary
->old_file
.data
, binary
->old_file
.datalen
);
867 if (binary
->new_file
.data
) {
868 patch
->binary
.new_file
.data
= git__malloc(binary
->new_file
.datalen
);
869 GITERR_CHECK_ALLOC(patch
->binary
.new_file
.data
);
871 memcpy((char *)patch
->binary
.new_file
.data
,
872 binary
->new_file
.data
, binary
->new_file
.datalen
);
878 static int git_patch_hunk_cb(
879 const git_diff_delta
*delta
,
880 const git_diff_hunk
*hunk_
,
883 git_patch_generated
*patch
= payload
;
884 git_patch_hunk
*hunk
;
888 hunk
= git_array_alloc(patch
->base
.hunks
);
889 GITERR_CHECK_ALLOC(hunk
);
891 memcpy(&hunk
->hunk
, hunk_
, sizeof(hunk
->hunk
));
893 patch
->base
.header_size
+= hunk_
->header_len
;
895 hunk
->line_start
= git_array_size(patch
->base
.lines
);
896 hunk
->line_count
= 0;
901 static int patch_generated_line_cb(
902 const git_diff_delta
*delta
,
903 const git_diff_hunk
*hunk_
,
904 const git_diff_line
*line_
,
907 git_patch_generated
*patch
= payload
;
908 git_patch_hunk
*hunk
;
914 hunk
= git_array_last(patch
->base
.hunks
);
915 assert(hunk
); /* programmer error if no hunk is available */
917 line
= git_array_alloc(patch
->base
.lines
);
918 GITERR_CHECK_ALLOC(line
);
920 memcpy(line
, line_
, sizeof(*line
));
922 /* do some bookkeeping so we can provide old/new line numbers */
924 patch
->base
.content_size
+= line
->content_len
;
926 if (line
->origin
== GIT_DIFF_LINE_ADDITION
||
927 line
->origin
== GIT_DIFF_LINE_DELETION
)
928 patch
->base
.content_size
+= 1;
929 else if (line
->origin
== GIT_DIFF_LINE_CONTEXT
) {
930 patch
->base
.content_size
+= 1;
931 patch
->base
.context_size
+= line
->content_len
+ 1;
932 } else if (line
->origin
== GIT_DIFF_LINE_CONTEXT_EOFNL
)
933 patch
->base
.context_size
+= line
->content_len
;
940 static void diff_output_init(
941 git_patch_generated_output
*out
,
942 const git_diff_options
*opts
,
943 git_diff_file_cb file_cb
,
944 git_diff_binary_cb binary_cb
,
945 git_diff_hunk_cb hunk_cb
,
946 git_diff_line_cb data_cb
,
951 memset(out
, 0, sizeof(*out
));
953 out
->file_cb
= file_cb
;
954 out
->binary_cb
= binary_cb
;
955 out
->hunk_cb
= hunk_cb
;
956 out
->data_cb
= data_cb
;
957 out
->payload
= payload
;
960 static void diff_output_to_patch(
961 git_patch_generated_output
*out
, git_patch_generated
*patch
)
966 patch_generated_file_cb
,
967 patch_generated_binary_cb
,
969 patch_generated_line_cb
,