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