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