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 * A filter that can transform file data
41 * This represents a filter that can be used to transform or even replace
42 * file data. Libgit2 includes one built in filter and it is possible to
43 * write your own (see git2/sys/filter.h for information on that).
45 * The two builtin filters are:
47 * * "crlf" which uses the complex rules with the "text", "eol", and
48 * "crlf" file attributes to decide how to convert between LF and CRLF
50 * * "ident" which replaces "$Id$" in a blob with "$Id: <blob OID>$" upon
51 * checkout and replaced "$Id: <anything>$" with "$Id$" on checkin.
53 typedef struct git_filter git_filter
;
56 * List of filters to be applied
58 * This represents a list of filters to be applied to a file / blob. You
59 * can build the list with one call, apply it with another, and dispose it
60 * with a third. In typical usage, there are not many occasions where a
61 * git_filter_list is needed directly since the library will generally
62 * handle conversions for you, but it can be convenient to be able to
63 * build and apply the list sometimes.
65 typedef struct git_filter_list git_filter_list
;
68 * Load the filter list for a given path.
70 * This will return 0 (success) but set the output git_filter_list to NULL
71 * if no filters are requested for the given file.
73 * @param filters Output newly created git_filter_list (or NULL)
74 * @param repo Repository object that contains `path`
75 * @param blob The blob to which the filter will be applied (if known)
76 * @param path Relative path of the file to be filtered
77 * @param mode Filtering direction (WT->ODB or ODB->WT)
78 * @return 0 on success (which could still return NULL if no filters are
79 * needed for the requested file), <0 on error
81 GIT_EXTERN(int) git_filter_list_load(
82 git_filter_list
**filters
,
84 git_blob
*blob
, /* can be NULL */
86 git_filter_mode_t mode
);
89 * Apply filter list to a data buffer.
91 * See `git2/buffer.h` for background on `git_buf` objects.
93 * If the `in` buffer holds data allocated by libgit2 (i.e. `in->asize` is
94 * not zero), then it will be overwritten when applying the filters. If
95 * not, then it will be left untouched.
97 * If there are no filters to apply (or `filters` is NULL), then the `out`
98 * buffer will reference the `in` buffer data (with `asize` set to zero)
99 * instead of allocating data. This keeps allocations to a minimum, but
100 * it means you have to be careful about freeing the `in` data since `out`
101 * may be pointing to it!
103 * @param out Buffer to store the result of the filtering
104 * @param filters A loaded git_filter_list (or NULL)
105 * @param in Buffer containing the data to filter
106 * @return 0 on success, an error code otherwise
108 GIT_EXTERN(int) git_filter_list_apply_to_data(
110 git_filter_list
*filters
,
114 * Apply filter list to the contents of a file on disk
116 GIT_EXTERN(int) git_filter_list_apply_to_file(
118 git_filter_list
*filters
,
119 git_repository
*repo
,
123 * Apply filter list to the contents of a blob
125 GIT_EXTERN(int) git_filter_list_apply_to_blob(
127 git_filter_list
*filters
,
131 * Free a git_filter_list
133 * @param filters A git_filter_list created by `git_filter_list_load`
135 GIT_EXTERN(void) git_filter_list_free(git_filter_list
*filters
);