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