Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 enum got_diff_algorithm {
18 GOT_DIFF_ALGORITHM_MYERS,
19 GOT_DIFF_ALGORITHM_PATIENCE,
20 };
22 /*
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).
26 */
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
40 };
42 struct got_diff_line {
43 off_t offset;
44 uint8_t type;
45 };
47 struct got_diffstat_cb_arg;
49 /*
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 SHA1 checksum 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.
63 */
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 *);
69 /*
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 */
78 const struct got_error *got_diff_blob_file(struct got_blob_object *, FILE *,
79 off_t, const char *, FILE *, int, struct stat *, const char *,
80 enum got_diff_algorithm, int, int, int, struct got_diffstat_cb_arg *,
81 FILE *);
83 /*
84 * A callback function invoked to handle the differences between two blobs
85 * when diffing trees with got_diff_tree(). This callback receives two blobs,
86 * their respective IDs, and two corresponding paths within the diffed trees.
87 * The first blob contains content from the old side of the diff, and
88 * the second blob contains content on the new side of the diff.
89 * Two open temporary files must be provided for internal use; these files
90 * can be obtained from got_opentemp() and must be closed by the caller.
91 * The blob object argument for either blob may be NULL to indicate
92 * that no content is present on its respective side of the diff.
93 * File modes from relevant tree objects which contain the blobs may
94 * also be passed. These will be zero if not available.
95 */
96 typedef const struct got_error *(*got_diff_blob_cb)(void *,
97 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
98 struct got_object_id *, struct got_object_id *,
99 const char *, const char *, mode_t, mode_t, struct got_repository *);
101 /*
102 * A pre-defined implementation of got_diff_blob_cb() which appends unidiff
103 * output to a file. The caller must allocate and fill in the argument
104 * structure.
105 */
106 struct got_diff_blob_output_unidiff_arg {
107 FILE *outfile; /* Unidiff text will be written here. */
108 int diff_context; /* Sets the number of context lines. */
109 int ignore_whitespace; /* Ignore whitespace differences. */
110 int force_text_diff; /* Assume text even if binary data detected. */
111 struct got_diffstat_cb_arg *diffstat; /* Compute diffstat of changes */
112 enum got_diff_algorithm diff_algo; /* Diffing algorithm to use. */
114 /*
115 * The number of lines contained in produced unidiff text output,
116 * and an array of got_diff_lines with byte offset and line type to
117 * each line. May be initialized to zero and NULL to ignore line
118 * metadata. If not NULL, then the array of line offsets and types will
119 * be populated. Optionally, the array can be pre-populated with line
120 * offsets and types, with nlines > 0 indicating the length of the
121 * pre-populated array. This is useful if the output file already
122 * contains some lines of text. The array will be grown as needed to
123 * accomodate additional offsets and types, and the last offset found
124 * in a pre-populated array will be added to all subsequent offsets.
125 */
126 size_t nlines;
127 struct got_diff_line *lines; /* Dispose of with free(3) when done. */
128 };
129 const struct got_error *got_diff_blob_output_unidiff(void *,
130 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
131 struct got_object_id *, struct got_object_id *,
132 const char *, const char *, mode_t, mode_t, struct got_repository *);
134 /*
135 * Compute the differences between two trees and invoke the provided
136 * got_diff_blob_cb() callback when content differs.
137 * Diffing of blob content can be suppressed by passing zero for the
138 * 'diff_content' parameter. The callback will then only receive blob
139 * object IDs and diff labels, but NULL pointers instead of blob objects.
140 * If 'diff_content' is set, two open temporary FILEs and two open
141 * temporary file descriptors must be provided for internal use; these
142 * files can be obtained from got_opentemp() and got_opentempfd(),
143 * and must be closed by the caller. Otherwise the files can be NULL.
144 * The set of arguments relating to either tree may be NULL to indicate
145 * that no content is present on its respective side of the diff.
146 */
147 const struct got_error *got_diff_tree(struct got_tree_object *,
148 struct got_tree_object *, FILE *, FILE *, int, int,
149 const char *, const char *,
150 struct got_repository *, got_diff_blob_cb cb, void *cb_arg, int);
152 /*
153 * Pre-defined implementations of got_diff_blob_cb(): the first of which
154 * collects a list of file paths that differ between two trees; the second
155 * also computes a diffstat of added/removed lines for each collected path
156 * and requires passing an initialized got_diffstat_cb_arg argument.
157 * The caller must allocate and initialize a got_pathlist_head * argument.
158 * Data pointers of entries added to the path list will point to a struct
159 * got_diff_changed_path object.
160 * The caller is expected to free both the path and data pointers of all
161 * entries on the path list.
162 */
163 struct got_diff_changed_path {
164 uint32_t add; /* number of lines added */
165 uint32_t rm; /* number of lines removed */
166 /*
167 * The modification status of this path. It can be GOT_STATUS_ADD,
168 * GOT_STATUS_DELETE, GOT_STATUS_MODIFY, or GOT_STATUS_MODE_CHANGE.
169 */
170 int status;
171 };
172 const struct got_error *got_diff_tree_collect_changed_paths(void *,
173 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
174 struct got_object_id *, struct got_object_id *,
175 const char *, const char *, mode_t, mode_t, struct got_repository *);
177 struct got_diffstat_cb_arg {
178 size_t max_path_len;
179 uint32_t ins;
180 uint32_t del;
181 int add_cols;
182 int rm_cols;
183 int nfiles;
184 struct got_pathlist_head *paths;
185 int ignore_ws;
186 int force_text;
187 enum got_diff_algorithm diff_algo;
188 };
189 const struct got_error *got_diff_tree_compute_diffstat(void *,
190 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
191 struct got_object_id *, struct got_object_id *, const char *, const char *,
192 mode_t, mode_t, struct got_repository *);
194 /*
195 * Diff two objects, assuming both objects are blobs. Two const char * diff
196 * header labels may be provided which will be used to identify each blob in
197 * the diff output. If a label is NULL, use the blob's SHA1 checksum instead.
198 * Two open temporary files and two temporary file descriptors must be
199 * provided for internal use; these files can be obtained from
200 * got_opentemp() and got_opentempfd(), and must be closed by the caller.
201 * The set of arguments relating to either blob may be NULL/-1 to indicate
202 * that no content is present on its respective side of the diff.
203 * The number of context lines to show in the diff must be specified as well.
204 * Write unified diff text to the provided output FILE.
205 * If not NULL, the two initial output arguments will be populated with an
206 * array of line offsets for, and the number of lines in, the unidiff text.
207 */
208 const struct got_error *got_diff_objects_as_blobs(struct got_diff_line **,
209 size_t *, FILE *, FILE *, int, int, struct got_object_id *,
210 struct got_object_id *, const char *, const char *, enum got_diff_algorithm,
211 int, int, int, struct got_diffstat_cb_arg *, struct got_repository *,
212 FILE *);
214 struct got_pathlist_head;
216 /*
217 * Diff two objects, assuming both objects are trees. Two const char * diff
218 * header labels may be provided which will be used to identify each blob in
219 * the trees. If a label is NULL, use the blob's SHA1 checksum instead.
220 * The number of context lines to show in diffs must be specified.
221 * Two open temporary files and two temporary file descriptors must be
222 * provided for internal use; these files can be obtained from
223 * got_opentemp() and got_opentempfd(), and must be closed by the caller.
224 * If 'diff_content' is not set, the files may be NULL / -1.
225 * The set of arguments relating to either tree may be NULL to indicate
226 * that no content is present on its respective side of the diff.
227 * Write unified diff text to the provided output FILE.
228 * If not NULL, the two initial output arguments will be populated with an
229 * array of line offsets for, and the number of lines in, the unidiff text.
230 */
231 const struct got_error *got_diff_objects_as_trees(struct got_diff_line **,
232 size_t *, FILE *, FILE *, int, int, struct got_object_id *,
233 struct got_object_id *, struct got_pathlist_head *, const char *,
234 const char *, enum got_diff_algorithm, int, int, int,
235 struct got_diffstat_cb_arg *, struct got_repository *, FILE *);
237 /*
238 * Diff two objects, assuming both objects are commits.
239 * The number of context lines to show in diffs must be specified.
240 * Two open temporary files and two temporary file descriptors must be
241 * provided for internal use; these files can be obtained from
242 * got_opentemp() and got_opentempfd(), and must be closed by the caller.
243 * The set of arguments relating to either commit may be NULL to indicate
244 * that no content is present on its respective side of the diff.
245 * Write unified diff text to the provided output FILE.
246 * If not NULL, the two initial output arguments will be populated with an
247 * array of line offsets for, and the number of lines in, the unidiff text.
248 */
249 const struct got_error *got_diff_objects_as_commits(struct got_diff_line **,
250 size_t *, FILE *, FILE *, int, int, struct got_object_id *,
251 struct got_object_id *, struct got_pathlist_head *, enum got_diff_algorithm,
252 int, int, int, struct got_diffstat_cb_arg *, struct got_repository *,
253 FILE *);
255 #define GOT_DIFF_MAX_CONTEXT 64