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 /** 8 * License: GPL-2.0(Linking Exception) 9 */ 10 module libgit2.filter; 11 12 13 private static import libgit2.buffer; 14 private static import libgit2.oid; 15 private static import libgit2.sys.filter; 16 private static import libgit2.types; 17 private import libgit2.common: GIT_EXTERN; 18 19 /* 20 * @file git2/filter.h 21 * @brief Git filter APIs 22 * 23 * @ingroup Git 24 * @{ 25 */ 26 extern (C): 27 nothrow @nogc: 28 public: 29 30 /** 31 * Filters are applied in one of two directions: smudging - which is 32 * exporting a file from the Git object database to the working directory, 33 * and cleaning - which is importing a file from the working directory to 34 * the Git object database. These values control which direction of 35 * change is being applied. 36 */ 37 enum git_filter_mode_t 38 { 39 GIT_FILTER_TO_WORKTREE = 0, 40 GIT_FILTER_SMUDGE = GIT_FILTER_TO_WORKTREE, 41 GIT_FILTER_TO_ODB = 1, 42 GIT_FILTER_CLEAN = GIT_FILTER_TO_ODB, 43 } 44 45 //Declaration name in C language 46 enum 47 { 48 GIT_FILTER_TO_WORKTREE = .git_filter_mode_t.GIT_FILTER_TO_WORKTREE, 49 GIT_FILTER_SMUDGE = .git_filter_mode_t.GIT_FILTER_SMUDGE, 50 GIT_FILTER_TO_ODB = .git_filter_mode_t.GIT_FILTER_TO_ODB, 51 GIT_FILTER_CLEAN = .git_filter_mode_t.GIT_FILTER_CLEAN, 52 } 53 54 /** 55 * Filter option flags. 56 */ 57 enum git_filter_flag_t 58 { 59 GIT_FILTER_DEFAULT = 0u, 60 61 /** 62 * Don't error for `safecrlf` violations, allow them to continue. 63 */ 64 GIT_FILTER_ALLOW_UNSAFE = 1u << 0, 65 66 /** 67 * Don't load `/etc/gitattributes` (or the system equivalent) 68 */ 69 GIT_FILTER_NO_SYSTEM_ATTRIBUTES = 1u << 1, 70 71 /** 72 * Load attributes from `.gitattributes` in the root of HEAD 73 */ 74 GIT_FILTER_ATTRIBUTES_FROM_HEAD = 1u << 2, 75 76 /** 77 * Load attributes from `.gitattributes` in a given commit. 78 * This can only be specified in a `git_filter_options`. 79 */ 80 GIT_FILTER_ATTRIBUTES_FROM_COMMIT = 1u << 3, 81 } 82 83 //Declaration name in C language 84 enum 85 { 86 GIT_FILTER_DEFAULT = .git_filter_flag_t.GIT_FILTER_DEFAULT, 87 GIT_FILTER_ALLOW_UNSAFE = .git_filter_flag_t.GIT_FILTER_ALLOW_UNSAFE, 88 GIT_FILTER_NO_SYSTEM_ATTRIBUTES = .git_filter_flag_t.GIT_FILTER_NO_SYSTEM_ATTRIBUTES, 89 GIT_FILTER_ATTRIBUTES_FROM_HEAD = .git_filter_flag_t.GIT_FILTER_ATTRIBUTES_FROM_HEAD, 90 GIT_FILTER_ATTRIBUTES_FROM_COMMIT = .git_filter_flag_t.GIT_FILTER_ATTRIBUTES_FROM_COMMIT, 91 } 92 93 /** 94 * Filtering options 95 */ 96 struct git_filter_options 97 { 98 uint version_; 99 100 /** 101 * See `git_filter_flag_t` above 102 */ 103 uint flags; 104 105 version (GIT_DEPRECATE_HARD) { 106 void* reserved; 107 } else { 108 libgit2.oid.git_oid* commit_id; 109 } 110 111 /** 112 * The commit to load attributes from, when 113 * `GIT_FILTER_ATTRIBUTES_FROM_COMMIT` is specified. 114 */ 115 libgit2.oid.git_oid attr_commit_id; 116 } 117 118 enum GIT_FILTER_OPTIONS_VERSION = 1; 119 120 pragma(inline, true) 121 pure nothrow @safe @nogc @live 122 .git_filter_options GIT_FILTER_OPTIONS_INIT() 123 124 do 125 { 126 .git_filter_options OUTPUT = 127 { 128 version_: .GIT_FILTER_OPTIONS_VERSION, 129 }; 130 131 return OUTPUT; 132 } 133 134 /** 135 * A filter that can transform file data 136 * 137 * This represents a filter that can be used to transform or even replace 138 * file data. Libgit2 includes one built in filter and it is possible to 139 * write your own (see git2/sys/filter.h for information on that). 140 * 141 * The two builtin filters are: 142 * 143 * * "crlf" which uses the complex rules with the "text", "eol", and 144 * "crlf" file attributes to decide how to convert between LF and CRLF 145 * line endings 146 * * "ident" which replaces "$Id$" in a blob with "$Id: <blob OID>$" upon 147 * checkout and replaced "$Id: <anything>$" with "$Id$" on checkin. 148 */ 149 alias git_filter = libgit2.sys.filter.git_filter; 150 151 /** 152 * List of filters to be applied 153 * 154 * This represents a list of filters to be applied to a file / blob. You 155 * can build the list with one call, apply it with another, and dispose it 156 * with a third. In typical usage, there are not many occasions where a 157 * git_filter_list is needed directly since the library will generally 158 * handle conversions for you, but it can be convenient to be able to 159 * build and apply the list sometimes. 160 */ 161 struct git_filter_list; 162 163 /** 164 * Load the filter list for a given path. 165 * 166 * This will return 0 (success) but set the output git_filter_list to null 167 * if no filters are requested for the given file. 168 * 169 * Params: 170 * filters = Output newly created git_filter_list (or null) 171 * repo = Repository object that contains `path` 172 * blob = The blob to which the filter will be applied (if known) 173 * path = Relative path of the file to be filtered 174 * mode = Filtering direction (WT->ODB or ODB->WT) 175 * flags = Combination of `git_filter_flag_t` flags 176 * 177 * Returns: 0 on success (which could still return null if no filters are needed for the requested file), <0 on error 178 */ 179 @GIT_EXTERN 180 int git_filter_list_load(.git_filter_list** filters, libgit2.types.git_repository* repo, libgit2.types.git_blob* blob, /* can be null */ 181 const (char)* path, .git_filter_mode_t mode, uint flags); 182 183 /** 184 * Load the filter list for a given path. 185 * 186 * This will return 0 (success) but set the output git_filter_list to null 187 * if no filters are requested for the given file. 188 * 189 * Params: 190 * filters = Output newly created git_filter_list (or null) 191 * repo = Repository object that contains `path` 192 * blob = The blob to which the filter will be applied (if known) 193 * path = Relative path of the file to be filtered 194 * mode = Filtering direction (WT->ODB or ODB->WT) 195 * opts = The `git_filter_options` to use when loading filters 196 * 197 * Returns: 0 on success (which could still return null if no filters are needed for the requested file), <0 on error 198 */ 199 @GIT_EXTERN 200 int git_filter_list_load_ext(.git_filter_list** filters, libgit2.types.git_repository* repo, libgit2.types.git_blob* blob, const (char)* path, .git_filter_mode_t mode, .git_filter_options* opts); 201 202 /** 203 * Query the filter list to see if a given filter (by name) will run. 204 * The built-in filters "crlf" and "ident" can be queried, otherwise this 205 * is the name of the filter specified by the filter attribute. 206 * 207 * This will return 0 if the given filter is not in the list, or 1 if 208 * the filter will be applied. 209 * 210 * Params: 211 * filters = A loaded git_filter_list (or null) 212 * name = The name of the filter to query 213 * 214 * Returns: 1 if the filter is in the list, 0 otherwise 215 */ 216 @GIT_EXTERN 217 int git_filter_list_contains(.git_filter_list* filters, const (char)* name); 218 219 /** 220 * Apply filter list to a data buffer. 221 * 222 * Params: 223 * out_ = Buffer to store the result of the filtering 224 * filters = A loaded git_filter_list (or null) 225 * in_ = Buffer containing the data to filter 226 * in_len = The length of the input buffer 227 * 228 * Returns: 0 on success, an error code otherwise 229 */ 230 @GIT_EXTERN 231 int git_filter_list_apply_to_buffer(libgit2.buffer.git_buf* out_, .git_filter_list* filters, const (char)* in_, size_t in_len); 232 233 /** 234 * Apply a filter list to the contents of a file on disk 235 * 236 * Params: 237 * out_ = buffer into which to store the filtered file 238 * filters = the list of filters to apply 239 * repo = the repository in which to perform the filtering 240 * path = the path of the file to filter, a relative path will be taken as relative to the workdir 241 * 242 * Returns: 0 or an error code. 243 */ 244 @GIT_EXTERN 245 int git_filter_list_apply_to_file(libgit2.buffer.git_buf* out_, .git_filter_list* filters, libgit2.types.git_repository* repo, const (char)* path); 246 247 /** 248 * Apply a filter list to the contents of a blob 249 * 250 * Params: 251 * out_ = buffer into which to store the filtered file 252 * filters = the list of filters to apply 253 * blob = the blob to filter 254 * 255 * Returns: 0 or an error code. 256 */ 257 @GIT_EXTERN 258 int git_filter_list_apply_to_blob(libgit2.buffer.git_buf* out_, .git_filter_list* filters, libgit2.types.git_blob* blob); 259 260 /** 261 * Apply a filter list to an arbitrary buffer as a stream 262 * 263 * Params: 264 * filters = the list of filters to apply 265 * data = the buffer to filter 266 * len = the size of the buffer 267 * target = the stream into which the data will be written 268 * 269 * Returns: 0 or an error code. 270 */ 271 @GIT_EXTERN 272 int git_filter_list_stream_buffer(.git_filter_list* filters, const (char)* data, size_t len, libgit2.types.git_writestream* target); 273 274 /** 275 * Apply a filter list to a file as a stream 276 * 277 * Params: 278 * filters = the list of filters to apply 279 * repo = the repository in which to perform the filtering 280 * path = the path of the file to filter, a relative path will be taken as relative to the workdir 281 * target = the stream into which the data will be written 282 * 283 * Returns: 0 or an error code. 284 */ 285 @GIT_EXTERN 286 int git_filter_list_stream_file(.git_filter_list* filters, libgit2.types.git_repository* repo, const (char)* path, libgit2.types.git_writestream* target); 287 288 /** 289 * Apply a filter list to a blob as a stream 290 * 291 * Params: 292 * filters = the list of filters to apply 293 * blob = the blob to filter 294 * target = the stream into which the data will be written 295 * 296 * Returns: 0 or an error code. 297 */ 298 @GIT_EXTERN 299 int git_filter_list_stream_blob(.git_filter_list* filters, libgit2.types.git_blob* blob, libgit2.types.git_writestream* target); 300 301 /** 302 * Free a git_filter_list 303 * 304 * Params: 305 * filters = A git_filter_list created by `git_filter_list_load` 306 */ 307 @GIT_EXTERN 308 void git_filter_list_free(.git_filter_list* filters); 309 310 /* @} */