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