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 /*
48 * Compute the differences between two blobs and write unified diff text
49 * to the provided output file. Two open temporary files must be provided
50 * for internal use; these files can be obtained from got_opentemp() and
51 * must be closed by the caller.
52 * If one of the blobs being diffed does not exist, all corresponding
53 * blob object arguments should be set to NULL.
54 * Two const char * diff header labels may be provided which will be used
55 * to identify each blob in the diff output.
56 * If a label is NULL, use the blob's SHA1 checksum instead.
57 * The number of context lines to show in the diff must be specified as well.
58 * Whitespace differences may optionally be ignored.
59 * If not NULL, the two initial output arguments will be populated with an
60 * array of line offsets for, and the number of lines in, the unidiff text.
61 */
62 const struct got_error *got_diff_blob(struct got_diff_line **, size_t *,
63 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
64 const char *, const char *, enum got_diff_algorithm, int, int, int,
65 FILE *);
67 /*
68 * Compute the differences between a blob and a file and write unified diff
69 * text to the provided output file. The blob object, its content, and its
70 * size must be provided. The file's size must be provided, as well as a
71 * const char * diff header label which identifies the file.
72 * An optional const char * diff header label for the blob may be provided, too.
73 * The number of context lines to show in the diff must be specified as well.
74 * Whitespace differences may optionally be ignored.
75 */
76 const struct got_error *got_diff_blob_file(struct got_blob_object *, FILE *,
77 off_t, const char *, FILE *, int, struct stat *, const char *,
78 enum got_diff_algorithm, int, int, int, FILE *);
80 /*
81 * A callback function invoked to handle the differences between two blobs
82 * when diffing trees with got_diff_tree(). This callback receives two blobs,
83 * their respective IDs, and two corresponding paths within the diffed trees.
84 * The first blob contains content from the old side of the diff, and
85 * the second blob contains content on the new side of the diff.
86 * Two open temporary files must be provided for internal use; these files
87 * can be obtained from got_opentemp() and must be closed by the caller.
88 * The blob object argument for either blob may be NULL to indicate
89 * that no content is present on its respective side of the diff.
90 * File modes from relevant tree objects which contain the blobs may
91 * also be passed. These will be zero if not available.
92 */
93 typedef const struct got_error *(*got_diff_blob_cb)(void *,
94 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
95 struct got_object_id *, struct got_object_id *,
96 const char *, const char *, mode_t, mode_t, struct got_repository *);
98 /*
99 * A pre-defined implementation of got_diff_blob_cb() which appends unidiff
100 * output to a file. The caller must allocate and fill in the argument
101 * structure.
102 */
103 struct got_diff_blob_output_unidiff_arg {
104 FILE *outfile; /* Unidiff text will be written here. */
105 int diff_context; /* Sets the number of context lines. */
106 int ignore_whitespace; /* Ignore whitespace differences. */
107 int force_text_diff; /* Assume text even if binary data detected. */
108 enum got_diff_algorithm diff_algo; /* Diffing algorithm to use. */
110 /*
111 * The number of lines contained in produced unidiff text output,
112 * and an array of got_diff_lines with byte offset and line type to
113 * each line. May be initialized to zero and NULL to ignore line
114 * metadata. If not NULL, then the array of line offsets and types will
115 * be populated. Optionally, the array can be pre-populated with line
116 * offsets and types, with nlines > 0 indicating the length of the
117 * pre-populated array. This is useful if the output file already
118 * contains some lines of text. The array will be grown as needed to
119 * accomodate additional offsets and types, and the last offset found
120 * in a pre-populated array will be added to all subsequent offsets.
121 */
122 size_t nlines;
123 struct got_diff_line *lines; /* Dispose of with free(3) when done. */
124 };
125 const struct got_error *got_diff_blob_output_unidiff(void *,
126 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
127 struct got_object_id *, struct got_object_id *,
128 const char *, const char *, mode_t, mode_t, struct got_repository *);
130 /*
131 * Compute the differences between two trees and invoke the provided
132 * got_diff_blob_cb() callback when content differs.
133 * Diffing of blob content can be suppressed by passing zero for the
134 * 'diff_content' parameter. The callback will then only receive blob
135 * object IDs and diff labels, but NULL pointers instead of blob objects.
136 * If 'diff_content' is set, two open temporary FILEs and two open
137 * temporary file descriptors must be provided for internal use; these
138 * files can be obtained from got_opentemp() and got_opentempfd(),
139 * and must be closed by the caller. Otherwise the files can be NULL.
140 * The set of arguments relating to either tree may be NULL to indicate
141 * that no content is present on its respective side of the diff.
142 */
143 const struct got_error *got_diff_tree(struct got_tree_object *,
144 struct got_tree_object *, FILE *, FILE *, int, int,
145 const char *, const char *,
146 struct got_repository *, got_diff_blob_cb cb, void *cb_arg, int);
148 /*
149 * A pre-defined implementation of got_diff_blob_cb() which collects a list
150 * of file paths that differ between two trees.
151 * The caller must allocate and initialize a got_pathlist_head * argument.
152 * Data pointers of entries added to the path list will point to a struct
153 * got_diff_changed_path object.
154 * The caller is expected to free both the path and data pointers of all
155 * entries on the path list.
156 */
157 struct got_diff_changed_path {
158 /*
159 * The modification status of this path. It can be GOT_STATUS_ADD,
160 * GOT_STATUS_DELETE, GOT_STATUS_MODIFY, or GOT_STATUS_MODE_CHANGE.
161 */
162 int status;
163 };
164 const struct got_error *got_diff_tree_collect_changed_paths(void *,
165 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
166 struct got_object_id *, struct got_object_id *,
167 const char *, const char *, mode_t, mode_t, struct got_repository *);
169 /*
170 * Diff two objects, assuming both objects are blobs. Two const char * diff
171 * header labels may be provided which will be used to identify each blob in
172 * the diff output. If a label is NULL, use the blob's SHA1 checksum instead.
173 * Two open temporary files and two temporary file descriptors must be
174 * provided for internal use; these files can be obtained from
175 * got_opentemp() and got_opentempfd(), and must be closed by the caller.
176 * The set of arguments relating to either blob may be NULL/-1 to indicate
177 * that no content is present on its respective side of the diff.
178 * The number of context lines to show in the diff must be specified as well.
179 * Write unified diff text to the provided output FILE.
180 * If not NULL, the two initial output arguments will be populated with an
181 * array of line offsets for, and the number of lines in, the unidiff text.
182 */
183 const struct got_error *got_diff_objects_as_blobs(struct got_diff_line **,
184 size_t *, FILE *, FILE *, int, int, struct got_object_id *,
185 struct got_object_id *, const char *, const char *, enum got_diff_algorithm,
186 int, int, int, struct got_repository *, FILE *);
188 struct got_pathlist_head;
190 /*
191 * Diff two objects, assuming both objects are trees. Two const char * diff
192 * header labels may be provided which will be used to identify each blob in
193 * the trees. If a label is NULL, use the blob's SHA1 checksum instead.
194 * The number of context lines to show in diffs must be specified.
195 * Two open temporary files and two temporary file descriptors must be
196 * provided for internal use; these files can be obtained from
197 * got_opentemp() and got_opentempfd(), and must be closed by the caller.
198 * If 'diff_content' is not set, the files may be NULL / -1.
199 * The set of arguments relating to either tree may be NULL to indicate
200 * that no content is present on its respective side of the diff.
201 * Write unified diff text to the provided output FILE.
202 * If not NULL, the two initial output arguments will be populated with an
203 * array of line offsets for, and the number of lines in, the unidiff text.
204 */
205 const struct got_error *got_diff_objects_as_trees(struct got_diff_line **,
206 size_t *, FILE *, FILE *, int, int, struct got_object_id *,
207 struct got_object_id *, struct got_pathlist_head *, const char *,
208 const char *, enum got_diff_algorithm, int, int, int,
209 struct got_repository *, FILE *);
211 /*
212 * Diff two objects, assuming both objects are commits.
213 * The number of context lines to show in diffs must be specified.
214 * Two open temporary files and two temporary file descriptors must be
215 * provided for internal use; these files can be obtained from
216 * got_opentemp() and got_opentempfd(), and must be closed by the caller.
217 * The set of arguments relating to either commit may be NULL to indicate
218 * that no content is present on its respective side of the diff.
219 * Write unified diff text to the provided output FILE.
220 * If not NULL, the two initial output arguments will be populated with an
221 * array of line offsets for, and the number of lines in, the unidiff text.
222 */
223 const struct got_error *got_diff_objects_as_commits(struct got_diff_line **,
224 size_t *, FILE *, FILE *, int, int, struct got_object_id *,
225 struct got_object_id *, struct got_pathlist_head *, enum got_diff_algorithm,
226 int, int, int, struct got_repository *, FILE *);
228 #define GOT_DIFF_MAX_CONTEXT 64