]> git.proxmox.com Git - libgit2.git/blob - src/filter.h
Merge pull request #1088 from arrbee/consolidate-text-functions
[libgit2.git] / src / filter.h
1 /*
2 * Copyright (C) 2009-2012 the libgit2 contributors
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 #ifndef INCLUDE_filter_h__
8 #define INCLUDE_filter_h__
9
10 #include "common.h"
11 #include "buffer.h"
12 #include "buf_text.h"
13 #include "git2/odb.h"
14 #include "git2/repository.h"
15
16 typedef struct git_filter {
17 int (*apply)(struct git_filter *self, git_buf *dest, const git_buf *source);
18 void (*do_free)(struct git_filter *self);
19 } git_filter;
20
21 typedef enum {
22 GIT_FILTER_TO_WORKTREE,
23 GIT_FILTER_TO_ODB
24 } git_filter_mode;
25
26 typedef enum {
27 GIT_CRLF_GUESS = -1,
28 GIT_CRLF_BINARY = 0,
29 GIT_CRLF_TEXT,
30 GIT_CRLF_INPUT,
31 GIT_CRLF_CRLF,
32 GIT_CRLF_AUTO,
33 } git_crlf_t;
34
35 /*
36 * FILTER API
37 */
38
39 /*
40 * For any given path in the working directory, fill the `filters`
41 * array with the relevant filters that need to be applied.
42 *
43 * Mode is either `GIT_FILTER_TO_WORKTREE` if you need to load the
44 * filters that will be used when checking out a file to the working
45 * directory, or `GIT_FILTER_TO_ODB` for the filters used when writing
46 * a file to the ODB.
47 *
48 * @param filters Vector where to store all the loaded filters
49 * @param repo Repository object that contains `path`
50 * @param path Relative path of the file to be filtered
51 * @param mode Filtering direction (WT->ODB or ODB->WT)
52 * @return the number of filters loaded for the file (0 if the file
53 * doesn't need filtering), or a negative error code
54 */
55 extern int git_filters_load(git_vector *filters, git_repository *repo, const char *path, int mode);
56
57 /*
58 * Apply one or more filters to a file.
59 *
60 * The file must have been loaded as a `git_buf` object. Both the `source`
61 * and `dest` buffers are owned by the caller and must be freed once
62 * they are no longer needed.
63 *
64 * NOTE: Because of the double-buffering schema, the `source` buffer that contains
65 * the original file may be tampered once the filtering is complete. Regardless,
66 * the `dest` buffer will always contain the final result of the filtering
67 *
68 * @param dest Buffer to store the result of the filtering
69 * @param source Buffer containing the document to filter
70 * @param filters A non-empty vector of filters as supplied by `git_filters_load`
71 * @return 0 on success, an error code otherwise
72 */
73 extern int git_filters_apply(git_buf *dest, git_buf *source, git_vector *filters);
74
75 /*
76 * Free the `filters` array generated by `git_filters_load`.
77 *
78 * Note that this frees both the array and its contents. The array will
79 * be clean/reusable after this call.
80 *
81 * @param filters A filters array as supplied by `git_filters_load`
82 */
83 extern void git_filters_free(git_vector *filters);
84
85 /*
86 * Available filters
87 */
88
89 /* Strip CRLF, from Worktree to ODB */
90 extern int git_filter_add__crlf_to_odb(git_vector *filters, git_repository *repo, const char *path);
91
92 /* Add CRLF, from ODB to worktree */
93 extern int git_filter_add__crlf_to_workdir(git_vector *filters, git_repository *repo, const char *path);
94
95 #endif