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 #ifndef INCLUDE_git_diff_h__ 8 #define INCLUDE_git_diff_h__ 9 10 #include "common.h" 11 #include "types.h" 12 #include "oid.h" 13 #include "tree.h" 14 #include "refs.h" 15 16 /** 17 * @file git2/diff.h 18 * @brief Git tree and file differencing routines. 19 * @ingroup Git 20 * @{ 21 */ 22 GIT_BEGIN_DECL 23 24 /** 25 * Flags for diff options. A combination of these flags can be passed 26 * in via the `flags` value in the `git_diff_options`. 27 */ 28 typedef enum { 29 /** Normal diff, the default */ 30 GIT_DIFF_NORMAL = 0, 31 32 /* 33 * Options controlling which files will be in the diff 34 */ 35 36 /** Reverse the sides of the diff */ 37 GIT_DIFF_REVERSE = (1u << 0), 38 39 /** Include ignored files in the diff */ 40 GIT_DIFF_INCLUDE_IGNORED = (1u << 1), 41 42 /** Even with GIT_DIFF_INCLUDE_IGNORED, an entire ignored directory 43 * will be marked with only a single entry in the diff; this flag 44 * adds all files under the directory as IGNORED entries, too. 45 */ 46 GIT_DIFF_RECURSE_IGNORED_DIRS = (1u << 2), 47 48 /** Include untracked files in the diff */ 49 GIT_DIFF_INCLUDE_UNTRACKED = (1u << 3), 50 51 /** Even with GIT_DIFF_INCLUDE_UNTRACKED, an entire untracked 52 * directory will be marked with only a single entry in the diff 53 * (a la what core Git does in `git status`); this flag adds *all* 54 * files under untracked directories as UNTRACKED entries, too. 55 */ 56 GIT_DIFF_RECURSE_UNTRACKED_DIRS = (1u << 4), 57 58 /** Include unmodified files in the diff */ 59 GIT_DIFF_INCLUDE_UNMODIFIED = (1u << 5), 60 61 /** Normally, a type change between files will be converted into a 62 * DELETED record for the old and an ADDED record for the new; this 63 * options enabled the generation of TYPECHANGE delta records. 64 */ 65 GIT_DIFF_INCLUDE_TYPECHANGE = (1u << 6), 66 67 /** Even with GIT_DIFF_INCLUDE_TYPECHANGE, blob->tree changes still 68 * generally show as a DELETED blob. This flag tries to correctly 69 * label blob->tree transitions as TYPECHANGE records with new_file's 70 * mode set to tree. Note: the tree SHA will not be available. 71 */ 72 GIT_DIFF_INCLUDE_TYPECHANGE_TREES = (1u << 7), 73 74 /** Ignore file mode changes */ 75 GIT_DIFF_IGNORE_FILEMODE = (1u << 8), 76 77 /** Treat all submodules as unmodified */ 78 GIT_DIFF_IGNORE_SUBMODULES = (1u << 9), 79 80 /** Use case insensitive filename comparisons */ 81 GIT_DIFF_IGNORE_CASE = (1u << 10), 82 83 /** May be combined with `GIT_DIFF_IGNORE_CASE` to specify that a file 84 * that has changed case will be returned as an add/delete pair. 85 */ 86 GIT_DIFF_INCLUDE_CASECHANGE = (1u << 11), 87 88 /** If the pathspec is set in the diff options, this flags indicates 89 * that the paths will be treated as literal paths instead of 90 * fnmatch patterns. Each path in the list must either be a full 91 * path to a file or a directory. (A trailing slash indicates that 92 * the path will _only_ match a directory). If a directory is 93 * specified, all children will be included. 94 */ 95 GIT_DIFF_DISABLE_PATHSPEC_MATCH = (1u << 12), 96 97 /** Disable updating of the `binary` flag in delta records. This is 98 * useful when iterating over a diff if you don't need hunk and data 99 * callbacks and want to avoid having to load file completely. 100 */ 101 GIT_DIFF_SKIP_BINARY_CHECK = (1u << 13), 102 103 /** When diff finds an untracked directory, to match the behavior of 104 * core Git, it scans the contents for IGNORED and UNTRACKED files. 105 * If *all* contents are IGNORED, then the directory is IGNORED; if 106 * any contents are not IGNORED, then the directory is UNTRACKED. 107 * This is extra work that may not matter in many cases. This flag 108 * turns off that scan and immediately labels an untracked directory 109 * as UNTRACKED (changing the behavior to not match core Git). 110 */ 111 GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS = (1u << 14), 112 113 /** When diff finds a file in the working directory with stat 114 * information different from the index, but the OID ends up being the 115 * same, write the correct stat information into the index. Note: 116 * without this flag, diff will always leave the index untouched. 117 */ 118 GIT_DIFF_UPDATE_INDEX = (1u << 15), 119 120 /** Include unreadable files in the diff */ 121 GIT_DIFF_INCLUDE_UNREADABLE = (1u << 16), 122 123 /** Include unreadable files in the diff */ 124 GIT_DIFF_INCLUDE_UNREADABLE_AS_UNTRACKED = (1u << 17), 125 126 /* 127 * Options controlling how output will be generated 128 */ 129 130 /** Use a heuristic that takes indentation and whitespace into account 131 * which generally can produce better diffs when dealing with ambiguous 132 * diff hunks. 133 */ 134 GIT_DIFF_INDENT_HEURISTIC = (1u << 18), 135 136 /** Ignore blank lines */ 137 GIT_DIFF_IGNORE_BLANK_LINES = (1u << 19), 138 139 /** Treat all files as text, disabling binary attributes & detection */ 140 GIT_DIFF_FORCE_TEXT = (1u << 20), 141 /** Treat all files as binary, disabling text diffs */ 142 GIT_DIFF_FORCE_BINARY = (1u << 21), 143 144 /** Ignore all whitespace */ 145 GIT_DIFF_IGNORE_WHITESPACE = (1u << 22), 146 /** Ignore changes in amount of whitespace */ 147 GIT_DIFF_IGNORE_WHITESPACE_CHANGE = (1u << 23), 148 /** Ignore whitespace at end of line */ 149 GIT_DIFF_IGNORE_WHITESPACE_EOL = (1u << 24), 150 151 /** When generating patch text, include the content of untracked 152 * files. This automatically turns on GIT_DIFF_INCLUDE_UNTRACKED but 153 * it does not turn on GIT_DIFF_RECURSE_UNTRACKED_DIRS. Add that 154 * flag if you want the content of every single UNTRACKED file. 155 */ 156 GIT_DIFF_SHOW_UNTRACKED_CONTENT = (1u << 25), 157 158 /** When generating output, include the names of unmodified files if 159 * they are included in the git_diff. Normally these are skipped in 160 * the formats that list files (e.g. name-only, name-status, raw). 161 * Even with this, these will not be included in patch format. 162 */ 163 GIT_DIFF_SHOW_UNMODIFIED = (1u << 26), 164 165 /** Use the "patience diff" algorithm */ 166 GIT_DIFF_PATIENCE = (1u << 28), 167 /** Take extra time to find minimal diff */ 168 GIT_DIFF_MINIMAL = (1u << 29), 169 170 /** Include the necessary deflate / delta information so that `git-apply` 171 * can apply given diff information to binary files. 172 */ 173 GIT_DIFF_SHOW_BINARY = (1u << 30), 174 } git_diff_option_t; 175 176 /** 177 * The diff object that contains all individual file deltas. 178 * 179 * A `diff` represents the cumulative list of differences between two 180 * snapshots of a repository (possibly filtered by a set of file name 181 * patterns). 182 * 183 * Calculating diffs is generally done in two phases: building a list of 184 * diffs then traversing it. This makes is easier to share logic across 185 * the various types of diffs (tree vs tree, workdir vs index, etc.), and 186 * also allows you to insert optional diff post-processing phases, 187 * such as rename detection, in between the steps. When you are done with 188 * a diff object, it must be freed. 189 * 190 * This is an opaque structure which will be allocated by one of the diff 191 * generator functions below (such as `git_diff_tree_to_tree`). You are 192 * responsible for releasing the object memory when done, using the 193 * `git_diff_free()` function. 194 * 195 */ 196 typedef struct git_diff git_diff; 197 198 /** 199 * Flags for the delta object and the file objects on each side. 200 * 201 * These flags are used for both the `flags` value of the `git_diff_delta` 202 * and the flags for the `git_diff_file` objects representing the old and 203 * new sides of the delta. Values outside of this public range should be 204 * considered reserved for internal or future use. 205 */ 206 typedef enum { 207 GIT_DIFF_FLAG_BINARY = (1u << 0), /**< file(s) treated as binary data */ 208 GIT_DIFF_FLAG_NOT_BINARY = (1u << 1), /**< file(s) treated as text data */ 209 GIT_DIFF_FLAG_VALID_ID = (1u << 2), /**< `id` value is known correct */ 210 GIT_DIFF_FLAG_EXISTS = (1u << 3), /**< file exists at this side of the delta */ 211 } git_diff_flag_t; 212 213 /** 214 * What type of change is described by a git_diff_delta? 215 * 216 * `GIT_DELTA_RENAMED` and `GIT_DELTA_COPIED` will only show up if you run 217 * `git_diff_find_similar()` on the diff object. 218 * 219 * `GIT_DELTA_TYPECHANGE` only shows up given `GIT_DIFF_INCLUDE_TYPECHANGE` 220 * in the option flags (otherwise type changes will be split into ADDED / 221 * DELETED pairs). 222 */ 223 typedef enum { 224 GIT_DELTA_UNMODIFIED = 0, /**< no changes */ 225 GIT_DELTA_ADDED = 1, /**< entry does not exist in old version */ 226 GIT_DELTA_DELETED = 2, /**< entry does not exist in new version */ 227 GIT_DELTA_MODIFIED = 3, /**< entry content changed between old and new */ 228 GIT_DELTA_RENAMED = 4, /**< entry was renamed between old and new */ 229 GIT_DELTA_COPIED = 5, /**< entry was copied from another old entry */ 230 GIT_DELTA_IGNORED = 6, /**< entry is ignored item in workdir */ 231 GIT_DELTA_UNTRACKED = 7, /**< entry is untracked item in workdir */ 232 GIT_DELTA_TYPECHANGE = 8, /**< type of entry changed between old and new */ 233 GIT_DELTA_UNREADABLE = 9, /**< entry is unreadable */ 234 GIT_DELTA_CONFLICTED = 10, /**< entry in the index is conflicted */ 235 } git_delta_t; 236 237 /** 238 * Description of one side of a delta. 239 * 240 * Although this is called a "file", it could represent a file, a symbolic 241 * link, a submodule commit id, or even a tree (although that only if you 242 * are tracking type changes or ignored/untracked directories). 243 */ 244 typedef struct { 245 /** 246 * The `git_oid` of the item. If the entry represents an 247 * absent side of a diff (e.g. the `old_file` of a `GIT_DELTA_ADDED` delta), 248 * then the oid will be zeroes. 249 */ 250 git_oid id; 251 252 /** 253 * The NUL-terminated path to the entry relative to the working 254 * directory of the repository. 255 */ 256 const char *path; 257 258 /** 259 * The size of the entry in bytes. 260 */ 261 git_object_size_t size; 262 263 /** 264 * A combination of the `git_diff_flag_t` types 265 */ 266 uint32_t flags; 267 268 /** 269 * Roughly, the stat() `st_mode` value for the item. This will 270 * be restricted to one of the `git_filemode_t` values. 271 */ 272 uint16_t mode; 273 274 /** 275 * Represents the known length of the `id` field, when 276 * converted to a hex string. It is generally `GIT_OID_HEXSZ`, unless this 277 * delta was created from reading a patch file, in which case it may be 278 * abbreviated to something reasonable, like 7 characters. 279 */ 280 uint16_t id_abbrev; 281 } git_diff_file; 282 283 /** 284 * Description of changes to one entry. 285 * 286 * A `delta` is a file pair with an old and new revision. The old version 287 * may be absent if the file was just created and the new version may be 288 * absent if the file was deleted. A diff is mostly just a list of deltas. 289 * 290 * When iterating over a diff, this will be passed to most callbacks and 291 * you can use the contents to understand exactly what has changed. 292 * 293 * The `old_file` represents the "from" side of the diff and the `new_file` 294 * represents to "to" side of the diff. What those means depend on the 295 * function that was used to generate the diff and will be documented below. 296 * You can also use the `GIT_DIFF_REVERSE` flag to flip it around. 297 * 298 * Although the two sides of the delta are named "old_file" and "new_file", 299 * they actually may correspond to entries that represent a file, a symbolic 300 * link, a submodule commit id, or even a tree (if you are tracking type 301 * changes or ignored/untracked directories). 302 * 303 * Under some circumstances, in the name of efficiency, not all fields will 304 * be filled in, but we generally try to fill in as much as possible. One 305 * example is that the "flags" field may not have either the `BINARY` or the 306 * `NOT_BINARY` flag set to avoid examining file contents if you do not pass 307 * in hunk and/or line callbacks to the diff foreach iteration function. It 308 * will just use the git attributes for those files. 309 * 310 * The similarity score is zero unless you call `git_diff_find_similar()` 311 * which does a similarity analysis of files in the diff. Use that 312 * function to do rename and copy detection, and to split heavily modified 313 * files in add/delete pairs. After that call, deltas with a status of 314 * GIT_DELTA_RENAMED or GIT_DELTA_COPIED will have a similarity score 315 * between 0 and 100 indicating how similar the old and new sides are. 316 * 317 * If you ask `git_diff_find_similar` to find heavily modified files to 318 * break, but to not *actually* break the records, then GIT_DELTA_MODIFIED 319 * records may have a non-zero similarity score if the self-similarity is 320 * below the split threshold. To display this value like core Git, invert 321 * the score (a la `printf("M%03d", 100 - delta->similarity)`). 322 */ 323 typedef struct { 324 git_delta_t status; 325 uint32_t flags; /**< git_diff_flag_t values */ 326 uint16_t similarity; /**< for RENAMED and COPIED, value 0-100 */ 327 uint16_t nfiles; /**< number of files in this delta */ 328 git_diff_file old_file; 329 git_diff_file new_file; 330 } git_diff_delta; 331 332 /** 333 * Diff notification callback function. 334 * 335 * The callback will be called for each file, just before the `git_diff_delta` 336 * gets inserted into the diff. 337 * 338 * When the callback: 339 * - returns < 0, the diff process will be aborted. 340 * - returns > 0, the delta will not be inserted into the diff, but the 341 * diff process continues. 342 * - returns 0, the delta is inserted into the diff, and the diff process 343 * continues. 344 */ 345 typedef int GIT_CALLBACK(git_diff_notify_cb)( 346 const git_diff *diff_so_far, 347 const git_diff_delta *delta_to_add, 348 const char *matched_pathspec, 349 void *payload); 350 351 /** 352 * Diff progress callback. 353 * 354 * Called before each file comparison. 355 * 356 * @param diff_so_far The diff being generated. 357 * @param old_path The path to the old file or NULL. 358 * @param new_path The path to the new file or NULL. 359 * @return Non-zero to abort the diff. 360 */ 361 typedef int GIT_CALLBACK(git_diff_progress_cb)( 362 const git_diff *diff_so_far, 363 const char *old_path, 364 const char *new_path, 365 void *payload); 366 367 /** 368 * Structure describing options about how the diff should be executed. 369 * 370 * Setting all values of the structure to zero will yield the default 371 * values. Similarly, passing NULL for the options structure will 372 * give the defaults. The default values are marked below. 373 * 374 */ 375 typedef struct { 376 unsigned int version; /**< version for the struct */ 377 378 /** 379 * A combination of `git_diff_option_t` values above. 380 * Defaults to GIT_DIFF_NORMAL 381 */ 382 uint32_t flags; 383 384 /* options controlling which files are in the diff */ 385 386 /** Overrides the submodule ignore setting for all submodules in the diff. */ 387 git_submodule_ignore_t ignore_submodules; 388 389 /** 390 * An array of paths / fnmatch patterns to constrain diff. 391 * All paths are included by default. 392 */ 393 git_strarray pathspec; 394 395 /** 396 * An optional callback function, notifying the consumer of changes to 397 * the diff as new deltas are added. 398 */ 399 git_diff_notify_cb notify_cb; 400 401 /** 402 * An optional callback function, notifying the consumer of which files 403 * are being examined as the diff is generated. 404 */ 405 git_diff_progress_cb progress_cb; 406 407 /** The payload to pass to the callback functions. */ 408 void *payload; 409 410 /* options controlling how to diff text is generated */ 411 412 /** 413 * The number of unchanged lines that define the boundary of a hunk 414 * (and to display before and after). Defaults to 3. 415 */ 416 uint32_t context_lines; 417 /** 418 * The maximum number of unchanged lines between hunk boundaries before 419 * the hunks will be merged into one. Defaults to 0. 420 */ 421 uint32_t interhunk_lines; 422 423 /** 424 * The abbreviation length to use when formatting object ids. 425 * Defaults to the value of 'core.abbrev' from the config, or 7 if unset. 426 */ 427 uint16_t id_abbrev; 428 429 /** 430 * A size (in bytes) above which a blob will be marked as binary 431 * automatically; pass a negative value to disable. 432 * Defaults to 512MB. 433 */ 434 git_off_t max_size; 435 436 /** 437 * The virtual "directory" prefix for old file names in hunk headers. 438 * Default is "a". 439 */ 440 const char *old_prefix; 441 442 /** 443 * The virtual "directory" prefix for new file names in hunk headers. 444 * Defaults to "b". 445 */ 446 const char *new_prefix; 447 } git_diff_options; 448 449 /* The current version of the diff options structure */ 450 #define GIT_DIFF_OPTIONS_VERSION 1 451 452 /* Stack initializer for diff options. Alternatively use 453 * `git_diff_options_init` programmatic initialization. 454 */ 455 #define GIT_DIFF_OPTIONS_INIT \ 456 {GIT_DIFF_OPTIONS_VERSION, 0, GIT_SUBMODULE_IGNORE_UNSPECIFIED, {NULL,0}, NULL, NULL, NULL, 3} 457 458 /** 459 * Initialize git_diff_options structure 460 * 461 * Initializes a `git_diff_options` with default values. Equivalent to creating 462 * an instance with GIT_DIFF_OPTIONS_INIT. 463 * 464 * @param opts The `git_diff_options` struct to initialize. 465 * @param version The struct version; pass `GIT_DIFF_OPTIONS_VERSION`. 466 * @return Zero on success; -1 on failure. 467 */ 468 GIT_EXTERN(int) git_diff_options_init( 469 git_diff_options *opts, 470 unsigned int version); 471 472 /** 473 * When iterating over a diff, callback that will be made per file. 474 * 475 * @param delta A pointer to the delta data for the file 476 * @param progress Goes from 0 to 1 over the diff 477 * @param payload User-specified pointer from foreach function 478 */ 479 typedef int GIT_CALLBACK(git_diff_file_cb)( 480 const git_diff_delta *delta, 481 float progress, 482 void *payload); 483 484 #define GIT_DIFF_HUNK_HEADER_SIZE 128 485 486 /** 487 * When producing a binary diff, the binary data returned will be 488 * either the deflated full ("literal") contents of the file, or 489 * the deflated binary delta between the two sides (whichever is 490 * smaller). 491 */ 492 typedef enum { 493 /** There is no binary delta. */ 494 GIT_DIFF_BINARY_NONE, 495 496 /** The binary data is the literal contents of the file. */ 497 GIT_DIFF_BINARY_LITERAL, 498 499 /** The binary data is the delta from one side to the other. */ 500 GIT_DIFF_BINARY_DELTA, 501 } git_diff_binary_t; 502 503 /** The contents of one of the files in a binary diff. */ 504 typedef struct { 505 /** The type of binary data for this file. */ 506 git_diff_binary_t type; 507 508 /** The binary data, deflated. */ 509 const char *data; 510 511 /** The length of the binary data. */ 512 size_t datalen; 513 514 /** The length of the binary data after inflation. */ 515 size_t inflatedlen; 516 } git_diff_binary_file; 517 518 /** 519 * Structure describing the binary contents of a diff. 520 * 521 * A `binary` file / delta is a file (or pair) for which no text diffs 522 * should be generated. A diff can contain delta entries that are 523 * binary, but no diff content will be output for those files. There is 524 * a base heuristic for binary detection and you can further tune the 525 * behavior with git attributes or diff flags and option settings. 526 */ 527 typedef struct { 528 /** 529 * Whether there is data in this binary structure or not. 530 * 531 * If this is `1`, then this was produced and included binary content. 532 * If this is `0` then this was generated knowing only that a binary 533 * file changed but without providing the data, probably from a patch 534 * that said `Binary files a/file.txt and b/file.txt differ`. 535 */ 536 unsigned int contains_data; 537 git_diff_binary_file old_file; /**< The contents of the old file. */ 538 git_diff_binary_file new_file; /**< The contents of the new file. */ 539 } git_diff_binary; 540 541 /** 542 * When iterating over a diff, callback that will be made for 543 * binary content within the diff. 544 */ 545 typedef int GIT_CALLBACK(git_diff_binary_cb)( 546 const git_diff_delta *delta, 547 const git_diff_binary *binary, 548 void *payload); 549 550 /** 551 * Structure describing a hunk of a diff. 552 * 553 * A `hunk` is a span of modified lines in a delta along with some stable 554 * surrounding context. You can configure the amount of context and other 555 * properties of how hunks are generated. Each hunk also comes with a 556 * header that described where it starts and ends in both the old and new 557 * versions in the delta. 558 */ 559 typedef struct { 560 int old_start; /**< Starting line number in old_file */ 561 int old_lines; /**< Number of lines in old_file */ 562 int new_start; /**< Starting line number in new_file */ 563 int new_lines; /**< Number of lines in new_file */ 564 size_t header_len; /**< Number of bytes in header text */ 565 char header[GIT_DIFF_HUNK_HEADER_SIZE]; /**< Header text, NUL-byte terminated */ 566 } git_diff_hunk; 567 568 /** 569 * When iterating over a diff, callback that will be made per hunk. 570 */ 571 typedef int GIT_CALLBACK(git_diff_hunk_cb)( 572 const git_diff_delta *delta, 573 const git_diff_hunk *hunk, 574 void *payload); 575 576 /** 577 * Line origin constants. 578 * 579 * These values describe where a line came from and will be passed to 580 * the git_diff_line_cb when iterating over a diff. There are some 581 * special origin constants at the end that are used for the text 582 * output callbacks to demarcate lines that are actually part of 583 * the file or hunk headers. 584 */ 585 typedef enum { 586 /* These values will be sent to `git_diff_line_cb` along with the line */ 587 GIT_DIFF_LINE_CONTEXT = ' ', 588 GIT_DIFF_LINE_ADDITION = '+', 589 GIT_DIFF_LINE_DELETION = '-', 590 591 GIT_DIFF_LINE_CONTEXT_EOFNL = '=', /**< Both files have no LF at end */ 592 GIT_DIFF_LINE_ADD_EOFNL = '>', /**< Old has no LF at end, new does */ 593 GIT_DIFF_LINE_DEL_EOFNL = '<', /**< Old has LF at end, new does not */ 594 595 /* The following values will only be sent to a `git_diff_line_cb` when 596 * the content of a diff is being formatted through `git_diff_print`. 597 */ 598 GIT_DIFF_LINE_FILE_HDR = 'F', 599 GIT_DIFF_LINE_HUNK_HDR = 'H', 600 GIT_DIFF_LINE_BINARY = 'B' /**< For "Binary files x and y differ" */ 601 } git_diff_line_t; 602 603 /** 604 * Structure describing a line (or data span) of a diff. 605 * 606 * A `line` is a range of characters inside a hunk. It could be a context 607 * line (i.e. in both old and new versions), an added line (i.e. only in 608 * the new version), or a removed line (i.e. only in the old version). 609 * Unfortunately, we don't know anything about the encoding of data in the 610 * file being diffed, so we cannot tell you much about the line content. 611 * Line data will not be NUL-byte terminated, however, because it will be 612 * just a span of bytes inside the larger file. 613 */ 614 typedef struct { 615 char origin; /**< A git_diff_line_t value */ 616 int old_lineno; /**< Line number in old file or -1 for added line */ 617 int new_lineno; /**< Line number in new file or -1 for deleted line */ 618 int num_lines; /**< Number of newline characters in content */ 619 size_t content_len; /**< Number of bytes of data */ 620 git_off_t content_offset; /**< Offset in the original file to the content */ 621 const char *content; /**< Pointer to diff text, not NUL-byte terminated */ 622 } git_diff_line; 623 624 /** 625 * When iterating over a diff, callback that will be made per text diff 626 * line. In this context, the provided range will be NULL. 627 * 628 * When printing a diff, callback that will be made to output each line 629 * of text. This uses some extra GIT_DIFF_LINE_... constants for output 630 * of lines of file and hunk headers. 631 */ 632 typedef int GIT_CALLBACK(git_diff_line_cb)( 633 const git_diff_delta *delta, /**< delta that contains this data */ 634 const git_diff_hunk *hunk, /**< hunk containing this data */ 635 const git_diff_line *line, /**< line data */ 636 void *payload); /**< user reference data */ 637 638 /** 639 * Flags to control the behavior of diff rename/copy detection. 640 */ 641 typedef enum { 642 /** Obey `diff.renames`. Overridden by any other GIT_DIFF_FIND_... flag. */ 643 GIT_DIFF_FIND_BY_CONFIG = 0, 644 645 /** Look for renames? (`--find-renames`) */ 646 GIT_DIFF_FIND_RENAMES = (1u << 0), 647 648 /** Consider old side of MODIFIED for renames? (`--break-rewrites=N`) */ 649 GIT_DIFF_FIND_RENAMES_FROM_REWRITES = (1u << 1), 650 651 /** Look for copies? (a la `--find-copies`). */ 652 GIT_DIFF_FIND_COPIES = (1u << 2), 653 654 /** Consider UNMODIFIED as copy sources? (`--find-copies-harder`). 655 * 656 * For this to work correctly, use GIT_DIFF_INCLUDE_UNMODIFIED when 657 * the initial `git_diff` is being generated. 658 */ 659 GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED = (1u << 3), 660 661 /** Mark significant rewrites for split (`--break-rewrites=/M`) */ 662 GIT_DIFF_FIND_REWRITES = (1u << 4), 663 /** Actually split large rewrites into delete/add pairs */ 664 GIT_DIFF_BREAK_REWRITES = (1u << 5), 665 /** Mark rewrites for split and break into delete/add pairs */ 666 GIT_DIFF_FIND_AND_BREAK_REWRITES = 667 (GIT_DIFF_FIND_REWRITES | GIT_DIFF_BREAK_REWRITES), 668 669 /** Find renames/copies for UNTRACKED items in working directory. 670 * 671 * For this to work correctly, use GIT_DIFF_INCLUDE_UNTRACKED when the 672 * initial `git_diff` is being generated (and obviously the diff must 673 * be against the working directory for this to make sense). 674 */ 675 GIT_DIFF_FIND_FOR_UNTRACKED = (1u << 6), 676 677 /** Turn on all finding features. */ 678 GIT_DIFF_FIND_ALL = (0x0ff), 679 680 /** Measure similarity ignoring leading whitespace (default) */ 681 GIT_DIFF_FIND_IGNORE_LEADING_WHITESPACE = 0, 682 /** Measure similarity ignoring all whitespace */ 683 GIT_DIFF_FIND_IGNORE_WHITESPACE = (1u << 12), 684 /** Measure similarity including all data */ 685 GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE = (1u << 13), 686 /** Measure similarity only by comparing SHAs (fast and cheap) */ 687 GIT_DIFF_FIND_EXACT_MATCH_ONLY = (1u << 14), 688 689 /** Do not break rewrites unless they contribute to a rename. 690 * 691 * Normally, GIT_DIFF_FIND_AND_BREAK_REWRITES will measure the self- 692 * similarity of modified files and split the ones that have changed a 693 * lot into a DELETE / ADD pair. Then the sides of that pair will be 694 * considered candidates for rename and copy detection. 695 * 696 * If you add this flag in and the split pair is *not* used for an 697 * actual rename or copy, then the modified record will be restored to 698 * a regular MODIFIED record instead of being split. 699 */ 700 GIT_DIFF_BREAK_REWRITES_FOR_RENAMES_ONLY = (1u << 15), 701 702 /** Remove any UNMODIFIED deltas after find_similar is done. 703 * 704 * Using GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED to emulate the 705 * --find-copies-harder behavior requires building a diff with the 706 * GIT_DIFF_INCLUDE_UNMODIFIED flag. If you do not want UNMODIFIED 707 * records in the final result, pass this flag to have them removed. 708 */ 709 GIT_DIFF_FIND_REMOVE_UNMODIFIED = (1u << 16), 710 } git_diff_find_t; 711 712 /** 713 * Pluggable similarity metric 714 */ 715 typedef struct { 716 int GIT_CALLBACK(file_signature)( 717 void **out, const git_diff_file *file, 718 const char *fullpath, void *payload); 719 int GIT_CALLBACK(buffer_signature)( 720 void **out, const git_diff_file *file, 721 const char *buf, size_t buflen, void *payload); 722 void GIT_CALLBACK(free_signature)(void *sig, void *payload); 723 int GIT_CALLBACK(similarity)(int *score, void *siga, void *sigb, void *payload); 724 void *payload; 725 } git_diff_similarity_metric; 726 727 /** 728 * Control behavior of rename and copy detection 729 * 730 * These options mostly mimic parameters that can be passed to git-diff. 731 */ 732 typedef struct { 733 unsigned int version; 734 735 /** 736 * Combination of git_diff_find_t values (default GIT_DIFF_FIND_BY_CONFIG). 737 * NOTE: if you don't explicitly set this, `diff.renames` could be set 738 * to false, resulting in `git_diff_find_similar` doing nothing. 739 */ 740 uint32_t flags; 741 742 /** 743 * Threshold above which similar files will be considered renames. 744 * This is equivalent to the -M option. Defaults to 50. 745 */ 746 uint16_t rename_threshold; 747 748 /** 749 * Threshold below which similar files will be eligible to be a rename source. 750 * This is equivalent to the first part of the -B option. Defaults to 50. 751 */ 752 uint16_t rename_from_rewrite_threshold; 753 754 /** 755 * Threshold above which similar files will be considered copies. 756 * This is equivalent to the -C option. Defaults to 50. 757 */ 758 uint16_t copy_threshold; 759 760 /** 761 * Treshold below which similar files will be split into a delete/add pair. 762 * This is equivalent to the last part of the -B option. Defaults to 60. 763 */ 764 uint16_t break_rewrite_threshold; 765 766 /** 767 * Maximum number of matches to consider for a particular file. 768 * 769 * This is a little different from the `-l` option from Git because we 770 * will still process up to this many matches before abandoning the search. 771 * Defaults to 200. 772 */ 773 size_t rename_limit; 774 775 /** 776 * The `metric` option allows you to plug in a custom similarity metric. 777 * 778 * Set it to NULL to use the default internal metric. 779 * 780 * The default metric is based on sampling hashes of ranges of data in 781 * the file, which is a pretty good similarity approximation that should 782 * work fairly well for both text and binary data while still being 783 * pretty fast with a fixed memory overhead. 784 */ 785 git_diff_similarity_metric *metric; 786 } git_diff_find_options; 787 788 #define GIT_DIFF_FIND_OPTIONS_VERSION 1 789 #define GIT_DIFF_FIND_OPTIONS_INIT {GIT_DIFF_FIND_OPTIONS_VERSION} 790 791 /** 792 * Initialize git_diff_find_options structure 793 * 794 * Initializes a `git_diff_find_options` with default values. Equivalent to creating 795 * an instance with GIT_DIFF_FIND_OPTIONS_INIT. 796 * 797 * @param opts The `git_diff_find_options` struct to initialize. 798 * @param version The struct version; pass `GIT_DIFF_FIND_OPTIONS_VERSION`. 799 * @return Zero on success; -1 on failure. 800 */ 801 GIT_EXTERN(int) git_diff_find_options_init( 802 git_diff_find_options *opts, 803 unsigned int version); 804 805 /** @name Diff Generator Functions 806 * 807 * These are the functions you would use to create (or destroy) a 808 * git_diff from various objects in a repository. 809 */ 810 /**@{*/ 811 812 /** 813 * Deallocate a diff. 814 * 815 * @param diff The previously created diff; cannot be used after free. 816 */ 817 GIT_EXTERN(void) git_diff_free(git_diff *diff); 818 819 /** 820 * Create a diff with the difference between two tree objects. 821 * 822 * This is equivalent to `git diff <old-tree> <new-tree>` 823 * 824 * The first tree will be used for the "old_file" side of the delta and the 825 * second tree will be used for the "new_file" side of the delta. You can 826 * pass NULL to indicate an empty tree, although it is an error to pass 827 * NULL for both the `old_tree` and `new_tree`. 828 * 829 * @param diff Output pointer to a git_diff pointer to be allocated. 830 * @param repo The repository containing the trees. 831 * @param old_tree A git_tree object to diff from, or NULL for empty tree. 832 * @param new_tree A git_tree object to diff to, or NULL for empty tree. 833 * @param opts Structure with options to influence diff or NULL for defaults. 834 */ 835 GIT_EXTERN(int) git_diff_tree_to_tree( 836 git_diff **diff, 837 git_repository *repo, 838 git_tree *old_tree, 839 git_tree *new_tree, 840 const git_diff_options *opts); 841 842 /** 843 * Create a diff between a tree and repository index. 844 * 845 * This is equivalent to `git diff --cached <treeish>` or if you pass 846 * the HEAD tree, then like `git diff --cached`. 847 * 848 * The tree you pass will be used for the "old_file" side of the delta, and 849 * the index will be used for the "new_file" side of the delta. 850 * 851 * If you pass NULL for the index, then the existing index of the `repo` 852 * will be used. In this case, the index will be refreshed from disk 853 * (if it has changed) before the diff is generated. 854 * 855 * @param diff Output pointer to a git_diff pointer to be allocated. 856 * @param repo The repository containing the tree and index. 857 * @param old_tree A git_tree object to diff from, or NULL for empty tree. 858 * @param index The index to diff with; repo index used if NULL. 859 * @param opts Structure with options to influence diff or NULL for defaults. 860 */ 861 GIT_EXTERN(int) git_diff_tree_to_index( 862 git_diff **diff, 863 git_repository *repo, 864 git_tree *old_tree, 865 git_index *index, 866 const git_diff_options *opts); 867 868 /** 869 * Create a diff between the repository index and the workdir directory. 870 * 871 * This matches the `git diff` command. See the note below on 872 * `git_diff_tree_to_workdir` for a discussion of the difference between 873 * `git diff` and `git diff HEAD` and how to emulate a `git diff <treeish>` 874 * using libgit2. 875 * 876 * The index will be used for the "old_file" side of the delta, and the 877 * working directory will be used for the "new_file" side of the delta. 878 * 879 * If you pass NULL for the index, then the existing index of the `repo` 880 * will be used. In this case, the index will be refreshed from disk 881 * (if it has changed) before the diff is generated. 882 * 883 * @param diff Output pointer to a git_diff pointer to be allocated. 884 * @param repo The repository. 885 * @param index The index to diff from; repo index used if NULL. 886 * @param opts Structure with options to influence diff or NULL for defaults. 887 */ 888 GIT_EXTERN(int) git_diff_index_to_workdir( 889 git_diff **diff, 890 git_repository *repo, 891 git_index *index, 892 const git_diff_options *opts); 893 894 /** 895 * Create a diff between a tree and the working directory. 896 * 897 * The tree you provide will be used for the "old_file" side of the delta, 898 * and the working directory will be used for the "new_file" side. 899 * 900 * This is not the same as `git diff <treeish>` or `git diff-index 901 * <treeish>`. Those commands use information from the index, whereas this 902 * function strictly returns the differences between the tree and the files 903 * in the working directory, regardless of the state of the index. Use 904 * `git_diff_tree_to_workdir_with_index` to emulate those commands. 905 * 906 * To see difference between this and `git_diff_tree_to_workdir_with_index`, 907 * consider the example of a staged file deletion where the file has then 908 * been put back into the working dir and further modified. The 909 * tree-to-workdir diff for that file is 'modified', but `git diff` would 910 * show status 'deleted' since there is a staged delete. 911 * 912 * @param diff A pointer to a git_diff pointer that will be allocated. 913 * @param repo The repository containing the tree. 914 * @param old_tree A git_tree object to diff from, or NULL for empty tree. 915 * @param opts Structure with options to influence diff or NULL for defaults. 916 */ 917 GIT_EXTERN(int) git_diff_tree_to_workdir( 918 git_diff **diff, 919 git_repository *repo, 920 git_tree *old_tree, 921 const git_diff_options *opts); 922 923 /** 924 * Create a diff between a tree and the working directory using index data 925 * to account for staged deletes, tracked files, etc. 926 * 927 * This emulates `git diff <tree>` by diffing the tree to the index and 928 * the index to the working directory and blending the results into a 929 * single diff that includes staged deleted, etc. 930 * 931 * @param diff A pointer to a git_diff pointer that will be allocated. 932 * @param repo The repository containing the tree. 933 * @param old_tree A git_tree object to diff from, or NULL for empty tree. 934 * @param opts Structure with options to influence diff or NULL for defaults. 935 */ 936 GIT_EXTERN(int) git_diff_tree_to_workdir_with_index( 937 git_diff **diff, 938 git_repository *repo, 939 git_tree *old_tree, 940 const git_diff_options *opts); 941 942 /** 943 * Create a diff with the difference between two index objects. 944 * 945 * The first index will be used for the "old_file" side of the delta and the 946 * second index will be used for the "new_file" side of the delta. 947 * 948 * @param diff Output pointer to a git_diff pointer to be allocated. 949 * @param repo The repository containing the indexes. 950 * @param old_index A git_index object to diff from. 951 * @param new_index A git_index object to diff to. 952 * @param opts Structure with options to influence diff or NULL for defaults. 953 */ 954 GIT_EXTERN(int) git_diff_index_to_index( 955 git_diff **diff, 956 git_repository *repo, 957 git_index *old_index, 958 git_index *new_index, 959 const git_diff_options *opts); 960 961 /** 962 * Merge one diff into another. 963 * 964 * This merges items from the "from" list into the "onto" list. The 965 * resulting diff will have all items that appear in either list. 966 * If an item appears in both lists, then it will be "merged" to appear 967 * as if the old version was from the "onto" list and the new version 968 * is from the "from" list (with the exception that if the item has a 969 * pending DELETE in the middle, then it will show as deleted). 970 * 971 * @param onto Diff to merge into. 972 * @param from Diff to merge. 973 */ 974 GIT_EXTERN(int) git_diff_merge( 975 git_diff *onto, 976 const git_diff *from); 977 978 /** 979 * Transform a diff marking file renames, copies, etc. 980 * 981 * This modifies a diff in place, replacing old entries that look 982 * like renames or copies with new entries reflecting those changes. 983 * This also will, if requested, break modified files into add/remove 984 * pairs if the amount of change is above a threshold. 985 * 986 * @param diff diff to run detection algorithms on 987 * @param options Control how detection should be run, NULL for defaults 988 * @return 0 on success, -1 on failure 989 */ 990 GIT_EXTERN(int) git_diff_find_similar( 991 git_diff *diff, 992 const git_diff_find_options *options); 993 994 /**@}*/ 995 996 997 /** @name Diff Processor Functions 998 * 999 * These are the functions you apply to a diff to process it 1000 * or read it in some way. 1001 */ 1002 /**@{*/ 1003 1004 /** 1005 * Query how many diff records are there in a diff. 1006 * 1007 * @param diff A git_diff generated by one of the above functions 1008 * @return Count of number of deltas in the list 1009 */ 1010 GIT_EXTERN(size_t) git_diff_num_deltas(const git_diff *diff); 1011 1012 /** 1013 * Query how many diff deltas are there in a diff filtered by type. 1014 * 1015 * This works just like `git_diff_num_deltas()` with an extra parameter 1016 * that is a `git_delta_t` and returns just the count of how many deltas 1017 * match that particular type. 1018 * 1019 * @param diff A git_diff generated by one of the above functions 1020 * @param type A git_delta_t value to filter the count 1021 * @return Count of number of deltas matching delta_t type 1022 */ 1023 GIT_EXTERN(size_t) git_diff_num_deltas_of_type( 1024 const git_diff *diff, git_delta_t type); 1025 1026 /** 1027 * Return the diff delta for an entry in the diff list. 1028 * 1029 * The `git_diff_delta` pointer points to internal data and you do not 1030 * have to release it when you are done with it. It will go away when 1031 * the * `git_diff` (or any associated `git_patch`) goes away. 1032 * 1033 * Note that the flags on the delta related to whether it has binary 1034 * content or not may not be set if there are no attributes set for the 1035 * file and there has been no reason to load the file data at this point. 1036 * For now, if you need those flags to be up to date, your only option is 1037 * to either use `git_diff_foreach` or create a `git_patch`. 1038 * 1039 * @param diff Diff list object 1040 * @param idx Index into diff list 1041 * @return Pointer to git_diff_delta (or NULL if `idx` out of range) 1042 */ 1043 GIT_EXTERN(const git_diff_delta *) git_diff_get_delta( 1044 const git_diff *diff, size_t idx); 1045 1046 /** 1047 * Check if deltas are sorted case sensitively or insensitively. 1048 * 1049 * @param diff diff to check 1050 * @return 0 if case sensitive, 1 if case is ignored 1051 */ 1052 GIT_EXTERN(int) git_diff_is_sorted_icase(const git_diff *diff); 1053 1054 /** 1055 * Loop over all deltas in a diff issuing callbacks. 1056 * 1057 * This will iterate through all of the files described in a diff. You 1058 * should provide a file callback to learn about each file. 1059 * 1060 * The "hunk" and "line" callbacks are optional, and the text diff of the 1061 * files will only be calculated if they are not NULL. Of course, these 1062 * callbacks will not be invoked for binary files on the diff or for 1063 * files whose only changed is a file mode change. 1064 * 1065 * Returning a non-zero value from any of the callbacks will terminate 1066 * the iteration and return the value to the user. 1067 * 1068 * @param diff A git_diff generated by one of the above functions. 1069 * @param file_cb Callback function to make per file in the diff. 1070 * @param binary_cb Optional callback to make for binary files. 1071 * @param hunk_cb Optional callback to make per hunk of text diff. This 1072 * callback is called to describe a range of lines in the 1073 * diff. It will not be issued for binary files. 1074 * @param line_cb Optional callback to make per line of diff text. This 1075 * same callback will be made for context lines, added, and 1076 * removed lines, and even for a deleted trailing newline. 1077 * @param payload Reference pointer that will be passed to your callbacks. 1078 * @return 0 on success, non-zero callback return value, or error code 1079 */ 1080 GIT_EXTERN(int) git_diff_foreach( 1081 git_diff *diff, 1082 git_diff_file_cb file_cb, 1083 git_diff_binary_cb binary_cb, 1084 git_diff_hunk_cb hunk_cb, 1085 git_diff_line_cb line_cb, 1086 void *payload); 1087 1088 /** 1089 * Look up the single character abbreviation for a delta status code. 1090 * 1091 * When you run `git diff --name-status` it uses single letter codes in 1092 * the output such as 'A' for added, 'D' for deleted, 'M' for modified, 1093 * etc. This function converts a git_delta_t value into these letters for 1094 * your own purposes. GIT_DELTA_UNTRACKED will return a space (i.e. ' '). 1095 * 1096 * @param status The git_delta_t value to look up 1097 * @return The single character label for that code 1098 */ 1099 GIT_EXTERN(char) git_diff_status_char(git_delta_t status); 1100 1101 /** 1102 * Possible output formats for diff data 1103 */ 1104 typedef enum { 1105 GIT_DIFF_FORMAT_PATCH = 1u, /**< full git diff */ 1106 GIT_DIFF_FORMAT_PATCH_HEADER = 2u, /**< just the file headers of patch */ 1107 GIT_DIFF_FORMAT_RAW = 3u, /**< like git diff --raw */ 1108 GIT_DIFF_FORMAT_NAME_ONLY = 4u, /**< like git diff --name-only */ 1109 GIT_DIFF_FORMAT_NAME_STATUS = 5u, /**< like git diff --name-status */ 1110 GIT_DIFF_FORMAT_PATCH_ID = 6u, /**< git diff as used by git patch-id */ 1111 } git_diff_format_t; 1112 1113 /** 1114 * Iterate over a diff generating formatted text output. 1115 * 1116 * Returning a non-zero value from the callbacks will terminate the 1117 * iteration and return the non-zero value to the caller. 1118 * 1119 * @param diff A git_diff generated by one of the above functions. 1120 * @param format A git_diff_format_t value to pick the text format. 1121 * @param print_cb Callback to make per line of diff text. 1122 * @param payload Reference pointer that will be passed to your callback. 1123 * @return 0 on success, non-zero callback return value, or error code 1124 */ 1125 GIT_EXTERN(int) git_diff_print( 1126 git_diff *diff, 1127 git_diff_format_t format, 1128 git_diff_line_cb print_cb, 1129 void *payload); 1130 1131 /** 1132 * Produce the complete formatted text output from a diff into a 1133 * buffer. 1134 * 1135 * @param out A pointer to a user-allocated git_buf that will 1136 * contain the diff text 1137 * @param diff A git_diff generated by one of the above functions. 1138 * @param format A git_diff_format_t value to pick the text format. 1139 * @return 0 on success or error code 1140 */ 1141 GIT_EXTERN(int) git_diff_to_buf( 1142 git_buf *out, 1143 git_diff *diff, 1144 git_diff_format_t format); 1145 1146 /**@}*/ 1147 1148 1149 /* 1150 * Misc 1151 */ 1152 1153 /** 1154 * Directly run a diff on two blobs. 1155 * 1156 * Compared to a file, a blob lacks some contextual information. As such, 1157 * the `git_diff_file` given to the callback will have some fake data; i.e. 1158 * `mode` will be 0 and `path` will be NULL. 1159 * 1160 * NULL is allowed for either `old_blob` or `new_blob` and will be treated 1161 * as an empty blob, with the `oid` set to NULL in the `git_diff_file` data. 1162 * Passing NULL for both blobs is a noop; no callbacks will be made at all. 1163 * 1164 * We do run a binary content check on the blob content and if either blob 1165 * looks like binary data, the `git_diff_delta` binary attribute will be set 1166 * to 1 and no call to the hunk_cb nor line_cb will be made (unless you pass 1167 * `GIT_DIFF_FORCE_TEXT` of course). 1168 * 1169 * @param old_blob Blob for old side of diff, or NULL for empty blob 1170 * @param old_as_path Treat old blob as if it had this filename; can be NULL 1171 * @param new_blob Blob for new side of diff, or NULL for empty blob 1172 * @param new_as_path Treat new blob as if it had this filename; can be NULL 1173 * @param options Options for diff, or NULL for default options 1174 * @param file_cb Callback for "file"; made once if there is a diff; can be NULL 1175 * @param binary_cb Callback for binary files; can be NULL 1176 * @param hunk_cb Callback for each hunk in diff; can be NULL 1177 * @param line_cb Callback for each line in diff; can be NULL 1178 * @param payload Payload passed to each callback function 1179 * @return 0 on success, non-zero callback return value, or error code 1180 */ 1181 GIT_EXTERN(int) git_diff_blobs( 1182 const git_blob *old_blob, 1183 const char *old_as_path, 1184 const git_blob *new_blob, 1185 const char *new_as_path, 1186 const git_diff_options *options, 1187 git_diff_file_cb file_cb, 1188 git_diff_binary_cb binary_cb, 1189 git_diff_hunk_cb hunk_cb, 1190 git_diff_line_cb line_cb, 1191 void *payload); 1192 1193 /** 1194 * Directly run a diff between a blob and a buffer. 1195 * 1196 * As with `git_diff_blobs`, comparing a blob and buffer lacks some context, 1197 * so the `git_diff_file` parameters to the callbacks will be faked a la the 1198 * rules for `git_diff_blobs()`. 1199 * 1200 * Passing NULL for `old_blob` will be treated as an empty blob (i.e. the 1201 * `file_cb` will be invoked with GIT_DELTA_ADDED and the diff will be the 1202 * entire content of the buffer added). Passing NULL to the buffer will do 1203 * the reverse, with GIT_DELTA_REMOVED and blob content removed. 1204 * 1205 * @param old_blob Blob for old side of diff, or NULL for empty blob 1206 * @param old_as_path Treat old blob as if it had this filename; can be NULL 1207 * @param buffer Raw data for new side of diff, or NULL for empty 1208 * @param buffer_len Length of raw data for new side of diff 1209 * @param buffer_as_path Treat buffer as if it had this filename; can be NULL 1210 * @param options Options for diff, or NULL for default options 1211 * @param file_cb Callback for "file"; made once if there is a diff; can be NULL 1212 * @param binary_cb Callback for binary files; can be NULL 1213 * @param hunk_cb Callback for each hunk in diff; can be NULL 1214 * @param line_cb Callback for each line in diff; can be NULL 1215 * @param payload Payload passed to each callback function 1216 * @return 0 on success, non-zero callback return value, or error code 1217 */ 1218 GIT_EXTERN(int) git_diff_blob_to_buffer( 1219 const git_blob *old_blob, 1220 const char *old_as_path, 1221 const char *buffer, 1222 size_t buffer_len, 1223 const char *buffer_as_path, 1224 const git_diff_options *options, 1225 git_diff_file_cb file_cb, 1226 git_diff_binary_cb binary_cb, 1227 git_diff_hunk_cb hunk_cb, 1228 git_diff_line_cb line_cb, 1229 void *payload); 1230 1231 /** 1232 * Directly run a diff between two buffers. 1233 * 1234 * Even more than with `git_diff_blobs`, comparing two buffer lacks 1235 * context, so the `git_diff_file` parameters to the callbacks will be 1236 * faked a la the rules for `git_diff_blobs()`. 1237 * 1238 * @param old_buffer Raw data for old side of diff, or NULL for empty 1239 * @param old_len Length of the raw data for old side of the diff 1240 * @param old_as_path Treat old buffer as if it had this filename; can be NULL 1241 * @param new_buffer Raw data for new side of diff, or NULL for empty 1242 * @param new_len Length of raw data for new side of diff 1243 * @param new_as_path Treat buffer as if it had this filename; can be NULL 1244 * @param options Options for diff, or NULL for default options 1245 * @param file_cb Callback for "file"; made once if there is a diff; can be NULL 1246 * @param binary_cb Callback for binary files; can be NULL 1247 * @param hunk_cb Callback for each hunk in diff; can be NULL 1248 * @param line_cb Callback for each line in diff; can be NULL 1249 * @param payload Payload passed to each callback function 1250 * @return 0 on success, non-zero callback return value, or error code 1251 */ 1252 GIT_EXTERN(int) git_diff_buffers( 1253 const void *old_buffer, 1254 size_t old_len, 1255 const char *old_as_path, 1256 const void *new_buffer, 1257 size_t new_len, 1258 const char *new_as_path, 1259 const git_diff_options *options, 1260 git_diff_file_cb file_cb, 1261 git_diff_binary_cb binary_cb, 1262 git_diff_hunk_cb hunk_cb, 1263 git_diff_line_cb line_cb, 1264 void *payload); 1265 1266 /** 1267 * Read the contents of a git patch file into a `git_diff` object. 1268 * 1269 * The diff object produced is similar to the one that would be 1270 * produced if you actually produced it computationally by comparing 1271 * two trees, however there may be subtle differences. For example, 1272 * a patch file likely contains abbreviated object IDs, so the 1273 * object IDs in a `git_diff_delta` produced by this function will 1274 * also be abbreviated. 1275 * 1276 * This function will only read patch files created by a git 1277 * implementation, it will not read unified diffs produced by 1278 * the `diff` program, nor any other types of patch files. 1279 * 1280 * @param out A pointer to a git_diff pointer that will be allocated. 1281 * @param content The contents of a patch file 1282 * @param content_len The length of the patch file contents 1283 * @return 0 or an error code 1284 */ 1285 GIT_EXTERN(int) git_diff_from_buffer( 1286 git_diff **out, 1287 const char *content, 1288 size_t content_len); 1289 1290 /** 1291 * This is an opaque structure which is allocated by `git_diff_get_stats`. 1292 * You are responsible for releasing the object memory when done, using the 1293 * `git_diff_stats_free()` function. 1294 */ 1295 typedef struct git_diff_stats git_diff_stats; 1296 1297 /** 1298 * Formatting options for diff stats 1299 */ 1300 typedef enum { 1301 /** No stats*/ 1302 GIT_DIFF_STATS_NONE = 0, 1303 1304 /** Full statistics, equivalent of `--stat` */ 1305 GIT_DIFF_STATS_FULL = (1u << 0), 1306 1307 /** Short statistics, equivalent of `--shortstat` */ 1308 GIT_DIFF_STATS_SHORT = (1u << 1), 1309 1310 /** Number statistics, equivalent of `--numstat` */ 1311 GIT_DIFF_STATS_NUMBER = (1u << 2), 1312 1313 /** Extended header information such as creations, renames and mode changes, equivalent of `--summary` */ 1314 GIT_DIFF_STATS_INCLUDE_SUMMARY = (1u << 3), 1315 } git_diff_stats_format_t; 1316 1317 /** 1318 * Accumulate diff statistics for all patches. 1319 * 1320 * @param out Structure containg the diff statistics. 1321 * @param diff A git_diff generated by one of the above functions. 1322 * @return 0 on success; non-zero on error 1323 */ 1324 GIT_EXTERN(int) git_diff_get_stats( 1325 git_diff_stats **out, 1326 git_diff *diff); 1327 1328 /** 1329 * Get the total number of files changed in a diff 1330 * 1331 * @param stats A `git_diff_stats` generated by one of the above functions. 1332 * @return total number of files changed in the diff 1333 */ 1334 GIT_EXTERN(size_t) git_diff_stats_files_changed( 1335 const git_diff_stats *stats); 1336 1337 /** 1338 * Get the total number of insertions in a diff 1339 * 1340 * @param stats A `git_diff_stats` generated by one of the above functions. 1341 * @return total number of insertions in the diff 1342 */ 1343 GIT_EXTERN(size_t) git_diff_stats_insertions( 1344 const git_diff_stats *stats); 1345 1346 /** 1347 * Get the total number of deletions in a diff 1348 * 1349 * @param stats A `git_diff_stats` generated by one of the above functions. 1350 * @return total number of deletions in the diff 1351 */ 1352 GIT_EXTERN(size_t) git_diff_stats_deletions( 1353 const git_diff_stats *stats); 1354 1355 /** 1356 * Print diff statistics to a `git_buf`. 1357 * 1358 * @param out buffer to store the formatted diff statistics in. 1359 * @param stats A `git_diff_stats` generated by one of the above functions. 1360 * @param format Formatting option. 1361 * @param width Target width for output (only affects GIT_DIFF_STATS_FULL) 1362 * @return 0 on success; non-zero on error 1363 */ 1364 GIT_EXTERN(int) git_diff_stats_to_buf( 1365 git_buf *out, 1366 const git_diff_stats *stats, 1367 git_diff_stats_format_t format, 1368 size_t width); 1369 1370 /** 1371 * Deallocate a `git_diff_stats`. 1372 * 1373 * @param stats The previously created statistics object; 1374 * cannot be used after free. 1375 */ 1376 GIT_EXTERN(void) git_diff_stats_free(git_diff_stats *stats); 1377 1378 /** 1379 * Patch ID options structure 1380 * 1381 * Initialize with `GIT_PATCHID_OPTIONS_INIT`. Alternatively, you can 1382 * use `git_diff_patchid_options_init`. 1383 * 1384 */ 1385 typedef struct git_diff_patchid_options { 1386 unsigned int version; 1387 } git_diff_patchid_options; 1388 1389 #define GIT_DIFF_PATCHID_OPTIONS_VERSION 1 1390 #define GIT_DIFF_PATCHID_OPTIONS_INIT { GIT_DIFF_PATCHID_OPTIONS_VERSION } 1391 1392 /** 1393 * Initialize git_diff_patchid_options structure 1394 * 1395 * Initializes a `git_diff_patchid_options` with default values. Equivalent to 1396 * creating an instance with `GIT_DIFF_PATCHID_OPTIONS_INIT`. 1397 * 1398 * @param opts The `git_diff_patchid_options` struct to initialize. 1399 * @param version The struct version; pass `GIT_DIFF_PATCHID_OPTIONS_VERSION`. 1400 * @return Zero on success; -1 on failure. 1401 */ 1402 GIT_EXTERN(int) git_diff_patchid_options_init( 1403 git_diff_patchid_options *opts, 1404 unsigned int version); 1405 1406 /** 1407 * Calculate the patch ID for the given patch. 1408 * 1409 * Calculate a stable patch ID for the given patch by summing the 1410 * hash of the file diffs, ignoring whitespace and line numbers. 1411 * This can be used to derive whether two diffs are the same with 1412 * a high probability. 1413 * 1414 * Currently, this function only calculates stable patch IDs, as 1415 * defined in git-patch-id(1), and should in fact generate the 1416 * same IDs as the upstream git project does. 1417 * 1418 * @param out Pointer where the calculated patch ID should be stored 1419 * @param diff The diff to calculate the ID for 1420 * @param opts Options for how to calculate the patch ID. This is 1421 * intended for future changes, as currently no options are 1422 * available. 1423 * @return 0 on success, an error code otherwise. 1424 */ 1425 GIT_EXTERN(int) git_diff_patchid(git_oid *out, git_diff *diff, git_diff_patchid_options *opts); 1426 1427 GIT_END_DECL 1428 1429 /** @} */ 1430 1431 #endif 1432