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