Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 1 | #include "cache.h" |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 2 | #include "attr.h" |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 3 | #include "run-command.h" |
Pete Wyckoff | a2b665d | 2010-12-22 06:40:13 -0800 | [diff] [blame] | 4 | #include "quote.h" |
Jehan Bing | 6424c2a | 2012-02-20 12:53:37 -0800 | [diff] [blame] | 5 | #include "sigchain.h" |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 6 | #include "pkt-line.h" |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 7 | |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 8 | /* |
| 9 | * convert.c - convert a file when checking it out and checking it in. |
| 10 | * |
| 11 | * This should use the pathname to decide on whether it wants to do some |
| 12 | * more interesting conversions (automatic gzip/unzip, general format |
| 13 | * conversions etc etc), but by default it just does automatic CRLF<->LF |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 14 | * translation when the "text" attribute or "auto_crlf" option is set. |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 15 | */ |
| 16 | |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 17 | /* Stat bits: When BIN is set, the txt bits are unset */ |
| 18 | #define CONVERT_STAT_BITS_TXT_LF 0x1 |
| 19 | #define CONVERT_STAT_BITS_TXT_CRLF 0x2 |
| 20 | #define CONVERT_STAT_BITS_BIN 0x4 |
| 21 | |
Junio C Hamano | c61dcff | 2011-05-09 13:12:57 -0700 | [diff] [blame] | 22 | enum crlf_action { |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 23 | CRLF_UNDEFINED, |
| 24 | CRLF_BINARY, |
Eyvind Bernhardsen | fd6cce9 | 2010-05-19 22:43:10 +0200 | [diff] [blame] | 25 | CRLF_TEXT, |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 26 | CRLF_TEXT_INPUT, |
| 27 | CRLF_TEXT_CRLF, |
| 28 | CRLF_AUTO, |
| 29 | CRLF_AUTO_INPUT, |
| 30 | CRLF_AUTO_CRLF |
Eyvind Bernhardsen | fd6cce9 | 2010-05-19 22:43:10 +0200 | [diff] [blame] | 31 | }; |
Junio C Hamano | 163b959 | 2007-04-19 22:37:19 -0700 | [diff] [blame] | 32 | |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 33 | struct text_stat { |
Dmitry Potapov | 2862419 | 2008-01-16 04:59:12 +0300 | [diff] [blame] | 34 | /* NUL, CR, LF and CRLF counts */ |
Torsten Bögershausen | 6e336a5 | 2016-02-10 17:24:43 +0100 | [diff] [blame] | 35 | unsigned nul, lonecr, lonelf, crlf; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 36 | |
| 37 | /* These are just approximations! */ |
| 38 | unsigned printable, nonprintable; |
| 39 | }; |
| 40 | |
| 41 | static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats) |
| 42 | { |
| 43 | unsigned long i; |
| 44 | |
| 45 | memset(stats, 0, sizeof(*stats)); |
| 46 | |
| 47 | for (i = 0; i < size; i++) { |
| 48 | unsigned char c = buf[i]; |
| 49 | if (c == '\r') { |
Torsten Bögershausen | 6e336a5 | 2016-02-10 17:24:43 +0100 | [diff] [blame] | 50 | if (i+1 < size && buf[i+1] == '\n') { |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 51 | stats->crlf++; |
Torsten Bögershausen | 6e336a5 | 2016-02-10 17:24:43 +0100 | [diff] [blame] | 52 | i++; |
| 53 | } else |
| 54 | stats->lonecr++; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 55 | continue; |
| 56 | } |
| 57 | if (c == '\n') { |
Torsten Bögershausen | 6e336a5 | 2016-02-10 17:24:43 +0100 | [diff] [blame] | 58 | stats->lonelf++; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 59 | continue; |
| 60 | } |
| 61 | if (c == 127) |
| 62 | /* DEL */ |
| 63 | stats->nonprintable++; |
| 64 | else if (c < 32) { |
| 65 | switch (c) { |
| 66 | /* BS, HT, ESC and FF */ |
| 67 | case '\b': case '\t': case '\033': case '\014': |
| 68 | stats->printable++; |
| 69 | break; |
Dmitry Potapov | 2862419 | 2008-01-16 04:59:12 +0300 | [diff] [blame] | 70 | case 0: |
| 71 | stats->nul++; |
| 72 | /* fall through */ |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 73 | default: |
| 74 | stats->nonprintable++; |
| 75 | } |
| 76 | } |
| 77 | else |
| 78 | stats->printable++; |
| 79 | } |
Dmitry Kakurin | f9dd4bf | 2008-07-11 18:48:16 +0200 | [diff] [blame] | 80 | |
| 81 | /* If file ends with EOF then don't count this EOF as non-printable. */ |
| 82 | if (size >= 1 && buf[size-1] == '\032') |
| 83 | stats->nonprintable--; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* |
| 87 | * The same heuristics as diff.c::mmfile_is_binary() |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 88 | * We treat files with bare CR as binary |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 89 | */ |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 90 | static int convert_is_binary(unsigned long size, const struct text_stat *stats) |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 91 | { |
Torsten Bögershausen | 6e336a5 | 2016-02-10 17:24:43 +0100 | [diff] [blame] | 92 | if (stats->lonecr) |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 93 | return 1; |
Dmitry Potapov | 2862419 | 2008-01-16 04:59:12 +0300 | [diff] [blame] | 94 | if (stats->nul) |
| 95 | return 1; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 96 | if ((stats->printable >> 7) < stats->nonprintable) |
| 97 | return 1; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 101 | static unsigned int gather_convert_stats(const char *data, unsigned long size) |
| 102 | { |
| 103 | struct text_stat stats; |
Torsten Bögershausen | 6e336a5 | 2016-02-10 17:24:43 +0100 | [diff] [blame] | 104 | int ret = 0; |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 105 | if (!data || !size) |
| 106 | return 0; |
| 107 | gather_stats(data, size, &stats); |
| 108 | if (convert_is_binary(size, &stats)) |
Torsten Bögershausen | 6e336a5 | 2016-02-10 17:24:43 +0100 | [diff] [blame] | 109 | ret |= CONVERT_STAT_BITS_BIN; |
| 110 | if (stats.crlf) |
| 111 | ret |= CONVERT_STAT_BITS_TXT_CRLF; |
| 112 | if (stats.lonelf) |
| 113 | ret |= CONVERT_STAT_BITS_TXT_LF; |
| 114 | |
| 115 | return ret; |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static const char *gather_convert_stats_ascii(const char *data, unsigned long size) |
| 119 | { |
| 120 | unsigned int convert_stats = gather_convert_stats(data, size); |
| 121 | |
| 122 | if (convert_stats & CONVERT_STAT_BITS_BIN) |
| 123 | return "-text"; |
| 124 | switch (convert_stats) { |
| 125 | case CONVERT_STAT_BITS_TXT_LF: |
| 126 | return "lf"; |
| 127 | case CONVERT_STAT_BITS_TXT_CRLF: |
| 128 | return "crlf"; |
| 129 | case CONVERT_STAT_BITS_TXT_LF | CONVERT_STAT_BITS_TXT_CRLF: |
| 130 | return "mixed"; |
| 131 | default: |
| 132 | return "none"; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | const char *get_cached_convert_stats_ascii(const char *path) |
| 137 | { |
| 138 | const char *ret; |
| 139 | unsigned long sz; |
| 140 | void *data = read_blob_data_from_cache(path, &sz); |
| 141 | ret = gather_convert_stats_ascii(data, sz); |
| 142 | free(data); |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | const char *get_wt_convert_stats_ascii(const char *path) |
| 147 | { |
| 148 | const char *ret = ""; |
| 149 | struct strbuf sb = STRBUF_INIT; |
| 150 | if (strbuf_read_file(&sb, path, 0) >= 0) |
| 151 | ret = gather_convert_stats_ascii(sb.buf, sb.len); |
| 152 | strbuf_release(&sb); |
| 153 | return ret; |
| 154 | } |
| 155 | |
Torsten Bögershausen | 4b4024f | 2016-02-05 17:13:25 +0100 | [diff] [blame] | 156 | static int text_eol_is_crlf(void) |
| 157 | { |
| 158 | if (auto_crlf == AUTO_CRLF_TRUE) |
| 159 | return 1; |
| 160 | else if (auto_crlf == AUTO_CRLF_INPUT) |
| 161 | return 0; |
| 162 | if (core_eol == EOL_CRLF) |
| 163 | return 1; |
| 164 | if (core_eol == EOL_UNSET && EOL_NATIVE == EOL_CRLF) |
| 165 | return 1; |
| 166 | return 0; |
| 167 | } |
| 168 | |
Junio C Hamano | c61dcff | 2011-05-09 13:12:57 -0700 | [diff] [blame] | 169 | static enum eol output_eol(enum crlf_action crlf_action) |
Eyvind Bernhardsen | f217f0e | 2010-07-02 21:20:47 +0200 | [diff] [blame] | 170 | { |
Junio C Hamano | c61dcff | 2011-05-09 13:12:57 -0700 | [diff] [blame] | 171 | switch (crlf_action) { |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 172 | case CRLF_BINARY: |
| 173 | return EOL_UNSET; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 174 | case CRLF_TEXT_CRLF: |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 175 | return EOL_CRLF; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 176 | case CRLF_TEXT_INPUT: |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 177 | return EOL_LF; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 178 | case CRLF_UNDEFINED: |
| 179 | case CRLF_AUTO_CRLF: |
Torsten Bögershausen | 6523728 | 2016-06-28 10:01:13 +0200 | [diff] [blame] | 180 | return EOL_CRLF; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 181 | case CRLF_AUTO_INPUT: |
Torsten Bögershausen | 6523728 | 2016-06-28 10:01:13 +0200 | [diff] [blame] | 182 | return EOL_LF; |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 183 | case CRLF_TEXT: |
| 184 | case CRLF_AUTO: |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 185 | /* fall through */ |
Torsten Bögershausen | 4b4024f | 2016-02-05 17:13:25 +0100 | [diff] [blame] | 186 | return text_eol_is_crlf() ? EOL_CRLF : EOL_LF; |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 187 | } |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 188 | warning("Illegal crlf_action %d\n", (int)crlf_action); |
Junio C Hamano | ec70f52 | 2011-05-09 12:52:12 -0700 | [diff] [blame] | 189 | return core_eol; |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Junio C Hamano | c61dcff | 2011-05-09 13:12:57 -0700 | [diff] [blame] | 192 | static void check_safe_crlf(const char *path, enum crlf_action crlf_action, |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 193 | struct text_stat *old_stats, struct text_stat *new_stats, |
| 194 | enum safe_crlf checksafe) |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 195 | { |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 196 | if (old_stats->crlf && !new_stats->crlf ) { |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 197 | /* |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 198 | * CRLFs would not be restored by checkout |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 199 | */ |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 200 | if (checksafe == SAFE_CRLF_WARN) |
Vasco Almeida | 87cb784 | 2016-10-17 13:15:27 +0000 | [diff] [blame] | 201 | warning(_("CRLF will be replaced by LF in %s.\n" |
| 202 | "The file will have its original line" |
| 203 | " endings in your working directory."), path); |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 204 | else /* i.e. SAFE_CRLF_FAIL */ |
Vasco Almeida | 87cb784 | 2016-10-17 13:15:27 +0000 | [diff] [blame] | 205 | die(_("CRLF would be replaced by LF in %s."), path); |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 206 | } else if (old_stats->lonelf && !new_stats->lonelf ) { |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 207 | /* |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 208 | * CRLFs would be added by checkout |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 209 | */ |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 210 | if (checksafe == SAFE_CRLF_WARN) |
Vasco Almeida | 87cb784 | 2016-10-17 13:15:27 +0000 | [diff] [blame] | 211 | warning(_("LF will be replaced by CRLF in %s.\n" |
| 212 | "The file will have its original line" |
| 213 | " endings in your working directory."), path); |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 214 | else /* i.e. SAFE_CRLF_FAIL */ |
Vasco Almeida | 87cb784 | 2016-10-17 13:15:27 +0000 | [diff] [blame] | 215 | die(_("LF would be replaced by CRLF in %s"), path); |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Finn Arne Gangstad | c480539 | 2010-05-12 00:37:57 +0200 | [diff] [blame] | 219 | static int has_cr_in_index(const char *path) |
| 220 | { |
Finn Arne Gangstad | c480539 | 2010-05-12 00:37:57 +0200 | [diff] [blame] | 221 | unsigned long sz; |
Finn Arne Gangstad | c480539 | 2010-05-12 00:37:57 +0200 | [diff] [blame] | 222 | void *data; |
| 223 | int has_cr; |
Finn Arne Gangstad | c480539 | 2010-05-12 00:37:57 +0200 | [diff] [blame] | 224 | |
Lukas Fleischer | 4982fd7 | 2013-04-13 15:28:32 +0200 | [diff] [blame] | 225 | data = read_blob_data_from_cache(path, &sz); |
| 226 | if (!data) |
Finn Arne Gangstad | c480539 | 2010-05-12 00:37:57 +0200 | [diff] [blame] | 227 | return 0; |
Finn Arne Gangstad | c480539 | 2010-05-12 00:37:57 +0200 | [diff] [blame] | 228 | has_cr = memchr(data, '\r', sz) != NULL; |
| 229 | free(data); |
| 230 | return has_cr; |
| 231 | } |
| 232 | |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 233 | static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, |
| 234 | enum crlf_action crlf_action) |
| 235 | { |
| 236 | if (output_eol(crlf_action) != EOL_CRLF) |
| 237 | return 0; |
| 238 | /* No "naked" LF? Nothing to convert, regardless. */ |
| 239 | if (!stats->lonelf) |
| 240 | return 0; |
| 241 | |
| 242 | if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) { |
| 243 | /* If we have any CR or CRLF line endings, we do not touch it */ |
| 244 | /* This is the new safer autocrlf-handling */ |
| 245 | if (stats->lonecr || stats->crlf) |
| 246 | return 0; |
| 247 | |
| 248 | if (convert_is_binary(len, stats)) |
| 249 | return 0; |
| 250 | } |
| 251 | return 1; |
| 252 | |
| 253 | } |
| 254 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 255 | static int crlf_to_git(const char *path, const char *src, size_t len, |
Junio C Hamano | c61dcff | 2011-05-09 13:12:57 -0700 | [diff] [blame] | 256 | struct strbuf *buf, |
| 257 | enum crlf_action crlf_action, enum safe_crlf checksafe) |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 258 | { |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 259 | struct text_stat stats; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 260 | char *dst; |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 261 | int convert_crlf_into_lf; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 262 | |
Junio C Hamano | c61dcff | 2011-05-09 13:12:57 -0700 | [diff] [blame] | 263 | if (crlf_action == CRLF_BINARY || |
Jeff King | 4c3b57b | 2012-02-24 17:05:03 -0500 | [diff] [blame] | 264 | (src && !len)) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 265 | return 0; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 266 | |
Jeff King | 4c3b57b | 2012-02-24 17:05:03 -0500 | [diff] [blame] | 267 | /* |
| 268 | * If we are doing a dry-run and have no source buffer, there is |
| 269 | * nothing to analyze; we must assume we would convert. |
| 270 | */ |
| 271 | if (!buf && !src) |
| 272 | return 1; |
| 273 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 274 | gather_stats(src, len, &stats); |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 275 | /* Optimization: No CRLF? Nothing to convert, regardless. */ |
| 276 | convert_crlf_into_lf = !!stats.crlf; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 277 | |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 278 | if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) { |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 279 | if (convert_is_binary(len, &stats)) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 280 | return 0; |
Torsten Bögershausen | 6523728 | 2016-06-28 10:01:13 +0200 | [diff] [blame] | 281 | /* |
Torsten Bögershausen | 1c25d2d | 2016-11-30 18:02:32 +0100 | [diff] [blame] | 282 | * If the file in the index has any CR in it, do not |
| 283 | * convert. This is the new safer autocrlf handling, |
| 284 | * unless we want to renormalize in a merge or |
| 285 | * cherry-pick. |
Torsten Bögershausen | 6523728 | 2016-06-28 10:01:13 +0200 | [diff] [blame] | 286 | */ |
Torsten Bögershausen | 1c25d2d | 2016-11-30 18:02:32 +0100 | [diff] [blame] | 287 | if ((checksafe != SAFE_CRLF_RENORMALIZE) && has_cr_in_index(path)) |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 288 | convert_crlf_into_lf = 0; |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 289 | } |
Torsten Bögershausen | 1c25d2d | 2016-11-30 18:02:32 +0100 | [diff] [blame] | 290 | if ((checksafe == SAFE_CRLF_WARN || |
| 291 | (checksafe == SAFE_CRLF_FAIL)) && len) { |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 292 | struct text_stat new_stats; |
| 293 | memcpy(&new_stats, &stats, sizeof(new_stats)); |
| 294 | /* simulate "git add" */ |
| 295 | if (convert_crlf_into_lf) { |
| 296 | new_stats.lonelf += new_stats.crlf; |
| 297 | new_stats.crlf = 0; |
| 298 | } |
| 299 | /* simulate "git checkout" */ |
| 300 | if (will_convert_lf_to_crlf(len, &new_stats, crlf_action)) { |
| 301 | new_stats.crlf += new_stats.lonelf; |
| 302 | new_stats.lonelf = 0; |
| 303 | } |
| 304 | check_safe_crlf(path, crlf_action, &stats, &new_stats, checksafe); |
| 305 | } |
| 306 | if (!convert_crlf_into_lf) |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 307 | return 0; |
| 308 | |
Jeff King | 92ac319 | 2012-02-24 17:02:37 -0500 | [diff] [blame] | 309 | /* |
| 310 | * At this point all of our source analysis is done, and we are sure we |
| 311 | * would convert. If we are in dry-run mode, we can give an answer. |
| 312 | */ |
| 313 | if (!buf) |
| 314 | return 1; |
| 315 | |
Pierre Habouzit | 90d16ec | 2007-10-05 10:11:59 +0200 | [diff] [blame] | 316 | /* only grow if not in place */ |
| 317 | if (strbuf_avail(buf) + buf->len < len) |
| 318 | strbuf_grow(buf, len - buf->len); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 319 | dst = buf->buf; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 320 | if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) { |
Junio C Hamano | 163b959 | 2007-04-19 22:37:19 -0700 | [diff] [blame] | 321 | /* |
| 322 | * If we guessed, we already know we rejected a file with |
| 323 | * lone CR, and we can strip a CR without looking at what |
| 324 | * follow it. |
| 325 | */ |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 326 | do { |
Alex Riesen | ac78e54 | 2007-04-19 02:05:03 +0200 | [diff] [blame] | 327 | unsigned char c = *src++; |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 328 | if (c != '\r') |
Alex Riesen | ac78e54 | 2007-04-19 02:05:03 +0200 | [diff] [blame] | 329 | *dst++ = c; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 330 | } while (--len); |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 331 | } else { |
| 332 | do { |
Alex Riesen | ac78e54 | 2007-04-19 02:05:03 +0200 | [diff] [blame] | 333 | unsigned char c = *src++; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 334 | if (! (c == '\r' && (1 < len && *src == '\n'))) |
Alex Riesen | ac78e54 | 2007-04-19 02:05:03 +0200 | [diff] [blame] | 335 | *dst++ = c; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 336 | } while (--len); |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 337 | } |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 338 | strbuf_setlen(buf, dst - buf->buf); |
| 339 | return 1; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 342 | static int crlf_to_worktree(const char *path, const char *src, size_t len, |
Junio C Hamano | c61dcff | 2011-05-09 13:12:57 -0700 | [diff] [blame] | 343 | struct strbuf *buf, enum crlf_action crlf_action) |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 344 | { |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 345 | char *to_free = NULL; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 346 | struct text_stat stats; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 347 | |
Junio C Hamano | c61dcff | 2011-05-09 13:12:57 -0700 | [diff] [blame] | 348 | if (!len || output_eol(crlf_action) != EOL_CRLF) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 349 | return 0; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 350 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 351 | gather_stats(src, len, &stats); |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 352 | if (!will_convert_lf_to_crlf(len, &stats, crlf_action)) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 353 | return 0; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 354 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 355 | /* are we "faking" in place editing ? */ |
| 356 | if (src == buf->buf) |
Pierre Habouzit | b315c5c | 2007-09-27 12:58:23 +0200 | [diff] [blame] | 357 | to_free = strbuf_detach(buf, NULL); |
Alex Riesen | ac78e54 | 2007-04-19 02:05:03 +0200 | [diff] [blame] | 358 | |
Torsten Bögershausen | 6e336a5 | 2016-02-10 17:24:43 +0100 | [diff] [blame] | 359 | strbuf_grow(buf, len + stats.lonelf); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 360 | for (;;) { |
| 361 | const char *nl = memchr(src, '\n', len); |
| 362 | if (!nl) |
| 363 | break; |
| 364 | if (nl > src && nl[-1] == '\r') { |
| 365 | strbuf_add(buf, src, nl + 1 - src); |
| 366 | } else { |
| 367 | strbuf_add(buf, src, nl - src); |
| 368 | strbuf_addstr(buf, "\r\n"); |
| 369 | } |
| 370 | len -= nl + 1 - src; |
| 371 | src = nl + 1; |
| 372 | } |
| 373 | strbuf_add(buf, src, len); |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 374 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 375 | free(to_free); |
| 376 | return 1; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 377 | } |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 378 | |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 379 | struct filter_params { |
| 380 | const char *src; |
| 381 | unsigned long size; |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 382 | int fd; |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 383 | const char *cmd; |
Pete Wyckoff | a2b665d | 2010-12-22 06:40:13 -0800 | [diff] [blame] | 384 | const char *path; |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 385 | }; |
| 386 | |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 387 | static int filter_buffer_or_fd(int in, int out, void *data) |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 388 | { |
| 389 | /* |
| 390 | * Spawn cmd and feed the buffer contents through its stdin. |
| 391 | */ |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 392 | struct child_process child_process = CHILD_PROCESS_INIT; |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 393 | struct filter_params *params = (struct filter_params *)data; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 394 | int write_err, status; |
Gary V. Vaughan | 66dbfd5 | 2010-05-14 09:31:33 +0000 | [diff] [blame] | 395 | const char *argv[] = { NULL, NULL }; |
| 396 | |
Pete Wyckoff | a2b665d | 2010-12-22 06:40:13 -0800 | [diff] [blame] | 397 | /* apply % substitution to cmd */ |
| 398 | struct strbuf cmd = STRBUF_INIT; |
| 399 | struct strbuf path = STRBUF_INIT; |
| 400 | struct strbuf_expand_dict_entry dict[] = { |
| 401 | { "f", NULL, }, |
| 402 | { NULL, NULL, }, |
| 403 | }; |
| 404 | |
| 405 | /* quote the path to preserve spaces, etc. */ |
| 406 | sq_quote_buf(&path, params->path); |
| 407 | dict[0].value = path.buf; |
| 408 | |
| 409 | /* expand all %f with the quoted path */ |
| 410 | strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict); |
| 411 | strbuf_release(&path); |
| 412 | |
| 413 | argv[0] = cmd.buf; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 414 | |
Johannes Sixt | dc1bfdc | 2007-10-19 21:47:55 +0200 | [diff] [blame] | 415 | child_process.argv = argv; |
Jeff King | ac0ba18 | 2009-12-30 05:53:57 -0500 | [diff] [blame] | 416 | child_process.use_shell = 1; |
Johannes Sixt | dc1bfdc | 2007-10-19 21:47:55 +0200 | [diff] [blame] | 417 | child_process.in = -1; |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 418 | child_process.out = out; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 419 | |
Johannes Sixt | dc1bfdc | 2007-10-19 21:47:55 +0200 | [diff] [blame] | 420 | if (start_command(&child_process)) |
Lars Schneider | 255f04d | 2016-10-16 16:20:25 -0700 | [diff] [blame] | 421 | return error("cannot fork to run external filter '%s'", params->cmd); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 422 | |
Jehan Bing | 6424c2a | 2012-02-20 12:53:37 -0800 | [diff] [blame] | 423 | sigchain_push(SIGPIPE, SIG_IGN); |
| 424 | |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 425 | if (params->src) { |
Junio C Hamano | 0c4dd67 | 2015-05-19 11:08:23 -0700 | [diff] [blame] | 426 | write_err = (write_in_full(child_process.in, |
| 427 | params->src, params->size) < 0); |
| 428 | if (errno == EPIPE) |
| 429 | write_err = 0; |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 430 | } else { |
| 431 | write_err = copy_fd(params->fd, child_process.in); |
Junio C Hamano | 0c4dd67 | 2015-05-19 11:08:23 -0700 | [diff] [blame] | 432 | if (write_err == COPY_WRITE_ERROR && errno == EPIPE) |
| 433 | write_err = 0; |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 434 | } |
| 435 | |
Johannes Sixt | dc1bfdc | 2007-10-19 21:47:55 +0200 | [diff] [blame] | 436 | if (close(child_process.in)) |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 437 | write_err = 1; |
| 438 | if (write_err) |
Lars Schneider | 255f04d | 2016-10-16 16:20:25 -0700 | [diff] [blame] | 439 | error("cannot feed the input to external filter '%s'", params->cmd); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 440 | |
Jehan Bing | 6424c2a | 2012-02-20 12:53:37 -0800 | [diff] [blame] | 441 | sigchain_pop(SIGPIPE); |
| 442 | |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 443 | status = finish_command(&child_process); |
| 444 | if (status) |
Lars Schneider | 255f04d | 2016-10-16 16:20:25 -0700 | [diff] [blame] | 445 | error("external filter '%s' failed %d", params->cmd, status); |
Pete Wyckoff | a2b665d | 2010-12-22 06:40:13 -0800 | [diff] [blame] | 446 | |
| 447 | strbuf_release(&cmd); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 448 | return (write_err || status); |
| 449 | } |
| 450 | |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 451 | static int apply_single_file_filter(const char *path, const char *src, size_t len, int fd, |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 452 | struct strbuf *dst, const char *cmd) |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 453 | { |
| 454 | /* |
| 455 | * Create a pipeline to have the command filter the buffer's |
| 456 | * contents. |
| 457 | * |
| 458 | * (child --> cmd) --> us |
| 459 | */ |
Lars Schneider | b84be55 | 2016-10-16 16:20:35 -0700 | [diff] [blame] | 460 | int err = 0; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 461 | struct strbuf nbuf = STRBUF_INIT; |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 462 | struct async async; |
| 463 | struct filter_params params; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 464 | |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 465 | memset(&async, 0, sizeof(async)); |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 466 | async.proc = filter_buffer_or_fd; |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 467 | async.data = ¶ms; |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 468 | async.out = -1; |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 469 | params.src = src; |
| 470 | params.size = len; |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 471 | params.fd = fd; |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 472 | params.cmd = cmd; |
Pete Wyckoff | a2b665d | 2010-12-22 06:40:13 -0800 | [diff] [blame] | 473 | params.path = path; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 474 | |
| 475 | fflush(NULL); |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 476 | if (start_async(&async)) |
| 477 | return 0; /* error was already reported */ |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 478 | |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 479 | if (strbuf_read(&nbuf, async.out, len) < 0) { |
Lars Schneider | b84be55 | 2016-10-16 16:20:35 -0700 | [diff] [blame] | 480 | err = error("read from external filter '%s' failed", cmd); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 481 | } |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 482 | if (close(async.out)) { |
Lars Schneider | b84be55 | 2016-10-16 16:20:35 -0700 | [diff] [blame] | 483 | err = error("read from external filter '%s' failed", cmd); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 484 | } |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 485 | if (finish_async(&async)) { |
Lars Schneider | b84be55 | 2016-10-16 16:20:35 -0700 | [diff] [blame] | 486 | err = error("external filter '%s' failed", cmd); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Lars Schneider | b84be55 | 2016-10-16 16:20:35 -0700 | [diff] [blame] | 489 | if (!err) { |
Pierre Habouzit | 90d16ec | 2007-10-05 10:11:59 +0200 | [diff] [blame] | 490 | strbuf_swap(dst, &nbuf); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 491 | } |
Pierre Habouzit | 90d16ec | 2007-10-05 10:11:59 +0200 | [diff] [blame] | 492 | strbuf_release(&nbuf); |
Lars Schneider | b84be55 | 2016-10-16 16:20:35 -0700 | [diff] [blame] | 493 | return !err; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 496 | #define CAP_CLEAN (1u<<0) |
| 497 | #define CAP_SMUDGE (1u<<1) |
| 498 | |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 499 | struct cmd2process { |
| 500 | struct hashmap_entry ent; /* must be the first member! */ |
| 501 | unsigned int supported_capabilities; |
| 502 | const char *cmd; |
| 503 | struct child_process process; |
| 504 | }; |
| 505 | |
| 506 | static int cmd_process_map_initialized; |
| 507 | static struct hashmap cmd_process_map; |
| 508 | |
| 509 | static int cmd2process_cmp(const struct cmd2process *e1, |
| 510 | const struct cmd2process *e2, |
| 511 | const void *unused) |
| 512 | { |
| 513 | return strcmp(e1->cmd, e2->cmd); |
| 514 | } |
| 515 | |
| 516 | static struct cmd2process *find_multi_file_filter_entry(struct hashmap *hashmap, const char *cmd) |
| 517 | { |
| 518 | struct cmd2process key; |
| 519 | hashmap_entry_init(&key, strhash(cmd)); |
| 520 | key.cmd = cmd; |
| 521 | return hashmap_get(hashmap, &key, NULL); |
| 522 | } |
| 523 | |
| 524 | static int packet_write_list(int fd, const char *line, ...) |
| 525 | { |
| 526 | va_list args; |
| 527 | int err; |
| 528 | va_start(args, line); |
| 529 | for (;;) { |
| 530 | if (!line) |
| 531 | break; |
| 532 | if (strlen(line) > LARGE_PACKET_DATA_MAX) |
| 533 | return -1; |
| 534 | err = packet_write_fmt_gently(fd, "%s\n", line); |
| 535 | if (err) |
| 536 | return err; |
| 537 | line = va_arg(args, const char*); |
| 538 | } |
| 539 | va_end(args); |
| 540 | return packet_flush_gently(fd); |
| 541 | } |
| 542 | |
| 543 | static void read_multi_file_filter_status(int fd, struct strbuf *status) |
| 544 | { |
| 545 | struct strbuf **pair; |
| 546 | char *line; |
| 547 | for (;;) { |
| 548 | line = packet_read_line(fd, NULL); |
| 549 | if (!line) |
| 550 | break; |
| 551 | pair = strbuf_split_str(line, '=', 2); |
| 552 | if (pair[0] && pair[0]->len && pair[1]) { |
| 553 | /* the last "status=<foo>" line wins */ |
| 554 | if (!strcmp(pair[0]->buf, "status=")) { |
| 555 | strbuf_reset(status); |
| 556 | strbuf_addbuf(status, pair[1]); |
| 557 | } |
| 558 | } |
| 559 | strbuf_list_free(pair); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | static void kill_multi_file_filter(struct hashmap *hashmap, struct cmd2process *entry) |
| 564 | { |
| 565 | if (!entry) |
| 566 | return; |
| 567 | |
| 568 | entry->process.clean_on_exit = 0; |
| 569 | kill(entry->process.pid, SIGTERM); |
| 570 | finish_command(&entry->process); |
| 571 | |
| 572 | hashmap_remove(hashmap, entry, NULL); |
| 573 | free(entry); |
| 574 | } |
| 575 | |
| 576 | static void stop_multi_file_filter(struct child_process *process) |
| 577 | { |
| 578 | sigchain_push(SIGPIPE, SIG_IGN); |
| 579 | /* Closing the pipe signals the filter to initiate a shutdown. */ |
| 580 | close(process->in); |
| 581 | close(process->out); |
| 582 | sigchain_pop(SIGPIPE); |
| 583 | /* Finish command will wait until the shutdown is complete. */ |
| 584 | finish_command(process); |
| 585 | } |
| 586 | |
| 587 | static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, const char *cmd) |
| 588 | { |
| 589 | int err; |
| 590 | struct cmd2process *entry; |
| 591 | struct child_process *process; |
| 592 | const char *argv[] = { cmd, NULL }; |
| 593 | struct string_list cap_list = STRING_LIST_INIT_NODUP; |
| 594 | char *cap_buf; |
| 595 | const char *cap_name; |
| 596 | |
| 597 | entry = xmalloc(sizeof(*entry)); |
| 598 | entry->cmd = cmd; |
| 599 | entry->supported_capabilities = 0; |
| 600 | process = &entry->process; |
| 601 | |
| 602 | child_process_init(process); |
| 603 | process->argv = argv; |
| 604 | process->use_shell = 1; |
| 605 | process->in = -1; |
| 606 | process->out = -1; |
| 607 | process->clean_on_exit = 1; |
| 608 | process->clean_on_exit_handler = stop_multi_file_filter; |
| 609 | |
| 610 | if (start_command(process)) { |
| 611 | error("cannot fork to run external filter '%s'", cmd); |
| 612 | return NULL; |
| 613 | } |
| 614 | |
| 615 | hashmap_entry_init(entry, strhash(cmd)); |
| 616 | |
| 617 | sigchain_push(SIGPIPE, SIG_IGN); |
| 618 | |
| 619 | err = packet_write_list(process->in, "git-filter-client", "version=2", NULL); |
| 620 | if (err) |
| 621 | goto done; |
| 622 | |
| 623 | err = strcmp(packet_read_line(process->out, NULL), "git-filter-server"); |
| 624 | if (err) { |
| 625 | error("external filter '%s' does not support filter protocol version 2", cmd); |
| 626 | goto done; |
| 627 | } |
| 628 | err = strcmp(packet_read_line(process->out, NULL), "version=2"); |
| 629 | if (err) |
| 630 | goto done; |
| 631 | err = packet_read_line(process->out, NULL) != NULL; |
| 632 | if (err) |
| 633 | goto done; |
| 634 | |
| 635 | err = packet_write_list(process->in, "capability=clean", "capability=smudge", NULL); |
| 636 | |
| 637 | for (;;) { |
| 638 | cap_buf = packet_read_line(process->out, NULL); |
| 639 | if (!cap_buf) |
| 640 | break; |
| 641 | string_list_split_in_place(&cap_list, cap_buf, '=', 1); |
| 642 | |
| 643 | if (cap_list.nr != 2 || strcmp(cap_list.items[0].string, "capability")) |
| 644 | continue; |
| 645 | |
| 646 | cap_name = cap_list.items[1].string; |
| 647 | if (!strcmp(cap_name, "clean")) { |
| 648 | entry->supported_capabilities |= CAP_CLEAN; |
| 649 | } else if (!strcmp(cap_name, "smudge")) { |
| 650 | entry->supported_capabilities |= CAP_SMUDGE; |
| 651 | } else { |
| 652 | warning( |
| 653 | "external filter '%s' requested unsupported filter capability '%s'", |
| 654 | cmd, cap_name |
| 655 | ); |
| 656 | } |
| 657 | |
| 658 | string_list_clear(&cap_list, 0); |
| 659 | } |
| 660 | |
| 661 | done: |
| 662 | sigchain_pop(SIGPIPE); |
| 663 | |
| 664 | if (err || errno == EPIPE) { |
| 665 | error("initialization for external filter '%s' failed", cmd); |
| 666 | kill_multi_file_filter(hashmap, entry); |
| 667 | return NULL; |
| 668 | } |
| 669 | |
| 670 | hashmap_add(hashmap, entry); |
| 671 | return entry; |
| 672 | } |
| 673 | |
| 674 | static int apply_multi_file_filter(const char *path, const char *src, size_t len, |
| 675 | int fd, struct strbuf *dst, const char *cmd, |
| 676 | const unsigned int wanted_capability) |
| 677 | { |
| 678 | int err; |
| 679 | struct cmd2process *entry; |
| 680 | struct child_process *process; |
| 681 | struct strbuf nbuf = STRBUF_INIT; |
| 682 | struct strbuf filter_status = STRBUF_INIT; |
| 683 | const char *filter_type; |
| 684 | |
| 685 | if (!cmd_process_map_initialized) { |
| 686 | cmd_process_map_initialized = 1; |
| 687 | hashmap_init(&cmd_process_map, (hashmap_cmp_fn) cmd2process_cmp, 0); |
| 688 | entry = NULL; |
| 689 | } else { |
| 690 | entry = find_multi_file_filter_entry(&cmd_process_map, cmd); |
| 691 | } |
| 692 | |
| 693 | fflush(NULL); |
| 694 | |
| 695 | if (!entry) { |
| 696 | entry = start_multi_file_filter(&cmd_process_map, cmd); |
| 697 | if (!entry) |
| 698 | return 0; |
| 699 | } |
| 700 | process = &entry->process; |
| 701 | |
| 702 | if (!(wanted_capability & entry->supported_capabilities)) |
| 703 | return 0; |
| 704 | |
| 705 | if (CAP_CLEAN & wanted_capability) |
| 706 | filter_type = "clean"; |
| 707 | else if (CAP_SMUDGE & wanted_capability) |
| 708 | filter_type = "smudge"; |
| 709 | else |
| 710 | die("unexpected filter type"); |
| 711 | |
| 712 | sigchain_push(SIGPIPE, SIG_IGN); |
| 713 | |
| 714 | assert(strlen(filter_type) < LARGE_PACKET_DATA_MAX - strlen("command=\n")); |
| 715 | err = packet_write_fmt_gently(process->in, "command=%s\n", filter_type); |
| 716 | if (err) |
| 717 | goto done; |
| 718 | |
| 719 | err = strlen(path) > LARGE_PACKET_DATA_MAX - strlen("pathname=\n"); |
| 720 | if (err) { |
| 721 | error("path name too long for external filter"); |
| 722 | goto done; |
| 723 | } |
| 724 | |
| 725 | err = packet_write_fmt_gently(process->in, "pathname=%s\n", path); |
| 726 | if (err) |
| 727 | goto done; |
| 728 | |
| 729 | err = packet_flush_gently(process->in); |
| 730 | if (err) |
| 731 | goto done; |
| 732 | |
| 733 | if (fd >= 0) |
| 734 | err = write_packetized_from_fd(fd, process->in); |
| 735 | else |
| 736 | err = write_packetized_from_buf(src, len, process->in); |
| 737 | if (err) |
| 738 | goto done; |
| 739 | |
| 740 | read_multi_file_filter_status(process->out, &filter_status); |
| 741 | err = strcmp(filter_status.buf, "success"); |
| 742 | if (err) |
| 743 | goto done; |
| 744 | |
| 745 | err = read_packetized_to_strbuf(process->out, &nbuf) < 0; |
| 746 | if (err) |
| 747 | goto done; |
| 748 | |
| 749 | read_multi_file_filter_status(process->out, &filter_status); |
| 750 | err = strcmp(filter_status.buf, "success"); |
| 751 | |
| 752 | done: |
| 753 | sigchain_pop(SIGPIPE); |
| 754 | |
| 755 | if (err || errno == EPIPE) { |
| 756 | if (!strcmp(filter_status.buf, "error")) { |
| 757 | /* The filter signaled a problem with the file. */ |
| 758 | } else if (!strcmp(filter_status.buf, "abort")) { |
| 759 | /* |
| 760 | * The filter signaled a permanent problem. Don't try to filter |
| 761 | * files with the same command for the lifetime of the current |
| 762 | * Git process. |
| 763 | */ |
| 764 | entry->supported_capabilities &= ~wanted_capability; |
| 765 | } else { |
| 766 | /* |
| 767 | * Something went wrong with the protocol filter. |
| 768 | * Force shutdown and restart if another blob requires filtering. |
| 769 | */ |
| 770 | error("external filter '%s' failed", cmd); |
| 771 | kill_multi_file_filter(&cmd_process_map, entry); |
| 772 | } |
| 773 | } else { |
| 774 | strbuf_swap(dst, &nbuf); |
| 775 | } |
| 776 | strbuf_release(&nbuf); |
| 777 | return !err; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | static struct convert_driver { |
| 781 | const char *name; |
| 782 | struct convert_driver *next; |
Brian Hetro | cd8be6c | 2008-07-05 01:24:42 -0400 | [diff] [blame] | 783 | const char *smudge; |
| 784 | const char *clean; |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 785 | const char *process; |
Jehan Bing | 36daaac | 2012-02-16 17:19:03 -0800 | [diff] [blame] | 786 | int required; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 787 | } *user_convert, **user_convert_tail; |
| 788 | |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 789 | static int apply_filter(const char *path, const char *src, size_t len, |
| 790 | int fd, struct strbuf *dst, struct convert_driver *drv, |
| 791 | const unsigned int wanted_capability) |
| 792 | { |
| 793 | const char *cmd = NULL; |
| 794 | |
| 795 | if (!drv) |
| 796 | return 0; |
| 797 | |
| 798 | if (!dst) |
| 799 | return 1; |
| 800 | |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 801 | if ((CAP_CLEAN & wanted_capability) && !drv->process && drv->clean) |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 802 | cmd = drv->clean; |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 803 | else if ((CAP_SMUDGE & wanted_capability) && !drv->process && drv->smudge) |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 804 | cmd = drv->smudge; |
| 805 | |
| 806 | if (cmd && *cmd) |
| 807 | return apply_single_file_filter(path, src, len, fd, dst, cmd); |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 808 | else if (drv->process && *drv->process) |
| 809 | return apply_multi_file_filter(path, src, len, fd, dst, drv->process, wanted_capability); |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 810 | |
| 811 | return 0; |
| 812 | } |
| 813 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 814 | static int read_convert_config(const char *var, const char *value, void *cb) |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 815 | { |
Jeff King | d731f0a | 2013-01-23 01:24:23 -0500 | [diff] [blame] | 816 | const char *key, *name; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 817 | int namelen; |
| 818 | struct convert_driver *drv; |
| 819 | |
| 820 | /* |
| 821 | * External conversion drivers are configured using |
| 822 | * "filter.<name>.variable". |
| 823 | */ |
Jeff King | d731f0a | 2013-01-23 01:24:23 -0500 | [diff] [blame] | 824 | if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name) |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 825 | return 0; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 826 | for (drv = user_convert; drv; drv = drv->next) |
| 827 | if (!strncmp(drv->name, name, namelen) && !drv->name[namelen]) |
| 828 | break; |
| 829 | if (!drv) { |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 830 | drv = xcalloc(1, sizeof(struct convert_driver)); |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 831 | drv->name = xmemdupz(name, namelen); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 832 | *user_convert_tail = drv; |
| 833 | user_convert_tail = &(drv->next); |
| 834 | } |
| 835 | |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 836 | /* |
| 837 | * filter.<name>.smudge and filter.<name>.clean specifies |
| 838 | * the command line: |
| 839 | * |
| 840 | * command-line |
| 841 | * |
| 842 | * The command-line will not be interpolated in any way. |
| 843 | */ |
| 844 | |
Jeff King | d731f0a | 2013-01-23 01:24:23 -0500 | [diff] [blame] | 845 | if (!strcmp("smudge", key)) |
Brian Hetro | cd8be6c | 2008-07-05 01:24:42 -0400 | [diff] [blame] | 846 | return git_config_string(&drv->smudge, var, value); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 847 | |
Jeff King | d731f0a | 2013-01-23 01:24:23 -0500 | [diff] [blame] | 848 | if (!strcmp("clean", key)) |
Brian Hetro | cd8be6c | 2008-07-05 01:24:42 -0400 | [diff] [blame] | 849 | return git_config_string(&drv->clean, var, value); |
| 850 | |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 851 | if (!strcmp("process", key)) |
| 852 | return git_config_string(&drv->process, var, value); |
| 853 | |
Jeff King | d731f0a | 2013-01-23 01:24:23 -0500 | [diff] [blame] | 854 | if (!strcmp("required", key)) { |
Jehan Bing | 36daaac | 2012-02-16 17:19:03 -0800 | [diff] [blame] | 855 | drv->required = git_config_bool(var, value); |
| 856 | return 0; |
| 857 | } |
| 858 | |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 859 | return 0; |
| 860 | } |
| 861 | |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 862 | static int count_ident(const char *cp, unsigned long size) |
| 863 | { |
| 864 | /* |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 865 | * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$" |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 866 | */ |
| 867 | int cnt = 0; |
| 868 | char ch; |
| 869 | |
| 870 | while (size) { |
| 871 | ch = *cp++; |
| 872 | size--; |
| 873 | if (ch != '$') |
| 874 | continue; |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 875 | if (size < 3) |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 876 | break; |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 877 | if (memcmp("Id", cp, 2)) |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 878 | continue; |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 879 | ch = cp[2]; |
| 880 | cp += 3; |
| 881 | size -= 3; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 882 | if (ch == '$') |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 883 | cnt++; /* $Id$ */ |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 884 | if (ch != ':') |
| 885 | continue; |
| 886 | |
| 887 | /* |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 888 | * "$Id: ... "; scan up to the closing dollar sign and discard. |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 889 | */ |
| 890 | while (size) { |
| 891 | ch = *cp++; |
| 892 | size--; |
| 893 | if (ch == '$') { |
| 894 | cnt++; |
| 895 | break; |
| 896 | } |
Henrik Grubbström | a9f3049 | 2010-04-06 14:46:37 +0200 | [diff] [blame] | 897 | if (ch == '\n') |
| 898 | break; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | return cnt; |
| 902 | } |
| 903 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 904 | static int ident_to_git(const char *path, const char *src, size_t len, |
| 905 | struct strbuf *buf, int ident) |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 906 | { |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 907 | char *dst, *dollar; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 908 | |
Jeff King | 4c3b57b | 2012-02-24 17:05:03 -0500 | [diff] [blame] | 909 | if (!ident || (src && !count_ident(src, len))) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 910 | return 0; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 911 | |
Jeff King | 92ac319 | 2012-02-24 17:02:37 -0500 | [diff] [blame] | 912 | if (!buf) |
| 913 | return 1; |
| 914 | |
Pierre Habouzit | 90d16ec | 2007-10-05 10:11:59 +0200 | [diff] [blame] | 915 | /* only grow if not in place */ |
| 916 | if (strbuf_avail(buf) + buf->len < len) |
| 917 | strbuf_grow(buf, len - buf->len); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 918 | dst = buf->buf; |
| 919 | for (;;) { |
| 920 | dollar = memchr(src, '$', len); |
| 921 | if (!dollar) |
| 922 | break; |
Thomas Rast | 7732118 | 2011-08-29 22:06:04 +0200 | [diff] [blame] | 923 | memmove(dst, src, dollar + 1 - src); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 924 | dst += dollar + 1 - src; |
| 925 | len -= dollar + 1 - src; |
| 926 | src = dollar + 1; |
| 927 | |
| 928 | if (len > 3 && !memcmp(src, "Id:", 3)) { |
| 929 | dollar = memchr(src + 3, '$', len - 3); |
| 930 | if (!dollar) |
| 931 | break; |
Henrik Grubbström | a9f3049 | 2010-04-06 14:46:37 +0200 | [diff] [blame] | 932 | if (memchr(src + 3, '\n', dollar - src - 3)) { |
| 933 | /* Line break before the next dollar. */ |
| 934 | continue; |
| 935 | } |
| 936 | |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 937 | memcpy(dst, "Id$", 3); |
| 938 | dst += 3; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 939 | len -= dollar + 1 - src; |
| 940 | src = dollar + 1; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 941 | } |
| 942 | } |
Thomas Rast | 7732118 | 2011-08-29 22:06:04 +0200 | [diff] [blame] | 943 | memmove(dst, src, len); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 944 | strbuf_setlen(buf, dst + len - buf->buf); |
| 945 | return 1; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 948 | static int ident_to_worktree(const char *path, const char *src, size_t len, |
| 949 | struct strbuf *buf, int ident) |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 950 | { |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 951 | unsigned char sha1[20]; |
Henrik Grubbström | 07814d9 | 2010-04-06 14:46:38 +0200 | [diff] [blame] | 952 | char *to_free = NULL, *dollar, *spc; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 953 | int cnt; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 954 | |
| 955 | if (!ident) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 956 | return 0; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 957 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 958 | cnt = count_ident(src, len); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 959 | if (!cnt) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 960 | return 0; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 961 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 962 | /* are we "faking" in place editing ? */ |
| 963 | if (src == buf->buf) |
Pierre Habouzit | b315c5c | 2007-09-27 12:58:23 +0200 | [diff] [blame] | 964 | to_free = strbuf_detach(buf, NULL); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 965 | hash_sha1_file(src, len, "blob", sha1); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 966 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 967 | strbuf_grow(buf, len + cnt * 43); |
| 968 | for (;;) { |
| 969 | /* step 1: run to the next '$' */ |
| 970 | dollar = memchr(src, '$', len); |
| 971 | if (!dollar) |
| 972 | break; |
| 973 | strbuf_add(buf, src, dollar + 1 - src); |
| 974 | len -= dollar + 1 - src; |
| 975 | src = dollar + 1; |
| 976 | |
| 977 | /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */ |
| 978 | if (len < 3 || memcmp("Id", src, 2)) |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 979 | continue; |
| 980 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 981 | /* step 3: skip over Id$ or Id:xxxxx$ */ |
| 982 | if (src[2] == '$') { |
| 983 | src += 3; |
| 984 | len -= 3; |
| 985 | } else if (src[2] == ':') { |
Andy Parkins | c23290d | 2007-05-25 11:50:08 +0100 | [diff] [blame] | 986 | /* |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 987 | * It's possible that an expanded Id has crept its way into the |
Henrik Grubbström | 07814d9 | 2010-04-06 14:46:38 +0200 | [diff] [blame] | 988 | * repository, we cope with that by stripping the expansion out. |
| 989 | * This is probably not a good idea, since it will cause changes |
| 990 | * on checkout, which won't go away by stash, but let's keep it |
| 991 | * for git-style ids. |
Andy Parkins | c23290d | 2007-05-25 11:50:08 +0100 | [diff] [blame] | 992 | */ |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 993 | dollar = memchr(src + 3, '$', len - 3); |
| 994 | if (!dollar) { |
| 995 | /* incomplete keyword, no more '$', so just quit the loop */ |
| 996 | break; |
| 997 | } |
| 998 | |
Henrik Grubbström | a9f3049 | 2010-04-06 14:46:37 +0200 | [diff] [blame] | 999 | if (memchr(src + 3, '\n', dollar - src - 3)) { |
| 1000 | /* Line break before the next dollar. */ |
| 1001 | continue; |
| 1002 | } |
| 1003 | |
Henrik Grubbström | 07814d9 | 2010-04-06 14:46:38 +0200 | [diff] [blame] | 1004 | spc = memchr(src + 4, ' ', dollar - src - 4); |
| 1005 | if (spc && spc < dollar-1) { |
| 1006 | /* There are spaces in unexpected places. |
| 1007 | * This is probably an id from some other |
| 1008 | * versioning system. Keep it for now. |
| 1009 | */ |
| 1010 | continue; |
| 1011 | } |
| 1012 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1013 | len -= dollar + 1 - src; |
| 1014 | src = dollar + 1; |
| 1015 | } else { |
| 1016 | /* it wasn't a "Id$" or "Id:xxxx$" */ |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1017 | continue; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1018 | } |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1019 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1020 | /* step 4: substitute */ |
| 1021 | strbuf_addstr(buf, "Id: "); |
| 1022 | strbuf_add(buf, sha1_to_hex(sha1), 40); |
| 1023 | strbuf_addstr(buf, " $"); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1024 | } |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1025 | strbuf_add(buf, src, len); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1026 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1027 | free(to_free); |
| 1028 | return 1; |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
Torsten Bögershausen | 92cce13 | 2016-02-05 17:13:22 +0100 | [diff] [blame] | 1031 | static enum crlf_action git_path_check_crlf(struct git_attr_check *check) |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 1032 | { |
Junio C Hamano | 6073ee8 | 2007-04-20 23:44:02 -0700 | [diff] [blame] | 1033 | const char *value = check->value; |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 1034 | |
Junio C Hamano | 6073ee8 | 2007-04-20 23:44:02 -0700 | [diff] [blame] | 1035 | if (ATTR_TRUE(value)) |
| 1036 | return CRLF_TEXT; |
| 1037 | else if (ATTR_FALSE(value)) |
| 1038 | return CRLF_BINARY; |
| 1039 | else if (ATTR_UNSET(value)) |
| 1040 | ; |
| 1041 | else if (!strcmp(value, "input")) |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 1042 | return CRLF_TEXT_INPUT; |
Eyvind Bernhardsen | fd6cce9 | 2010-05-19 22:43:10 +0200 | [diff] [blame] | 1043 | else if (!strcmp(value, "auto")) |
| 1044 | return CRLF_AUTO; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 1045 | return CRLF_UNDEFINED; |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Torsten Bögershausen | 92cce13 | 2016-02-05 17:13:22 +0100 | [diff] [blame] | 1048 | static enum eol git_path_check_eol(struct git_attr_check *check) |
Eyvind Bernhardsen | fd6cce9 | 2010-05-19 22:43:10 +0200 | [diff] [blame] | 1049 | { |
| 1050 | const char *value = check->value; |
| 1051 | |
| 1052 | if (ATTR_UNSET(value)) |
| 1053 | ; |
| 1054 | else if (!strcmp(value, "lf")) |
| 1055 | return EOL_LF; |
| 1056 | else if (!strcmp(value, "crlf")) |
| 1057 | return EOL_CRLF; |
| 1058 | return EOL_UNSET; |
| 1059 | } |
| 1060 | |
Torsten Bögershausen | 92cce13 | 2016-02-05 17:13:22 +0100 | [diff] [blame] | 1061 | static struct convert_driver *git_path_check_convert(struct git_attr_check *check) |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 1062 | { |
| 1063 | const char *value = check->value; |
| 1064 | struct convert_driver *drv; |
| 1065 | |
| 1066 | if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value)) |
| 1067 | return NULL; |
| 1068 | for (drv = user_convert; drv; drv = drv->next) |
| 1069 | if (!strcmp(value, drv->name)) |
| 1070 | return drv; |
| 1071 | return NULL; |
| 1072 | } |
| 1073 | |
Torsten Bögershausen | 92cce13 | 2016-02-05 17:13:22 +0100 | [diff] [blame] | 1074 | static int git_path_check_ident(struct git_attr_check *check) |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1075 | { |
| 1076 | const char *value = check->value; |
| 1077 | |
| 1078 | return !!ATTR_TRUE(value); |
| 1079 | } |
| 1080 | |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1081 | struct conv_attrs { |
| 1082 | struct convert_driver *drv; |
Torsten Bögershausen | bb211b4 | 2016-02-05 17:13:23 +0100 | [diff] [blame] | 1083 | enum crlf_action attr_action; /* What attr says */ |
| 1084 | enum crlf_action crlf_action; /* When no attr is set, use core.autocrlf */ |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1085 | int ident; |
| 1086 | }; |
| 1087 | |
Junio C Hamano | 8329596 | 2011-05-09 11:23:04 -0700 | [diff] [blame] | 1088 | static const char *conv_attr_name[] = { |
| 1089 | "crlf", "ident", "filter", "eol", "text", |
| 1090 | }; |
| 1091 | #define NUM_CONV_ATTRS ARRAY_SIZE(conv_attr_name) |
| 1092 | |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1093 | static void convert_attrs(struct conv_attrs *ca, const char *path) |
Junio C Hamano | 8329596 | 2011-05-09 11:23:04 -0700 | [diff] [blame] | 1094 | { |
| 1095 | int i; |
| 1096 | static struct git_attr_check ccheck[NUM_CONV_ATTRS]; |
| 1097 | |
| 1098 | if (!ccheck[0].attr) { |
| 1099 | for (i = 0; i < NUM_CONV_ATTRS; i++) |
| 1100 | ccheck[i].attr = git_attr(conv_attr_name[i]); |
| 1101 | user_convert_tail = &user_convert; |
| 1102 | git_config(read_convert_config, NULL); |
| 1103 | } |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1104 | |
Michael Haggerty | d932f4e | 2011-08-04 06:36:33 +0200 | [diff] [blame] | 1105 | if (!git_check_attr(path, NUM_CONV_ATTRS, ccheck)) { |
Torsten Bögershausen | bb211b4 | 2016-02-05 17:13:23 +0100 | [diff] [blame] | 1106 | ca->crlf_action = git_path_check_crlf(ccheck + 4); |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 1107 | if (ca->crlf_action == CRLF_UNDEFINED) |
Torsten Bögershausen | 92cce13 | 2016-02-05 17:13:22 +0100 | [diff] [blame] | 1108 | ca->crlf_action = git_path_check_crlf(ccheck + 0); |
Torsten Bögershausen | bb211b4 | 2016-02-05 17:13:23 +0100 | [diff] [blame] | 1109 | ca->attr_action = ca->crlf_action; |
Torsten Bögershausen | 92cce13 | 2016-02-05 17:13:22 +0100 | [diff] [blame] | 1110 | ca->ident = git_path_check_ident(ccheck + 1); |
| 1111 | ca->drv = git_path_check_convert(ccheck + 2); |
Torsten Bögershausen | 817a0c7 | 2016-02-23 18:07:19 +0100 | [diff] [blame] | 1112 | if (ca->crlf_action != CRLF_BINARY) { |
| 1113 | enum eol eol_attr = git_path_check_eol(ccheck + 3); |
Torsten Bögershausen | 6523728 | 2016-06-28 10:01:13 +0200 | [diff] [blame] | 1114 | if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_LF) |
| 1115 | ca->crlf_action = CRLF_AUTO_INPUT; |
| 1116 | else if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_CRLF) |
| 1117 | ca->crlf_action = CRLF_AUTO_CRLF; |
| 1118 | else if (eol_attr == EOL_LF) |
Torsten Bögershausen | 817a0c7 | 2016-02-23 18:07:19 +0100 | [diff] [blame] | 1119 | ca->crlf_action = CRLF_TEXT_INPUT; |
| 1120 | else if (eol_attr == EOL_CRLF) |
| 1121 | ca->crlf_action = CRLF_TEXT_CRLF; |
| 1122 | } |
| 1123 | ca->attr_action = ca->crlf_action; |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1124 | } else { |
| 1125 | ca->drv = NULL; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 1126 | ca->crlf_action = CRLF_UNDEFINED; |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1127 | ca->ident = 0; |
| 1128 | } |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 1129 | if (ca->crlf_action == CRLF_TEXT) |
| 1130 | ca->crlf_action = text_eol_is_crlf() ? CRLF_TEXT_CRLF : CRLF_TEXT_INPUT; |
| 1131 | if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_FALSE) |
| 1132 | ca->crlf_action = CRLF_BINARY; |
| 1133 | if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_TRUE) |
| 1134 | ca->crlf_action = CRLF_AUTO_CRLF; |
| 1135 | if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_INPUT) |
| 1136 | ca->crlf_action = CRLF_AUTO_INPUT; |
Junio C Hamano | 8329596 | 2011-05-09 11:23:04 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 1139 | int would_convert_to_git_filter_fd(const char *path) |
| 1140 | { |
| 1141 | struct conv_attrs ca; |
| 1142 | |
| 1143 | convert_attrs(&ca, path); |
| 1144 | if (!ca.drv) |
| 1145 | return 0; |
| 1146 | |
| 1147 | /* |
| 1148 | * Apply a filter to an fd only if the filter is required to succeed. |
| 1149 | * We must die if the filter fails, because the original data before |
| 1150 | * filtering is not available. |
| 1151 | */ |
| 1152 | if (!ca.drv->required) |
| 1153 | return 0; |
| 1154 | |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 1155 | return apply_filter(path, NULL, 0, -1, NULL, ca.drv, CAP_CLEAN); |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 1156 | } |
| 1157 | |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 1158 | const char *get_convert_attr_ascii(const char *path) |
| 1159 | { |
| 1160 | struct conv_attrs ca; |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 1161 | |
| 1162 | convert_attrs(&ca, path); |
Torsten Bögershausen | bb211b4 | 2016-02-05 17:13:23 +0100 | [diff] [blame] | 1163 | switch (ca.attr_action) { |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 1164 | case CRLF_UNDEFINED: |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 1165 | return ""; |
| 1166 | case CRLF_BINARY: |
| 1167 | return "-text"; |
| 1168 | case CRLF_TEXT: |
| 1169 | return "text"; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 1170 | case CRLF_TEXT_INPUT: |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 1171 | return "text eol=lf"; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 1172 | case CRLF_TEXT_CRLF: |
| 1173 | return "text eol=crlf"; |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 1174 | case CRLF_AUTO: |
| 1175 | return "text=auto"; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 1176 | case CRLF_AUTO_CRLF: |
Torsten Bögershausen | 6523728 | 2016-06-28 10:01:13 +0200 | [diff] [blame] | 1177 | return "text=auto eol=crlf"; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 1178 | case CRLF_AUTO_INPUT: |
Torsten Bögershausen | 6523728 | 2016-06-28 10:01:13 +0200 | [diff] [blame] | 1179 | return "text=auto eol=lf"; |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 1180 | } |
| 1181 | return ""; |
| 1182 | } |
| 1183 | |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 1184 | int convert_to_git(const char *path, const char *src, size_t len, |
| 1185 | struct strbuf *dst, enum safe_crlf checksafe) |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 1186 | { |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1187 | int ret = 0; |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1188 | struct conv_attrs ca; |
Junio C Hamano | 6073ee8 | 2007-04-20 23:44:02 -0700 | [diff] [blame] | 1189 | |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1190 | convert_attrs(&ca, path); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1191 | |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 1192 | ret |= apply_filter(path, src, len, -1, dst, ca.drv, CAP_CLEAN); |
| 1193 | if (!ret && ca.drv && ca.drv->required) |
Jehan Bing | 36daaac | 2012-02-16 17:19:03 -0800 | [diff] [blame] | 1194 | die("%s: clean filter '%s' failed", path, ca.drv->name); |
| 1195 | |
Jeff King | 92ac319 | 2012-02-24 17:02:37 -0500 | [diff] [blame] | 1196 | if (ret && dst) { |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1197 | src = dst->buf; |
| 1198 | len = dst->len; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 1199 | } |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1200 | ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe); |
Jeff King | 92ac319 | 2012-02-24 17:02:37 -0500 | [diff] [blame] | 1201 | if (ret && dst) { |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1202 | src = dst->buf; |
| 1203 | len = dst->len; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1204 | } |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1205 | return ret | ident_to_git(path, src, len, dst, ca.ident); |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 1208 | void convert_to_git_filter_fd(const char *path, int fd, struct strbuf *dst, |
| 1209 | enum safe_crlf checksafe) |
| 1210 | { |
| 1211 | struct conv_attrs ca; |
| 1212 | convert_attrs(&ca, path); |
| 1213 | |
| 1214 | assert(ca.drv); |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 1215 | assert(ca.drv->clean || ca.drv->process); |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 1216 | |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 1217 | if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN)) |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 1218 | die("%s: clean filter '%s' failed", path, ca.drv->name); |
| 1219 | |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 1220 | crlf_to_git(path, dst->buf, dst->len, dst, ca.crlf_action, checksafe); |
| 1221 | ident_to_git(path, dst->buf, dst->len, dst, ca.ident); |
| 1222 | } |
| 1223 | |
Eyvind Bernhardsen | 43dd233 | 2010-07-02 21:20:49 +0200 | [diff] [blame] | 1224 | static int convert_to_working_tree_internal(const char *path, const char *src, |
| 1225 | size_t len, struct strbuf *dst, |
| 1226 | int normalizing) |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 1227 | { |
Jehan Bing | 36daaac | 2012-02-16 17:19:03 -0800 | [diff] [blame] | 1228 | int ret = 0, ret_filter = 0; |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1229 | struct conv_attrs ca; |
Junio C Hamano | 6073ee8 | 2007-04-20 23:44:02 -0700 | [diff] [blame] | 1230 | |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1231 | convert_attrs(&ca, path); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1232 | |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1233 | ret |= ident_to_worktree(path, src, len, dst, ca.ident); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1234 | if (ret) { |
| 1235 | src = dst->buf; |
| 1236 | len = dst->len; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1237 | } |
Eyvind Bernhardsen | 43dd233 | 2010-07-02 21:20:49 +0200 | [diff] [blame] | 1238 | /* |
| 1239 | * CRLF conversion can be skipped if normalizing, unless there |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 1240 | * is a smudge or process filter (even if the process filter doesn't |
| 1241 | * support smudge). The filters might expect CRLFs. |
Eyvind Bernhardsen | 43dd233 | 2010-07-02 21:20:49 +0200 | [diff] [blame] | 1242 | */ |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 1243 | if ((ca.drv && (ca.drv->smudge || ca.drv->process)) || !normalizing) { |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1244 | ret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action); |
Eyvind Bernhardsen | 43dd233 | 2010-07-02 21:20:49 +0200 | [diff] [blame] | 1245 | if (ret) { |
| 1246 | src = dst->buf; |
| 1247 | len = dst->len; |
| 1248 | } |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 1249 | } |
Jehan Bing | 36daaac | 2012-02-16 17:19:03 -0800 | [diff] [blame] | 1250 | |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 1251 | ret_filter = apply_filter(path, src, len, -1, dst, ca.drv, CAP_SMUDGE); |
| 1252 | if (!ret_filter && ca.drv && ca.drv->required) |
Jehan Bing | 36daaac | 2012-02-16 17:19:03 -0800 | [diff] [blame] | 1253 | die("%s: smudge filter %s failed", path, ca.drv->name); |
| 1254 | |
| 1255 | return ret | ret_filter; |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 1256 | } |
Eyvind Bernhardsen | f217f0e | 2010-07-02 21:20:47 +0200 | [diff] [blame] | 1257 | |
Eyvind Bernhardsen | 43dd233 | 2010-07-02 21:20:49 +0200 | [diff] [blame] | 1258 | int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst) |
| 1259 | { |
| 1260 | return convert_to_working_tree_internal(path, src, len, dst, 0); |
| 1261 | } |
| 1262 | |
Eyvind Bernhardsen | f217f0e | 2010-07-02 21:20:47 +0200 | [diff] [blame] | 1263 | int renormalize_buffer(const char *path, const char *src, size_t len, struct strbuf *dst) |
| 1264 | { |
Eyvind Bernhardsen | 43dd233 | 2010-07-02 21:20:49 +0200 | [diff] [blame] | 1265 | int ret = convert_to_working_tree_internal(path, src, len, dst, 1); |
Eyvind Bernhardsen | f217f0e | 2010-07-02 21:20:47 +0200 | [diff] [blame] | 1266 | if (ret) { |
| 1267 | src = dst->buf; |
| 1268 | len = dst->len; |
| 1269 | } |
Torsten Bögershausen | 6523728 | 2016-06-28 10:01:13 +0200 | [diff] [blame] | 1270 | return ret | convert_to_git(path, src, len, dst, SAFE_CRLF_RENORMALIZE); |
Eyvind Bernhardsen | f217f0e | 2010-07-02 21:20:47 +0200 | [diff] [blame] | 1271 | } |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 1272 | |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 1273 | /***************************************************************** |
| 1274 | * |
Ondřej Bílka | 749f763 | 2013-07-22 23:02:23 +0200 | [diff] [blame] | 1275 | * Streaming conversion support |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 1276 | * |
| 1277 | *****************************************************************/ |
| 1278 | |
| 1279 | typedef int (*filter_fn)(struct stream_filter *, |
| 1280 | const char *input, size_t *isize_p, |
| 1281 | char *output, size_t *osize_p); |
| 1282 | typedef void (*free_fn)(struct stream_filter *); |
| 1283 | |
| 1284 | struct stream_filter_vtbl { |
| 1285 | filter_fn filter; |
| 1286 | free_fn free; |
| 1287 | }; |
| 1288 | |
| 1289 | struct stream_filter { |
| 1290 | struct stream_filter_vtbl *vtbl; |
| 1291 | }; |
| 1292 | |
| 1293 | static int null_filter_fn(struct stream_filter *filter, |
| 1294 | const char *input, size_t *isize_p, |
| 1295 | char *output, size_t *osize_p) |
| 1296 | { |
Junio C Hamano | 4ae6670 | 2011-05-21 14:05:51 -0700 | [diff] [blame] | 1297 | size_t count; |
| 1298 | |
| 1299 | if (!input) |
| 1300 | return 0; /* we do not keep any states */ |
| 1301 | count = *isize_p; |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 1302 | if (*osize_p < count) |
| 1303 | count = *osize_p; |
| 1304 | if (count) { |
| 1305 | memmove(output, input, count); |
| 1306 | *isize_p -= count; |
| 1307 | *osize_p -= count; |
| 1308 | } |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
| 1312 | static void null_free_fn(struct stream_filter *filter) |
| 1313 | { |
| 1314 | ; /* nothing -- null instances are shared */ |
| 1315 | } |
| 1316 | |
| 1317 | static struct stream_filter_vtbl null_vtbl = { |
| 1318 | null_filter_fn, |
| 1319 | null_free_fn, |
| 1320 | }; |
| 1321 | |
| 1322 | static struct stream_filter null_filter_singleton = { |
| 1323 | &null_vtbl, |
| 1324 | }; |
| 1325 | |
| 1326 | int is_null_stream_filter(struct stream_filter *filter) |
| 1327 | { |
| 1328 | return filter == &null_filter_singleton; |
| 1329 | } |
| 1330 | |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1331 | |
| 1332 | /* |
| 1333 | * LF-to-CRLF filter |
| 1334 | */ |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1335 | |
| 1336 | struct lf_to_crlf_filter { |
| 1337 | struct stream_filter filter; |
Junio C Hamano | 8496f56 | 2011-12-16 15:44:18 -0800 | [diff] [blame] | 1338 | unsigned has_held:1; |
| 1339 | char held; |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1340 | }; |
| 1341 | |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1342 | static int lf_to_crlf_filter_fn(struct stream_filter *filter, |
| 1343 | const char *input, size_t *isize_p, |
| 1344 | char *output, size_t *osize_p) |
| 1345 | { |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1346 | size_t count, o = 0; |
| 1347 | struct lf_to_crlf_filter *lf_to_crlf = (struct lf_to_crlf_filter *)filter; |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1348 | |
Junio C Hamano | 8496f56 | 2011-12-16 15:44:18 -0800 | [diff] [blame] | 1349 | /* |
| 1350 | * We may be holding onto the CR to see if it is followed by a |
| 1351 | * LF, in which case we would need to go to the main loop. |
| 1352 | * Otherwise, just emit it to the output stream. |
| 1353 | */ |
| 1354 | if (lf_to_crlf->has_held && (lf_to_crlf->held != '\r' || !input)) { |
| 1355 | output[o++] = lf_to_crlf->held; |
| 1356 | lf_to_crlf->has_held = 0; |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1357 | } |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1358 | |
Junio C Hamano | 87afe9a | 2011-12-16 14:39:37 -0800 | [diff] [blame] | 1359 | /* We are told to drain */ |
| 1360 | if (!input) { |
| 1361 | *osize_p -= o; |
| 1362 | return 0; |
| 1363 | } |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1364 | |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1365 | count = *isize_p; |
Junio C Hamano | 8496f56 | 2011-12-16 15:44:18 -0800 | [diff] [blame] | 1366 | if (count || lf_to_crlf->has_held) { |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1367 | size_t i; |
Junio C Hamano | 8496f56 | 2011-12-16 15:44:18 -0800 | [diff] [blame] | 1368 | int was_cr = 0; |
| 1369 | |
| 1370 | if (lf_to_crlf->has_held) { |
| 1371 | was_cr = 1; |
| 1372 | lf_to_crlf->has_held = 0; |
| 1373 | } |
| 1374 | |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1375 | for (i = 0; o < *osize_p && i < count; i++) { |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1376 | char ch = input[i]; |
Junio C Hamano | 8496f56 | 2011-12-16 15:44:18 -0800 | [diff] [blame] | 1377 | |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1378 | if (ch == '\n') { |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1379 | output[o++] = '\r'; |
Junio C Hamano | 8496f56 | 2011-12-16 15:44:18 -0800 | [diff] [blame] | 1380 | } else if (was_cr) { |
| 1381 | /* |
| 1382 | * Previous round saw CR and it is not followed |
| 1383 | * by a LF; emit the CR before processing the |
| 1384 | * current character. |
| 1385 | */ |
| 1386 | output[o++] = '\r'; |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1387 | } |
Junio C Hamano | 8496f56 | 2011-12-16 15:44:18 -0800 | [diff] [blame] | 1388 | |
| 1389 | /* |
| 1390 | * We may have consumed the last output slot, |
| 1391 | * in which case we need to break out of this |
| 1392 | * loop; hold the current character before |
| 1393 | * returning. |
| 1394 | */ |
| 1395 | if (*osize_p <= o) { |
| 1396 | lf_to_crlf->has_held = 1; |
| 1397 | lf_to_crlf->held = ch; |
| 1398 | continue; /* break but increment i */ |
| 1399 | } |
| 1400 | |
| 1401 | if (ch == '\r') { |
| 1402 | was_cr = 1; |
| 1403 | continue; |
| 1404 | } |
| 1405 | |
| 1406 | was_cr = 0; |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1407 | output[o++] = ch; |
| 1408 | } |
| 1409 | |
| 1410 | *osize_p -= o; |
| 1411 | *isize_p -= i; |
Junio C Hamano | 8496f56 | 2011-12-16 15:44:18 -0800 | [diff] [blame] | 1412 | |
| 1413 | if (!lf_to_crlf->has_held && was_cr) { |
| 1414 | lf_to_crlf->has_held = 1; |
| 1415 | lf_to_crlf->held = '\r'; |
| 1416 | } |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1417 | } |
| 1418 | return 0; |
| 1419 | } |
| 1420 | |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1421 | static void lf_to_crlf_free_fn(struct stream_filter *filter) |
| 1422 | { |
| 1423 | free(filter); |
| 1424 | } |
| 1425 | |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1426 | static struct stream_filter_vtbl lf_to_crlf_vtbl = { |
| 1427 | lf_to_crlf_filter_fn, |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1428 | lf_to_crlf_free_fn, |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1429 | }; |
| 1430 | |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1431 | static struct stream_filter *lf_to_crlf_filter(void) |
| 1432 | { |
Junio C Hamano | 87afe9a | 2011-12-16 14:39:37 -0800 | [diff] [blame] | 1433 | struct lf_to_crlf_filter *lf_to_crlf = xcalloc(1, sizeof(*lf_to_crlf)); |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1434 | |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1435 | lf_to_crlf->filter.vtbl = &lf_to_crlf_vtbl; |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1436 | return (struct stream_filter *)lf_to_crlf; |
| 1437 | } |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1438 | |
| 1439 | /* |
Junio C Hamano | a265a7f | 2011-05-21 18:28:41 -0700 | [diff] [blame] | 1440 | * Cascade filter |
| 1441 | */ |
| 1442 | #define FILTER_BUFFER 1024 |
| 1443 | struct cascade_filter { |
| 1444 | struct stream_filter filter; |
| 1445 | struct stream_filter *one; |
| 1446 | struct stream_filter *two; |
| 1447 | char buf[FILTER_BUFFER]; |
| 1448 | int end, ptr; |
| 1449 | }; |
| 1450 | |
| 1451 | static int cascade_filter_fn(struct stream_filter *filter, |
| 1452 | const char *input, size_t *isize_p, |
| 1453 | char *output, size_t *osize_p) |
| 1454 | { |
| 1455 | struct cascade_filter *cas = (struct cascade_filter *) filter; |
| 1456 | size_t filled = 0; |
| 1457 | size_t sz = *osize_p; |
| 1458 | size_t to_feed, remaining; |
| 1459 | |
| 1460 | /* |
| 1461 | * input -- (one) --> buf -- (two) --> output |
| 1462 | */ |
| 1463 | while (filled < sz) { |
| 1464 | remaining = sz - filled; |
| 1465 | |
| 1466 | /* do we already have something to feed two with? */ |
| 1467 | if (cas->ptr < cas->end) { |
| 1468 | to_feed = cas->end - cas->ptr; |
| 1469 | if (stream_filter(cas->two, |
| 1470 | cas->buf + cas->ptr, &to_feed, |
| 1471 | output + filled, &remaining)) |
| 1472 | return -1; |
| 1473 | cas->ptr += (cas->end - cas->ptr) - to_feed; |
| 1474 | filled = sz - remaining; |
| 1475 | continue; |
| 1476 | } |
| 1477 | |
| 1478 | /* feed one from upstream and have it emit into our buffer */ |
| 1479 | to_feed = input ? *isize_p : 0; |
| 1480 | if (input && !to_feed) |
| 1481 | break; |
| 1482 | remaining = sizeof(cas->buf); |
| 1483 | if (stream_filter(cas->one, |
| 1484 | input, &to_feed, |
| 1485 | cas->buf, &remaining)) |
| 1486 | return -1; |
| 1487 | cas->end = sizeof(cas->buf) - remaining; |
| 1488 | cas->ptr = 0; |
| 1489 | if (input) { |
| 1490 | size_t fed = *isize_p - to_feed; |
| 1491 | *isize_p -= fed; |
| 1492 | input += fed; |
| 1493 | } |
| 1494 | |
| 1495 | /* do we know that we drained one completely? */ |
| 1496 | if (input || cas->end) |
| 1497 | continue; |
| 1498 | |
| 1499 | /* tell two to drain; we have nothing more to give it */ |
| 1500 | to_feed = 0; |
| 1501 | remaining = sz - filled; |
| 1502 | if (stream_filter(cas->two, |
| 1503 | NULL, &to_feed, |
| 1504 | output + filled, &remaining)) |
| 1505 | return -1; |
| 1506 | if (remaining == (sz - filled)) |
| 1507 | break; /* completely drained two */ |
| 1508 | filled = sz - remaining; |
| 1509 | } |
| 1510 | *osize_p -= filled; |
| 1511 | return 0; |
| 1512 | } |
| 1513 | |
| 1514 | static void cascade_free_fn(struct stream_filter *filter) |
| 1515 | { |
| 1516 | struct cascade_filter *cas = (struct cascade_filter *)filter; |
| 1517 | free_stream_filter(cas->one); |
| 1518 | free_stream_filter(cas->two); |
| 1519 | free(filter); |
| 1520 | } |
| 1521 | |
| 1522 | static struct stream_filter_vtbl cascade_vtbl = { |
| 1523 | cascade_filter_fn, |
| 1524 | cascade_free_fn, |
| 1525 | }; |
| 1526 | |
| 1527 | static struct stream_filter *cascade_filter(struct stream_filter *one, |
| 1528 | struct stream_filter *two) |
| 1529 | { |
| 1530 | struct cascade_filter *cascade; |
| 1531 | |
| 1532 | if (!one || is_null_stream_filter(one)) |
| 1533 | return two; |
| 1534 | if (!two || is_null_stream_filter(two)) |
| 1535 | return one; |
| 1536 | |
| 1537 | cascade = xmalloc(sizeof(*cascade)); |
| 1538 | cascade->one = one; |
| 1539 | cascade->two = two; |
| 1540 | cascade->end = cascade->ptr = 0; |
| 1541 | cascade->filter.vtbl = &cascade_vtbl; |
| 1542 | return (struct stream_filter *)cascade; |
| 1543 | } |
| 1544 | |
| 1545 | /* |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1546 | * ident filter |
| 1547 | */ |
| 1548 | #define IDENT_DRAINING (-1) |
| 1549 | #define IDENT_SKIPPING (-2) |
| 1550 | struct ident_filter { |
| 1551 | struct stream_filter filter; |
| 1552 | struct strbuf left; |
| 1553 | int state; |
| 1554 | char ident[45]; /* ": x40 $" */ |
| 1555 | }; |
| 1556 | |
| 1557 | static int is_foreign_ident(const char *str) |
| 1558 | { |
| 1559 | int i; |
| 1560 | |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 1561 | if (!skip_prefix(str, "$Id: ", &str)) |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1562 | return 0; |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 1563 | for (i = 0; str[i]; i++) { |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1564 | if (isspace(str[i]) && str[i+1] != '$') |
| 1565 | return 1; |
| 1566 | } |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
| 1570 | static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p) |
| 1571 | { |
| 1572 | size_t to_drain = ident->left.len; |
| 1573 | |
| 1574 | if (*osize_p < to_drain) |
| 1575 | to_drain = *osize_p; |
| 1576 | if (to_drain) { |
| 1577 | memcpy(*output_p, ident->left.buf, to_drain); |
| 1578 | strbuf_remove(&ident->left, 0, to_drain); |
| 1579 | *output_p += to_drain; |
| 1580 | *osize_p -= to_drain; |
| 1581 | } |
| 1582 | if (!ident->left.len) |
| 1583 | ident->state = 0; |
| 1584 | } |
| 1585 | |
| 1586 | static int ident_filter_fn(struct stream_filter *filter, |
| 1587 | const char *input, size_t *isize_p, |
| 1588 | char *output, size_t *osize_p) |
| 1589 | { |
| 1590 | struct ident_filter *ident = (struct ident_filter *)filter; |
| 1591 | static const char head[] = "$Id"; |
| 1592 | |
| 1593 | if (!input) { |
| 1594 | /* drain upon eof */ |
| 1595 | switch (ident->state) { |
| 1596 | default: |
| 1597 | strbuf_add(&ident->left, head, ident->state); |
| 1598 | case IDENT_SKIPPING: |
| 1599 | /* fallthru */ |
| 1600 | case IDENT_DRAINING: |
| 1601 | ident_drain(ident, &output, osize_p); |
| 1602 | } |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
| 1606 | while (*isize_p || (ident->state == IDENT_DRAINING)) { |
| 1607 | int ch; |
| 1608 | |
| 1609 | if (ident->state == IDENT_DRAINING) { |
| 1610 | ident_drain(ident, &output, osize_p); |
| 1611 | if (!*osize_p) |
| 1612 | break; |
| 1613 | continue; |
| 1614 | } |
| 1615 | |
| 1616 | ch = *(input++); |
| 1617 | (*isize_p)--; |
| 1618 | |
| 1619 | if (ident->state == IDENT_SKIPPING) { |
| 1620 | /* |
| 1621 | * Skipping until '$' or LF, but keeping them |
| 1622 | * in case it is a foreign ident. |
| 1623 | */ |
| 1624 | strbuf_addch(&ident->left, ch); |
| 1625 | if (ch != '\n' && ch != '$') |
| 1626 | continue; |
| 1627 | if (ch == '$' && !is_foreign_ident(ident->left.buf)) { |
| 1628 | strbuf_setlen(&ident->left, sizeof(head) - 1); |
| 1629 | strbuf_addstr(&ident->left, ident->ident); |
| 1630 | } |
| 1631 | ident->state = IDENT_DRAINING; |
| 1632 | continue; |
| 1633 | } |
| 1634 | |
| 1635 | if (ident->state < sizeof(head) && |
| 1636 | head[ident->state] == ch) { |
| 1637 | ident->state++; |
| 1638 | continue; |
| 1639 | } |
| 1640 | |
| 1641 | if (ident->state) |
| 1642 | strbuf_add(&ident->left, head, ident->state); |
| 1643 | if (ident->state == sizeof(head) - 1) { |
| 1644 | if (ch != ':' && ch != '$') { |
| 1645 | strbuf_addch(&ident->left, ch); |
| 1646 | ident->state = 0; |
| 1647 | continue; |
| 1648 | } |
| 1649 | |
| 1650 | if (ch == ':') { |
| 1651 | strbuf_addch(&ident->left, ch); |
| 1652 | ident->state = IDENT_SKIPPING; |
| 1653 | } else { |
| 1654 | strbuf_addstr(&ident->left, ident->ident); |
| 1655 | ident->state = IDENT_DRAINING; |
| 1656 | } |
| 1657 | continue; |
| 1658 | } |
| 1659 | |
| 1660 | strbuf_addch(&ident->left, ch); |
| 1661 | ident->state = IDENT_DRAINING; |
| 1662 | } |
| 1663 | return 0; |
| 1664 | } |
| 1665 | |
| 1666 | static void ident_free_fn(struct stream_filter *filter) |
| 1667 | { |
| 1668 | struct ident_filter *ident = (struct ident_filter *)filter; |
| 1669 | strbuf_release(&ident->left); |
| 1670 | free(filter); |
| 1671 | } |
| 1672 | |
| 1673 | static struct stream_filter_vtbl ident_vtbl = { |
| 1674 | ident_filter_fn, |
| 1675 | ident_free_fn, |
| 1676 | }; |
| 1677 | |
| 1678 | static struct stream_filter *ident_filter(const unsigned char *sha1) |
| 1679 | { |
| 1680 | struct ident_filter *ident = xmalloc(sizeof(*ident)); |
| 1681 | |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 1682 | xsnprintf(ident->ident, sizeof(ident->ident), |
| 1683 | ": %s $", sha1_to_hex(sha1)); |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1684 | strbuf_init(&ident->left, 0); |
| 1685 | ident->filter.vtbl = &ident_vtbl; |
| 1686 | ident->state = 0; |
| 1687 | return (struct stream_filter *)ident; |
| 1688 | } |
| 1689 | |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 1690 | /* |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 1691 | * Return an appropriately constructed filter for the path, or NULL if |
| 1692 | * the contents cannot be filtered without reading the whole thing |
| 1693 | * in-core. |
| 1694 | * |
| 1695 | * Note that you would be crazy to set CRLF, smuge/clean or ident to a |
| 1696 | * large binary blob you would want us not to slurp into the memory! |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 1697 | */ |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 1698 | struct stream_filter *get_stream_filter(const char *path, const unsigned char *sha1) |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 1699 | { |
| 1700 | struct conv_attrs ca; |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1701 | struct stream_filter *filter = NULL; |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 1702 | |
| 1703 | convert_attrs(&ca, path); |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 1704 | if (ca.drv && (ca.drv->process || ca.drv->smudge || ca.drv->clean)) |
Torsten Bögershausen | caa47ad | 2016-04-25 18:56:32 +0200 | [diff] [blame] | 1705 | return NULL; |
| 1706 | |
| 1707 | if (ca.crlf_action == CRLF_AUTO || ca.crlf_action == CRLF_AUTO_CRLF) |
| 1708 | return NULL; |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1709 | |
| 1710 | if (ca.ident) |
| 1711 | filter = ident_filter(sha1); |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 1712 | |
Torsten Bögershausen | caa47ad | 2016-04-25 18:56:32 +0200 | [diff] [blame] | 1713 | if (output_eol(ca.crlf_action) == EOL_CRLF) |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1714 | filter = cascade_filter(filter, lf_to_crlf_filter()); |
Torsten Bögershausen | caa47ad | 2016-04-25 18:56:32 +0200 | [diff] [blame] | 1715 | else |
| 1716 | filter = cascade_filter(filter, &null_filter_singleton); |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1717 | |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1718 | return filter; |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | void free_stream_filter(struct stream_filter *filter) |
| 1722 | { |
| 1723 | filter->vtbl->free(filter); |
| 1724 | } |
| 1725 | |
| 1726 | int stream_filter(struct stream_filter *filter, |
| 1727 | const char *input, size_t *isize_p, |
| 1728 | char *output, size_t *osize_p) |
| 1729 | { |
| 1730 | return filter->vtbl->filter(filter, input, isize_p, output, osize_p); |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 1731 | } |