Blame


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