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