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 must be provided
106 * for internal use; these files can be obtained from got_opentemp()
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 *, const char *, const char *,
113 struct got_repository *, got_diff_blob_cb cb, void *cb_arg, int);
115 /*
116 * A pre-defined implementation of got_diff_blob_cb() which collects a list
117 * of file paths that differ between two trees.
118 * The caller must allocate and initialize a got_pathlist_head * argument.
119 * Data pointers of entries added to the path list will point to a struct
120 * got_diff_changed_path object.
121 * The caller is expected to free both the path and data pointers of all
122 * entries on the path list.
123 */
124 struct got_diff_changed_path {
125 /*
126 * The modification status of this path. It can be GOT_STATUS_ADD,
127 * GOT_STATUS_DELETE, GOT_STATUS_MODIFY, or GOT_STATUS_MODE_CHANGE.
128 */
129 int status;
130 };
131 const struct got_error *got_diff_tree_collect_changed_paths(void *,
132 struct got_blob_object *, struct got_blob_object *, FILE *, FILE *,
133 struct got_object_id *, struct got_object_id *,
134 const char *, const char *, mode_t, mode_t, struct got_repository *);
136 /*
137 * Diff two objects, assuming both objects are blobs. Two const char * diff
138 * header labels may be provided which will be used to identify each blob in
139 * the diff output. If a label is NULL, use the blob's SHA1 checksum instead.
140 * Two open temporary files must be provided for internal use; these files
141 * can be obtained from got_opentemp() and must be closed by the caller.
142 * The set of arguments relating to either blob may be NULL to indicate
143 * that no content is present on its respective side of the diff.
144 * The number of context lines to show in the diff must be specified as well.
145 * Write unified diff text to the provided output FILE.
146 * If not NULL, the two initial output arguments will be populated with an
147 * array of line offsets for, and the number of lines in, the unidiff text.
148 */
149 const struct got_error *got_diff_objects_as_blobs(off_t **, size_t *,
150 FILE *, FILE *, struct got_object_id *, struct got_object_id *,
151 const char *, const char *, int, int, int,
152 struct got_repository *, FILE *);
154 /*
155 * Diff two objects, assuming both objects are trees. Two const char * diff
156 * header labels may be provided which will be used to identify each blob in
157 * the trees. If a label is NULL, use the blob's SHA1 checksum instead.
158 * The number of context lines to show in diffs must be specified.
159 * Two open temporary files must be provided for internal use; these files
160 * can be obtained from got_opentemp() and must be closed by the caller.
161 * The set of arguments relating to either tree may be NULL to indicate
162 * that no content is present on its respective side of the diff.
163 * Write unified diff text to the provided output FILE.
164 * If not NULL, the two initial output arguments will be populated with an
165 * array of line offsets for, and the number of lines in, the unidiff text.
166 */
167 const struct got_error *got_diff_objects_as_trees(off_t **, size_t *,
168 FILE *, FILE *, struct got_object_id *, struct got_object_id *,
169 struct got_pathlist_head *, char *, char *, int, int, int,
170 struct got_repository *, FILE *);
172 /*
173 * Diff two objects, assuming both objects are commits.
174 * The number of context lines to show in diffs must be specified.
175 * Two open temporary files must be provided for internal use; these files
176 * can be obtained from got_opentemp() and must be closed by the caller.
177 * The set of arguments relating to either commit may be NULL to indicate
178 * that no content is present on its respective side of the diff.
179 * Write unified diff text to the provided output FILE.
180 * If not NULL, the two initial output arguments will be populated with an
181 * array of line offsets for, and the number of lines in, the unidiff text.
182 */
183 const struct got_error *got_diff_objects_as_commits(off_t **, size_t *,
184 FILE *, FILE *, struct got_object_id *, struct got_object_id *,
185 struct got_pathlist_head *, int, int, int, struct got_repository *, FILE *);
187 #define GOT_DIFF_MAX_CONTEXT 64