1 /*
2  * Copyright (c) 2011, Google Inc.
3  */
4 #ifndef CONVERT_H
5 #define CONVERT_H
6 
7 #include "hash.h"
8 #include "string-list.h"
9 
10 struct index_state;
11 struct strbuf;
12 
13 #define CONV_EOL_RNDTRP_DIE   (1<<0) /* Die if CRLF to LF to CRLF is different */
14 #define CONV_EOL_RNDTRP_WARN  (1<<1) /* Warn if CRLF to LF to CRLF is different */
15 #define CONV_EOL_RENORMALIZE  (1<<2) /* Convert CRLF to LF */
16 #define CONV_EOL_KEEP_CRLF    (1<<3) /* Keep CRLF line endings as is */
17 #define CONV_WRITE_OBJECT     (1<<4) /* Content is written to the index */
18 
19 extern int global_conv_flags_eol;
20 
21 enum auto_crlf {
22 	AUTO_CRLF_FALSE = 0,
23 	AUTO_CRLF_TRUE = 1,
24 	AUTO_CRLF_INPUT = -1
25 };
26 
27 extern enum auto_crlf auto_crlf;
28 
29 enum eol {
30 	EOL_UNSET,
31 	EOL_CRLF,
32 	EOL_LF,
33 #ifdef NATIVE_CRLF
34 	EOL_NATIVE = EOL_CRLF
35 #else
36 	EOL_NATIVE = EOL_LF
37 #endif
38 };
39 
40 enum ce_delay_state {
41 	CE_NO_DELAY = 0,
42 	CE_CAN_DELAY = 1,
43 	CE_RETRY = 2
44 };
45 
46 struct delayed_checkout {
47 	/*
48 	 * State of the currently processed cache entry. If the state is
49 	 * CE_CAN_DELAY, then the filter can delay the current cache entry.
50 	 * If the state is CE_RETRY, then this signals the filter that the
51 	 * cache entry was requested before.
52 	 */
53 	enum ce_delay_state state;
54 	/* List of filter drivers that signaled delayed blobs. */
55 	struct string_list filters;
56 	/* List of delayed blobs identified by their path. */
57 	struct string_list paths;
58 };
59 
60 struct checkout_metadata {
61 	const char *refname;
62 	struct object_id treeish;
63 	struct object_id blob;
64 };
65 
66 enum convert_crlf_action {
67 	CRLF_UNDEFINED,
68 	CRLF_BINARY,
69 	CRLF_TEXT,
70 	CRLF_TEXT_INPUT,
71 	CRLF_TEXT_CRLF,
72 	CRLF_AUTO,
73 	CRLF_AUTO_INPUT,
74 	CRLF_AUTO_CRLF
75 };
76 
77 struct convert_driver;
78 
79 struct conv_attrs {
80 	struct convert_driver *drv;
81 	enum convert_crlf_action attr_action; /* What attr says */
82 	enum convert_crlf_action crlf_action; /* When no attr is set, use core.autocrlf */
83 	int ident;
84 	const char *working_tree_encoding; /* Supported encoding or default encoding if NULL */
85 };
86 
87 void convert_attrs(struct index_state *istate,
88 		   struct conv_attrs *ca, const char *path);
89 
90 extern enum eol core_eol;
91 extern char *check_roundtrip_encoding;
92 const char *get_cached_convert_stats_ascii(struct index_state *istate,
93 					   const char *path);
94 const char *get_wt_convert_stats_ascii(const char *path);
95 const char *get_convert_attr_ascii(struct index_state *istate,
96 				   const char *path);
97 
98 /* returns 1 if *dst was used */
99 int convert_to_git(struct index_state *istate,
100 		   const char *path, const char *src, size_t len,
101 		   struct strbuf *dst, int conv_flags);
102 int convert_to_working_tree_ca(const struct conv_attrs *ca,
103 			       const char *path, const char *src,
104 			       size_t len, struct strbuf *dst,
105 			       const struct checkout_metadata *meta);
106 int async_convert_to_working_tree_ca(const struct conv_attrs *ca,
107 				     const char *path, const char *src,
108 				     size_t len, struct strbuf *dst,
109 				     const struct checkout_metadata *meta,
110 				     void *dco);
convert_to_working_tree(struct index_state * istate,const char * path,const char * src,size_t len,struct strbuf * dst,const struct checkout_metadata * meta)111 static inline int convert_to_working_tree(struct index_state *istate,
112 					  const char *path, const char *src,
113 					  size_t len, struct strbuf *dst,
114 					  const struct checkout_metadata *meta)
115 {
116 	struct conv_attrs ca;
117 	convert_attrs(istate, &ca, path);
118 	return convert_to_working_tree_ca(&ca, path, src, len, dst, meta);
119 }
async_convert_to_working_tree(struct index_state * istate,const char * path,const char * src,size_t len,struct strbuf * dst,const struct checkout_metadata * meta,void * dco)120 static inline int async_convert_to_working_tree(struct index_state *istate,
121 						const char *path, const char *src,
122 						size_t len, struct strbuf *dst,
123 						const struct checkout_metadata *meta,
124 						void *dco)
125 {
126 	struct conv_attrs ca;
127 	convert_attrs(istate, &ca, path);
128 	return async_convert_to_working_tree_ca(&ca, path, src, len, dst, meta, dco);
129 }
130 int async_query_available_blobs(const char *cmd,
131 				struct string_list *available_paths);
132 int renormalize_buffer(struct index_state *istate,
133 		       const char *path, const char *src, size_t len,
134 		       struct strbuf *dst);
would_convert_to_git(struct index_state * istate,const char * path)135 static inline int would_convert_to_git(struct index_state *istate,
136 				       const char *path)
137 {
138 	return convert_to_git(istate, path, NULL, 0, NULL, 0);
139 }
140 /* Precondition: would_convert_to_git_filter_fd(path) == true */
141 void convert_to_git_filter_fd(struct index_state *istate,
142 			      const char *path, int fd,
143 			      struct strbuf *dst,
144 			      int conv_flags);
145 int would_convert_to_git_filter_fd(struct index_state *istate,
146 				   const char *path);
147 
148 /*
149  * Initialize the checkout metadata with the given values.  Any argument may be
150  * NULL if it is not applicable.  The treeish should be a commit if that is
151  * available, and a tree otherwise.
152  *
153  * The refname is not copied and must be valid for the lifetime of the struct.
154  * THe object IDs are copied.
155  */
156 void init_checkout_metadata(struct checkout_metadata *meta, const char *refname,
157 			    const struct object_id *treeish,
158 			    const struct object_id *blob);
159 
160 /* Copy the metadata from src to dst, updating the blob. */
161 void clone_checkout_metadata(struct checkout_metadata *dst,
162 			     const struct checkout_metadata *src,
163 			     const struct object_id *blob);
164 
165 /*
166  * Reset the internal list of attributes used by convert_to_git and
167  * convert_to_working_tree.
168  */
169 void reset_parsed_attributes(void);
170 
171 /*****************************************************************
172  *
173  * Streaming conversion support
174  *
175  *****************************************************************/
176 
177 struct stream_filter; /* opaque */
178 
179 struct stream_filter *get_stream_filter(struct index_state *istate,
180 					const char *path,
181 					const struct object_id *);
182 struct stream_filter *get_stream_filter_ca(const struct conv_attrs *ca,
183 					   const struct object_id *oid);
184 void free_stream_filter(struct stream_filter *);
185 int is_null_stream_filter(struct stream_filter *);
186 
187 /*
188  * Use as much input up to *isize_p and fill output up to *osize_p;
189  * update isize_p and osize_p to indicate how much buffer space was
190  * consumed and filled. Return 0 on success, non-zero on error.
191  *
192  * Some filters may need to buffer the input and look-ahead inside it
193  * to decide what to output, and they may consume more than zero bytes
194  * of input and still not produce any output. After feeding all the
195  * input, pass NULL as input and keep calling this function, to let
196  * such filters know there is no more input coming and it is time for
197  * them to produce the remaining output based on the buffered input.
198  */
199 int stream_filter(struct stream_filter *,
200 		  const char *input, size_t *isize_p,
201 		  char *output, size_t *osize_p);
202 
203 enum conv_attrs_classification {
204 	/*
205 	 * The blob must be loaded into a buffer before it can be
206 	 * smudged. All smudging is done in-proc.
207 	 */
208 	CA_CLASS_INCORE,
209 
210 	/*
211 	 * The blob must be loaded into a buffer, but uses a
212 	 * single-file driver filter, such as rot13.
213 	 */
214 	CA_CLASS_INCORE_FILTER,
215 
216 	/*
217 	 * The blob must be loaded into a buffer, but uses a
218 	 * long-running driver process, such as LFS. This might or
219 	 * might not use delayed operations. (The important thing is
220 	 * that there is a single subordinate long-running process
221 	 * handling all associated blobs and in case of delayed
222 	 * operations, may hold per-blob state.)
223 	 */
224 	CA_CLASS_INCORE_PROCESS,
225 
226 	/*
227 	 * The blob can be streamed and smudged without needing to
228 	 * completely read it into a buffer.
229 	 */
230 	CA_CLASS_STREAMABLE,
231 };
232 
233 enum conv_attrs_classification classify_conv_attrs(
234 	const struct conv_attrs *ca);
235 
236 #endif /* CONVERT_H */
237