Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 enum got_diff_algorithm {
18 GOT_DIFF_ALGORITHM_MYERS,
19 GOT_DIFF_ALGORITHM_PATIENCE,
20 };
22 /*
23 * Compute the differences between two blobs and write unified diff text
24 * to the provided output file. Two open temporary files must be provided
25 * for internal use; these files can be obtained from got_opentemp() and
26 * must be closed by the caller.
27 * If one of the blobs being diffed does not exist, all corresponding
28 * blob object arguments should be set to NULL.
29 * Two const char * diff header labels may be provided which will be used
30 * to identify each blob in the diff output.
31 * If a label is NULL, use the blob's SHA1 checksum instead.
32 * The number of context lines to show in the diff must be specified as well.
33 * Whitespace differences may optionally be ignored.
34 * If not NULL, the two initial output arguments will be populated with an
35 * array of line offsets for, and the number of lines in, the unidiff text.
36 */
37 const struct got_error *got_diff_blob(off_t **, size_t *,
38 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
39 const char *, const char *, enum got_diff_algorithm, int, int, int,
40 FILE *);
42 /*
43 * Compute the differences between a blob and a file and write unified diff
44 * text to the provided output file. The blob object, its content, and its
45 * size must be provided. The file's size must be provided, as well as a
46 * const char * diff header label which identifies the file.
47 * An optional const char * diff header label for the blob may be provided, too.
48 * The number of context lines to show in the diff must be specified as well.
49 * Whitespace differences may optionally be ignored.
50 */
51 const struct got_error *got_diff_blob_file(struct got_blob_object *, FILE *,
52 off_t, const char *, FILE *, int, size_t, const char *,
53 enum got_diff_algorithm, int, int, int, FILE *);
55 /*
56 * A callback function invoked to handle the differences between two blobs
57 * when diffing trees with got_diff_tree(). This callback receives two blobs,
58 * their respective IDs, and two corresponding paths within the diffed trees.
59 * The first blob contains content from the old side of the diff, and
60 * the second blob contains content on the new side of the diff.
61 * Two open temporary files must be provided for internal use; these files
62 * can be obtained from got_opentemp() and must be closed by the caller.
63 * The blob object argument for either blob may be NULL to indicate
64 * that no content is present on its respective side of the diff.
65 * File modes from relevant tree objects which contain the blobs may
66 * also be passed. These will be zero if not available.
67 */
68 typedef const struct got_error *(*got_diff_blob_cb)(void *,
69 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
70 struct got_object_id *, struct got_object_id *,
71 const char *, const char *, mode_t, mode_t, struct got_repository *);
73 /*
74 * A pre-defined implementation of got_diff_blob_cb() which appends unidiff
75 * output to a file. The caller must allocate and fill in the argument
76 * structure.
77 */
78 struct got_diff_blob_output_unidiff_arg {
79 FILE *outfile; /* Unidiff text will be written here. */
80 int diff_context; /* Sets the number of context lines. */
81 int ignore_whitespace; /* Ignore whitespace differences. */
82 int force_text_diff; /* Assume text even if binary data detected. */
83 enum got_diff_algorithm diff_algo; /* Diffing algorithm to use. */
85 /*
86 * The number of lines contained in produced unidiff text output,
87 * and an array of byte offsets to each line. May be initialized to
88 * zero and NULL to ignore line offsets. If not NULL, then the line
89 * offsets array will be populated. Optionally, the array can be
90 * pre-populated with line offsets, with nlines > 0 indicating
91 * the length of the pre-populated array. This is useful if the
92 * output file already contains some lines of text.
93 * The array will be grown as needed to accomodate additional line
94 * offsets, and the last offset found in a pre-populated array will
95 * be added to all subsequent offsets.
96 */
97 size_t nlines;
98 off_t *line_offsets; /* Dispose of with free(3) when done. */
99 };
100 const struct got_error *got_diff_blob_output_unidiff(void *,
101 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
102 struct got_object_id *, struct got_object_id *,
103 const char *, const char *, mode_t, mode_t, struct got_repository *);
105 /*
106 * Compute the differences between two trees and invoke the provided
107 * got_diff_blob_cb() callback when content differs.
108 * Diffing of blob content can be suppressed by passing zero for the
109 * 'diff_content' parameter. The callback will then only receive blob
110 * object IDs and diff labels, but NULL pointers instead of blob objects.
111 * If 'diff_content' is set, two open temporary FILEs and two open
112 * temporary file descriptors must be provided for internal use; these
113 * files can be obtained from got_opentemp() and got_opentempfd(),
114 * and must be closed by the caller. Otherwise the files can be NULL.
115 * The set of arguments relating to either tree may be NULL to indicate
116 * that no content is present on its respective side of the diff.
117 */
118 const struct got_error *got_diff_tree(struct got_tree_object *,
119 struct got_tree_object *, FILE *, FILE *, int, int,
120 const char *, const char *,
121 struct got_repository *, got_diff_blob_cb cb, void *cb_arg, int);
123 /*
124 * A pre-defined implementation of got_diff_blob_cb() which collects a list
125 * of file paths that differ between two trees.
126 * The caller must allocate and initialize a got_pathlist_head * argument.
127 * Data pointers of entries added to the path list will point to a struct
128 * got_diff_changed_path object.
129 * The caller is expected to free both the path and data pointers of all
130 * entries on the path list.
131 */
132 struct got_diff_changed_path {
133 /*
134 * The modification status of this path. It can be GOT_STATUS_ADD,
135 * GOT_STATUS_DELETE, GOT_STATUS_MODIFY, or GOT_STATUS_MODE_CHANGE.
136 */
137 int status;
138 };
139 const struct got_error *got_diff_tree_collect_changed_paths(void *,
140 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
141 struct got_object_id *, struct got_object_id *,
142 const char *, const char *, mode_t, mode_t, struct got_repository *);
144 /*
145 * Diff two objects, assuming both objects are blobs. Two const char * diff
146 * header labels may be provided which will be used to identify each blob in
147 * the diff output. If a label is NULL, use the blob's SHA1 checksum instead.
148 * Two open temporary files and two temporary file descriptors must be
149 * provided for internal use; these files can be obtained from
150 * got_opentemp() and got_opentempfd(), and must be closed by the caller.
151 * The set of arguments relating to either blob may be NULL/-1 to indicate
152 * that no content is present on its respective side of the diff.
153 * The number of context lines to show in the diff must be specified as well.
154 * Write unified diff text to the provided output FILE.
155 * If not NULL, the two initial output arguments will be populated with an
156 * array of line offsets for, and the number of lines in, the unidiff text.
157 */
158 const struct got_error *got_diff_objects_as_blobs(off_t **, size_t *,
159 FILE *, FILE *, int, int, struct got_object_id *, struct got_object_id *,
160 const char *, const char *, enum got_diff_algorithm, int, int, int,
161 struct got_repository *, FILE *);
163 struct got_pathlist_head;
165 /*
166 * Diff two objects, assuming both objects are trees. Two const char * diff
167 * header labels may be provided which will be used to identify each blob in
168 * the trees. If a label is NULL, use the blob's SHA1 checksum instead.
169 * The number of context lines to show in diffs must be specified.
170 * Two open temporary files and two temporary file descriptors must be
171 * provided for internal use; these files can be obtained from
172 * got_opentemp() and got_opentempfd(), and must be closed by the caller.
173 * If 'diff_content' is not set, the files may be NULL / -1.
174 * The set of arguments relating to either tree may be NULL to indicate
175 * that no content is present on its respective side of the diff.
176 * Write unified diff text to the provided output FILE.
177 * If not NULL, the two initial output arguments will be populated with an
178 * array of line offsets for, and the number of lines in, the unidiff text.
179 */
180 const struct got_error *got_diff_objects_as_trees(off_t **, size_t *,
181 FILE *, FILE *, int, int, struct got_object_id *, struct got_object_id *,
182 struct got_pathlist_head *, const char *, const char *,
183 enum got_diff_algorithm, int, int, int, struct got_repository *, FILE *);
185 /*
186 * Diff two objects, assuming both objects are commits.
187 * The number of context lines to show in diffs must be specified.
188 * Two open temporary files and two temporary file descriptors must be
189 * provided for internal use; these files can be obtained from
190 * got_opentemp() and got_opentempfd(), and must be closed by the caller.
191 * The set of arguments relating to either commit may be NULL to indicate
192 * that no content is present on its respective side of the diff.
193 * Write unified diff text to the provided output FILE.
194 * If not NULL, the two initial output arguments will be populated with an
195 * array of line offsets for, and the number of lines in, the unidiff text.
196 */
197 const struct got_error *got_diff_objects_as_commits(off_t **, size_t *,
198 FILE *, FILE *, int, int, struct got_object_id *, struct got_object_id *,
199 struct got_pathlist_head *, enum got_diff_algorithm, int, int, int,
200 struct got_repository *, FILE *);
202 #define GOT_DIFF_MAX_CONTEXT 64