2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
7 #ifndef INCLUDE_git_filter_h__
8 #define INCLUDE_git_filter_h__
17 * @brief Git filter APIs
25 * Filters are applied in one of two directions: smudging - which is
26 * exporting a file from the Git object database to the working directory,
27 * and cleaning - which is importing a file from the working directory to
28 * the Git object database. These values control which direction of
29 * change is being applied.
32 GIT_FILTER_TO_WORKTREE
= 0,
33 GIT_FILTER_SMUDGE
= GIT_FILTER_TO_WORKTREE
,
34 GIT_FILTER_TO_ODB
= 1,
35 GIT_FILTER_CLEAN
= GIT_FILTER_TO_ODB
,
39 GIT_FILTER_OPT_DEFAULT
= 0u,
40 GIT_FILTER_OPT_ALLOW_UNSAFE
= (1u << 0),
44 * A filter that can transform file data
46 * This represents a filter that can be used to transform or even replace
47 * file data. Libgit2 includes one built in filter and it is possible to
48 * write your own (see git2/sys/filter.h for information on that).
50 * The two builtin filters are:
52 * * "crlf" which uses the complex rules with the "text", "eol", and
53 * "crlf" file attributes to decide how to convert between LF and CRLF
55 * * "ident" which replaces "$Id$" in a blob with "$Id: <blob OID>$" upon
56 * checkout and replaced "$Id: <anything>$" with "$Id$" on checkin.
58 typedef struct git_filter git_filter
;
61 * List of filters to be applied
63 * This represents a list of filters to be applied to a file / blob. You
64 * can build the list with one call, apply it with another, and dispose it
65 * with a third. In typical usage, there are not many occasions where a
66 * git_filter_list is needed directly since the library will generally
67 * handle conversions for you, but it can be convenient to be able to
68 * build and apply the list sometimes.
70 typedef struct git_filter_list git_filter_list
;
73 * Load the filter list for a given path.
75 * This will return 0 (success) but set the output git_filter_list to NULL
76 * if no filters are requested for the given file.
78 * @param filters Output newly created git_filter_list (or NULL)
79 * @param repo Repository object that contains `path`
80 * @param blob The blob to which the filter will be applied (if known)
81 * @param path Relative path of the file to be filtered
82 * @param mode Filtering direction (WT->ODB or ODB->WT)
83 * @param options Combination of `git_filter_opt_t` flags
84 * @return 0 on success (which could still return NULL if no filters are
85 * needed for the requested file), <0 on error
87 GIT_EXTERN(int) git_filter_list_load(
88 git_filter_list
**filters
,
90 git_blob
*blob
, /* can be NULL */
92 git_filter_mode_t mode
,
96 * Apply filter list to a data buffer.
98 * See `git2/buffer.h` for background on `git_buf` objects.
100 * If the `in` buffer holds data allocated by libgit2 (i.e. `in->asize` is
101 * not zero), then it will be overwritten when applying the filters. If
102 * not, then it will be left untouched.
104 * If there are no filters to apply (or `filters` is NULL), then the `out`
105 * buffer will reference the `in` buffer data (with `asize` set to zero)
106 * instead of allocating data. This keeps allocations to a minimum, but
107 * it means you have to be careful about freeing the `in` data since `out`
108 * may be pointing to it!
110 * @param out Buffer to store the result of the filtering
111 * @param filters A loaded git_filter_list (or NULL)
112 * @param in Buffer containing the data to filter
113 * @return 0 on success, an error code otherwise
115 GIT_EXTERN(int) git_filter_list_apply_to_data(
117 git_filter_list
*filters
,
121 * Apply filter list to the contents of a file on disk
123 GIT_EXTERN(int) git_filter_list_apply_to_file(
125 git_filter_list
*filters
,
126 git_repository
*repo
,
130 * Apply filter list to the contents of a blob
132 GIT_EXTERN(int) git_filter_list_apply_to_blob(
134 git_filter_list
*filters
,
138 * Free a git_filter_list
140 * @param filters A git_filter_list created by `git_filter_list_load`
142 GIT_EXTERN(void) git_filter_list_free(git_filter_list
*filters
);