]> git.proxmox.com Git - libgit2.git/blob - src/filter.h
Checkout: add structure for CRLF.
[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 "git2/odb.h"
13 #include "git2/repository.h"
14
15 typedef struct git_filter {
16 int (*apply)(struct git_filter *self, git_buf *dest, const git_buf *source);
17 void (*do_free)(struct git_filter *self);
18 } git_filter;
19
20 typedef enum {
21 GIT_FILTER_TO_WORKTREE,
22 GIT_FILTER_TO_ODB
23 } git_filter_mode;
24
25 typedef enum {
26 GIT_CRLF_GUESS = -1,
27 GIT_CRLF_BINARY = 0,
28 GIT_CRLF_TEXT,
29 GIT_CRLF_INPUT,
30 GIT_CRLF_CRLF,
31 GIT_CRLF_AUTO,
32 } git_crlf_t;
33
34 typedef struct {
35 /* NUL, CR, LF and CRLF counts */
36 unsigned int nul, cr, lf, crlf;
37
38 /* These are just approximations! */
39 unsigned int printable, nonprintable;
40 } git_text_stats;
41
42 /*
43 * FILTER API
44 */
45
46 /*
47 * For any given path in the working directory, fill the `filters`
48 * array with the relevant filters that need to be applied.
49 *
50 * Mode is either `GIT_FILTER_TO_WORKTREE` if you need to load the
51 * filters that will be used when checking out a file to the working
52 * directory, or `GIT_FILTER_TO_ODB` for the filters used when writing
53 * a file to the ODB.
54 *
55 * @param filters Vector where to store all the loaded filters
56 * @param repo Repository object that contains `path`
57 * @param path Relative path of the file to be filtered
58 * @param mode Filtering direction (WT->ODB or ODB->WT)
59 * @return the number of filters loaded for the file (0 if the file
60 * doesn't need filtering), or a negative error code
61 */
62 extern int git_filters_load(git_vector *filters, git_repository *repo, const char *path, int mode);
63
64 /*
65 * Apply one or more filters to a file.
66 *
67 * The file must have been loaded as a `git_buf` object. Both the `source`
68 * and `dest` buffers are owned by the caller and must be freed once
69 * they are no longer needed.
70 *
71 * NOTE: Because of the double-buffering schema, the `source` buffer that contains
72 * the original file may be tampered once the filtering is complete. Regardless,
73 * the `dest` buffer will always contain the final result of the filtering
74 *
75 * @param dest Buffer to store the result of the filtering
76 * @param source Buffer containing the document to filter
77 * @param filters A non-empty vector of filters as supplied by `git_filters_load`
78 * @return 0 on success, an error code otherwise
79 */
80 extern int git_filters_apply(git_buf *dest, git_buf *source, git_vector *filters);
81
82 /*
83 * Free the `filters` array generated by `git_filters_load`.
84 *
85 * Note that this frees both the array and its contents. The array will
86 * be clean/reusable after this call.
87 *
88 * @param filters A filters array as supplied by `git_filters_load`
89 */
90 extern void git_filters_free(git_vector *filters);
91
92 /*
93 * Available filters
94 */
95
96 /* Strip CRLF, from Worktree to ODB */
97 extern int git_filter_add__crlf_to_odb(git_vector *filters, git_repository *repo, const char *path);
98
99 /* Add CRLF, from ODB to worktree */
100 extern int git_filter_add__crlf_to_workdir(git_vector *filters, git_repository *repo, const char *path);
101
102
103 /*
104 * PLAINTEXT API
105 */
106
107 /*
108 * Gather stats for a piece of text
109 *
110 * Fill the `stats` structure with information on the number of
111 * unreadable characters, carriage returns, etc, so it can be
112 * used in heuristics.
113 */
114 extern void git_text_gather_stats(git_text_stats *stats, const git_buf *text);
115
116 /*
117 * Process `git_text_stats` data generated by `git_text_stat` to see
118 * if it qualifies as a binary file
119 */
120 extern int git_text_is_binary(git_text_stats *stats);
121
122 #endif