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 4b752015 2022-06-30 stsp enum got_diff_algorithm {
18 4b752015 2022-06-30 stsp GOT_DIFF_ALGORITHM_MYERS,
19 4b752015 2022-06-30 stsp GOT_DIFF_ALGORITHM_PATIENCE,
20 4b752015 2022-06-30 stsp };
21 4b752015 2022-06-30 stsp
22 0c60ce5a 2018-04-02 stsp /*
23 c7d5c43c 2022-08-04 mark * List of all line types in a diff (including '{got,tog} log' lines).
24 c7d5c43c 2022-08-04 mark * XXX GOT_DIFF_LINE_HUNK to GOT_DIFF_LINE_NONE inclusive must map to the
25 c7d5c43c 2022-08-04 mark * DIFF_LINE_* macro counterparts defined in lib/diff_output.h (i.e., 60-64).
26 c7d5c43c 2022-08-04 mark */
27 c7d5c43c 2022-08-04 mark enum got_diff_line_type {
28 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_LOGMSG,
29 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_AUTHOR,
30 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_DATE,
31 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_CHANGES,
32 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META,
33 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_BLOB_MIN,
34 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_BLOB_PLUS,
35 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_HUNK = 60,
36 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_MINUS,
37 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_PLUS,
38 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_CONTEXT,
39 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_NONE
40 c7d5c43c 2022-08-04 mark };
41 c7d5c43c 2022-08-04 mark
42 c7d5c43c 2022-08-04 mark struct got_diff_line {
43 c7d5c43c 2022-08-04 mark off_t offset;
44 c7d5c43c 2022-08-04 mark uint8_t type;
45 c7d5c43c 2022-08-04 mark };
46 c7d5c43c 2022-08-04 mark
47 a76e88e5 2023-01-10 mark struct got_diffstat_cb_arg;
48 a76e88e5 2023-01-10 mark
49 c7d5c43c 2022-08-04 mark /*
50 0c60ce5a 2018-04-02 stsp * Compute the differences between two blobs and write unified diff text
51 b72706c3 2022-06-01 stsp * to the provided output file. Two open temporary files must be provided
52 b72706c3 2022-06-01 stsp * for internal use; these files can be obtained from got_opentemp() and
53 b72706c3 2022-06-01 stsp * must be closed by the caller.
54 b72706c3 2022-06-01 stsp * If one of the blobs being diffed does not exist, all corresponding
55 49d4a017 2022-06-30 stsp * blob object arguments should be set to NULL.
56 b72706c3 2022-06-01 stsp * Two const char * diff header labels may be provided which will be used
57 b72706c3 2022-06-01 stsp * 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 c7d5c43c 2022-08-04 mark const struct got_error *got_diff_blob(struct got_diff_line **, size_t *,
65 b72706c3 2022-06-01 stsp struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
66 1f3405c9 2023-01-17 mark const char *, const char *, enum got_diff_algorithm, int, int, int,
67 a76e88e5 2023-01-10 mark 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 b72706c3 2022-06-01 stsp * text to the provided output file. The blob object, its content, and its
72 49d4a017 2022-06-30 stsp * size must be provided. The file's size must be provided, as well as a
73 b72706c3 2022-06-01 stsp * 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 b72706c3 2022-06-01 stsp const struct got_error *got_diff_blob_file(struct got_blob_object *, FILE *,
79 c87842d5 2022-09-23 mark off_t, const char *, FILE *, int, struct stat *, const char *,
80 1f3405c9 2023-01-17 mark enum got_diff_algorithm, int, int, int, struct got_diffstat_cb_arg *,
81 a76e88e5 2023-01-10 mark 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 b72706c3 2022-06-01 stsp * Two open temporary files must be provided for internal use; these files
90 b72706c3 2022-06-01 stsp * can be obtained from got_opentemp() and must be closed by the caller.
91 49d4a017 2022-06-30 stsp * 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 b72706c3 2022-06-01 stsp 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 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg *diffstat; /* Compute diffstat of changes */
112 4b752015 2022-06-30 stsp 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 c7d5c43c 2022-08-04 mark * and an array of got_diff_lines with byte offset and line type to
117 c7d5c43c 2022-08-04 mark * each line. May be initialized to zero and NULL to ignore line
118 c7d5c43c 2022-08-04 mark * metadata. If not NULL, then the array of line offsets and types will
119 c7d5c43c 2022-08-04 mark * be populated. Optionally, the array can be pre-populated with line
120 c7d5c43c 2022-08-04 mark * offsets and types, with nlines > 0 indicating the length of the
121 c7d5c43c 2022-08-04 mark * pre-populated array. This is useful if the output file already
122 c7d5c43c 2022-08-04 mark * contains some lines of text. The array will be grown as needed to
123 c7d5c43c 2022-08-04 mark * accomodate additional offsets and types, and the last offset found
124 c7d5c43c 2022-08-04 mark * 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 c7d5c43c 2022-08-04 mark 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 b72706c3 2022-06-01 stsp 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 f9d37699 2022-06-28 stsp * If 'diff_content' is set, two open temporary FILEs and two open
141 f9d37699 2022-06-28 stsp * temporary file descriptors must be provided for internal use; these
142 f9d37699 2022-06-28 stsp * files can be obtained from got_opentemp() and got_opentempfd(),
143 b72706c3 2022-06-01 stsp * and must be closed by the caller. Otherwise the files can be NULL.
144 b72706c3 2022-06-01 stsp * The set of arguments relating to either tree may be NULL to indicate
145 b72706c3 2022-06-01 stsp * 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 f9d37699 2022-06-28 stsp struct got_tree_object *, FILE *, FILE *, int, int,
149 f9d37699 2022-06-28 stsp 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 5191b70b 2023-01-07 mark * Pre-defined implementations of got_diff_blob_cb(): the first of which
154 5191b70b 2023-01-07 mark * collects a list of file paths that differ between two trees; the second
155 5191b70b 2023-01-07 mark * also computes a diffstat of added/removed lines for each collected path
156 5191b70b 2023-01-07 mark * 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 5191b70b 2023-01-07 mark uint32_t add; /* number of lines added */
165 5191b70b 2023-01-07 mark 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 b72706c3 2022-06-01 stsp 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 5191b70b 2023-01-07 mark struct got_diffstat_cb_arg {
178 5191b70b 2023-01-07 mark size_t max_path_len;
179 5191b70b 2023-01-07 mark uint32_t ins;
180 5191b70b 2023-01-07 mark uint32_t del;
181 5191b70b 2023-01-07 mark int add_cols;
182 5191b70b 2023-01-07 mark int rm_cols;
183 5191b70b 2023-01-07 mark int nfiles;
184 5191b70b 2023-01-07 mark struct got_pathlist_head *paths;
185 5191b70b 2023-01-07 mark int ignore_ws;
186 5191b70b 2023-01-07 mark int force_text;
187 5191b70b 2023-01-07 mark enum got_diff_algorithm diff_algo;
188 5191b70b 2023-01-07 mark };
189 5191b70b 2023-01-07 mark const struct got_error *got_diff_tree_compute_diffstat(void *,
190 5191b70b 2023-01-07 mark struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
191 5191b70b 2023-01-07 mark struct got_object_id *, struct got_object_id *, const char *, const char *,
192 5191b70b 2023-01-07 mark mode_t, mode_t, struct got_repository *);
193 5191b70b 2023-01-07 mark
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 f9d37699 2022-06-28 stsp * Two open temporary files and two temporary file descriptors must be
199 f9d37699 2022-06-28 stsp * provided for internal use; these files can be obtained from
200 f9d37699 2022-06-28 stsp * got_opentemp() and got_opentempfd(), and must be closed by the caller.
201 f9d37699 2022-06-28 stsp * The set of arguments relating to either blob may be NULL/-1 to indicate
202 b72706c3 2022-06-01 stsp * 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 c7d5c43c 2022-08-04 mark const struct got_error *got_diff_objects_as_blobs(struct got_diff_line **,
209 c7d5c43c 2022-08-04 mark size_t *, FILE *, FILE *, int, int, struct got_object_id *,
210 c7d5c43c 2022-08-04 mark struct got_object_id *, const char *, const char *, enum got_diff_algorithm,
211 1f3405c9 2023-01-17 mark int, int, int, struct got_diffstat_cb_arg *, struct got_repository *,
212 a76e88e5 2023-01-10 mark FILE *);
213 11528a82 2018-05-19 stsp
214 4b752015 2022-06-30 stsp struct got_pathlist_head;
215 4b752015 2022-06-30 stsp
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 f9d37699 2022-06-28 stsp * Two open temporary files and two temporary file descriptors must be
222 f9d37699 2022-06-28 stsp * provided for internal use; these files can be obtained from
223 f9d37699 2022-06-28 stsp * got_opentemp() and got_opentempfd(), and must be closed by the caller.
224 f9d37699 2022-06-28 stsp * If 'diff_content' is not set, the files may be NULL / -1.
225 b72706c3 2022-06-01 stsp * The set of arguments relating to either tree may be NULL to indicate
226 b72706c3 2022-06-01 stsp * 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 c7d5c43c 2022-08-04 mark const struct got_error *got_diff_objects_as_trees(struct got_diff_line **,
232 c7d5c43c 2022-08-04 mark size_t *, FILE *, FILE *, int, int, struct got_object_id *,
233 c7d5c43c 2022-08-04 mark struct got_object_id *, struct got_pathlist_head *, const char *,
234 1f3405c9 2023-01-17 mark const char *, enum got_diff_algorithm, int, int, int,
235 a76e88e5 2023-01-10 mark 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 f9d37699 2022-06-28 stsp * Two open temporary files and two temporary file descriptors must be
241 f9d37699 2022-06-28 stsp * provided for internal use; these files can be obtained from
242 f9d37699 2022-06-28 stsp * got_opentemp() and got_opentempfd(), and must be closed by the caller.
243 b72706c3 2022-06-01 stsp * The set of arguments relating to either commit may be NULL to indicate
244 b72706c3 2022-06-01 stsp * 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 c7d5c43c 2022-08-04 mark const struct got_error *got_diff_objects_as_commits(struct got_diff_line **,
250 c7d5c43c 2022-08-04 mark size_t *, FILE *, FILE *, int, int, struct got_object_id *,
251 c7d5c43c 2022-08-04 mark struct got_object_id *, struct got_pathlist_head *, enum got_diff_algorithm,
252 1f3405c9 2023-01-17 mark int, int, int, struct got_diffstat_cb_arg *, struct got_repository *,
253 a76e88e5 2023-01-10 mark FILE *);
254 4a8520aa 2018-10-18 stsp
255 4a8520aa 2018-10-18 stsp #define GOT_DIFF_MAX_CONTEXT 64