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