|
||||
File indexing completed on 2025-01-18 09:59:36
0001 /* 0002 * Copyright (C) the libgit2 contributors. All rights reserved. 0003 * 0004 * This file is part of libgit2, distributed under the GNU GPL v2 with 0005 * a Linking Exception. For full terms see the included COPYING file. 0006 */ 0007 #ifndef INCLUDE_sys_git_filter_h__ 0008 #define INCLUDE_sys_git_filter_h__ 0009 0010 #include "git2/filter.h" 0011 0012 /** 0013 * @file git2/sys/filter.h 0014 * @brief Git filter backend and plugin routines 0015 * @defgroup git_backend Git custom backend APIs 0016 * @ingroup Git 0017 * @{ 0018 */ 0019 GIT_BEGIN_DECL 0020 0021 /** 0022 * Look up a filter by name 0023 * 0024 * @param name The name of the filter 0025 * @return Pointer to the filter object or NULL if not found 0026 */ 0027 GIT_EXTERN(git_filter *) git_filter_lookup(const char *name); 0028 0029 #define GIT_FILTER_CRLF "crlf" 0030 #define GIT_FILTER_IDENT "ident" 0031 0032 /** 0033 * This is priority that the internal CRLF filter will be registered with 0034 */ 0035 #define GIT_FILTER_CRLF_PRIORITY 0 0036 0037 /** 0038 * This is priority that the internal ident filter will be registered with 0039 */ 0040 #define GIT_FILTER_IDENT_PRIORITY 100 0041 0042 /** 0043 * This is priority to use with a custom filter to imitate a core Git 0044 * filter driver, so that it will be run last on checkout and first on 0045 * checkin. You do not have to use this, but it helps compatibility. 0046 */ 0047 #define GIT_FILTER_DRIVER_PRIORITY 200 0048 0049 /** 0050 * Create a new empty filter list 0051 * 0052 * Normally you won't use this because `git_filter_list_load` will create 0053 * the filter list for you, but you can use this in combination with the 0054 * `git_filter_lookup` and `git_filter_list_push` functions to assemble 0055 * your own chains of filters. 0056 */ 0057 GIT_EXTERN(int) git_filter_list_new( 0058 git_filter_list **out, 0059 git_repository *repo, 0060 git_filter_mode_t mode, 0061 uint32_t options); 0062 0063 /** 0064 * Add a filter to a filter list with the given payload. 0065 * 0066 * Normally you won't have to do this because the filter list is created 0067 * by calling the "check" function on registered filters when the filter 0068 * attributes are set, but this does allow more direct manipulation of 0069 * filter lists when desired. 0070 * 0071 * Note that normally the "check" function can set up a payload for the 0072 * filter. Using this function, you can either pass in a payload if you 0073 * know the expected payload format, or you can pass NULL. Some filters 0074 * may fail with a NULL payload. Good luck! 0075 */ 0076 GIT_EXTERN(int) git_filter_list_push( 0077 git_filter_list *fl, git_filter *filter, void *payload); 0078 0079 /** 0080 * Look up how many filters are in the list 0081 * 0082 * We will attempt to apply all of these filters to any data passed in, 0083 * but note that the filter apply action still has the option of skipping 0084 * data that is passed in (for example, the CRLF filter will skip data 0085 * that appears to be binary). 0086 * 0087 * @param fl A filter list 0088 * @return The number of filters in the list 0089 */ 0090 GIT_EXTERN(size_t) git_filter_list_length(const git_filter_list *fl); 0091 0092 /** 0093 * A filter source represents a file/blob to be processed 0094 */ 0095 typedef struct git_filter_source git_filter_source; 0096 0097 /** 0098 * Get the repository that the source data is coming from. 0099 */ 0100 GIT_EXTERN(git_repository *) git_filter_source_repo(const git_filter_source *src); 0101 0102 /** 0103 * Get the path that the source data is coming from. 0104 */ 0105 GIT_EXTERN(const char *) git_filter_source_path(const git_filter_source *src); 0106 0107 /** 0108 * Get the file mode of the source file 0109 * If the mode is unknown, this will return 0 0110 */ 0111 GIT_EXTERN(uint16_t) git_filter_source_filemode(const git_filter_source *src); 0112 0113 /** 0114 * Get the OID of the source 0115 * If the OID is unknown (often the case with GIT_FILTER_CLEAN) then 0116 * this will return NULL. 0117 */ 0118 GIT_EXTERN(const git_oid *) git_filter_source_id(const git_filter_source *src); 0119 0120 /** 0121 * Get the git_filter_mode_t to be used 0122 */ 0123 GIT_EXTERN(git_filter_mode_t) git_filter_source_mode(const git_filter_source *src); 0124 0125 /** 0126 * Get the combination git_filter_flag_t options to be applied 0127 */ 0128 GIT_EXTERN(uint32_t) git_filter_source_flags(const git_filter_source *src); 0129 0130 /** 0131 * Initialize callback on filter 0132 * 0133 * Specified as `filter.initialize`, this is an optional callback invoked 0134 * before a filter is first used. It will be called once at most. 0135 * 0136 * If non-NULL, the filter's `initialize` callback will be invoked right 0137 * before the first use of the filter, so you can defer expensive 0138 * initialization operations (in case libgit2 is being used in a way that 0139 * doesn't need the filter). 0140 */ 0141 typedef int GIT_CALLBACK(git_filter_init_fn)(git_filter *self); 0142 0143 /** 0144 * Shutdown callback on filter 0145 * 0146 * Specified as `filter.shutdown`, this is an optional callback invoked 0147 * when the filter is unregistered or when libgit2 is shutting down. It 0148 * will be called once at most and should release resources as needed. 0149 * This may be called even if the `initialize` callback was not made. 0150 * 0151 * Typically this function will free the `git_filter` object itself. 0152 */ 0153 typedef void GIT_CALLBACK(git_filter_shutdown_fn)(git_filter *self); 0154 0155 /** 0156 * Callback to decide if a given source needs this filter 0157 * 0158 * Specified as `filter.check`, this is an optional callback that checks 0159 * if filtering is needed for a given source. 0160 * 0161 * It should return 0 if the filter should be applied (i.e. success), 0162 * GIT_PASSTHROUGH if the filter should not be applied, or an error code 0163 * to fail out of the filter processing pipeline and return to the caller. 0164 * 0165 * The `attr_values` will be set to the values of any attributes given in 0166 * the filter definition. See `git_filter` below for more detail. 0167 * 0168 * The `payload` will be a pointer to a reference payload for the filter. 0169 * This will start as NULL, but `check` can assign to this pointer for 0170 * later use by the `stream` callback. Note that the value should be heap 0171 * allocated (not stack), so that it doesn't go away before the `stream` 0172 * callback can use it. If a filter allocates and assigns a value to the 0173 * `payload`, it will need a `cleanup` callback to free the payload. 0174 */ 0175 typedef int GIT_CALLBACK(git_filter_check_fn)( 0176 git_filter *self, 0177 void **payload, /* NULL on entry, may be set */ 0178 const git_filter_source *src, 0179 const char **attr_values); 0180 0181 #ifndef GIT_DEPRECATE_HARD 0182 /** 0183 * Callback to actually perform the data filtering 0184 * 0185 * Specified as `filter.apply`, this is the callback that actually filters 0186 * data. If it successfully writes the output, it should return 0. Like 0187 * `check`, it can return GIT_PASSTHROUGH to indicate that the filter 0188 * doesn't want to run. Other error codes will stop filter processing and 0189 * return to the caller. 0190 * 0191 * The `payload` value will refer to any payload that was set by the 0192 * `check` callback. It may be read from or written to as needed. 0193 * 0194 * @deprecated use git_filter_stream_fn 0195 */ 0196 typedef int GIT_CALLBACK(git_filter_apply_fn)( 0197 git_filter *self, 0198 void **payload, /* may be read and/or set */ 0199 git_buf *to, 0200 const git_buf *from, 0201 const git_filter_source *src); 0202 #endif 0203 0204 /** 0205 * Callback to perform the data filtering. 0206 * 0207 * Specified as `filter.stream`, this is a callback that filters data 0208 * in a streaming manner. This function will provide a 0209 * `git_writestream` that will the original data will be written to; 0210 * with that data, the `git_writestream` will then perform the filter 0211 * translation and stream the filtered data out to the `next` location. 0212 */ 0213 typedef int GIT_CALLBACK(git_filter_stream_fn)( 0214 git_writestream **out, 0215 git_filter *self, 0216 void **payload, 0217 const git_filter_source *src, 0218 git_writestream *next); 0219 0220 /** 0221 * Callback to clean up after filtering has been applied 0222 * 0223 * Specified as `filter.cleanup`, this is an optional callback invoked 0224 * after the filter has been applied. If the `check`, `apply`, or 0225 * `stream` callbacks allocated a `payload` to keep per-source filter 0226 * state, use this callback to free that payload and release resources 0227 * as required. 0228 */ 0229 typedef void GIT_CALLBACK(git_filter_cleanup_fn)( 0230 git_filter *self, 0231 void *payload); 0232 0233 /** 0234 * Filter structure used to register custom filters. 0235 * 0236 * To associate extra data with a filter, allocate extra data and put the 0237 * `git_filter` struct at the start of your data buffer, then cast the 0238 * `self` pointer to your larger structure when your callback is invoked. 0239 */ 0240 struct git_filter { 0241 /** The `version` field should be set to `GIT_FILTER_VERSION`. */ 0242 unsigned int version; 0243 0244 /** 0245 * A whitespace-separated list of attribute names to check for this 0246 * filter (e.g. "eol crlf text"). If the attribute name is bare, it 0247 * will be simply loaded and passed to the `check` callback. If it 0248 * has a value (i.e. "name=value"), the attribute must match that 0249 * value for the filter to be applied. The value may be a wildcard 0250 * (eg, "name=*"), in which case the filter will be invoked for any 0251 * value for the given attribute name. See the attribute parameter 0252 * of the `check` callback for the attribute value that was specified. 0253 */ 0254 const char *attributes; 0255 0256 /** Called when the filter is first used for any file. */ 0257 git_filter_init_fn initialize; 0258 0259 /** Called when the filter is removed or unregistered from the system. */ 0260 git_filter_shutdown_fn shutdown; 0261 0262 /** 0263 * Called to determine whether the filter should be invoked for a 0264 * given file. If this function returns `GIT_PASSTHROUGH` then the 0265 * `stream` or `apply` functions will not be invoked and the 0266 * contents will be passed through unmodified. 0267 */ 0268 git_filter_check_fn check; 0269 0270 #ifdef GIT_DEPRECATE_HARD 0271 void *reserved; 0272 #else 0273 /** 0274 * Provided for backward compatibility; this will apply the 0275 * filter to the given contents in a `git_buf`. Callers should 0276 * provide a `stream` function instead. 0277 */ 0278 git_filter_apply_fn apply; 0279 #endif 0280 0281 /** 0282 * Called to apply the filter, this function will provide a 0283 * `git_writestream` that will the original data will be 0284 * written to; with that data, the `git_writestream` will then 0285 * perform the filter translation and stream the filtered data 0286 * out to the `next` location. 0287 */ 0288 git_filter_stream_fn stream; 0289 0290 /** Called when the system is done filtering for a file. */ 0291 git_filter_cleanup_fn cleanup; 0292 }; 0293 0294 #define GIT_FILTER_VERSION 1 0295 #define GIT_FILTER_INIT {GIT_FILTER_VERSION} 0296 0297 /** 0298 * Initializes a `git_filter` with default values. Equivalent to 0299 * creating an instance with GIT_FILTER_INIT. 0300 * 0301 * @param filter the `git_filter` struct to initialize. 0302 * @param version Version the struct; pass `GIT_FILTER_VERSION` 0303 * @return Zero on success; -1 on failure. 0304 */ 0305 GIT_EXTERN(int) git_filter_init(git_filter *filter, unsigned int version); 0306 0307 /** 0308 * Register a filter under a given name with a given priority. 0309 * 0310 * As mentioned elsewhere, the initialize callback will not be invoked 0311 * immediately. It is deferred until the filter is used in some way. 0312 * 0313 * A filter's attribute checks and `check` and `stream` (or `apply`) 0314 * callbacks will be issued in order of `priority` on smudge (to 0315 * workdir), and in reverse order of `priority` on clean (to odb). 0316 * 0317 * Two filters are preregistered with libgit2: 0318 * - GIT_FILTER_CRLF with priority 0 0319 * - GIT_FILTER_IDENT with priority 100 0320 * 0321 * Currently the filter registry is not thread safe, so any registering or 0322 * deregistering of filters must be done outside of any possible usage of 0323 * the filters (i.e. during application setup or shutdown). 0324 * 0325 * @param name A name by which the filter can be referenced. Attempting 0326 * to register with an in-use name will return GIT_EEXISTS. 0327 * @param filter The filter definition. This pointer will be stored as is 0328 * by libgit2 so it must be a durable allocation (either static 0329 * or on the heap). 0330 * @param priority The priority for filter application 0331 * @return 0 on successful registry, error code <0 on failure 0332 */ 0333 GIT_EXTERN(int) git_filter_register( 0334 const char *name, git_filter *filter, int priority); 0335 0336 /** 0337 * Remove the filter with the given name 0338 * 0339 * Attempting to remove the builtin libgit2 filters is not permitted and 0340 * will return an error. 0341 * 0342 * Currently the filter registry is not thread safe, so any registering or 0343 * deregistering of filters must be done outside of any possible usage of 0344 * the filters (i.e. during application setup or shutdown). 0345 * 0346 * @param name The name under which the filter was registered 0347 * @return 0 on success, error code <0 on failure 0348 */ 0349 GIT_EXTERN(int) git_filter_unregister(const char *name); 0350 0351 /** @} */ 0352 GIT_END_DECL 0353 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |