Blame


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