Hash :
74ded024
Author :
Date :
2013-06-17T17:03:34
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "common.h"
#include "diff.h"
#include "diff_file.h"
#include "diff_driver.h"
#include "diff_patch.h"
#include "diff_xdiff.h"
/* cached information about a single span in a diff */
typedef struct diff_patch_line diff_patch_line;
struct diff_patch_line {
const char *ptr;
size_t len;
size_t lines, oldno, newno;
char origin;
};
/* cached information about a hunk in a diff */
typedef struct diff_patch_hunk diff_patch_hunk;
struct diff_patch_hunk {
git_diff_range range;
char header[128];
size_t header_len;
size_t line_start;
size_t line_count;
};
struct git_diff_patch {
git_refcount rc;
git_diff_list *diff; /* for refcount purposes, maybe NULL for blob diffs */
git_diff_delta *delta;
size_t delta_index;
git_diff_file_content ofile;
git_diff_file_content nfile;
uint32_t flags;
git_array_t(diff_patch_hunk) hunks;
git_array_t(diff_patch_line) lines;
size_t oldno, newno;
size_t content_size;
git_pool flattened;
};
enum {
GIT_DIFF_PATCH_ALLOCATED = (1 << 0),
GIT_DIFF_PATCH_INITIALIZED = (1 << 1),
GIT_DIFF_PATCH_LOADED = (1 << 2),
GIT_DIFF_PATCH_DIFFABLE = (1 << 3),
GIT_DIFF_PATCH_DIFFED = (1 << 4),
GIT_DIFF_PATCH_FLATTENED = (1 << 5),
};
static void diff_output_init(git_diff_output*, const git_diff_options*,
git_diff_file_cb, git_diff_hunk_cb, git_diff_data_cb, void*);
static void diff_output_to_patch(git_diff_output *, git_diff_patch *);
static void diff_patch_update_binary(git_diff_patch *patch)
{
if ((patch->delta->flags & DIFF_FLAGS_KNOWN_BINARY) != 0)
return;
if ((patch->ofile.file->flags & GIT_DIFF_FLAG_BINARY) != 0 ||
(patch->nfile.file->flags & GIT_DIFF_FLAG_BINARY) != 0)
patch->delta->flags |= GIT_DIFF_FLAG_BINARY;
else if ((patch->ofile.file->flags & DIFF_FLAGS_NOT_BINARY) != 0 &&
(patch->nfile.file->flags & DIFF_FLAGS_NOT_BINARY) != 0)
patch->delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;
}
static void diff_patch_init_common(git_diff_patch *patch)
{
diff_patch_update_binary(patch);
if ((patch->delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
patch->flags |= GIT_DIFF_PATCH_LOADED; /* set LOADED but not DIFFABLE */
patch->flags |= GIT_DIFF_PATCH_INITIALIZED;
if (patch->diff)
git_diff_list_addref(patch->diff);
}
static int diff_patch_init_from_diff(
git_diff_patch *patch, git_diff_list *diff, size_t delta_index)
{
int error = 0;
memset(patch, 0, sizeof(*patch));
patch->diff = diff;
patch->delta = git_vector_get(&diff->deltas, delta_index);
patch->delta_index = delta_index;
if ((error = git_diff_file_content__init_from_diff(
&patch->ofile, diff, delta_index, true)) < 0 ||
(error = git_diff_file_content__init_from_diff(
&patch->nfile, diff, delta_index, false)) < 0)
return error;
diff_patch_init_common(patch);
return 0;
}
static int diff_patch_alloc_from_diff(
git_diff_patch **out,
git_diff_list *diff,
size_t delta_index)
{
int error;
git_diff_patch *patch = git__calloc(1, sizeof(git_diff_patch));
GITERR_CHECK_ALLOC(patch);
if (!(error = diff_patch_init_from_diff(patch, diff, delta_index))) {
patch->flags |= GIT_DIFF_PATCH_ALLOCATED;
GIT_REFCOUNT_INC(patch);
} else {
git__free(patch);
patch = NULL;
}
*out = patch;
return error;
}
static int diff_patch_load(git_diff_patch *patch, git_diff_output *output)
{
int error = 0;
bool incomplete_data;
if ((patch->flags & GIT_DIFF_PATCH_LOADED) != 0)
return 0;
/* if no hunk and data callbacks and user doesn't care if data looks
* binary, then there is no need to actually load the data
*/
if ((patch->ofile.opts_flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0 &&
output && !output->hunk_cb && !output->data_cb)
return 0;
incomplete_data =
(((patch->ofile.flags & GIT_DIFF_FLAG__NO_DATA) != 0 ||
(patch->ofile.file->flags & GIT_DIFF_FLAG_VALID_OID) != 0) &&
((patch->nfile.flags & GIT_DIFF_FLAG__NO_DATA) != 0 ||
(patch->nfile.file->flags & GIT_DIFF_FLAG_VALID_OID) != 0));
/* always try to load workdir content first because filtering may
* need 2x data size and this minimizes peak memory footprint
*/
if (patch->ofile.src == GIT_ITERATOR_TYPE_WORKDIR) {
if ((error = git_diff_file_content__load(&patch->ofile)) < 0 ||
(patch->ofile.file->flags & GIT_DIFF_FLAG_BINARY) != 0)
goto cleanup;
}
if (patch->nfile.src == GIT_ITERATOR_TYPE_WORKDIR) {
if ((error = git_diff_file_content__load(&patch->nfile)) < 0 ||
(patch->nfile.file->flags & GIT_DIFF_FLAG_BINARY) != 0)
goto cleanup;
}
/* once workdir has been tried, load other data as needed */
if (patch->ofile.src != GIT_ITERATOR_TYPE_WORKDIR) {
if ((error = git_diff_file_content__load(&patch->ofile)) < 0 ||
(patch->ofile.file->flags & GIT_DIFF_FLAG_BINARY) != 0)
goto cleanup;
}
if (patch->nfile.src != GIT_ITERATOR_TYPE_WORKDIR) {
if ((error = git_diff_file_content__load(&patch->nfile)) < 0 ||
(patch->nfile.file->flags & GIT_DIFF_FLAG_BINARY) != 0)
goto cleanup;
}
/* if we were previously missing an oid, update MODIFIED->UNMODIFIED */
if (incomplete_data &&
patch->ofile.file->mode == patch->nfile.file->mode &&
git_oid_equal(&patch->ofile.file->oid, &patch->nfile.file->oid) &&
patch->delta->status == GIT_DELTA_MODIFIED) /* not RENAMED/COPIED! */
patch->delta->status = GIT_DELTA_UNMODIFIED;
cleanup:
diff_patch_update_binary(patch);
if (!error) {
/* patch is diffable only for non-binary, modified files where
* at least one side has data and the data actually changed
*/
if ((patch->delta->flags & GIT_DIFF_FLAG_BINARY) == 0 &&
patch->delta->status != GIT_DELTA_UNMODIFIED &&
(patch->ofile.map.len || patch->nfile.map.len) &&
(patch->ofile.map.len != patch->nfile.map.len ||
!git_oid_equal(&patch->ofile.file->oid, &patch->nfile.file->oid)))
patch->flags |= GIT_DIFF_PATCH_DIFFABLE;
patch->flags |= GIT_DIFF_PATCH_LOADED;
}
return error;
}
static int diff_patch_file_callback(
git_diff_patch *patch, git_diff_output *output)
{
float progress;
if (!output->file_cb)
return 0;
progress = patch->diff ?
((float)patch->delta_index / patch->diff->deltas.length) : 1.0f;
if (output->file_cb(patch->delta, progress, output->payload) != 0)
output->error = GIT_EUSER;
return output->error;
}
static int diff_patch_generate(git_diff_patch *patch, git_diff_output *output)
{
int error = 0;
if ((patch->flags & GIT_DIFF_PATCH_DIFFED) != 0)
return 0;
if ((patch->flags & GIT_DIFF_PATCH_LOADED) == 0 &&
(error = diff_patch_load(patch, output)) < 0)
return error;
if ((patch->flags & GIT_DIFF_PATCH_DIFFABLE) == 0)
return 0;
if (output->diff_cb != NULL &&
!(error = output->diff_cb(output, patch)))
patch->flags |= GIT_DIFF_PATCH_DIFFED;
return error;
}
static void diff_patch_free(git_diff_patch *patch)
{
git_diff_file_content__clear(&patch->ofile);
git_diff_file_content__clear(&patch->nfile);
git_array_clear(patch->lines);
git_array_clear(patch->hunks);
git_diff_list_free(patch->diff); /* decrements refcount */
patch->diff = NULL;
git_pool_clear(&patch->flattened);
if (patch->flags & GIT_DIFF_PATCH_ALLOCATED)
git__free(patch);
}
static int diff_required(git_diff_list *diff, const char *action)
{
if (diff)
return 0;
giterr_set(GITERR_INVALID, "Must provide valid diff to %s", action);
return -1;
}
int git_diff_foreach(
git_diff_list *diff,
git_diff_file_cb file_cb,
git_diff_hunk_cb hunk_cb,
git_diff_data_cb data_cb,
void *payload)
{
int error = 0;
git_xdiff_output xo;
size_t idx;
git_diff_patch patch;
if (diff_required(diff, "git_diff_foreach") < 0)
return -1;
diff_output_init((git_diff_output *)&xo,
&diff->opts, file_cb, hunk_cb, data_cb, payload);
git_xdiff_init(&xo, &diff->opts);
git_vector_foreach(&diff->deltas, idx, patch.delta) {
/* check flags against patch status */
if (git_diff_delta__should_skip(&diff->opts, patch.delta))
continue;
if (!(error = diff_patch_init_from_diff(&patch, diff, idx))) {
error = diff_patch_file_callback(&patch, (git_diff_output *)&xo);
if (!error)
error = diff_patch_generate(&patch, (git_diff_output *)&xo);
git_diff_patch_free(&patch);
}
if (error < 0)
break;
}
if (error == GIT_EUSER)
giterr_clear(); /* don't leave error message set invalidly */
return error;
}
typedef struct {
git_diff_patch patch;
git_diff_delta delta;
char paths[GIT_FLEX_ARRAY];
} diff_patch_with_delta;
static int diff_single_generate(diff_patch_with_delta *pd, git_xdiff_output *xo)
{
int error = 0;
git_diff_patch *patch = &pd->patch;
bool has_old = ((patch->ofile.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
bool has_new = ((patch->nfile.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
pd->delta.status = has_new ?
(has_old ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) :
(has_old ? GIT_DELTA_DELETED : GIT_DELTA_UNTRACKED);
if (git_oid_equal(&patch->nfile.file->oid, &patch->ofile.file->oid))
pd->delta.status = GIT_DELTA_UNMODIFIED;
patch->delta = &pd->delta;
diff_patch_init_common(patch);
if (pd->delta.status == GIT_DELTA_UNMODIFIED &&
!(patch->ofile.opts_flags & GIT_DIFF_INCLUDE_UNMODIFIED))
return error;
error = diff_patch_file_callback(patch, (git_diff_output *)xo);
if (!error)
error = diff_patch_generate(patch, (git_diff_output *)xo);
if (error == GIT_EUSER)
giterr_clear(); /* don't leave error message set invalidly */
return error;
}
static int diff_patch_from_blobs(
diff_patch_with_delta *pd,
git_xdiff_output *xo,
const git_blob *old_blob,
const char *old_path,
const git_blob *new_blob,
const char *new_path,
const git_diff_options *opts)
{
int error = 0;
git_repository *repo =
new_blob ? git_object_owner((const git_object *)new_blob) :
old_blob ? git_object_owner((const git_object *)old_blob) : NULL;
GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options");
if (opts && (opts->flags & GIT_DIFF_REVERSE) != 0) {
const git_blob *tmp_blob;
const char *tmp_path;
tmp_blob = old_blob; old_blob = new_blob; new_blob = tmp_blob;
tmp_path = old_path; old_path = new_path; new_path = tmp_path;
}
pd->patch.delta = &pd->delta;
pd->delta.old_file.path = old_path;
pd->delta.new_file.path = new_path;
if ((error = git_diff_file_content__init_from_blob(
&pd->patch.ofile, repo, opts, old_blob, &pd->delta.old_file)) < 0 ||
(error = git_diff_file_content__init_from_blob(
&pd->patch.nfile, repo, opts, new_blob, &pd->delta.new_file)) < 0)
return error;
return diff_single_generate(pd, xo);
}
static int diff_patch_with_delta_alloc(
diff_patch_with_delta **out,
const char **old_path,
const char **new_path)
{
diff_patch_with_delta *pd;
size_t old_len = *old_path ? strlen(*old_path) : 0;
size_t new_len = *new_path ? strlen(*new_path) : 0;
*out = pd = git__calloc(1, sizeof(*pd) + old_len + new_len + 2);
GITERR_CHECK_ALLOC(pd);
pd->patch.flags = GIT_DIFF_PATCH_ALLOCATED;
if (*old_path) {
memcpy(&pd->paths[0], *old_path, old_len);
*old_path = &pd->paths[0];
} else if (*new_path)
*old_path = &pd->paths[old_len + 1];
if (*new_path) {
memcpy(&pd->paths[old_len + 1], *new_path, new_len);
*new_path = &pd->paths[old_len + 1];
} else if (*old_path)
*new_path = &pd->paths[0];
return 0;
}
int git_diff_blobs(
const git_blob *old_blob,
const char *old_path,
const git_blob *new_blob,
const char *new_path,
const git_diff_options *opts,
git_diff_file_cb file_cb,
git_diff_hunk_cb hunk_cb,
git_diff_data_cb data_cb,
void *payload)
{
int error = 0;
diff_patch_with_delta pd;
git_xdiff_output xo;
memset(&pd, 0, sizeof(pd));
memset(&xo, 0, sizeof(xo));
diff_output_init(
(git_diff_output *)&xo, opts, file_cb, hunk_cb, data_cb, payload);
git_xdiff_init(&xo, opts);
if (!old_path && new_path)
old_path = new_path;
else if (!new_path && old_path)
new_path = old_path;
error = diff_patch_from_blobs(
&pd, &xo, old_blob, old_path, new_blob, new_path, opts);
git_diff_patch_free((git_diff_patch *)&pd);
return error;
}
int git_diff_patch_from_blobs(
git_diff_patch **out,
const git_blob *old_blob,
const char *old_path,
const git_blob *new_blob,
const char *new_path,
const git_diff_options *opts)
{
int error = 0;
diff_patch_with_delta *pd;
git_xdiff_output xo;
assert(out);
*out = NULL;
if (diff_patch_with_delta_alloc(&pd, &old_path, &new_path) < 0)
return -1;
memset(&xo, 0, sizeof(xo));
diff_output_to_patch((git_diff_output *)&xo, &pd->patch);
git_xdiff_init(&xo, opts);
error = diff_patch_from_blobs(
pd, &xo, old_blob, old_path, new_blob, new_path, opts);
if (!error)
*out = (git_diff_patch *)pd;
else
git_diff_patch_free((git_diff_patch *)pd);
return error;
}
static int diff_patch_from_blob_and_buffer(
diff_patch_with_delta *pd,
git_xdiff_output *xo,
const git_blob *old_blob,
const char *old_path,
const char *buf,
size_t buflen,
const char *buf_path,
const git_diff_options *opts)
{
int error = 0;
git_repository *repo =
old_blob ? git_object_owner((const git_object *)old_blob) : NULL;
GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options");
pd->patch.delta = &pd->delta;
if (opts && (opts->flags & GIT_DIFF_REVERSE) != 0) {
pd->delta.old_file.path = buf_path;
pd->delta.new_file.path = old_path;
if (!(error = git_diff_file_content__init_from_raw(
&pd->patch.ofile, repo, opts, buf, buflen, &pd->delta.old_file)))
error = git_diff_file_content__init_from_blob(
&pd->patch.nfile, repo, opts, old_blob, &pd->delta.new_file);
} else {
pd->delta.old_file.path = old_path;
pd->delta.new_file.path = buf_path;
if (!(error = git_diff_file_content__init_from_blob(
&pd->patch.ofile, repo, opts, old_blob, &pd->delta.old_file)))
error = git_diff_file_content__init_from_raw(
&pd->patch.nfile, repo, opts, buf, buflen, &pd->delta.new_file);
}
if (error < 0)
return error;
return diff_single_generate(pd, xo);
}
int git_diff_blob_to_buffer(
const git_blob *old_blob,
const char *old_path,
const char *buf,
size_t buflen,
const char *buf_path,
const git_diff_options *opts,
git_diff_file_cb file_cb,
git_diff_hunk_cb hunk_cb,
git_diff_data_cb data_cb,
void *payload)
{
int error = 0;
diff_patch_with_delta pd;
git_xdiff_output xo;
memset(&pd, 0, sizeof(pd));
memset(&xo, 0, sizeof(xo));
diff_output_init(
(git_diff_output *)&xo, opts, file_cb, hunk_cb, data_cb, payload);
git_xdiff_init(&xo, opts);
if (!old_path && buf_path)
old_path = buf_path;
else if (!buf_path && old_path)
buf_path = old_path;
error = diff_patch_from_blob_and_buffer(
&pd, &xo, old_blob, old_path, buf, buflen, buf_path, opts);
git_diff_patch_free((git_diff_patch *)&pd);
return error;
}
int git_diff_patch_from_blob_and_buffer(
git_diff_patch **out,
const git_blob *old_blob,
const char *old_path,
const char *buf,
size_t buflen,
const char *buf_path,
const git_diff_options *opts)
{
int error = 0;
diff_patch_with_delta *pd;
git_xdiff_output xo;
assert(out);
*out = NULL;
if (diff_patch_with_delta_alloc(&pd, &old_path, &buf_path) < 0)
return -1;
memset(&xo, 0, sizeof(xo));
diff_output_to_patch((git_diff_output *)&xo, &pd->patch);
git_xdiff_init(&xo, opts);
error = diff_patch_from_blob_and_buffer(
pd, &xo, old_blob, old_path, buf, buflen, buf_path, opts);
if (!error)
*out = (git_diff_patch *)pd;
else
git_diff_patch_free((git_diff_patch *)pd);
return error;
}
int git_diff_get_patch(
git_diff_patch **patch_ptr,
const git_diff_delta **delta_ptr,
git_diff_list *diff,
size_t idx)
{
int error = 0;
git_xdiff_output xo;
git_diff_delta *delta = NULL;
git_diff_patch *patch = NULL;
if (patch_ptr) *patch_ptr = NULL;
if (delta_ptr) *delta_ptr = NULL;
if (diff_required(diff, "git_diff_get_patch") < 0)
return -1;
delta = git_vector_get(&diff->deltas, idx);
if (!delta) {
giterr_set(GITERR_INVALID, "Index out of range for delta in diff");
return GIT_ENOTFOUND;
}
if (delta_ptr)
*delta_ptr = delta;
if (git_diff_delta__should_skip(&diff->opts, delta))
return 0;
/* don't load the patch data unless we need it for binary check */
if (!patch_ptr &&
((delta->flags & DIFF_FLAGS_KNOWN_BINARY) != 0 ||
(diff->opts.flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0))
return 0;
if ((error = diff_patch_alloc_from_diff(&patch, diff, idx)) < 0)
return error;
diff_output_to_patch((git_diff_output *)&xo, patch);
git_xdiff_init(&xo, &diff->opts);
error = diff_patch_file_callback(patch, (git_diff_output *)&xo);
if (!error)
error = diff_patch_generate(patch, (git_diff_output *)&xo);
if (!error) {
/* if cumulative diff size is < 0.5 total size, flatten the patch */
/* unload the file content */
}
if (error || !patch_ptr)
git_diff_patch_free(patch);
else
*patch_ptr = patch;
if (error == GIT_EUSER)
giterr_clear(); /* don't leave error message set invalidly */
return error;
}
void git_diff_patch_free(git_diff_patch *patch)
{
if (patch)
GIT_REFCOUNT_DEC(patch, diff_patch_free);
}
const git_diff_delta *git_diff_patch_delta(git_diff_patch *patch)
{
assert(patch);
return patch->delta;
}
size_t git_diff_patch_num_hunks(git_diff_patch *patch)
{
assert(patch);
return git_array_size(patch->hunks);
}
int git_diff_patch_line_stats(
size_t *total_ctxt,
size_t *total_adds,
size_t *total_dels,
const git_diff_patch *patch)
{
size_t totals[3], idx;
memset(totals, 0, sizeof(totals));
for (idx = 0; idx < git_array_size(patch->lines); ++idx) {
diff_patch_line *line = git_array_get(patch->lines, idx);
if (!line)
continue;
switch (line->origin) {
case GIT_DIFF_LINE_CONTEXT: totals[0]++; break;
case GIT_DIFF_LINE_ADDITION: totals[1]++; break;
case GIT_DIFF_LINE_DELETION: totals[2]++; break;
default:
/* diff --stat and --numstat don't count EOFNL marks because
* they will always be paired with a ADDITION or DELETION line.
*/
break;
}
}
if (total_ctxt)
*total_ctxt = totals[0];
if (total_adds)
*total_adds = totals[1];
if (total_dels)
*total_dels = totals[2];
return 0;
}
static int diff_error_outofrange(const char *thing)
{
giterr_set(GITERR_INVALID, "Diff patch %s index out of range", thing);
return GIT_ENOTFOUND;
}
int git_diff_patch_get_hunk(
const git_diff_range **range,
const char **header,
size_t *header_len,
size_t *lines_in_hunk,
git_diff_patch *patch,
size_t hunk_idx)
{
diff_patch_hunk *hunk;
assert(patch);
hunk = git_array_get(patch->hunks, hunk_idx);
if (!hunk) {
if (range) *range = NULL;
if (header) *header = NULL;
if (header_len) *header_len = 0;
if (lines_in_hunk) *lines_in_hunk = 0;
return diff_error_outofrange("hunk");
}
if (range) *range = &hunk->range;
if (header) *header = hunk->header;
if (header_len) *header_len = hunk->header_len;
if (lines_in_hunk) *lines_in_hunk = hunk->line_count;
return 0;
}
int git_diff_patch_num_lines_in_hunk(git_diff_patch *patch, size_t hunk_idx)
{
diff_patch_hunk *hunk;
assert(patch);
if (!(hunk = git_array_get(patch->hunks, hunk_idx)))
return diff_error_outofrange("hunk");
return (int)hunk->line_count;
}
int git_diff_patch_get_line_in_hunk(
char *line_origin,
const char **content,
size_t *content_len,
int *old_lineno,
int *new_lineno,
git_diff_patch *patch,
size_t hunk_idx,
size_t line_of_hunk)
{
diff_patch_hunk *hunk;
diff_patch_line *line;
const char *thing;
assert(patch);
if (!(hunk = git_array_get(patch->hunks, hunk_idx))) {
thing = "hunk";
goto notfound;
}
if (line_of_hunk >= hunk->line_count ||
!(line = git_array_get(
patch->lines, hunk->line_start + line_of_hunk))) {
thing = "line";
goto notfound;
}
if (line_origin) *line_origin = line->origin;
if (content) *content = line->ptr;
if (content_len) *content_len = line->len;
if (old_lineno) *old_lineno = (int)line->oldno;
if (new_lineno) *new_lineno = (int)line->newno;
return 0;
notfound:
if (line_origin) *line_origin = GIT_DIFF_LINE_CONTEXT;
if (content) *content = NULL;
if (content_len) *content_len = 0;
if (old_lineno) *old_lineno = -1;
if (new_lineno) *new_lineno = -1;
return diff_error_outofrange(thing);
}
git_diff_list *git_diff_patch__diff(git_diff_patch *patch)
{
return patch->diff;
}
git_diff_driver *git_diff_patch__driver(git_diff_patch *patch)
{
/* ofile driver is representative for whole patch */
return patch->ofile.driver;
}
void git_diff_patch__old_data(
char **ptr, size_t *len, git_diff_patch *patch)
{
*ptr = patch->ofile.map.data;
*len = patch->ofile.map.len;
}
void git_diff_patch__new_data(
char **ptr, size_t *len, git_diff_patch *patch)
{
*ptr = patch->nfile.map.data;
*len = patch->nfile.map.len;
}
int git_diff_patch__invoke_callbacks(
git_diff_patch *patch,
git_diff_file_cb file_cb,
git_diff_hunk_cb hunk_cb,
git_diff_data_cb line_cb,
void *payload)
{
int error = 0;
uint32_t i, j;
if (file_cb)
error = file_cb(patch->delta, 0, payload);
if (!hunk_cb && !line_cb)
return error;
for (i = 0; !error && i < git_array_size(patch->hunks); ++i) {
diff_patch_hunk *h = git_array_get(patch->hunks, i);
error = hunk_cb(
patch->delta, &h->range, h->header, h->header_len, payload);
if (!line_cb)
continue;
for (j = 0; !error && j < h->line_count; ++j) {
diff_patch_line *l =
git_array_get(patch->lines, h->line_start + j);
error = line_cb(
patch->delta, &h->range, l->origin, l->ptr, l->len, payload);
}
}
return error;
}
static int diff_patch_file_cb(
const git_diff_delta *delta,
float progress,
void *payload)
{
GIT_UNUSED(delta); GIT_UNUSED(progress); GIT_UNUSED(payload);
return 0;
}
static int diff_patch_hunk_cb(
const git_diff_delta *delta,
const git_diff_range *range,
const char *header,
size_t header_len,
void *payload)
{
git_diff_patch *patch = payload;
diff_patch_hunk *hunk;
GIT_UNUSED(delta);
hunk = git_array_alloc(patch->hunks);
GITERR_CHECK_ALLOC(hunk);
memcpy(&hunk->range, range, sizeof(hunk->range));
assert(header_len + 1 < sizeof(hunk->header));
memcpy(&hunk->header, header, header_len);
hunk->header[header_len] = '\0';
hunk->header_len = header_len;
hunk->line_start = git_array_size(patch->lines);
hunk->line_count = 0;
patch->oldno = range->old_start;
patch->newno = range->new_start;
return 0;
}
static int diff_patch_line_cb(
const git_diff_delta *delta,
const git_diff_range *range,
char line_origin,
const char *content,
size_t content_len,
void *payload)
{
git_diff_patch *patch = payload;
diff_patch_hunk *hunk;
diff_patch_line *line;
GIT_UNUSED(delta);
GIT_UNUSED(range);
hunk = git_array_last(patch->hunks);
GITERR_CHECK_ALLOC(hunk);
line = git_array_alloc(patch->lines);
GITERR_CHECK_ALLOC(line);
line->ptr = content;
line->len = content_len;
line->origin = line_origin;
patch->content_size += content_len;
/* do some bookkeeping so we can provide old/new line numbers */
for (line->lines = 0; content_len > 0; --content_len) {
if (*content++ == '\n')
++line->lines;
}
switch (line_origin) {
case GIT_DIFF_LINE_ADDITION:
case GIT_DIFF_LINE_DEL_EOFNL:
line->oldno = -1;
line->newno = patch->newno;
patch->newno += line->lines;
break;
case GIT_DIFF_LINE_DELETION:
case GIT_DIFF_LINE_ADD_EOFNL:
line->oldno = patch->oldno;
line->newno = -1;
patch->oldno += line->lines;
break;
default:
line->oldno = patch->oldno;
line->newno = patch->newno;
patch->oldno += line->lines;
patch->newno += line->lines;
break;
}
hunk->line_count++;
return 0;
}
static void diff_output_init(
git_diff_output *out,
const git_diff_options *opts,
git_diff_file_cb file_cb,
git_diff_hunk_cb hunk_cb,
git_diff_data_cb data_cb,
void *payload)
{
GIT_UNUSED(opts);
memset(out, 0, sizeof(*out));
out->file_cb = file_cb;
out->hunk_cb = hunk_cb;
out->data_cb = data_cb;
out->payload = payload;
}
static void diff_output_to_patch(git_diff_output *out, git_diff_patch *patch)
{
diff_output_init(
out, NULL,
diff_patch_file_cb, diff_patch_hunk_cb, diff_patch_line_cb, patch);
}