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, 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, 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 int show_diffstat; /* Compute diffstat of changes */
112 struct got_diffstat_cb_arg *diffstat;
113 enum got_diff_algorithm diff_algo; /* Diffing algorithm to use. */
115 /*
116 * The number of lines contained in produced unidiff text output,
117 * and an array of got_diff_lines with byte offset and line type to
118 * each line. May be initialized to zero and NULL to ignore line
119 * metadata. If not NULL, then the array of line offsets and types will
120 * be populated. Optionally, the array can be pre-populated with line
121 * offsets and types, with nlines > 0 indicating the length of the
122 * pre-populated array. This is useful if the output file already
123 * contains some lines of text. The array will be grown as needed to
124 * accomodate additional offsets and types, and the last offset found
125 * in a pre-populated array will be added to all subsequent offsets.
126 */
127 size_t nlines;
128 struct got_diff_line *lines; /* Dispose of with free(3) when done. */
129 };
130 const struct got_error *got_diff_blob_output_unidiff(void *,
131 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
132 struct got_object_id *, struct got_object_id *,
133 const char *, const char *, mode_t, mode_t, struct got_repository *);
135 /*
136 * Compute the differences between two trees and invoke the provided
137 * got_diff_blob_cb() callback when content differs.
138 * Diffing of blob content can be suppressed by passing zero for the
139 * 'diff_content' parameter. The callback will then only receive blob
140 * object IDs and diff labels, but NULL pointers instead of blob objects.
141 * If 'diff_content' is set, two open temporary FILEs and two open
142 * temporary file descriptors must be provided for internal use; these
143 * files can be obtained from got_opentemp() and got_opentempfd(),
144 * and must be closed by the caller. Otherwise the files can be NULL.
145 * The set of arguments relating to either tree may be NULL to indicate
146 * that no content is present on its respective side of the diff.
147 */
148 const struct got_error *got_diff_tree(struct got_tree_object *,
149 struct got_tree_object *, FILE *, FILE *, int, int,
150 const char *, const char *,
151 struct got_repository *, got_diff_blob_cb cb, void *cb_arg, int);
153 /*
154 * Pre-defined implementations of got_diff_blob_cb(): the first of which
155 * collects a list of file paths that differ between two trees; the second
156 * also computes a diffstat of added/removed lines for each collected path
157 * and requires passing an initialized got_diffstat_cb_arg argument.
158 * The caller must allocate and initialize a got_pathlist_head * argument.
159 * Data pointers of entries added to the path list will point to a struct
160 * got_diff_changed_path object.
161 * The caller is expected to free both the path and data pointers of all
162 * entries on the path list.
163 */
164 struct got_diff_changed_path {
165 uint32_t add; /* number of lines added */
166 uint32_t rm; /* number of lines removed */
167 /*
168 * The modification status of this path. It can be GOT_STATUS_ADD,
169 * GOT_STATUS_DELETE, GOT_STATUS_MODIFY, or GOT_STATUS_MODE_CHANGE.
170 */
171 int status;
172 };
173 const struct got_error *got_diff_tree_collect_changed_paths(void *,
174 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
175 struct got_object_id *, struct got_object_id *,
176 const char *, const char *, mode_t, mode_t, struct got_repository *);
178 struct got_diffstat_cb_arg {
179 size_t max_path_len;
180 uint32_t ins;
181 uint32_t del;
182 int add_cols;
183 int rm_cols;
184 int nfiles;
185 struct got_pathlist_head *paths;
186 int ignore_ws;
187 int force_text;
188 enum got_diff_algorithm diff_algo;
189 };
190 const struct got_error *got_diff_tree_compute_diffstat(void *,
191 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
192 struct got_object_id *, struct got_object_id *, const char *, const char *,
193 mode_t, mode_t, struct got_repository *);
195 /*
196 * Diff two objects, assuming both objects are blobs. Two const char * diff
197 * header labels may be provided which will be used to identify each blob in
198 * the diff output. If a label is NULL, use the blob's SHA1 checksum instead.
199 * Two open temporary files and two temporary file descriptors must be
200 * provided for internal use; these files can be obtained from
201 * got_opentemp() and got_opentempfd(), and must be closed by the caller.
202 * The set of arguments relating to either blob may be NULL/-1 to indicate
203 * that no content is present on its respective side of the diff.
204 * The number of context lines to show in the diff must be specified as well.
205 * Write unified diff text to the provided output FILE.
206 * If not NULL, the two initial output arguments will be populated with an
207 * array of line offsets for, and the number of lines in, the unidiff text.
208 */
209 const struct got_error *got_diff_objects_as_blobs(struct got_diff_line **,
210 size_t *, FILE *, FILE *, int, int, struct got_object_id *,
211 struct got_object_id *, const char *, const char *, enum got_diff_algorithm,
212 int, int, int, int, struct got_diffstat_cb_arg *, struct got_repository *,
213 FILE *);
215 struct got_pathlist_head;
217 /*
218 * Diff two objects, assuming both objects are trees. Two const char * diff
219 * header labels may be provided which will be used to identify each blob in
220 * the trees. If a label is NULL, use the blob's SHA1 checksum instead.
221 * The number of context lines to show in diffs must be specified.
222 * Two open temporary files and two temporary file descriptors must be
223 * provided for internal use; these files can be obtained from
224 * got_opentemp() and got_opentempfd(), and must be closed by the caller.
225 * If 'diff_content' is not set, the files may be NULL / -1.
226 * The set of arguments relating to either tree may be NULL to indicate
227 * that no content is present on its respective side of the diff.
228 * Write unified diff text to the provided output FILE.
229 * If not NULL, the two initial output arguments will be populated with an
230 * array of line offsets for, and the number of lines in, the unidiff text.
231 */
232 const struct got_error *got_diff_objects_as_trees(struct got_diff_line **,
233 size_t *, FILE *, FILE *, int, int, struct got_object_id *,
234 struct got_object_id *, struct got_pathlist_head *, const char *,
235 const char *, enum got_diff_algorithm, int, int, int, int,
236 struct got_diffstat_cb_arg *, struct got_repository *, FILE *);
238 /*
239 * Diff two objects, assuming both objects are commits.
240 * The number of context lines to show in diffs must be specified.
241 * Two open temporary files and two temporary file descriptors must be
242 * provided for internal use; these files can be obtained from
243 * got_opentemp() and got_opentempfd(), and must be closed by the caller.
244 * The set of arguments relating to either commit may be NULL to indicate
245 * that no content is present on its respective side of the diff.
246 * Write unified diff text to the provided output FILE.
247 * If not NULL, the two initial output arguments will be populated with an
248 * array of line offsets for, and the number of lines in, the unidiff text.
249 */
250 const struct got_error *got_diff_objects_as_commits(struct got_diff_line **,
251 size_t *, FILE *, FILE *, int, int, struct got_object_id *,
252 struct got_object_id *, struct got_pathlist_head *, enum got_diff_algorithm,
253 int, int, int, int, struct got_diffstat_cb_arg *, struct got_repository *,
254 FILE *);
256 #define GOT_DIFF_MAX_CONTEXT 64