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.filter; 8 9 10 private static import libgit2_d.buffer; 11 private static import libgit2_d.sys.filter; 12 private static import libgit2_d.types; 13 14 /** 15 * @file git2/filter.h 16 * @brief Git filter APIs 17 * 18 * @ingroup Git 19 * @{ 20 */ 21 extern (C): 22 nothrow @nogc: 23 public: 24 25 /** 26 * Filters are applied in one of two directions: smudging - which is 27 * exporting a file from the Git object database to the working directory, 28 * and cleaning - which is importing a file from the working directory to 29 * the Git object database. These values control which direction of 30 * change is being applied. 31 */ 32 enum git_filter_mode_t 33 { 34 GIT_FILTER_TO_WORKTREE = 0, 35 GIT_FILTER_SMUDGE = GIT_FILTER_TO_WORKTREE, 36 GIT_FILTER_TO_ODB = 1, 37 GIT_FILTER_CLEAN = GIT_FILTER_TO_ODB, 38 } 39 40 /** 41 * Filter option flags. 42 */ 43 enum git_filter_flag_t 44 { 45 GIT_FILTER_DEFAULT = 0u, 46 47 /** 48 * Don't error for `safecrlf` violations, allow them to continue. 49 */ 50 GIT_FILTER_ALLOW_UNSAFE = 1u << 0, 51 52 /** 53 * Don't load `/etc/gitattributes` (or the system equivalent) 54 */ 55 GIT_FILTER_NO_SYSTEM_ATTRIBUTES = 1u << 1, 56 57 /** 58 * Load attributes from `.gitattributes` in the root of HEAD 59 */ 60 GIT_FILTER_ATTRIBUTES_FROM_HEAD = 1u << 2, 61 } 62 63 /** 64 * A filter that can transform file data 65 * 66 * This represents a filter that can be used to transform or even replace 67 * file data. Libgit2 includes one built in filter and it is possible to 68 * write your own (see git2/sys/filter.h for information on that). 69 * 70 * The two builtin filters are: 71 * 72 * * "crlf" which uses the complex rules with the "text", "eol", and 73 * "crlf" file attributes to decide how to convert between LF and CRLF 74 * line endings 75 * * "ident" which replaces "$Id$" in a blob with "$Id: <blob OID>$" upon 76 * checkout and replaced "$Id: <anything>$" with "$Id$" on checkin. 77 */ 78 alias git_filter = libgit2_d.sys.filter.git_filter; 79 80 /** 81 * List of filters to be applied 82 * 83 * This represents a list of filters to be applied to a file / blob. You 84 * can build the list with one call, apply it with another, and dispose it 85 * with a third. In typical usage, there are not many occasions where a 86 * git_filter_list is needed directly since the library will generally 87 * handle conversions for you, but it can be convenient to be able to 88 * build and apply the list sometimes. 89 */ 90 struct git_filter_list; 91 92 /** 93 * Load the filter list for a given path. 94 * 95 * This will return 0 (success) but set the output git_filter_list to null 96 * if no filters are requested for the given file. 97 * 98 * Params: 99 * filters = Output newly created git_filter_list (or null) 100 * repo = Repository object that contains `path` 101 * blob = The blob to which the filter will be applied (if known) 102 * path = Relative path of the file to be filtered 103 * mode = Filtering direction (WT->ODB or ODB->WT) 104 * flags = Combination of `git_filter_flag_t` flags 105 * 106 * Returns: 0 on success (which could still return null if no filters are needed for the requested file), <0 on error 107 */ 108 //GIT_EXTERN 109 int git_filter_list_load(.git_filter_list** filters, libgit2_d.types.git_repository* repo, libgit2_d.types.git_blob* blob, /* can be null */ 110 const (char)* path, .git_filter_mode_t mode, uint flags); 111 112 /** 113 * Query the filter list to see if a given filter (by name) will run. 114 * The built-in filters "crlf" and "ident" can be queried, otherwise this 115 * is the name of the filter specified by the filter attribute. 116 * 117 * This will return 0 if the given filter is not in the list, or 1 if 118 * the filter will be applied. 119 * 120 * Params: 121 * filters = A loaded git_filter_list (or null) 122 * name = The name of the filter to query 123 * 124 * Returns: 1 if the filter is in the list, 0 otherwise 125 */ 126 //GIT_EXTERN 127 int git_filter_list_contains(.git_filter_list* filters, const (char)* name); 128 129 /** 130 * Apply filter list to a data buffer. 131 * 132 * See `git2/buffer.h` for background on `git_buf` objects. 133 * 134 * If the `in` buffer holds data allocated by libgit2 (i.e. `in_->asize` is 135 * not zero), then it will be overwritten when applying the filters. If 136 * not, then it will be left untouched. 137 * 138 * If there are no filters to apply (or `filters` is null), then the `out` 139 * buffer will reference the `in` buffer data (with `asize` set to zero) 140 * instead of allocating data. This keeps allocations to a minimum, but 141 * it means you have to be careful about freeing the `in` data since `out` 142 * may be pointing to it! 143 * 144 * Params: 145 * out_ = Buffer to store the result of the filtering 146 * filters = A loaded git_filter_list (or null) 147 * in_ = Buffer containing the data to filter 148 * 149 * Returns: 0 on success, an error code otherwise 150 */ 151 //GIT_EXTERN 152 int git_filter_list_apply_to_data(libgit2_d.buffer.git_buf* out_, .git_filter_list* filters, libgit2_d.buffer.git_buf* in_); 153 154 /** 155 * Apply a filter list to the contents of a file on disk 156 * 157 * Params: 158 * out_ = buffer into which to store the filtered file 159 * filters = the list of filters to apply 160 * repo = the repository in which to perform the filtering 161 * path = the path of the file to filter, a relative path will be taken as relative to the workdir 162 */ 163 //GIT_EXTERN 164 int git_filter_list_apply_to_file(libgit2_d.buffer.git_buf* out_, .git_filter_list* filters, libgit2_d.types.git_repository* repo, const (char)* path); 165 166 /** 167 * Apply a filter list to the contents of a blob 168 * 169 * Params: 170 * out_ = buffer into which to store the filtered file 171 * filters = the list of filters to apply 172 * blob = the blob to filter 173 */ 174 //GIT_EXTERN 175 int git_filter_list_apply_to_blob(libgit2_d.buffer.git_buf* out_, .git_filter_list* filters, libgit2_d.types.git_blob* blob); 176 177 /** 178 * Apply a filter list to an arbitrary buffer as a stream 179 * 180 * Params: 181 * filters = the list of filters to apply 182 * data = the buffer to filter 183 * target = the stream into which the data will be written 184 */ 185 //GIT_EXTERN 186 int git_filter_list_stream_data(.git_filter_list* filters, libgit2_d.buffer.git_buf* data, libgit2_d.types.git_writestream* target); 187 188 /** 189 * Apply a filter list to a file as a stream 190 * 191 * Params: 192 * filters = the list of filters to apply 193 * repo = the repository in which to perform the filtering 194 * path = the path of the file to filter, a relative path will be taken as relative to the workdir 195 * target = the stream into which the data will be written 196 */ 197 //GIT_EXTERN 198 int git_filter_list_stream_file(.git_filter_list* filters, libgit2_d.types.git_repository* repo, const (char)* path, libgit2_d.types.git_writestream* target); 199 200 /** 201 * Apply a filter list to a blob as a stream 202 * 203 * Params: 204 * filters = the list of filters to apply 205 * blob = the blob to filter 206 * target = the stream into which the data will be written 207 */ 208 //GIT_EXTERN 209 int git_filter_list_stream_blob(.git_filter_list* filters, libgit2_d.types.git_blob* blob, libgit2_d.types.git_writestream* target); 210 211 /** 212 * Free a git_filter_list 213 * 214 * Params: 215 * filters = A git_filter_list created by `git_filter_list_load` 216 */ 217 //GIT_EXTERN 218 void git_filter_list_free(.git_filter_list* filters); 219 220 /** @} */