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