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