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 * Params: 48 * out_ = Output parameter for the delta patch object 49 * diff = Diff list object 50 * idx = Index into diff list 51 * 52 * Returns: 0 on success, other value < 0 on error 53 */ 54 //GIT_EXTERN 55 int git_patch_from_diff(.git_patch** out_, libgit2_d.diff.git_diff* diff, size_t idx); 56 57 /** 58 * Directly generate a patch from the difference between two blobs. 59 * 60 * This is just like `git_diff_blobs()` except it generates a patch object 61 * for the difference instead of directly making callbacks. You can use the 62 * standard `git_patch` accessor functions to read the patch data, and 63 * you must call `git_patch_free()` on the patch when done. 64 * 65 * Params: 66 * out_ = The generated patch; null on error 67 * old_blob = Blob for old side of diff, or null for empty blob 68 * old_as_path = Treat old blob as if it had this filename; can be null 69 * new_blob = Blob for new side of diff, or null for empty blob 70 * new_as_path = Treat new blob as if it had this filename; can be null 71 * opts = Options for diff, or null for default options 72 * 73 * Returns: 0 on success or error code < 0 74 */ 75 //GIT_EXTERN 76 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); 77 78 /** 79 * Directly generate a patch from the difference between a blob and a buffer. 80 * 81 * This is just like `git_diff_blob_to_buffer()` except it generates a patch 82 * object for the difference instead of directly making callbacks. You can 83 * use the standard `git_patch` accessor functions to read the patch 84 * data, and you must call `git_patch_free()` on the patch when done. 85 * 86 * Params: 87 * out_ = The generated patch; null on error 88 * old_blob = Blob for old side of diff, or null for empty blob 89 * old_as_path = Treat old blob as if it had this filename; can be null 90 * buffer = Raw data for new side of diff, or null for empty 91 * buffer_len = Length of raw data for new side of diff 92 * buffer_as_path = Treat buffer as if it had this filename; can be null 93 * opts = Options for diff, or null for default options 94 * 95 * Returns: 0 on success or error code < 0 96 */ 97 //GIT_EXTERN 98 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); 99 100 /** 101 * Directly generate a patch from the difference between two buffers. 102 * 103 * This is just like `git_diff_buffers()` except it generates a patch 104 * object for the difference instead of directly making callbacks. You can 105 * use the standard `git_patch` accessor functions to read the patch 106 * data, and you must call `git_patch_free()` on the patch when done. 107 * 108 * Params: 109 * out_ = The generated patch; null on error 110 * old_buffer = Raw data for old side of diff, or null for empty 111 * old_len = Length of the raw data for old side of the diff 112 * old_as_path = Treat old buffer as if it had this filename; can be null 113 * new_buffer = Raw data for new side of diff, or null for empty 114 * new_len = Length of raw data for new side of diff 115 * new_as_path = Treat buffer as if it had this filename; can be null 116 * opts = Options for diff, or null for default options 117 * 118 * Returns: 0 on success or error code < 0 119 */ 120 //GIT_EXTERN 121 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); 122 123 /** 124 * Free a git_patch object. 125 */ 126 //GIT_EXTERN 127 void git_patch_free(.git_patch* patch); 128 129 /** 130 * Get the delta associated with a patch. This delta points to internal 131 * data and you do not have to release it when you are done with it. 132 */ 133 //GIT_EXTERN 134 const (libgit2_d.diff.git_diff_delta)* git_patch_get_delta(const (.git_patch)* patch); 135 136 /** 137 * Get the number of hunks in a patch 138 */ 139 //GIT_EXTERN 140 size_t git_patch_num_hunks(const (.git_patch)* patch); 141 142 /** 143 * Get line counts of each type in a patch. 144 * 145 * This helps imitate a diff --numstat type of output. For that purpose, 146 * you only need the `total_additions` and `total_deletions` values, but we 147 * include the `total_context` line count in case you want the total number 148 * of lines of diff output that will be generated. 149 * 150 * All outputs are optional. Pass null if you don't need a particular count. 151 * 152 * Params: 153 * total_context = Count of context lines in output, can be null. 154 * total_additions = Count of addition lines in output, can be null. 155 * total_deletions = Count of deletion lines in output, can be null. 156 * patch = The git_patch object 157 * 158 * Returns: 0 on success, <0 on error 159 */ 160 //GIT_EXTERN 161 int git_patch_line_stats(size_t* total_context, size_t* total_additions, size_t* total_deletions, const (.git_patch)* patch); 162 163 /** 164 * Get the information about a hunk in a patch 165 * 166 * Given a patch and a hunk index into the patch, this returns detailed 167 * information about that hunk. Any of the output pointers can be passed 168 * as null if you don't care about that particular piece of information. 169 * 170 * Params: 171 * out_ = Output pointer to git_diff_hunk of hunk 172 * lines_in_hunk = Output count of total lines in this hunk 173 * patch = Input pointer to patch object 174 * hunk_idx = Input index of hunk to get information about 175 * 176 * Returns: 0 on success, git_error_code.GIT_ENOTFOUND if hunk_idx out of range, <0 on error 177 */ 178 //GIT_EXTERN 179 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); 180 181 /** 182 * Get the number of lines in a hunk. 183 * 184 * Params: 185 * patch = The git_patch object 186 * hunk_idx = Index of the hunk 187 * 188 * Returns: Number of lines in hunk or git_error_code.GIT_ENOTFOUND if invalid hunk index 189 */ 190 //GIT_EXTERN 191 int git_patch_num_lines_in_hunk(const (.git_patch)* patch, size_t hunk_idx); 192 193 /** 194 * Get data about a line in a hunk of a patch. 195 * 196 * Given a patch, a hunk index, and a line index in the hunk, this 197 * will return a lot of details about that line. If you pass a hunk 198 * index larger than the number of hunks or a line index larger than 199 * the number of lines in the hunk, this will return -1. 200 * 201 * Params: 202 * out_ = The git_diff_line data for this line 203 * patch = The patch to look in 204 * hunk_idx = The index of the hunk 205 * line_of_hunk = The index of the line in the hunk 206 * 207 * Returns: 0 on success, <0 on failure 208 */ 209 //GIT_EXTERN 210 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); 211 212 /** 213 * Look up size of patch diff data in bytes 214 * 215 * This returns the raw size of the patch data. This only includes the 216 * actual data from the lines of the diff, not the file or hunk headers. 217 * 218 * If you pass `include_context` as true (non-zero), this will be the size 219 * of all of the diff output; if you pass it as false (zero), this will 220 * only include the actual changed lines (as if `context_lines` was 0). 221 * 222 * Params: 223 * patch = A git_patch representing changes to one file 224 * include_context = Include context lines in size if non-zero 225 * include_hunk_headers = Include hunk header lines if non-zero 226 * include_file_headers = Include file header lines if non-zero 227 * 228 * Returns: The number of bytes of data 229 */ 230 //GIT_EXTERN 231 size_t git_patch_size(.git_patch* patch, int include_context, int include_hunk_headers, int include_file_headers); 232 233 /** 234 * Serialize the patch to text via callback. 235 * 236 * Returning a non-zero value from the callback will terminate the iteration 237 * and return that value to the caller. 238 * 239 * Params: 240 * patch = A git_patch representing changes to one file 241 * print_cb = Callback function to output lines of the patch. Will be called for file headers, hunk headers, and diff lines. 242 * payload = Reference pointer that will be passed to your callbacks. 243 * 244 * Returns: 0 on success, non-zero callback return value, or error code 245 */ 246 //GIT_EXTERN 247 int git_patch_print(.git_patch* patch, libgit2_d.diff.git_diff_line_cb print_cb, void* payload); 248 249 /** 250 * Get the content of a patch as a single diff text. 251 * 252 * Params: 253 * out_ = The git_buf to be filled in 254 * patch = A git_patch representing changes to one file 255 * 256 * Returns: 0 on success, <0 on failure. 257 */ 258 //GIT_EXTERN 259 int git_patch_to_buf(libgit2_d.buffer.git_buf* out_, .git_patch* patch); 260 261 /**@}*/