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