1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 module libgit2_d.patch;
8 
9 
10 private static import libgit2_d.buffer;
11 private static import libgit2_d.diff;
12 private static import libgit2_d.types;
13 
14 /**
15  * @file git2/patch.h
16  * @brief Patch handling routines.
17  * @ingroup Git
18  * @{
19  */
20 extern (C):
21 nothrow @nogc:
22 public:
23 
24 /**
25  * The diff patch is used to store all the text diffs for a delta.
26  *
27  * You can easily loop over the content of patches and get information about
28  * them.
29  */
30 struct git_patch;
31 
32 /**
33  * Return a patch for an entry in the diff list.
34  *
35  * The `git_patch` is a newly created object contains the text diffs
36  * for the delta.  You have to call `git_patch_free()` when you are
37  * done with it.  You can use the patch object to loop over all the hunks
38  * and lines in the diff of the one delta.
39  *
40  * For an unchanged file or a binary file, no `git_patch` will be
41  * created, the output will be set to null, and the `binary` flag will be
42  * set true in the `git_diff_delta` structure.
43  *
44  * It is okay to pass null for either of the output parameters; if you pass
45  * null for the `git_patch`, then the text diff will not be calculated.
46  *
47  * @param out_ Output parameter for the delta patch object
48  * @param diff Diff list object
49  * @param idx Index into diff list
50  * @return 0 on success, other value < 0 on error
51  */
52 //GIT_EXTERN
53 int git_patch_from_diff(.git_patch** out_, libgit2_d.diff.git_diff* diff, size_t idx);
54 
55 /**
56  * Directly generate a patch from the difference between two blobs.
57  *
58  * This is just like `git_diff_blobs()` except it generates a patch object
59  * for the difference instead of directly making callbacks.  You can use the
60  * standard `git_patch` accessor functions to read the patch data, and
61  * you must call `git_patch_free()` on the patch when done.
62  *
63  * @param out_ The generated patch; null on error
64  * @param old_blob Blob for old side of diff, or null for empty blob
65  * @param old_as_path Treat old blob as if it had this filename; can be null
66  * @param new_blob Blob for new side of diff, or null for empty blob
67  * @param new_as_path Treat new blob as if it had this filename; can be null
68  * @param opts Options for diff, or null for default options
69  * @return 0 on success or error code < 0
70  */
71 //GIT_EXTERN
72 int git_patch_from_blobs(.git_patch** out_, const (libgit2_d.types.git_blob)* old_blob, const (char)* old_as_path, const (libgit2_d.types.git_blob)* new_blob, const (char)* new_as_path, const (libgit2_d.diff.git_diff_options)* opts);
73 
74 /**
75  * Directly generate a patch from the difference between a blob and a buffer.
76  *
77  * This is just like `git_diff_blob_to_buffer()` except it generates a patch
78  * object for the difference instead of directly making callbacks.  You can
79  * use the standard `git_patch` accessor functions to read the patch
80  * data, and you must call `git_patch_free()` on the patch when done.
81  *
82  * @param out_ The generated patch; null on error
83  * @param old_blob Blob for old side of diff, or null for empty blob
84  * @param old_as_path Treat old blob as if it had this filename; can be null
85  * @param buffer Raw data for new side of diff, or null for empty
86  * @param buffer_len Length of raw data for new side of diff
87  * @param buffer_as_path Treat buffer as if it had this filename; can be null
88  * @param opts Options for diff, or null for default options
89  * @return 0 on success or error code < 0
90  */
91 //GIT_EXTERN
92 int git_patch_from_blob_and_buffer(.git_patch** out_, const (libgit2_d.types.git_blob)* old_blob, const (char)* old_as_path, const (void)* buffer, size_t buffer_len, const (char)* buffer_as_path, const (libgit2_d.diff.git_diff_options)* opts);
93 
94 /**
95  * Directly generate a patch from the difference between two buffers.
96  *
97  * This is just like `git_diff_buffers()` except it generates a patch
98  * object for the difference instead of directly making callbacks.  You can
99  * use the standard `git_patch` accessor functions to read the patch
100  * data, and you must call `git_patch_free()` on the patch when done.
101  *
102  * @param out_ The generated patch; null on error
103  * @param old_buffer Raw data for old side of diff, or null for empty
104  * @param old_len Length of the raw data for old side of the diff
105  * @param old_as_path Treat old buffer as if it had this filename; can be null
106  * @param new_buffer Raw data for new side of diff, or null for empty
107  * @param new_len Length of raw data for new side of diff
108  * @param new_as_path Treat buffer as if it had this filename; can be null
109  * @param opts Options for diff, or null for default options
110  * @return 0 on success or error code < 0
111  */
112 //GIT_EXTERN
113 int git_patch_from_buffers(.git_patch** out_, const (void)* old_buffer, size_t old_len, const (char)* old_as_path, const (void)* new_buffer, size_t new_len, const (char)* new_as_path, const (libgit2_d.diff.git_diff_options)* opts);
114 
115 /**
116  * Free a git_patch object.
117  */
118 //GIT_EXTERN
119 void git_patch_free(.git_patch* patch);
120 
121 /**
122  * Get the delta associated with a patch.  This delta points to internal
123  * data and you do not have to release it when you are done with it.
124  */
125 //GIT_EXTERN
126 const (libgit2_d.diff.git_diff_delta)* git_patch_get_delta(const (.git_patch)* patch);
127 
128 /**
129  * Get the number of hunks in a patch
130  */
131 //GIT_EXTERN
132 size_t git_patch_num_hunks(const (.git_patch)* patch);
133 
134 /**
135  * Get line counts of each type in a patch.
136  *
137  * This helps imitate a diff --numstat type of output.  For that purpose,
138  * you only need the `total_additions` and `total_deletions` values, but we
139  * include the `total_context` line count in case you want the total number
140  * of lines of diff output that will be generated.
141  *
142  * All outputs are optional. Pass null if you don't need a particular count.
143  *
144  * @param total_context Count of context lines in output, can be null.
145  * @param total_additions Count of addition lines in output, can be null.
146  * @param total_deletions Count of deletion lines in output, can be null.
147  * @param patch The git_patch object
148  * @return 0 on success, <0 on error
149  */
150 //GIT_EXTERN
151 int git_patch_line_stats(size_t* total_context, size_t* total_additions, size_t* total_deletions, const (.git_patch)* patch);
152 
153 /**
154  * Get the information about a hunk in a patch
155  *
156  * Given a patch and a hunk index into the patch, this returns detailed
157  * information about that hunk.  Any of the output pointers can be passed
158  * as null if you don't care about that particular piece of information.
159  *
160  * @param out_ Output pointer to git_diff_hunk of hunk
161  * @param lines_in_hunk Output count of total lines in this hunk
162  * @param patch Input pointer to patch object
163  * @param hunk_idx Input index of hunk to get information about
164  * @return 0 on success, git_error_code.GIT_ENOTFOUND if hunk_idx out of range, <0 on error
165  */
166 //GIT_EXTERN
167 int git_patch_get_hunk(const (libgit2_d.diff.git_diff_hunk)** out_, size_t* lines_in_hunk, .git_patch* patch, size_t hunk_idx);
168 
169 /**
170  * Get the number of lines in a hunk.
171  *
172  * @param patch The git_patch object
173  * @param hunk_idx Index of the hunk
174  * @return Number of lines in hunk or git_error_code.GIT_ENOTFOUND if invalid hunk index
175  */
176 //GIT_EXTERN
177 int git_patch_num_lines_in_hunk(const (.git_patch)* patch, size_t hunk_idx);
178 
179 /**
180  * Get data about a line in a hunk of a patch.
181  *
182  * Given a patch, a hunk index, and a line index in the hunk, this
183  * will return a lot of details about that line.  If you pass a hunk
184  * index larger than the number of hunks or a line index larger than
185  * the number of lines in the hunk, this will return -1.
186  *
187  * @param out_ The git_diff_line data for this line
188  * @param patch The patch to look in
189  * @param hunk_idx The index of the hunk
190  * @param line_of_hunk The index of the line in the hunk
191  * @return 0 on success, <0 on failure
192  */
193 //GIT_EXTERN
194 int git_patch_get_line_in_hunk(const (libgit2_d.diff.git_diff_line)** out_, .git_patch* patch, size_t hunk_idx, size_t line_of_hunk);
195 
196 /**
197  * Look up size of patch diff data in bytes
198  *
199  * This returns the raw size of the patch data.  This only includes the
200  * actual data from the lines of the diff, not the file or hunk headers.
201  *
202  * If you pass `include_context` as true (non-zero), this will be the size
203  * of all of the diff output; if you pass it as false (zero), this will
204  * only include the actual changed lines (as if `context_lines` was 0).
205  *
206  * @param patch A git_patch representing changes to one file
207  * @param include_context Include context lines in size if non-zero
208  * @param include_hunk_headers Include hunk header lines if non-zero
209  * @param include_file_headers Include file header lines if non-zero
210  * @return The number of bytes of data
211  */
212 //GIT_EXTERN
213 size_t git_patch_size(.git_patch* patch, int include_context, int include_hunk_headers, int include_file_headers);
214 
215 /**
216  * Serialize the patch to text via callback.
217  *
218  * Returning a non-zero value from the callback will terminate the iteration
219  * and return that value to the caller.
220  *
221  * @param patch A git_patch representing changes to one file
222  * @param print_cb Callback function to output lines of the patch.  Will be
223  *                 called for file headers, hunk headers, and diff lines.
224  * @param payload Reference pointer that will be passed to your callbacks.
225  * @return 0 on success, non-zero callback return value, or error code
226  */
227 //GIT_EXTERN
228 int git_patch_print(.git_patch* patch, libgit2_d.diff.git_diff_line_cb print_cb, void* payload);
229 
230 /**
231  * Get the content of a patch as a single diff text.
232  *
233  * @param out_ The git_buf to be filled in
234  * @param patch A git_patch representing changes to one file
235  * @return 0 on success, <0 on failure.
236  */
237 //GIT_EXTERN
238 int git_patch_to_buf(libgit2_d.buffer.git_buf* out_, .git_patch* patch);
239 
240 /**@}*/