add more info about the test suite to README.portable
[got-portable.git] / include / got_diff.h
blobf418902034cad556fba11cdc5011da9c70bfcafe
1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 enum got_diff_algorithm {
18 GOT_DIFF_ALGORITHM_MYERS,
19 GOT_DIFF_ALGORITHM_PATIENCE,
23 * List of all line types in a diff (including '{got,tog} log' lines).
24 * XXX GOT_DIFF_LINE_HUNK to GOT_DIFF_LINE_NONE inclusive must map to the
25 * DIFF_LINE_* macro counterparts defined in lib/diff_output.h (i.e., 60-64).
27 enum got_diff_line_type {
28 GOT_DIFF_LINE_LOGMSG,
29 GOT_DIFF_LINE_AUTHOR,
30 GOT_DIFF_LINE_DATE,
31 GOT_DIFF_LINE_CHANGES,
32 GOT_DIFF_LINE_META,
33 GOT_DIFF_LINE_BLOB_MIN,
34 GOT_DIFF_LINE_BLOB_PLUS,
35 GOT_DIFF_LINE_HUNK = 60,
36 GOT_DIFF_LINE_MINUS,
37 GOT_DIFF_LINE_PLUS,
38 GOT_DIFF_LINE_CONTEXT,
39 GOT_DIFF_LINE_NONE
42 struct got_diff_line {
43 off_t offset;
44 uint8_t type;
47 struct got_diffstat_cb_arg;
50 * Compute the differences between two blobs and write unified diff text
51 * to the provided output file. Two open temporary files must be provided
52 * for internal use; these files can be obtained from got_opentemp() and
53 * must be closed by the caller.
54 * If one of the blobs being diffed does not exist, all corresponding
55 * blob object arguments should be set to NULL.
56 * Two const char * diff header labels may be provided which will be used
57 * to identify each blob in the diff output.
58 * If a label is NULL, use the blob's hash instead.
59 * The number of context lines to show in the diff must be specified as well.
60 * Whitespace differences may optionally be ignored.
61 * If not NULL, the two initial output arguments will be populated with an
62 * array of line offsets for, and the number of lines in, the unidiff text.
64 const struct got_error *got_diff_blob(struct got_diff_line **, size_t *,
65 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
66 const char *, const char *, enum got_diff_algorithm, int, int, int,
67 struct got_diffstat_cb_arg *, FILE *);
70 * Compute the differences between a blob and a file and write unified diff
71 * text to the provided output file. The blob object, its content, and its
72 * size must be provided. The file's size must be provided, as well as a
73 * const char * diff header label which identifies the file.
74 * An optional const char * diff header label for the blob may be provided, too.
75 * The number of context lines to show in the diff must be specified as well.
76 * Whitespace differences may optionally be ignored.
77 * If not NULL, the two initial output arguments will be populated with an
78 * array of line metadata for, and the number of lines in, the unidiff text.
80 const struct got_error *got_diff_blob_file(struct got_diff_line **, size_t *,
81 struct got_blob_object *, FILE *, off_t, const char *, FILE *, int,
82 struct stat *, const char *, enum got_diff_algorithm, int, int, int,
83 struct got_diffstat_cb_arg *, FILE *);
86 * A callback function invoked to handle the differences between two blobs
87 * when diffing trees with got_diff_tree(). This callback receives two blobs,
88 * their respective IDs, and two corresponding paths within the diffed trees.
89 * The first blob contains content from the old side of the diff, and
90 * the second blob contains content on the new side of the diff.
91 * Two open temporary files must be provided for internal use; these files
92 * can be obtained from got_opentemp() and must be closed by the caller.
93 * The blob object argument for either blob may be NULL to indicate
94 * that no content is present on its respective side of the diff.
95 * File modes from relevant tree objects which contain the blobs may
96 * also be passed. These will be zero if not available.
98 typedef const struct got_error *(*got_diff_blob_cb)(void *,
99 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
100 struct got_object_id *, struct got_object_id *,
101 const char *, const char *, mode_t, mode_t, struct got_repository *);
104 * A pre-defined implementation of got_diff_blob_cb() which appends unidiff
105 * output to a file. The caller must allocate and fill in the argument
106 * structure.
108 struct got_diff_blob_output_unidiff_arg {
109 FILE *outfile; /* Unidiff text will be written here. */
110 int diff_context; /* Sets the number of context lines. */
111 int ignore_whitespace; /* Ignore whitespace differences. */
112 int force_text_diff; /* Assume text even if binary data detected. */
113 struct got_diffstat_cb_arg *diffstat; /* Compute diffstat of changes */
114 enum got_diff_algorithm diff_algo; /* Diffing algorithm to use. */
117 * The number of lines contained in produced unidiff text output,
118 * and an array of got_diff_lines with byte offset and line type to
119 * each line. May be initialized to zero and NULL to ignore line
120 * metadata. If not NULL, then the array of line offsets and types will
121 * be populated. Optionally, the array can be pre-populated with line
122 * offsets and types, with nlines > 0 indicating the length of the
123 * pre-populated array. This is useful if the output file already
124 * contains some lines of text. The array will be grown as needed to
125 * accommodate additional offsets and types, and the last offset found
126 * in a pre-populated array will be added to all subsequent offsets.
128 size_t nlines;
129 struct got_diff_line *lines; /* Dispose of with free(3) when done. */
131 const struct got_error *got_diff_blob_output_unidiff(void *,
132 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
133 struct got_object_id *, struct got_object_id *,
134 const char *, const char *, mode_t, mode_t, struct got_repository *);
137 * Compute the differences between two trees and invoke the provided
138 * got_diff_blob_cb() callback when content differs.
139 * Diffing of blob content can be suppressed by passing zero for the
140 * 'diff_content' parameter. The callback will then only receive blob
141 * object IDs and diff labels, but NULL pointers instead of blob objects.
142 * If 'diff_content' is set, two open temporary FILEs and two open
143 * temporary file descriptors must be provided for internal use; these
144 * files can be obtained from got_opentemp() and got_opentempfd(),
145 * and must be closed by the caller. Otherwise the files can be NULL.
146 * The set of arguments relating to either tree may be NULL to indicate
147 * that no content is present on its respective side of the diff.
149 const struct got_error *got_diff_tree(struct got_tree_object *,
150 struct got_tree_object *, FILE *, FILE *, int, int,
151 const char *, const char *,
152 struct got_repository *, got_diff_blob_cb cb, void *cb_arg, int);
155 * Pre-defined implementations of got_diff_blob_cb(): the first of which
156 * collects a list of file paths that differ between two trees; the second
157 * also computes a diffstat of added/removed lines for each collected path
158 * and requires passing an initialized got_diffstat_cb_arg argument.
159 * The caller must allocate and initialize a got_pathlist_head * argument.
160 * Data pointers of entries added to the path list will point to a struct
161 * got_diff_changed_path object.
162 * The caller is expected to free both the path and data pointers of all
163 * entries on the path list.
165 struct got_diff_changed_path {
166 uint32_t add; /* number of lines added */
167 uint32_t rm; /* number of lines removed */
169 * The modification status of this path. It can be GOT_STATUS_ADD,
170 * GOT_STATUS_DELETE, GOT_STATUS_MODIFY, or GOT_STATUS_MODE_CHANGE.
172 int status;
174 const struct got_error *got_diff_tree_collect_changed_paths(void *,
175 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
176 struct got_object_id *, struct got_object_id *,
177 const char *, const char *, mode_t, mode_t, struct got_repository *);
179 struct got_diffstat_cb_arg {
180 size_t max_path_len;
181 uint32_t ins;
182 uint32_t del;
183 int add_cols;
184 int rm_cols;
185 int nfiles;
186 struct got_pathlist_head *paths;
187 int ignore_ws;
188 int force_text;
189 enum got_diff_algorithm diff_algo;
191 const struct got_error *got_diff_tree_compute_diffstat(void *,
192 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
193 struct got_object_id *, struct got_object_id *, const char *, const char *,
194 mode_t, mode_t, struct got_repository *);
197 * Diff two objects, assuming both objects are blobs. Two const char * diff
198 * header labels may be provided which will be used to identify each blob in
199 * the diff output. If a label is NULL, use the blob's hash instead.
200 * Two open temporary files and two temporary file descriptors must be
201 * provided for internal use; these files can be obtained from
202 * got_opentemp() and got_opentempfd(), and must be closed by the caller.
203 * The set of arguments relating to either blob may be NULL/-1 to indicate
204 * that no content is present on its respective side of the diff.
205 * The number of context lines to show in the diff must be specified as well.
206 * Write unified diff text to the provided output FILE.
207 * If not NULL, the two initial output arguments will be populated with an
208 * array of line offsets for, and the number of lines in, the unidiff text.
210 const struct got_error *got_diff_objects_as_blobs(struct got_diff_line **,
211 size_t *, FILE *, FILE *, int, int, struct got_object_id *,
212 struct got_object_id *, const char *, const char *, enum got_diff_algorithm,
213 int, int, int, struct got_diffstat_cb_arg *, struct got_repository *,
214 FILE *);
216 struct got_pathlist_head;
219 * Diff two objects, assuming both objects are trees. Two const char * diff
220 * header labels may be provided which will be used to identify each blob in
221 * the trees. If a label is NULL, use the blob's hash instead.
222 * The number of context lines to show in diffs must be specified.
223 * Two open temporary files and two temporary file descriptors must be
224 * provided for internal use; these files can be obtained from
225 * got_opentemp() and got_opentempfd(), and must be closed by the caller.
226 * If 'diff_content' is not set, the files may be NULL / -1.
227 * The set of arguments relating to either tree may be NULL to indicate
228 * that no content is present on its respective side of the diff.
229 * Write unified diff text to the provided output FILE.
230 * If not NULL, the two initial output arguments will be populated with an
231 * array of line offsets for, and the number of lines in, the unidiff text.
233 const struct got_error *got_diff_objects_as_trees(struct got_diff_line **,
234 size_t *, FILE *, FILE *, int, int, struct got_object_id *,
235 struct got_object_id *, struct got_pathlist_head *, const char *,
236 const char *, enum got_diff_algorithm, int, int, int,
237 struct got_diffstat_cb_arg *, struct got_repository *, FILE *);
240 * Diff two objects, assuming both objects are commits.
241 * The number of context lines to show in diffs must be specified.
242 * Two open temporary files and two temporary file descriptors must be
243 * provided for internal use; these files can be obtained from
244 * got_opentemp() and got_opentempfd(), and must be closed by the caller.
245 * The set of arguments relating to either commit may be NULL to indicate
246 * that no content is present on its respective side of the diff.
247 * Write unified diff text to the provided output FILE.
248 * If not NULL, the two initial output arguments will be populated with an
249 * array of line offsets for, and the number of lines in, the unidiff text.
251 const struct got_error *got_diff_objects_as_commits(struct got_diff_line **,
252 size_t *, FILE *, FILE *, int, int, struct got_object_id *,
253 struct got_object_id *, struct got_pathlist_head *, enum got_diff_algorithm,
254 int, int, int, struct got_diffstat_cb_arg *, struct got_repository *,
255 FILE *);
257 #define GOT_DIFF_MAX_CONTEXT 64