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