@matheusbsbs / matheus-bagliano / commits / 13c4d7eb22
Fork 0

diff: move doc to diff.h and diffcore.h

Move the documentation from Documentation/technical/api-diff.txt to both diff.h and diffcore.h as it's easier for the developers to find the usage information beside the code instead of looking for it in another doc file. Also documentation/technical/api-diff.txt is removed because the information it has is now redundant and it'll be hard to keep it up to date and synchronized with the documentation in the header files. There are three members documented in the doc file that weren't found in the header files, assuming the doc wasn't up to date and the members no longer exist: touched_flags, COLOR_DIFF_WORDS and QUIET. Signed-off-by: Heba Waly <heba.waly@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Heba Waly committed Nov 17, 2019 at 21:04 UTC 13c4d7eb228ea29dba77f8800daf2bd0ac54755a
3 files changed +158 -174
Documentation/technical/api-diff.txt deleted
-174
@@ -1,174 +0,0 @@
1 -diff API
2 -========
3 -
4 -The diff API is for programs that compare two sets of files (e.g. two
5 -trees, one tree and the index) and present the found difference in
6 -various ways. The calling program is responsible for feeding the API
7 -pairs of files, one from the "old" set and the corresponding one from
8 -"new" set, that are different. The library called through this API is
9 -called diffcore, and is responsible for two things.
10 -
11 -* finding total rewrites (`-B`), renames (`-M`) and copies (`-C`), and
12 - changes that touch a string (`-S`), as specified by the caller.
13 -
14 -* outputting the differences in various formats, as specified by the
15 - caller.
16 -
17 -Calling sequence
18 -----------------
19 -
20 -* Prepare `struct diff_options` to record the set of diff options, and
21 - then call `repo_diff_setup()` to initialize this structure. This
22 - sets up the vanilla default.
23 -
24 -* Fill in the options structure to specify desired output format, rename
25 - detection, etc. `diff_opt_parse()` can be used to parse options given
26 - from the command line in a way consistent with existing git-diff
27 - family of programs.
28 -
29 -* Call `diff_setup_done()`; this inspects the options set up so far for
30 - internal consistency and make necessary tweaking to it (e.g. if
31 - textual patch output was asked, recursive behaviour is turned on);
32 - the callback set_default in diff_options can be used to tweak this more.
33 -
34 -* As you find different pairs of files, call `diff_change()` to feed
35 - modified files, `diff_addremove()` to feed created or deleted files,
36 - or `diff_unmerge()` to feed a file whose state is 'unmerged' to the
37 - API. These are thin wrappers to a lower-level `diff_queue()` function
38 - that is flexible enough to record any of these kinds of changes.
39 -
40 -* Once you finish feeding the pairs of files, call `diffcore_std()`.
41 - This will tell the diffcore library to go ahead and do its work.
42 -
43 -* Calling `diff_flush()` will produce the output.
44 -
45 -
46 -Data structures
47 ----------------
48 -
49 -* `struct diff_filespec`
50 -
51 -This is the internal representation for a single file (blob). It
52 -records the blob object name (if known -- for a work tree file it
53 -typically is a NUL SHA-1), filemode and pathname. This is what the
54 -`diff_addremove()`, `diff_change()` and `diff_unmerge()` synthesize and
55 -feed `diff_queue()` function with.
56 -
57 -* `struct diff_filepair`
58 -
59 -This records a pair of `struct diff_filespec`; the filespec for a file
60 -in the "old" set (i.e. preimage) is called `one`, and the filespec for a
61 -file in the "new" set (i.e. postimage) is called `two`. A change that
62 -represents file creation has NULL in `one`, and file deletion has NULL
63 -in `two`.
64 -
65 -A `filepair` starts pointing at `one` and `two` that are from the same
66 -filename, but `diffcore_std()` can break pairs and match component
67 -filespecs with other filespecs from a different filepair to form new
68 -filepair. This is called 'rename detection'.
69 -
70 -* `struct diff_queue`
71 -
72 -This is a collection of filepairs. Notable members are:
73 -
74 -`queue`::
75 -
76 - An array of pointers to `struct diff_filepair`. This
77 - dynamically grows as you add filepairs;
78 -
79 -`alloc`::
80 -
81 - The allocated size of the `queue` array;
82 -
83 -`nr`::
84 -
85 - The number of elements in the `queue` array.
86 -
87 -
88 -* `struct diff_options`
89 -
90 -This describes the set of options the calling program wants to affect
91 -the operation of diffcore library with.
92 -
93 -Notable members are:
94 -
95 -`output_format`::
96 - The output format used when `diff_flush()` is run.
97 -
98 -`context`::
99 - Number of context lines to generate in patch output.
100 -
101 -`break_opt`, `detect_rename`, `rename-score`, `rename_limit`::
102 - Affects the way detection logic for complete rewrites, renames
103 - and copies.
104 -
105 -`abbrev`::
106 - Number of hexdigits to abbreviate raw format output to.
107 -
108 -`pickaxe`::
109 - A constant string (can and typically does contain newlines to
110 - look for a block of text, not just a single line) to filter out
111 - the filepairs that do not change the number of strings contained
112 - in its preimage and postimage of the diff_queue.
113 -
114 -`flags`::
115 - This is mostly a collection of boolean options that affects the
116 - operation, but some do not have anything to do with the diffcore
117 - library.
118 -
119 -`touched_flags`::
120 - Records whether a flag has been changed due to user request
121 - (rather than just set/unset by default).
122 -
123 -`set_default`::
124 - Callback which allows tweaking the options in diff_setup_done().
125 -
126 -BINARY, TEXT;;
127 - Affects the way how a file that is seemingly binary is treated.
128 -
129 -FULL_INDEX;;
130 - Tells the patch output format not to use abbreviated object
131 - names on the "index" lines.
132 -
133 -FIND_COPIES_HARDER;;
134 - Tells the diffcore library that the caller is feeding unchanged
135 - filepairs to allow copies from unmodified files be detected.
136 -
137 -COLOR_DIFF;;
138 - Output should be colored.
139 -
140 -COLOR_DIFF_WORDS;;
141 - Output is a colored word-diff.
142 -
143 -NO_INDEX;;
144 - Tells diff-files that the input is not tracked files but files
145 - in random locations on the filesystem.
146 -
147 -ALLOW_EXTERNAL;;
148 - Tells output routine that it is Ok to call user specified patch
149 - output routine. Plumbing disables this to ensure stable output.
150 -
151 -QUIET;;
152 - Do not show any output.
153 -
154 -REVERSE_DIFF;;
155 - Tells the library that the calling program is feeding the
156 - filepairs reversed; `one` is two, and `two` is one.
157 -
158 -EXIT_WITH_STATUS;;
159 - For communication between the calling program and the options
160 - parser; tell the calling program to signal the presence of
161 - difference using program exit code.
162 -
163 -HAS_CHANGES;;
164 - Internal; used for optimization to see if there is any change.
165 -
166 -SILENT_ON_REMOVE;;
167 - Affects if diff-files shows removed files.
168 -
169 -RECURSIVE, TREE_IN_RECURSIVE;;
170 - Tells if tree traversal done by tree-diff should recursively
171 - descend into a tree object pair that are different in preimage
172 - and postimage set.
173 -
174 -(JC)
diff.h
+126
@@ -9,6 +9,49 @@
9 #include "object.h"
10 #include "oidset.h"
11
12 +/**
13 + * The diff API is for programs that compare two sets of files (e.g. two trees,
14 + * one tree and the index) and present the found difference in various ways.
15 + * The calling program is responsible for feeding the API pairs of files, one
16 + * from the "old" set and the corresponding one from "new" set, that are
17 + * different.
18 + * The library called through this API is called diffcore, and is responsible
19 + * for two things.
20 + *
21 + * - finding total rewrites (`-B`), renames (`-M`) and copies (`-C`), and
22 + * changes that touch a string (`-S`), as specified by the caller.
23 + *
24 + * - outputting the differences in various formats, as specified by the caller.
25 + *
26 + * Calling sequence
27 + * ----------------
28 + *
29 + * - Prepare `struct diff_options` to record the set of diff options, and then
30 + * call `repo_diff_setup()` to initialize this structure. This sets up the
31 + * vanilla default.
32 + *
33 + * - Fill in the options structure to specify desired output format, rename
34 + * detection, etc. `diff_opt_parse()` can be used to parse options given
35 + * from the command line in a way consistent with existing git-diff family
36 + * of programs.
37 + *
38 + * - Call `diff_setup_done()`; this inspects the options set up so far for
39 + * internal consistency and make necessary tweaking to it (e.g. if textual
40 + * patch output was asked, recursive behaviour is turned on); the callback
41 + * set_default in diff_options can be used to tweak this more.
42 + *
43 + * - As you find different pairs of files, call `diff_change()` to feed
44 + * modified files, `diff_addremove()` to feed created or deleted files, or
45 + * `diff_unmerge()` to feed a file whose state is 'unmerged' to the API.
46 + * These are thin wrappers to a lower-level `diff_queue()` function that is
47 + * flexible enough to record any of these kinds of changes.
48 + *
49 + * - Once you finish feeding the pairs of files, call `diffcore_std()`.
50 + * This will tell the diffcore library to go ahead and do its work.
51 + *
52 + * - Calling `diff_flush()` will produce the output.
53 + */
54 +
55 struct combine_diff_path;
56 struct commit;
57 struct diff_filespec;
@@ -65,21 +108,66 @@ typedef struct strbuf *(*diff_prefix_fn_t)(struct diff_options *opt, void *data)
108
109 #define DIFF_FLAGS_INIT { 0 }
110 struct diff_flags {
111 +
112 + /**
113 + * Tells if tree traversal done by tree-diff should recursively descend
114 + * into a tree object pair that are different in preimage and postimage set.
115 + */
116 unsigned recursive;
117 unsigned tree_in_recursive;
118 +
119 + /* Affects the way how a file that is seemingly binary is treated. */
120 unsigned binary;
121 unsigned text;
122 +
123 + /**
124 + * Tells the patch output format not to use abbreviated object names on the
125 + * "index" lines.
126 + */
127 unsigned full_index;
128 +
129 + /* Affects if diff-files shows removed files. */
130 unsigned silent_on_remove;
131 +
132 + /**
133 + * Tells the diffcore library that the caller is feeding unchanged
134 + * filepairs to allow copies from unmodified files be detected.
135 + */
136 unsigned find_copies_harder;
137 +
138 unsigned follow_renames;
139 unsigned rename_empty;
140 +
141 + /* Internal; used for optimization to see if there is any change. */
142 unsigned has_changes;
143 +
144 unsigned quick;
145 +
146 + /**
147 + * Tells diff-files that the input is not tracked files but files in random
148 + * locations on the filesystem.
149 + */
150 unsigned no_index;
151 +
152 + /**
153 + * Tells output routine that it is Ok to call user specified patch output
154 + * routine. Plumbing disables this to ensure stable output.
155 + */
156 unsigned allow_external;
157 +
158 + /**
159 + * For communication between the calling program and the options parser;
160 + * tell the calling program to signal the presence of difference using
161 + * program exit code.
162 + */
163 unsigned exit_with_status;
164 +
165 + /**
166 + * Tells the library that the calling program is feeding the filepairs
167 + * reversed; `one` is two, and `two` is one.
168 + */
169 unsigned reverse_diff;
170 +
171 unsigned check_failed;
172 unsigned relative_name;
173 unsigned ignore_submodules;
@@ -131,36 +219,72 @@ enum diff_submodule_format {
219 DIFF_SUBMODULE_INLINE_DIFF
220 };
221
222 +/**
223 + * the set of options the calling program wants to affect the operation of
224 + * diffcore library with.
225 + */
226 struct diff_options {
227 const char *orderfile;
228 +
229 + /**
230 + * A constant string (can and typically does contain newlines to look for
231 + * a block of text, not just a single line) to filter out the filepairs
232 + * that do not change the number of strings contained in its preimage and
233 + * postimage of the diff_queue.
234 + */
235 const char *pickaxe;
236 +
237 const char *single_follow;
238 const char *a_prefix, *b_prefix;
239 const char *line_prefix;
240 size_t line_prefix_length;
241 +
242 + /**
243 + * collection of boolean options that affects the operation, but some do
244 + * not have anything to do with the diffcore library.
245 + */
246 struct diff_flags flags;
247
248 /* diff-filter bits */
249 unsigned int filter;
250
251 int use_color;
252 +
253 + /* Number of context lines to generate in patch output. */
254 int context;
255 +
256 int interhunkcontext;
257 +
258 + /* Affects the way detection logic for complete rewrites, renames and
259 + * copies.
260 + */
261 int break_opt;
262 int detect_rename;
263 +
264 int irreversible_delete;
265 int skip_stat_unmatch;
266 int line_termination;
267 +
268 + /* The output format used when `diff_flush()` is run. */
269 int output_format;
270 +
271 unsigned pickaxe_opts;
272 +
273 + /* Affects the way detection logic for complete rewrites, renames and
274 + * copies.
275 + */
276 int rename_score;
277 int rename_limit;
278 +
279 int needed_rename_limit;
280 int degraded_cc_to_c;
281 int show_rename_progress;
282 int dirstat_permille;
283 int setup;
284 +
285 + /* Number of hexdigits to abbreviate raw format output to. */
286 int abbrev;
287 +
288 int ita_invisible_in_index;
289 /* white-space error highlighting */
290 #define WSEH_NEW (1<<12)
@@ -192,6 +316,7 @@ struct diff_options {
316 /* to support internal diff recursion by --follow hack*/
317 int found_follow;
318
319 + /* Callback which allows tweaking the options in diff_setup_done(). */
320 void (*set_default)(struct diff_options *);
321
322 FILE *file;
@@ -270,6 +395,7 @@ enum color_diff {
395 DIFF_FILE_OLD_BOLD = 21,
396 DIFF_FILE_NEW_BOLD = 22,
397 };
398 +
399 const char *diff_get_color(int diff_use_color, enum color_diff ix);
400 #define diff_get_color_opt(o, ix) \
401 diff_get_color((o)->use_color, ix)
diffcore.h
+32
@@ -28,6 +28,12 @@ struct userdiff_driver;
28
29 #define MINIMUM_BREAK_SIZE 400 /* do not break a file smaller than this */
30
31 +/**
32 + * the internal representation for a single file (blob). It records the blob
33 + * object name (if known -- for a work tree file it typically is a NUL SHA-1),
34 + * filemode and pathname. This is what the `diff_addremove()`, `diff_change()`
35 + * and `diff_unmerge()` synthesize and feed `diff_queue()` function with.
36 + */
37 struct diff_filespec {
38 struct object_id oid;
39 char *path;
@@ -66,6 +72,17 @@ void diff_free_filespec_data(struct diff_filespec *);
72 void diff_free_filespec_blob(struct diff_filespec *);
73 int diff_filespec_is_binary(struct repository *, struct diff_filespec *);
74
75 +/**
76 + * This records a pair of `struct diff_filespec`; the filespec for a file in
77 + * the "old" set (i.e. preimage) is called `one`, and the filespec for a file
78 + * in the "new" set (i.e. postimage) is called `two`. A change that represents
79 + * file creation has NULL in `one`, and file deletion has NULL in `two`.
80 + *
81 + * A `filepair` starts pointing at `one` and `two` that are from the same
82 + * filename, but `diffcore_std()` can break pairs and match component filespecs
83 + * with other filespecs from a different filepair to form new filepair. This is
84 + * called 'rename detection'.
85 + */
86 struct diff_filepair {
87 struct diff_filespec *one;
88 struct diff_filespec *two;
@@ -77,6 +94,7 @@ struct diff_filepair {
94 unsigned done_skip_stat_unmatch : 1;
95 unsigned skip_stat_unmatch_result : 1;
96 };
97 +
98 #define DIFF_PAIR_UNMERGED(p) ((p)->is_unmerged)
99
100 #define DIFF_PAIR_RENAME(p) ((p)->renamed_pair)
@@ -94,11 +112,25 @@ void diff_free_filepair(struct diff_filepair *);
112
113 int diff_unmodified_pair(struct diff_filepair *);
114
115 +/**
116 + * This is a collection of filepairs. Notable members are:
117 + *
118 + * - `queue`:
119 + * An array of pointers to `struct diff_filepair`. This dynamically grows as
120 + * you add filepairs;
121 + *
122 + * - `alloc`:
123 + * The allocated size of the `queue` array;
124 + *
125 + * - `nr`:
126 + * The number of elements in the `queue` array.
127 + */
128 struct diff_queue_struct {
129 struct diff_filepair **queue;
130 int alloc;
131 int nr;
132 };
133 +
134 #define DIFF_QUEUE_CLEAR(q) \
135 do { \
136 (q)->queue = NULL; \