Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 1 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 3 | #include "object-store.h" |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 4 | #include "attr.h" |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 5 | #include "run-command.h" |
Pete Wyckoff | a2b665d | 2010-12-22 06:40:13 -0800 | [diff] [blame] | 6 | #include "quote.h" |
Jehan Bing | 6424c2a | 2012-02-20 12:53:37 -0800 | [diff] [blame] | 7 | #include "sigchain.h" |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 8 | #include "pkt-line.h" |
Ben Peart | 99605d6 | 2017-05-05 11:28:01 -0400 | [diff] [blame] | 9 | #include "sub-process.h" |
Lars Schneider | 107642f | 2018-04-15 20:16:07 +0200 | [diff] [blame] | 10 | #include "utf8.h" |
brian m. carlson | 2c65d90 | 2019-09-02 22:39:44 +0000 | [diff] [blame] | 11 | #include "ll-merge.h" |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 12 | |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 13 | /* |
| 14 | * convert.c - convert a file when checking it out and checking it in. |
| 15 | * |
| 16 | * This should use the pathname to decide on whether it wants to do some |
| 17 | * more interesting conversions (automatic gzip/unzip, general format |
| 18 | * conversions etc etc), but by default it just does automatic CRLF<->LF |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 19 | * translation when the "text" attribute or "auto_crlf" option is set. |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 22 | /* Stat bits: When BIN is set, the txt bits are unset */ |
| 23 | #define CONVERT_STAT_BITS_TXT_LF 0x1 |
| 24 | #define CONVERT_STAT_BITS_TXT_CRLF 0x2 |
| 25 | #define CONVERT_STAT_BITS_BIN 0x4 |
| 26 | |
Junio C Hamano | c61dcff | 2011-05-09 13:12:57 -0700 | [diff] [blame] | 27 | enum crlf_action { |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 28 | CRLF_UNDEFINED, |
| 29 | CRLF_BINARY, |
Eyvind Bernhardsen | fd6cce9 | 2010-05-19 22:43:10 +0200 | [diff] [blame] | 30 | CRLF_TEXT, |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 31 | CRLF_TEXT_INPUT, |
| 32 | CRLF_TEXT_CRLF, |
| 33 | CRLF_AUTO, |
| 34 | CRLF_AUTO_INPUT, |
| 35 | CRLF_AUTO_CRLF |
Eyvind Bernhardsen | fd6cce9 | 2010-05-19 22:43:10 +0200 | [diff] [blame] | 36 | }; |
Junio C Hamano | 163b959 | 2007-04-19 22:37:19 -0700 | [diff] [blame] | 37 | |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 38 | struct text_stat { |
Dmitry Potapov | 2862419 | 2008-01-16 04:59:12 +0300 | [diff] [blame] | 39 | /* NUL, CR, LF and CRLF counts */ |
Torsten Bögershausen | 6e336a5 | 2016-02-10 17:24:43 +0100 | [diff] [blame] | 40 | unsigned nul, lonecr, lonelf, crlf; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 41 | |
| 42 | /* These are just approximations! */ |
| 43 | unsigned printable, nonprintable; |
| 44 | }; |
| 45 | |
| 46 | static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats) |
| 47 | { |
| 48 | unsigned long i; |
| 49 | |
| 50 | memset(stats, 0, sizeof(*stats)); |
| 51 | |
| 52 | for (i = 0; i < size; i++) { |
| 53 | unsigned char c = buf[i]; |
| 54 | if (c == '\r') { |
Torsten Bögershausen | 6e336a5 | 2016-02-10 17:24:43 +0100 | [diff] [blame] | 55 | if (i+1 < size && buf[i+1] == '\n') { |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 56 | stats->crlf++; |
Torsten Bögershausen | 6e336a5 | 2016-02-10 17:24:43 +0100 | [diff] [blame] | 57 | i++; |
| 58 | } else |
| 59 | stats->lonecr++; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 60 | continue; |
| 61 | } |
| 62 | if (c == '\n') { |
Torsten Bögershausen | 6e336a5 | 2016-02-10 17:24:43 +0100 | [diff] [blame] | 63 | stats->lonelf++; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 64 | continue; |
| 65 | } |
| 66 | if (c == 127) |
| 67 | /* DEL */ |
| 68 | stats->nonprintable++; |
| 69 | else if (c < 32) { |
| 70 | switch (c) { |
| 71 | /* BS, HT, ESC and FF */ |
| 72 | case '\b': case '\t': case '\033': case '\014': |
| 73 | stats->printable++; |
| 74 | break; |
Dmitry Potapov | 2862419 | 2008-01-16 04:59:12 +0300 | [diff] [blame] | 75 | case 0: |
| 76 | stats->nul++; |
| 77 | /* fall through */ |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 78 | default: |
| 79 | stats->nonprintable++; |
| 80 | } |
| 81 | } |
| 82 | else |
| 83 | stats->printable++; |
| 84 | } |
Dmitry Kakurin | f9dd4bf | 2008-07-11 18:48:16 +0200 | [diff] [blame] | 85 | |
| 86 | /* If file ends with EOF then don't count this EOF as non-printable. */ |
| 87 | if (size >= 1 && buf[size-1] == '\032') |
| 88 | stats->nonprintable--; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
| 92 | * The same heuristics as diff.c::mmfile_is_binary() |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 93 | * We treat files with bare CR as binary |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 94 | */ |
Jeff King | 129beee | 2019-01-24 08:12:41 -0500 | [diff] [blame] | 95 | static int convert_is_binary(const struct text_stat *stats) |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 96 | { |
Torsten Bögershausen | 6e336a5 | 2016-02-10 17:24:43 +0100 | [diff] [blame] | 97 | if (stats->lonecr) |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 98 | return 1; |
Dmitry Potapov | 2862419 | 2008-01-16 04:59:12 +0300 | [diff] [blame] | 99 | if (stats->nul) |
| 100 | return 1; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 101 | if ((stats->printable >> 7) < stats->nonprintable) |
| 102 | return 1; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 106 | static unsigned int gather_convert_stats(const char *data, unsigned long size) |
| 107 | { |
| 108 | struct text_stat stats; |
Torsten Bögershausen | 6e336a5 | 2016-02-10 17:24:43 +0100 | [diff] [blame] | 109 | int ret = 0; |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 110 | if (!data || !size) |
| 111 | return 0; |
| 112 | gather_stats(data, size, &stats); |
Jeff King | 129beee | 2019-01-24 08:12:41 -0500 | [diff] [blame] | 113 | if (convert_is_binary(&stats)) |
Torsten Bögershausen | 6e336a5 | 2016-02-10 17:24:43 +0100 | [diff] [blame] | 114 | ret |= CONVERT_STAT_BITS_BIN; |
| 115 | if (stats.crlf) |
| 116 | ret |= CONVERT_STAT_BITS_TXT_CRLF; |
| 117 | if (stats.lonelf) |
| 118 | ret |= CONVERT_STAT_BITS_TXT_LF; |
| 119 | |
| 120 | return ret; |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static const char *gather_convert_stats_ascii(const char *data, unsigned long size) |
| 124 | { |
| 125 | unsigned int convert_stats = gather_convert_stats(data, size); |
| 126 | |
| 127 | if (convert_stats & CONVERT_STAT_BITS_BIN) |
| 128 | return "-text"; |
| 129 | switch (convert_stats) { |
| 130 | case CONVERT_STAT_BITS_TXT_LF: |
| 131 | return "lf"; |
| 132 | case CONVERT_STAT_BITS_TXT_CRLF: |
| 133 | return "crlf"; |
| 134 | case CONVERT_STAT_BITS_TXT_LF | CONVERT_STAT_BITS_TXT_CRLF: |
| 135 | return "mixed"; |
| 136 | default: |
| 137 | return "none"; |
| 138 | } |
| 139 | } |
| 140 | |
Brandon Williams | a7609c5 | 2017-06-12 15:13:52 -0700 | [diff] [blame] | 141 | const char *get_cached_convert_stats_ascii(const struct index_state *istate, |
| 142 | const char *path) |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 143 | { |
| 144 | const char *ret; |
| 145 | unsigned long sz; |
Brandon Williams | a7609c5 | 2017-06-12 15:13:52 -0700 | [diff] [blame] | 146 | void *data = read_blob_data_from_index(istate, path, &sz); |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 147 | ret = gather_convert_stats_ascii(data, sz); |
| 148 | free(data); |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | const char *get_wt_convert_stats_ascii(const char *path) |
| 153 | { |
| 154 | const char *ret = ""; |
| 155 | struct strbuf sb = STRBUF_INIT; |
| 156 | if (strbuf_read_file(&sb, path, 0) >= 0) |
| 157 | ret = gather_convert_stats_ascii(sb.buf, sb.len); |
| 158 | strbuf_release(&sb); |
| 159 | return ret; |
| 160 | } |
| 161 | |
Torsten Bögershausen | 4b4024f | 2016-02-05 17:13:25 +0100 | [diff] [blame] | 162 | static int text_eol_is_crlf(void) |
| 163 | { |
| 164 | if (auto_crlf == AUTO_CRLF_TRUE) |
| 165 | return 1; |
| 166 | else if (auto_crlf == AUTO_CRLF_INPUT) |
| 167 | return 0; |
| 168 | if (core_eol == EOL_CRLF) |
| 169 | return 1; |
| 170 | if (core_eol == EOL_UNSET && EOL_NATIVE == EOL_CRLF) |
| 171 | return 1; |
| 172 | return 0; |
| 173 | } |
| 174 | |
Junio C Hamano | c61dcff | 2011-05-09 13:12:57 -0700 | [diff] [blame] | 175 | static enum eol output_eol(enum crlf_action crlf_action) |
Eyvind Bernhardsen | f217f0e | 2010-07-02 21:20:47 +0200 | [diff] [blame] | 176 | { |
Junio C Hamano | c61dcff | 2011-05-09 13:12:57 -0700 | [diff] [blame] | 177 | switch (crlf_action) { |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 178 | case CRLF_BINARY: |
| 179 | return EOL_UNSET; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 180 | case CRLF_TEXT_CRLF: |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 181 | return EOL_CRLF; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 182 | case CRLF_TEXT_INPUT: |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 183 | return EOL_LF; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 184 | case CRLF_UNDEFINED: |
| 185 | case CRLF_AUTO_CRLF: |
Torsten Bögershausen | 6523728 | 2016-06-28 10:01:13 +0200 | [diff] [blame] | 186 | return EOL_CRLF; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 187 | case CRLF_AUTO_INPUT: |
Torsten Bögershausen | 6523728 | 2016-06-28 10:01:13 +0200 | [diff] [blame] | 188 | return EOL_LF; |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 189 | case CRLF_TEXT: |
| 190 | case CRLF_AUTO: |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 191 | /* fall through */ |
Torsten Bögershausen | 4b4024f | 2016-02-05 17:13:25 +0100 | [diff] [blame] | 192 | return text_eol_is_crlf() ? EOL_CRLF : EOL_LF; |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 193 | } |
Nguyễn Thái Ngọc Duy | d26a328 | 2018-07-21 09:49:29 +0200 | [diff] [blame] | 194 | warning(_("illegal crlf_action %d"), (int)crlf_action); |
Junio C Hamano | ec70f52 | 2011-05-09 12:52:12 -0700 | [diff] [blame] | 195 | return core_eol; |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 198 | static void check_global_conv_flags_eol(const char *path, enum crlf_action crlf_action, |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 199 | struct text_stat *old_stats, struct text_stat *new_stats, |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 200 | int conv_flags) |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 201 | { |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 202 | if (old_stats->crlf && !new_stats->crlf ) { |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 203 | /* |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 204 | * CRLFs would not be restored by checkout |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 205 | */ |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 206 | if (conv_flags & CONV_EOL_RNDTRP_DIE) |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 207 | die(_("CRLF would be replaced by LF in %s"), path); |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 208 | else if (conv_flags & CONV_EOL_RNDTRP_WARN) |
Vasco Almeida | 87cb784 | 2016-10-17 13:15:27 +0000 | [diff] [blame] | 209 | warning(_("CRLF will be replaced by LF in %s.\n" |
| 210 | "The file will have its original line" |
Nguyễn Thái Ngọc Duy | d26a328 | 2018-07-21 09:49:29 +0200 | [diff] [blame] | 211 | " endings in your working directory"), path); |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 212 | } else if (old_stats->lonelf && !new_stats->lonelf ) { |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 213 | /* |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 214 | * CRLFs would be added by checkout |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 215 | */ |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 216 | if (conv_flags & CONV_EOL_RNDTRP_DIE) |
| 217 | die(_("LF would be replaced by CRLF in %s"), path); |
| 218 | else if (conv_flags & CONV_EOL_RNDTRP_WARN) |
Vasco Almeida | 87cb784 | 2016-10-17 13:15:27 +0000 | [diff] [blame] | 219 | warning(_("LF will be replaced by CRLF in %s.\n" |
| 220 | "The file will have its original line" |
Nguyễn Thái Ngọc Duy | d26a328 | 2018-07-21 09:49:29 +0200 | [diff] [blame] | 221 | " endings in your working directory"), path); |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Torsten Bögershausen | 86ff70a | 2017-11-26 13:20:52 +0100 | [diff] [blame] | 225 | static int has_crlf_in_index(const struct index_state *istate, const char *path) |
Finn Arne Gangstad | c480539 | 2010-05-12 00:37:57 +0200 | [diff] [blame] | 226 | { |
Finn Arne Gangstad | c480539 | 2010-05-12 00:37:57 +0200 | [diff] [blame] | 227 | unsigned long sz; |
Finn Arne Gangstad | c480539 | 2010-05-12 00:37:57 +0200 | [diff] [blame] | 228 | void *data; |
Torsten Bögershausen | 86ff70a | 2017-11-26 13:20:52 +0100 | [diff] [blame] | 229 | const char *crp; |
| 230 | int has_crlf = 0; |
Finn Arne Gangstad | c480539 | 2010-05-12 00:37:57 +0200 | [diff] [blame] | 231 | |
Brandon Williams | 49a6d31 | 2017-06-12 15:13:53 -0700 | [diff] [blame] | 232 | data = read_blob_data_from_index(istate, path, &sz); |
Lukas Fleischer | 4982fd7 | 2013-04-13 15:28:32 +0200 | [diff] [blame] | 233 | if (!data) |
Finn Arne Gangstad | c480539 | 2010-05-12 00:37:57 +0200 | [diff] [blame] | 234 | return 0; |
Torsten Bögershausen | 86ff70a | 2017-11-26 13:20:52 +0100 | [diff] [blame] | 235 | |
| 236 | crp = memchr(data, '\r', sz); |
| 237 | if (crp) { |
| 238 | unsigned int ret_stats; |
| 239 | ret_stats = gather_convert_stats(data, sz); |
| 240 | if (!(ret_stats & CONVERT_STAT_BITS_BIN) && |
| 241 | (ret_stats & CONVERT_STAT_BITS_TXT_CRLF)) |
| 242 | has_crlf = 1; |
| 243 | } |
Finn Arne Gangstad | c480539 | 2010-05-12 00:37:57 +0200 | [diff] [blame] | 244 | free(data); |
Torsten Bögershausen | 86ff70a | 2017-11-26 13:20:52 +0100 | [diff] [blame] | 245 | return has_crlf; |
Finn Arne Gangstad | c480539 | 2010-05-12 00:37:57 +0200 | [diff] [blame] | 246 | } |
| 247 | |
Jeff King | 129beee | 2019-01-24 08:12:41 -0500 | [diff] [blame] | 248 | static int will_convert_lf_to_crlf(struct text_stat *stats, |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 249 | enum crlf_action crlf_action) |
| 250 | { |
| 251 | if (output_eol(crlf_action) != EOL_CRLF) |
| 252 | return 0; |
| 253 | /* No "naked" LF? Nothing to convert, regardless. */ |
| 254 | if (!stats->lonelf) |
| 255 | return 0; |
| 256 | |
| 257 | if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) { |
| 258 | /* If we have any CR or CRLF line endings, we do not touch it */ |
| 259 | /* This is the new safer autocrlf-handling */ |
| 260 | if (stats->lonecr || stats->crlf) |
| 261 | return 0; |
| 262 | |
Jeff King | 129beee | 2019-01-24 08:12:41 -0500 | [diff] [blame] | 263 | if (convert_is_binary(stats)) |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 264 | return 0; |
| 265 | } |
| 266 | return 1; |
| 267 | |
| 268 | } |
| 269 | |
Lars Schneider | 7a17918 | 2018-04-15 20:16:08 +0200 | [diff] [blame] | 270 | static int validate_encoding(const char *path, const char *enc, |
| 271 | const char *data, size_t len, int die_on_error) |
| 272 | { |
René Scharfe | ed28358 | 2019-11-08 21:27:34 +0100 | [diff] [blame] | 273 | const char *stripped; |
| 274 | |
Lars Schneider | 7a17918 | 2018-04-15 20:16:08 +0200 | [diff] [blame] | 275 | /* We only check for UTF here as UTF?? can be an alias for UTF-?? */ |
René Scharfe | ed28358 | 2019-11-08 21:27:34 +0100 | [diff] [blame] | 276 | if (skip_iprefix(enc, "UTF", &stripped)) { |
| 277 | skip_prefix(stripped, "-", &stripped); |
| 278 | |
Lars Schneider | 7a17918 | 2018-04-15 20:16:08 +0200 | [diff] [blame] | 279 | /* |
| 280 | * Check for detectable errors in UTF encodings |
| 281 | */ |
| 282 | if (has_prohibited_utf_bom(enc, data, len)) { |
| 283 | const char *error_msg = _( |
| 284 | "BOM is prohibited in '%s' if encoded as %s"); |
| 285 | /* |
| 286 | * This advice is shown for UTF-??BE and UTF-??LE encodings. |
| 287 | * We cut off the last two characters of the encoding name |
| 288 | * to generate the encoding name suitable for BOMs. |
| 289 | */ |
| 290 | const char *advise_msg = _( |
| 291 | "The file '%s' contains a byte order " |
René Scharfe | ed28358 | 2019-11-08 21:27:34 +0100 | [diff] [blame] | 292 | "mark (BOM). Please use UTF-%.*s as " |
Lars Schneider | 7a17918 | 2018-04-15 20:16:08 +0200 | [diff] [blame] | 293 | "working-tree-encoding."); |
René Scharfe | ed28358 | 2019-11-08 21:27:34 +0100 | [diff] [blame] | 294 | int stripped_len = strlen(stripped) - strlen("BE"); |
| 295 | advise(advise_msg, path, stripped_len, stripped); |
Lars Schneider | 7a17918 | 2018-04-15 20:16:08 +0200 | [diff] [blame] | 296 | if (die_on_error) |
| 297 | die(error_msg, path, enc); |
| 298 | else { |
| 299 | return error(error_msg, path, enc); |
| 300 | } |
| 301 | |
| 302 | } else if (is_missing_required_utf_bom(enc, data, len)) { |
| 303 | const char *error_msg = _( |
| 304 | "BOM is required in '%s' if encoded as %s"); |
| 305 | const char *advise_msg = _( |
| 306 | "The file '%s' is missing a byte order " |
| 307 | "mark (BOM). Please use UTF-%sBE or UTF-%sLE " |
| 308 | "(depending on the byte order) as " |
| 309 | "working-tree-encoding."); |
Lars Schneider | 7a17918 | 2018-04-15 20:16:08 +0200 | [diff] [blame] | 310 | advise(advise_msg, path, stripped, stripped); |
Lars Schneider | 7a17918 | 2018-04-15 20:16:08 +0200 | [diff] [blame] | 311 | if (die_on_error) |
| 312 | die(error_msg, path, enc); |
| 313 | else { |
| 314 | return error(error_msg, path, enc); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | } |
| 319 | return 0; |
| 320 | } |
| 321 | |
Lars Schneider | 541d059 | 2018-04-15 20:16:09 +0200 | [diff] [blame] | 322 | static void trace_encoding(const char *context, const char *path, |
| 323 | const char *encoding, const char *buf, size_t len) |
| 324 | { |
| 325 | static struct trace_key coe = TRACE_KEY_INIT(WORKING_TREE_ENCODING); |
| 326 | struct strbuf trace = STRBUF_INIT; |
| 327 | int i; |
| 328 | |
| 329 | strbuf_addf(&trace, "%s (%s, considered %s):\n", context, path, encoding); |
| 330 | for (i = 0; i < len && buf; ++i) { |
| 331 | strbuf_addf( |
Beat Bolli | 8302f50 | 2018-07-09 21:25:34 +0200 | [diff] [blame] | 332 | &trace, "| \033[2m%2i:\033[0m %2x \033[2m%c\033[0m%c", |
Lars Schneider | 541d059 | 2018-04-15 20:16:09 +0200 | [diff] [blame] | 333 | i, |
| 334 | (unsigned char) buf[i], |
| 335 | (buf[i] > 32 && buf[i] < 127 ? buf[i] : ' '), |
| 336 | ((i+1) % 8 && (i+1) < len ? ' ' : '\n') |
| 337 | ); |
| 338 | } |
| 339 | strbuf_addchars(&trace, '\n', 1); |
| 340 | |
| 341 | trace_strbuf(&coe, &trace); |
| 342 | strbuf_release(&trace); |
| 343 | } |
| 344 | |
Lars Schneider | e92d622 | 2018-04-15 20:16:10 +0200 | [diff] [blame] | 345 | static int check_roundtrip(const char *enc_name) |
| 346 | { |
| 347 | /* |
| 348 | * check_roundtrip_encoding contains a string of comma and/or |
| 349 | * space separated encodings (eg. "UTF-16, ASCII, CP1125"). |
| 350 | * Search for the given encoding in that string. |
| 351 | */ |
| 352 | const char *found = strcasestr(check_roundtrip_encoding, enc_name); |
| 353 | const char *next; |
| 354 | int len; |
| 355 | if (!found) |
| 356 | return 0; |
| 357 | next = found + strlen(enc_name); |
| 358 | len = strlen(check_roundtrip_encoding); |
| 359 | return (found && ( |
| 360 | /* |
| 361 | * check that the found encoding is at the |
| 362 | * beginning of check_roundtrip_encoding or |
| 363 | * that it is prefixed with a space or comma |
| 364 | */ |
| 365 | found == check_roundtrip_encoding || ( |
| 366 | (isspace(found[-1]) || found[-1] == ',') |
| 367 | ) |
| 368 | ) && ( |
| 369 | /* |
| 370 | * check that the found encoding is at the |
| 371 | * end of check_roundtrip_encoding or |
| 372 | * that it is suffixed with a space or comma |
| 373 | */ |
| 374 | next == check_roundtrip_encoding + len || ( |
| 375 | next < check_roundtrip_encoding + len && |
| 376 | (isspace(next[0]) || next[0] == ',') |
| 377 | ) |
| 378 | )); |
| 379 | } |
| 380 | |
Lars Schneider | 107642f | 2018-04-15 20:16:07 +0200 | [diff] [blame] | 381 | static const char *default_encoding = "UTF-8"; |
| 382 | |
| 383 | static int encode_to_git(const char *path, const char *src, size_t src_len, |
| 384 | struct strbuf *buf, const char *enc, int conv_flags) |
| 385 | { |
| 386 | char *dst; |
Jeff King | c7d017d | 2018-07-24 06:50:33 -0400 | [diff] [blame] | 387 | size_t dst_len; |
Lars Schneider | 107642f | 2018-04-15 20:16:07 +0200 | [diff] [blame] | 388 | int die_on_error = conv_flags & CONV_WRITE_OBJECT; |
| 389 | |
| 390 | /* |
| 391 | * No encoding is specified or there is nothing to encode. |
| 392 | * Tell the caller that the content was not modified. |
| 393 | */ |
| 394 | if (!enc || (src && !src_len)) |
| 395 | return 0; |
| 396 | |
| 397 | /* |
| 398 | * Looks like we got called from "would_convert_to_git()". |
| 399 | * This means Git wants to know if it would encode (= modify!) |
| 400 | * the content. Let's answer with "yes", since an encoding was |
| 401 | * specified. |
| 402 | */ |
| 403 | if (!buf && !src) |
| 404 | return 1; |
| 405 | |
Lars Schneider | 7a17918 | 2018-04-15 20:16:08 +0200 | [diff] [blame] | 406 | if (validate_encoding(path, enc, src, src_len, die_on_error)) |
| 407 | return 0; |
| 408 | |
Lars Schneider | 541d059 | 2018-04-15 20:16:09 +0200 | [diff] [blame] | 409 | trace_encoding("source", path, enc, src, src_len); |
Lars Schneider | 107642f | 2018-04-15 20:16:07 +0200 | [diff] [blame] | 410 | dst = reencode_string_len(src, src_len, default_encoding, enc, |
| 411 | &dst_len); |
| 412 | if (!dst) { |
| 413 | /* |
| 414 | * We could add the blob "as-is" to Git. However, on checkout |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 415 | * we would try to re-encode to the original encoding. This |
Lars Schneider | 107642f | 2018-04-15 20:16:07 +0200 | [diff] [blame] | 416 | * would fail and we would leave the user with a messed-up |
| 417 | * working tree. Let's try to avoid this by screaming loud. |
| 418 | */ |
| 419 | const char* msg = _("failed to encode '%s' from %s to %s"); |
| 420 | if (die_on_error) |
| 421 | die(msg, path, enc, default_encoding); |
| 422 | else { |
| 423 | error(msg, path, enc, default_encoding); |
| 424 | return 0; |
| 425 | } |
| 426 | } |
Lars Schneider | 541d059 | 2018-04-15 20:16:09 +0200 | [diff] [blame] | 427 | trace_encoding("destination", path, default_encoding, dst, dst_len); |
Lars Schneider | 107642f | 2018-04-15 20:16:07 +0200 | [diff] [blame] | 428 | |
Lars Schneider | e92d622 | 2018-04-15 20:16:10 +0200 | [diff] [blame] | 429 | /* |
| 430 | * UTF supports lossless conversion round tripping [1] and conversions |
| 431 | * between UTF and other encodings are mostly round trip safe as |
| 432 | * Unicode aims to be a superset of all other character encodings. |
| 433 | * However, certain encodings (e.g. SHIFT-JIS) are known to have round |
| 434 | * trip issues [2]. Check the round trip conversion for all encodings |
| 435 | * listed in core.checkRoundtripEncoding. |
| 436 | * |
| 437 | * The round trip check is only performed if content is written to Git. |
| 438 | * This ensures that no information is lost during conversion to/from |
| 439 | * the internal UTF-8 representation. |
| 440 | * |
| 441 | * Please note, the code below is not tested because I was not able to |
| 442 | * generate a faulty round trip without an iconv error. Iconv errors |
| 443 | * are already caught above. |
| 444 | * |
| 445 | * [1] http://unicode.org/faq/utf_bom.html#gen2 |
| 446 | * [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode |
| 447 | */ |
| 448 | if (die_on_error && check_roundtrip(enc)) { |
| 449 | char *re_src; |
Jeff King | c7d017d | 2018-07-24 06:50:33 -0400 | [diff] [blame] | 450 | size_t re_src_len; |
Lars Schneider | e92d622 | 2018-04-15 20:16:10 +0200 | [diff] [blame] | 451 | |
| 452 | re_src = reencode_string_len(dst, dst_len, |
| 453 | enc, default_encoding, |
| 454 | &re_src_len); |
| 455 | |
| 456 | trace_printf("Checking roundtrip encoding for %s...\n", enc); |
| 457 | trace_encoding("reencoded source", path, enc, |
| 458 | re_src, re_src_len); |
| 459 | |
| 460 | if (!re_src || src_len != re_src_len || |
| 461 | memcmp(src, re_src, src_len)) { |
| 462 | const char* msg = _("encoding '%s' from %s to %s and " |
| 463 | "back is not the same"); |
| 464 | die(msg, path, enc, default_encoding); |
| 465 | } |
| 466 | |
| 467 | free(re_src); |
| 468 | } |
| 469 | |
Lars Schneider | 107642f | 2018-04-15 20:16:07 +0200 | [diff] [blame] | 470 | strbuf_attach(buf, dst, dst_len, dst_len + 1); |
| 471 | return 1; |
| 472 | } |
| 473 | |
| 474 | static int encode_to_worktree(const char *path, const char *src, size_t src_len, |
| 475 | struct strbuf *buf, const char *enc) |
| 476 | { |
| 477 | char *dst; |
Jeff King | c7d017d | 2018-07-24 06:50:33 -0400 | [diff] [blame] | 478 | size_t dst_len; |
Lars Schneider | 107642f | 2018-04-15 20:16:07 +0200 | [diff] [blame] | 479 | |
| 480 | /* |
| 481 | * No encoding is specified or there is nothing to encode. |
| 482 | * Tell the caller that the content was not modified. |
| 483 | */ |
| 484 | if (!enc || (src && !src_len)) |
| 485 | return 0; |
| 486 | |
| 487 | dst = reencode_string_len(src, src_len, enc, default_encoding, |
| 488 | &dst_len); |
| 489 | if (!dst) { |
Nguyễn Thái Ngọc Duy | d26a328 | 2018-07-21 09:49:29 +0200 | [diff] [blame] | 490 | error(_("failed to encode '%s' from %s to %s"), |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 491 | path, default_encoding, enc); |
Lars Schneider | 107642f | 2018-04-15 20:16:07 +0200 | [diff] [blame] | 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | strbuf_attach(buf, dst, dst_len, dst_len + 1); |
| 496 | return 1; |
| 497 | } |
| 498 | |
Brandon Williams | 49a6d31 | 2017-06-12 15:13:53 -0700 | [diff] [blame] | 499 | static int crlf_to_git(const struct index_state *istate, |
| 500 | const char *path, const char *src, size_t len, |
Junio C Hamano | c61dcff | 2011-05-09 13:12:57 -0700 | [diff] [blame] | 501 | struct strbuf *buf, |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 502 | enum crlf_action crlf_action, int conv_flags) |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 503 | { |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 504 | struct text_stat stats; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 505 | char *dst; |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 506 | int convert_crlf_into_lf; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 507 | |
Junio C Hamano | c61dcff | 2011-05-09 13:12:57 -0700 | [diff] [blame] | 508 | if (crlf_action == CRLF_BINARY || |
Jeff King | 4c3b57b | 2012-02-24 17:05:03 -0500 | [diff] [blame] | 509 | (src && !len)) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 510 | return 0; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 511 | |
Jeff King | 4c3b57b | 2012-02-24 17:05:03 -0500 | [diff] [blame] | 512 | /* |
| 513 | * If we are doing a dry-run and have no source buffer, there is |
| 514 | * nothing to analyze; we must assume we would convert. |
| 515 | */ |
| 516 | if (!buf && !src) |
| 517 | return 1; |
| 518 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 519 | gather_stats(src, len, &stats); |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 520 | /* Optimization: No CRLF? Nothing to convert, regardless. */ |
| 521 | convert_crlf_into_lf = !!stats.crlf; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 522 | |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 523 | if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) { |
Jeff King | 129beee | 2019-01-24 08:12:41 -0500 | [diff] [blame] | 524 | if (convert_is_binary(&stats)) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 525 | return 0; |
Torsten Bögershausen | 6523728 | 2016-06-28 10:01:13 +0200 | [diff] [blame] | 526 | /* |
Torsten Bögershausen | 1c25d2d | 2016-11-30 18:02:32 +0100 | [diff] [blame] | 527 | * If the file in the index has any CR in it, do not |
| 528 | * convert. This is the new safer autocrlf handling, |
| 529 | * unless we want to renormalize in a merge or |
| 530 | * cherry-pick. |
Torsten Bögershausen | 6523728 | 2016-06-28 10:01:13 +0200 | [diff] [blame] | 531 | */ |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 532 | if ((!(conv_flags & CONV_EOL_RENORMALIZE)) && |
Torsten Bögershausen | 86ff70a | 2017-11-26 13:20:52 +0100 | [diff] [blame] | 533 | has_crlf_in_index(istate, path)) |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 534 | convert_crlf_into_lf = 0; |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 535 | } |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 536 | if (((conv_flags & CONV_EOL_RNDTRP_WARN) || |
| 537 | ((conv_flags & CONV_EOL_RNDTRP_DIE) && len))) { |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 538 | struct text_stat new_stats; |
| 539 | memcpy(&new_stats, &stats, sizeof(new_stats)); |
| 540 | /* simulate "git add" */ |
| 541 | if (convert_crlf_into_lf) { |
| 542 | new_stats.lonelf += new_stats.crlf; |
| 543 | new_stats.crlf = 0; |
| 544 | } |
| 545 | /* simulate "git checkout" */ |
Jeff King | 129beee | 2019-01-24 08:12:41 -0500 | [diff] [blame] | 546 | if (will_convert_lf_to_crlf(&new_stats, crlf_action)) { |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 547 | new_stats.crlf += new_stats.lonelf; |
| 548 | new_stats.lonelf = 0; |
| 549 | } |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 550 | check_global_conv_flags_eol(path, crlf_action, &stats, &new_stats, conv_flags); |
Torsten Bögershausen | a0ad53c | 2016-08-13 23:29:27 +0200 | [diff] [blame] | 551 | } |
| 552 | if (!convert_crlf_into_lf) |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 553 | return 0; |
| 554 | |
Jeff King | 92ac319 | 2012-02-24 17:02:37 -0500 | [diff] [blame] | 555 | /* |
| 556 | * At this point all of our source analysis is done, and we are sure we |
| 557 | * would convert. If we are in dry-run mode, we can give an answer. |
| 558 | */ |
| 559 | if (!buf) |
| 560 | return 1; |
| 561 | |
Pierre Habouzit | 90d16ec | 2007-10-05 10:11:59 +0200 | [diff] [blame] | 562 | /* only grow if not in place */ |
| 563 | if (strbuf_avail(buf) + buf->len < len) |
| 564 | strbuf_grow(buf, len - buf->len); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 565 | dst = buf->buf; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 566 | 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] | 567 | /* |
| 568 | * If we guessed, we already know we rejected a file with |
| 569 | * lone CR, and we can strip a CR without looking at what |
| 570 | * follow it. |
| 571 | */ |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 572 | do { |
Alex Riesen | ac78e54 | 2007-04-19 02:05:03 +0200 | [diff] [blame] | 573 | unsigned char c = *src++; |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 574 | if (c != '\r') |
Alex Riesen | ac78e54 | 2007-04-19 02:05:03 +0200 | [diff] [blame] | 575 | *dst++ = c; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 576 | } while (--len); |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 577 | } else { |
| 578 | do { |
Alex Riesen | ac78e54 | 2007-04-19 02:05:03 +0200 | [diff] [blame] | 579 | unsigned char c = *src++; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 580 | if (! (c == '\r' && (1 < len && *src == '\n'))) |
Alex Riesen | ac78e54 | 2007-04-19 02:05:03 +0200 | [diff] [blame] | 581 | *dst++ = c; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 582 | } while (--len); |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 583 | } |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 584 | strbuf_setlen(buf, dst - buf->buf); |
| 585 | return 1; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 586 | } |
| 587 | |
Jeff King | 55ad152 | 2019-01-24 08:12:51 -0500 | [diff] [blame] | 588 | static int crlf_to_worktree(const char *src, size_t len, |
Junio C Hamano | c61dcff | 2011-05-09 13:12:57 -0700 | [diff] [blame] | 589 | struct strbuf *buf, enum crlf_action crlf_action) |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 590 | { |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 591 | char *to_free = NULL; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 592 | struct text_stat stats; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 593 | |
Junio C Hamano | c61dcff | 2011-05-09 13:12:57 -0700 | [diff] [blame] | 594 | if (!len || output_eol(crlf_action) != EOL_CRLF) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 595 | return 0; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 596 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 597 | gather_stats(src, len, &stats); |
Jeff King | 129beee | 2019-01-24 08:12:41 -0500 | [diff] [blame] | 598 | if (!will_convert_lf_to_crlf(&stats, crlf_action)) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 599 | return 0; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 600 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 601 | /* are we "faking" in place editing ? */ |
| 602 | if (src == buf->buf) |
Pierre Habouzit | b315c5c | 2007-09-27 12:58:23 +0200 | [diff] [blame] | 603 | to_free = strbuf_detach(buf, NULL); |
Alex Riesen | ac78e54 | 2007-04-19 02:05:03 +0200 | [diff] [blame] | 604 | |
Torsten Bögershausen | 6e336a5 | 2016-02-10 17:24:43 +0100 | [diff] [blame] | 605 | strbuf_grow(buf, len + stats.lonelf); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 606 | for (;;) { |
| 607 | const char *nl = memchr(src, '\n', len); |
| 608 | if (!nl) |
| 609 | break; |
| 610 | if (nl > src && nl[-1] == '\r') { |
| 611 | strbuf_add(buf, src, nl + 1 - src); |
| 612 | } else { |
| 613 | strbuf_add(buf, src, nl - src); |
| 614 | strbuf_addstr(buf, "\r\n"); |
| 615 | } |
| 616 | len -= nl + 1 - src; |
| 617 | src = nl + 1; |
| 618 | } |
| 619 | strbuf_add(buf, src, len); |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 620 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 621 | free(to_free); |
| 622 | return 1; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 623 | } |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 624 | |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 625 | struct filter_params { |
| 626 | const char *src; |
| 627 | unsigned long size; |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 628 | int fd; |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 629 | const char *cmd; |
Pete Wyckoff | a2b665d | 2010-12-22 06:40:13 -0800 | [diff] [blame] | 630 | const char *path; |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 631 | }; |
| 632 | |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 633 | 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] | 634 | { |
| 635 | /* |
| 636 | * Spawn cmd and feed the buffer contents through its stdin. |
| 637 | */ |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 638 | struct child_process child_process = CHILD_PROCESS_INIT; |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 639 | struct filter_params *params = (struct filter_params *)data; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 640 | int write_err, status; |
Gary V. Vaughan | 66dbfd5 | 2010-05-14 09:31:33 +0000 | [diff] [blame] | 641 | const char *argv[] = { NULL, NULL }; |
| 642 | |
Pete Wyckoff | a2b665d | 2010-12-22 06:40:13 -0800 | [diff] [blame] | 643 | /* apply % substitution to cmd */ |
| 644 | struct strbuf cmd = STRBUF_INIT; |
| 645 | struct strbuf path = STRBUF_INIT; |
| 646 | struct strbuf_expand_dict_entry dict[] = { |
| 647 | { "f", NULL, }, |
| 648 | { NULL, NULL, }, |
| 649 | }; |
| 650 | |
| 651 | /* quote the path to preserve spaces, etc. */ |
| 652 | sq_quote_buf(&path, params->path); |
| 653 | dict[0].value = path.buf; |
| 654 | |
| 655 | /* expand all %f with the quoted path */ |
| 656 | strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict); |
| 657 | strbuf_release(&path); |
| 658 | |
| 659 | argv[0] = cmd.buf; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 660 | |
Johannes Sixt | dc1bfdc | 2007-10-19 21:47:55 +0200 | [diff] [blame] | 661 | child_process.argv = argv; |
Jeff King | ac0ba18 | 2009-12-30 05:53:57 -0500 | [diff] [blame] | 662 | child_process.use_shell = 1; |
Johannes Sixt | dc1bfdc | 2007-10-19 21:47:55 +0200 | [diff] [blame] | 663 | child_process.in = -1; |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 664 | child_process.out = out; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 665 | |
Rene Scharfe | f31f1d3 | 2017-08-30 19:49:40 +0200 | [diff] [blame] | 666 | if (start_command(&child_process)) { |
| 667 | strbuf_release(&cmd); |
Nguyễn Thái Ngọc Duy | d26a328 | 2018-07-21 09:49:29 +0200 | [diff] [blame] | 668 | return error(_("cannot fork to run external filter '%s'"), |
| 669 | params->cmd); |
Rene Scharfe | f31f1d3 | 2017-08-30 19:49:40 +0200 | [diff] [blame] | 670 | } |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 671 | |
Jehan Bing | 6424c2a | 2012-02-20 12:53:37 -0800 | [diff] [blame] | 672 | sigchain_push(SIGPIPE, SIG_IGN); |
| 673 | |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 674 | if (params->src) { |
Junio C Hamano | 0c4dd67 | 2015-05-19 11:08:23 -0700 | [diff] [blame] | 675 | write_err = (write_in_full(child_process.in, |
| 676 | params->src, params->size) < 0); |
| 677 | if (errno == EPIPE) |
| 678 | write_err = 0; |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 679 | } else { |
| 680 | write_err = copy_fd(params->fd, child_process.in); |
Junio C Hamano | 0c4dd67 | 2015-05-19 11:08:23 -0700 | [diff] [blame] | 681 | if (write_err == COPY_WRITE_ERROR && errno == EPIPE) |
| 682 | write_err = 0; |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 683 | } |
| 684 | |
Johannes Sixt | dc1bfdc | 2007-10-19 21:47:55 +0200 | [diff] [blame] | 685 | if (close(child_process.in)) |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 686 | write_err = 1; |
| 687 | if (write_err) |
Nguyễn Thái Ngọc Duy | d26a328 | 2018-07-21 09:49:29 +0200 | [diff] [blame] | 688 | error(_("cannot feed the input to external filter '%s'"), |
| 689 | params->cmd); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 690 | |
Jehan Bing | 6424c2a | 2012-02-20 12:53:37 -0800 | [diff] [blame] | 691 | sigchain_pop(SIGPIPE); |
| 692 | |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 693 | status = finish_command(&child_process); |
| 694 | if (status) |
Nguyễn Thái Ngọc Duy | d26a328 | 2018-07-21 09:49:29 +0200 | [diff] [blame] | 695 | error(_("external filter '%s' failed %d"), params->cmd, status); |
Pete Wyckoff | a2b665d | 2010-12-22 06:40:13 -0800 | [diff] [blame] | 696 | |
| 697 | strbuf_release(&cmd); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 698 | return (write_err || status); |
| 699 | } |
| 700 | |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 701 | static int apply_single_file_filter(const char *path, const char *src, size_t len, int fd, |
Nguyễn Thái Ngọc Duy | ec36c42 | 2018-12-06 16:42:06 +0100 | [diff] [blame] | 702 | struct strbuf *dst, const char *cmd) |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 703 | { |
| 704 | /* |
| 705 | * Create a pipeline to have the command filter the buffer's |
| 706 | * contents. |
| 707 | * |
| 708 | * (child --> cmd) --> us |
| 709 | */ |
Lars Schneider | b84be55 | 2016-10-16 16:20:35 -0700 | [diff] [blame] | 710 | int err = 0; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 711 | struct strbuf nbuf = STRBUF_INIT; |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 712 | struct async async; |
| 713 | struct filter_params params; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 714 | |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 715 | memset(&async, 0, sizeof(async)); |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 716 | async.proc = filter_buffer_or_fd; |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 717 | async.data = ¶ms; |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 718 | async.out = -1; |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 719 | params.src = src; |
| 720 | params.size = len; |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 721 | params.fd = fd; |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 722 | params.cmd = cmd; |
Pete Wyckoff | a2b665d | 2010-12-22 06:40:13 -0800 | [diff] [blame] | 723 | params.path = path; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 724 | |
| 725 | fflush(NULL); |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 726 | if (start_async(&async)) |
| 727 | return 0; /* error was already reported */ |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 728 | |
Joey Hess | 02156ab | 2019-03-07 14:56:57 -0500 | [diff] [blame] | 729 | if (strbuf_read(&nbuf, async.out, 0) < 0) { |
Nguyễn Thái Ngọc Duy | d26a328 | 2018-07-21 09:49:29 +0200 | [diff] [blame] | 730 | err = error(_("read from external filter '%s' failed"), cmd); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 731 | } |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 732 | if (close(async.out)) { |
Nguyễn Thái Ngọc Duy | d26a328 | 2018-07-21 09:49:29 +0200 | [diff] [blame] | 733 | err = error(_("read from external filter '%s' failed"), cmd); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 734 | } |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 735 | if (finish_async(&async)) { |
Nguyễn Thái Ngọc Duy | d26a328 | 2018-07-21 09:49:29 +0200 | [diff] [blame] | 736 | err = error(_("external filter '%s' failed"), cmd); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 737 | } |
| 738 | |
Lars Schneider | b84be55 | 2016-10-16 16:20:35 -0700 | [diff] [blame] | 739 | if (!err) { |
Pierre Habouzit | 90d16ec | 2007-10-05 10:11:59 +0200 | [diff] [blame] | 740 | strbuf_swap(dst, &nbuf); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 741 | } |
Pierre Habouzit | 90d16ec | 2007-10-05 10:11:59 +0200 | [diff] [blame] | 742 | strbuf_release(&nbuf); |
Lars Schneider | b84be55 | 2016-10-16 16:20:35 -0700 | [diff] [blame] | 743 | return !err; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 746 | #define CAP_CLEAN (1u<<0) |
| 747 | #define CAP_SMUDGE (1u<<1) |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 748 | #define CAP_DELAY (1u<<2) |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 749 | |
Ben Peart | 1b0b46e | 2017-05-05 11:27:58 -0400 | [diff] [blame] | 750 | struct cmd2process { |
| 751 | struct subprocess_entry subprocess; /* must be the first member! */ |
| 752 | unsigned int supported_capabilities; |
| 753 | }; |
| 754 | |
Ben Peart | f514d7d | 2017-05-05 11:28:00 -0400 | [diff] [blame] | 755 | static int subprocess_map_initialized; |
| 756 | static struct hashmap subprocess_map; |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 757 | |
Ben Peart | 7ddb9b2 | 2017-05-05 11:27:59 -0400 | [diff] [blame] | 758 | static int start_multi_file_filter_fn(struct subprocess_entry *subprocess) |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 759 | { |
Jonathan Tan | fa64a2f | 2017-07-26 11:17:29 -0700 | [diff] [blame] | 760 | static int versions[] = {2, 0}; |
| 761 | static struct subprocess_capability capabilities[] = { |
Lars Schneider | 1514c8e | 2017-06-30 22:41:27 +0200 | [diff] [blame] | 762 | { "clean", CAP_CLEAN }, |
| 763 | { "smudge", CAP_SMUDGE }, |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 764 | { "delay", CAP_DELAY }, |
Jonathan Tan | fa64a2f | 2017-07-26 11:17:29 -0700 | [diff] [blame] | 765 | { NULL, 0 } |
Lars Schneider | 1514c8e | 2017-06-30 22:41:27 +0200 | [diff] [blame] | 766 | }; |
Jonathan Tan | fa64a2f | 2017-07-26 11:17:29 -0700 | [diff] [blame] | 767 | struct cmd2process *entry = (struct cmd2process *)subprocess; |
| 768 | return subprocess_handshake(subprocess, "git-filter", versions, NULL, |
| 769 | capabilities, |
| 770 | &entry->supported_capabilities); |
Ben Peart | a810ea9 | 2017-05-05 11:27:57 -0400 | [diff] [blame] | 771 | } |
| 772 | |
Lars Schneider | 9364fc2 | 2017-06-28 23:29:51 +0200 | [diff] [blame] | 773 | static void handle_filter_error(const struct strbuf *filter_status, |
| 774 | struct cmd2process *entry, |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 775 | const unsigned int wanted_capability) |
| 776 | { |
Lars Schneider | 9364fc2 | 2017-06-28 23:29:51 +0200 | [diff] [blame] | 777 | if (!strcmp(filter_status->buf, "error")) |
| 778 | ; /* The filter signaled a problem with the file. */ |
| 779 | else if (!strcmp(filter_status->buf, "abort") && wanted_capability) { |
| 780 | /* |
| 781 | * The filter signaled a permanent problem. Don't try to filter |
| 782 | * files with the same command for the lifetime of the current |
| 783 | * Git process. |
| 784 | */ |
| 785 | entry->supported_capabilities &= ~wanted_capability; |
| 786 | } else { |
| 787 | /* |
| 788 | * Something went wrong with the protocol filter. |
| 789 | * Force shutdown and restart if another blob requires filtering. |
| 790 | */ |
Nguyễn Thái Ngọc Duy | d26a328 | 2018-07-21 09:49:29 +0200 | [diff] [blame] | 791 | error(_("external filter '%s' failed"), entry->subprocess.cmd); |
Lars Schneider | 9364fc2 | 2017-06-28 23:29:51 +0200 | [diff] [blame] | 792 | subprocess_stop(&subprocess_map, &entry->subprocess); |
| 793 | free(entry); |
| 794 | } |
| 795 | } |
| 796 | |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 797 | static int apply_multi_file_filter(const char *path, const char *src, size_t len, |
| 798 | int fd, struct strbuf *dst, const char *cmd, |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 799 | const unsigned int wanted_capability, |
| 800 | struct delayed_checkout *dco) |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 801 | { |
| 802 | int err; |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 803 | int can_delay = 0; |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 804 | struct cmd2process *entry; |
| 805 | struct child_process *process; |
| 806 | struct strbuf nbuf = STRBUF_INIT; |
| 807 | struct strbuf filter_status = STRBUF_INIT; |
| 808 | const char *filter_type; |
| 809 | |
Ben Peart | f514d7d | 2017-05-05 11:28:00 -0400 | [diff] [blame] | 810 | if (!subprocess_map_initialized) { |
| 811 | subprocess_map_initialized = 1; |
Stefan Beller | 9ab4295 | 2017-06-30 17:28:33 -0700 | [diff] [blame] | 812 | hashmap_init(&subprocess_map, cmd2process_cmp, NULL, 0); |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 813 | entry = NULL; |
| 814 | } else { |
Ben Peart | f514d7d | 2017-05-05 11:28:00 -0400 | [diff] [blame] | 815 | entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd); |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | fflush(NULL); |
| 819 | |
| 820 | if (!entry) { |
Ben Peart | 7ddb9b2 | 2017-05-05 11:27:59 -0400 | [diff] [blame] | 821 | entry = xmalloc(sizeof(*entry)); |
| 822 | entry->supported_capabilities = 0; |
| 823 | |
Ben Peart | f514d7d | 2017-05-05 11:28:00 -0400 | [diff] [blame] | 824 | if (subprocess_start(&subprocess_map, &entry->subprocess, cmd, start_multi_file_filter_fn)) { |
Ben Peart | 7ddb9b2 | 2017-05-05 11:27:59 -0400 | [diff] [blame] | 825 | free(entry); |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 826 | return 0; |
Ben Peart | 7ddb9b2 | 2017-05-05 11:27:59 -0400 | [diff] [blame] | 827 | } |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 828 | } |
Ben Peart | 1b0b46e | 2017-05-05 11:27:58 -0400 | [diff] [blame] | 829 | process = &entry->subprocess.process; |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 830 | |
Lars Schneider | 42b0a86 | 2017-06-28 23:29:50 +0200 | [diff] [blame] | 831 | if (!(entry->supported_capabilities & wanted_capability)) |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 832 | return 0; |
| 833 | |
Lars Schneider | 42b0a86 | 2017-06-28 23:29:50 +0200 | [diff] [blame] | 834 | if (wanted_capability & CAP_CLEAN) |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 835 | filter_type = "clean"; |
Lars Schneider | 42b0a86 | 2017-06-28 23:29:50 +0200 | [diff] [blame] | 836 | else if (wanted_capability & CAP_SMUDGE) |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 837 | filter_type = "smudge"; |
| 838 | else |
Nguyễn Thái Ngọc Duy | d26a328 | 2018-07-21 09:49:29 +0200 | [diff] [blame] | 839 | die(_("unexpected filter type")); |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 840 | |
| 841 | sigchain_push(SIGPIPE, SIG_IGN); |
| 842 | |
| 843 | assert(strlen(filter_type) < LARGE_PACKET_DATA_MAX - strlen("command=\n")); |
| 844 | err = packet_write_fmt_gently(process->in, "command=%s\n", filter_type); |
| 845 | if (err) |
| 846 | goto done; |
| 847 | |
| 848 | err = strlen(path) > LARGE_PACKET_DATA_MAX - strlen("pathname=\n"); |
| 849 | if (err) { |
Nguyễn Thái Ngọc Duy | d26a328 | 2018-07-21 09:49:29 +0200 | [diff] [blame] | 850 | error(_("path name too long for external filter")); |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 851 | goto done; |
| 852 | } |
| 853 | |
| 854 | err = packet_write_fmt_gently(process->in, "pathname=%s\n", path); |
| 855 | if (err) |
| 856 | goto done; |
| 857 | |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 858 | if ((entry->supported_capabilities & CAP_DELAY) && |
| 859 | dco && dco->state == CE_CAN_DELAY) { |
| 860 | can_delay = 1; |
| 861 | err = packet_write_fmt_gently(process->in, "can-delay=1\n"); |
| 862 | if (err) |
| 863 | goto done; |
| 864 | } |
| 865 | |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 866 | err = packet_flush_gently(process->in); |
| 867 | if (err) |
| 868 | goto done; |
| 869 | |
| 870 | if (fd >= 0) |
| 871 | err = write_packetized_from_fd(fd, process->in); |
| 872 | else |
| 873 | err = write_packetized_from_buf(src, len, process->in); |
| 874 | if (err) |
| 875 | goto done; |
| 876 | |
Ben Peart | 4f2a2e9 | 2017-05-05 11:28:02 -0400 | [diff] [blame] | 877 | err = subprocess_read_status(process->out, &filter_status); |
| 878 | if (err) |
| 879 | goto done; |
| 880 | |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 881 | if (can_delay && !strcmp(filter_status.buf, "delayed")) { |
| 882 | string_list_insert(&dco->filters, cmd); |
| 883 | string_list_insert(&dco->paths, path); |
| 884 | } else { |
| 885 | /* The filter got the blob and wants to send us a response. */ |
| 886 | err = strcmp(filter_status.buf, "success"); |
| 887 | if (err) |
| 888 | goto done; |
| 889 | |
| 890 | err = read_packetized_to_strbuf(process->out, &nbuf) < 0; |
| 891 | if (err) |
| 892 | goto done; |
| 893 | |
| 894 | err = subprocess_read_status(process->out, &filter_status); |
| 895 | if (err) |
| 896 | goto done; |
| 897 | |
| 898 | err = strcmp(filter_status.buf, "success"); |
| 899 | } |
| 900 | |
| 901 | done: |
| 902 | sigchain_pop(SIGPIPE); |
| 903 | |
| 904 | if (err) |
| 905 | handle_filter_error(&filter_status, entry, wanted_capability); |
| 906 | else |
| 907 | strbuf_swap(dst, &nbuf); |
| 908 | strbuf_release(&nbuf); |
| 909 | return !err; |
| 910 | } |
| 911 | |
| 912 | |
| 913 | int async_query_available_blobs(const char *cmd, struct string_list *available_paths) |
| 914 | { |
| 915 | int err; |
| 916 | char *line; |
| 917 | struct cmd2process *entry; |
| 918 | struct child_process *process; |
| 919 | struct strbuf filter_status = STRBUF_INIT; |
| 920 | |
| 921 | assert(subprocess_map_initialized); |
| 922 | entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd); |
| 923 | if (!entry) { |
Nguyễn Thái Ngọc Duy | d26a328 | 2018-07-21 09:49:29 +0200 | [diff] [blame] | 924 | error(_("external filter '%s' is not available anymore although " |
| 925 | "not all paths have been filtered"), cmd); |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 926 | return 0; |
| 927 | } |
| 928 | process = &entry->subprocess.process; |
| 929 | sigchain_push(SIGPIPE, SIG_IGN); |
| 930 | |
| 931 | err = packet_write_fmt_gently( |
| 932 | process->in, "command=list_available_blobs\n"); |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 933 | if (err) |
| 934 | goto done; |
| 935 | |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 936 | err = packet_flush_gently(process->in); |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 937 | if (err) |
| 938 | goto done; |
| 939 | |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 940 | while ((line = packet_read_line(process->out, NULL))) { |
| 941 | const char *path; |
| 942 | if (skip_prefix(line, "pathname=", &path)) |
| 943 | string_list_insert(available_paths, xstrdup(path)); |
| 944 | else |
| 945 | ; /* ignore unknown keys */ |
| 946 | } |
| 947 | |
Ben Peart | 4f2a2e9 | 2017-05-05 11:28:02 -0400 | [diff] [blame] | 948 | err = subprocess_read_status(process->out, &filter_status); |
| 949 | if (err) |
| 950 | goto done; |
| 951 | |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 952 | err = strcmp(filter_status.buf, "success"); |
| 953 | |
| 954 | done: |
| 955 | sigchain_pop(SIGPIPE); |
| 956 | |
Lars Schneider | 9364fc2 | 2017-06-28 23:29:51 +0200 | [diff] [blame] | 957 | if (err) |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 958 | handle_filter_error(&filter_status, entry, 0); |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 959 | return !err; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | static struct convert_driver { |
| 963 | const char *name; |
| 964 | struct convert_driver *next; |
Brian Hetro | cd8be6c | 2008-07-05 01:24:42 -0400 | [diff] [blame] | 965 | const char *smudge; |
| 966 | const char *clean; |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 967 | const char *process; |
Jehan Bing | 36daaac | 2012-02-16 17:19:03 -0800 | [diff] [blame] | 968 | int required; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 969 | } *user_convert, **user_convert_tail; |
| 970 | |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 971 | static int apply_filter(const char *path, const char *src, size_t len, |
| 972 | int fd, struct strbuf *dst, struct convert_driver *drv, |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 973 | const unsigned int wanted_capability, |
| 974 | struct delayed_checkout *dco) |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 975 | { |
| 976 | const char *cmd = NULL; |
| 977 | |
| 978 | if (!drv) |
| 979 | return 0; |
| 980 | |
| 981 | if (!dst) |
| 982 | return 1; |
| 983 | |
Lars Schneider | 42b0a86 | 2017-06-28 23:29:50 +0200 | [diff] [blame] | 984 | if ((wanted_capability & CAP_CLEAN) && !drv->process && drv->clean) |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 985 | cmd = drv->clean; |
Lars Schneider | 42b0a86 | 2017-06-28 23:29:50 +0200 | [diff] [blame] | 986 | else if ((wanted_capability & CAP_SMUDGE) && !drv->process && drv->smudge) |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 987 | cmd = drv->smudge; |
| 988 | |
| 989 | if (cmd && *cmd) |
| 990 | return apply_single_file_filter(path, src, len, fd, dst, cmd); |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 991 | else if (drv->process && *drv->process) |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 992 | return apply_multi_file_filter(path, src, len, fd, dst, |
| 993 | drv->process, wanted_capability, dco); |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 994 | |
| 995 | return 0; |
| 996 | } |
| 997 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 998 | 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] | 999 | { |
Jeff King | d731f0a | 2013-01-23 01:24:23 -0500 | [diff] [blame] | 1000 | const char *key, *name; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 1001 | int namelen; |
| 1002 | struct convert_driver *drv; |
| 1003 | |
| 1004 | /* |
| 1005 | * External conversion drivers are configured using |
| 1006 | * "filter.<name>.variable". |
| 1007 | */ |
Jeff King | d731f0a | 2013-01-23 01:24:23 -0500 | [diff] [blame] | 1008 | if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name) |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 1009 | return 0; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 1010 | for (drv = user_convert; drv; drv = drv->next) |
| 1011 | if (!strncmp(drv->name, name, namelen) && !drv->name[namelen]) |
| 1012 | break; |
| 1013 | if (!drv) { |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 1014 | drv = xcalloc(1, sizeof(struct convert_driver)); |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 1015 | drv->name = xmemdupz(name, namelen); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 1016 | *user_convert_tail = drv; |
| 1017 | user_convert_tail = &(drv->next); |
| 1018 | } |
| 1019 | |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 1020 | /* |
| 1021 | * filter.<name>.smudge and filter.<name>.clean specifies |
| 1022 | * the command line: |
| 1023 | * |
| 1024 | * command-line |
| 1025 | * |
| 1026 | * The command-line will not be interpolated in any way. |
| 1027 | */ |
| 1028 | |
Jeff King | d731f0a | 2013-01-23 01:24:23 -0500 | [diff] [blame] | 1029 | if (!strcmp("smudge", key)) |
Brian Hetro | cd8be6c | 2008-07-05 01:24:42 -0400 | [diff] [blame] | 1030 | return git_config_string(&drv->smudge, var, value); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 1031 | |
Jeff King | d731f0a | 2013-01-23 01:24:23 -0500 | [diff] [blame] | 1032 | if (!strcmp("clean", key)) |
Brian Hetro | cd8be6c | 2008-07-05 01:24:42 -0400 | [diff] [blame] | 1033 | return git_config_string(&drv->clean, var, value); |
| 1034 | |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 1035 | if (!strcmp("process", key)) |
| 1036 | return git_config_string(&drv->process, var, value); |
| 1037 | |
Jeff King | d731f0a | 2013-01-23 01:24:23 -0500 | [diff] [blame] | 1038 | if (!strcmp("required", key)) { |
Jehan Bing | 36daaac | 2012-02-16 17:19:03 -0800 | [diff] [blame] | 1039 | drv->required = git_config_bool(var, value); |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 1043 | return 0; |
| 1044 | } |
| 1045 | |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1046 | static int count_ident(const char *cp, unsigned long size) |
| 1047 | { |
| 1048 | /* |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 1049 | * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$" |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1050 | */ |
| 1051 | int cnt = 0; |
| 1052 | char ch; |
| 1053 | |
| 1054 | while (size) { |
| 1055 | ch = *cp++; |
| 1056 | size--; |
| 1057 | if (ch != '$') |
| 1058 | continue; |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 1059 | if (size < 3) |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1060 | break; |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 1061 | if (memcmp("Id", cp, 2)) |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1062 | continue; |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 1063 | ch = cp[2]; |
| 1064 | cp += 3; |
| 1065 | size -= 3; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1066 | if (ch == '$') |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 1067 | cnt++; /* $Id$ */ |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1068 | if (ch != ':') |
| 1069 | continue; |
| 1070 | |
| 1071 | /* |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 1072 | * "$Id: ... "; scan up to the closing dollar sign and discard. |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1073 | */ |
| 1074 | while (size) { |
| 1075 | ch = *cp++; |
| 1076 | size--; |
| 1077 | if (ch == '$') { |
| 1078 | cnt++; |
| 1079 | break; |
| 1080 | } |
Henrik Grubbström | a9f3049 | 2010-04-06 14:46:37 +0200 | [diff] [blame] | 1081 | if (ch == '\n') |
| 1082 | break; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1083 | } |
| 1084 | } |
| 1085 | return cnt; |
| 1086 | } |
| 1087 | |
Jeff King | 55ad152 | 2019-01-24 08:12:51 -0500 | [diff] [blame] | 1088 | static int ident_to_git(const char *src, size_t len, |
Nguyễn Thái Ngọc Duy | ec36c42 | 2018-12-06 16:42:06 +0100 | [diff] [blame] | 1089 | struct strbuf *buf, int ident) |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1090 | { |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1091 | char *dst, *dollar; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1092 | |
Jeff King | 4c3b57b | 2012-02-24 17:05:03 -0500 | [diff] [blame] | 1093 | if (!ident || (src && !count_ident(src, len))) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1094 | return 0; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1095 | |
Jeff King | 92ac319 | 2012-02-24 17:02:37 -0500 | [diff] [blame] | 1096 | if (!buf) |
| 1097 | return 1; |
| 1098 | |
Pierre Habouzit | 90d16ec | 2007-10-05 10:11:59 +0200 | [diff] [blame] | 1099 | /* only grow if not in place */ |
| 1100 | if (strbuf_avail(buf) + buf->len < len) |
| 1101 | strbuf_grow(buf, len - buf->len); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1102 | dst = buf->buf; |
| 1103 | for (;;) { |
| 1104 | dollar = memchr(src, '$', len); |
| 1105 | if (!dollar) |
| 1106 | break; |
Thomas Rast | 7732118 | 2011-08-29 22:06:04 +0200 | [diff] [blame] | 1107 | memmove(dst, src, dollar + 1 - src); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1108 | dst += dollar + 1 - src; |
| 1109 | len -= dollar + 1 - src; |
| 1110 | src = dollar + 1; |
| 1111 | |
| 1112 | if (len > 3 && !memcmp(src, "Id:", 3)) { |
| 1113 | dollar = memchr(src + 3, '$', len - 3); |
| 1114 | if (!dollar) |
| 1115 | break; |
Henrik Grubbström | a9f3049 | 2010-04-06 14:46:37 +0200 | [diff] [blame] | 1116 | if (memchr(src + 3, '\n', dollar - src - 3)) { |
| 1117 | /* Line break before the next dollar. */ |
| 1118 | continue; |
| 1119 | } |
| 1120 | |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 1121 | memcpy(dst, "Id$", 3); |
| 1122 | dst += 3; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1123 | len -= dollar + 1 - src; |
| 1124 | src = dollar + 1; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1125 | } |
| 1126 | } |
Thomas Rast | 7732118 | 2011-08-29 22:06:04 +0200 | [diff] [blame] | 1127 | memmove(dst, src, len); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1128 | strbuf_setlen(buf, dst + len - buf->buf); |
| 1129 | return 1; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Jeff King | 55ad152 | 2019-01-24 08:12:51 -0500 | [diff] [blame] | 1132 | static int ident_to_worktree(const char *src, size_t len, |
Nguyễn Thái Ngọc Duy | ec36c42 | 2018-12-06 16:42:06 +0100 | [diff] [blame] | 1133 | struct strbuf *buf, int ident) |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1134 | { |
Patryk Obara | f070fac | 2018-01-28 01:13:13 +0100 | [diff] [blame] | 1135 | struct object_id oid; |
Henrik Grubbström | 07814d9 | 2010-04-06 14:46:38 +0200 | [diff] [blame] | 1136 | char *to_free = NULL, *dollar, *spc; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1137 | int cnt; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1138 | |
| 1139 | if (!ident) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1140 | return 0; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1141 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1142 | cnt = count_ident(src, len); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1143 | if (!cnt) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1144 | return 0; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1145 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1146 | /* are we "faking" in place editing ? */ |
| 1147 | if (src == buf->buf) |
Pierre Habouzit | b315c5c | 2007-09-27 12:58:23 +0200 | [diff] [blame] | 1148 | to_free = strbuf_detach(buf, NULL); |
Patryk Obara | f070fac | 2018-01-28 01:13:13 +0100 | [diff] [blame] | 1149 | hash_object_file(src, len, "blob", &oid); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1150 | |
brian m. carlson | 1a75044 | 2018-03-12 02:27:56 +0000 | [diff] [blame] | 1151 | strbuf_grow(buf, len + cnt * (the_hash_algo->hexsz + 3)); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1152 | for (;;) { |
| 1153 | /* step 1: run to the next '$' */ |
| 1154 | dollar = memchr(src, '$', len); |
| 1155 | if (!dollar) |
| 1156 | break; |
| 1157 | strbuf_add(buf, src, dollar + 1 - src); |
| 1158 | len -= dollar + 1 - src; |
| 1159 | src = dollar + 1; |
| 1160 | |
| 1161 | /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */ |
| 1162 | if (len < 3 || memcmp("Id", src, 2)) |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1163 | continue; |
| 1164 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1165 | /* step 3: skip over Id$ or Id:xxxxx$ */ |
| 1166 | if (src[2] == '$') { |
| 1167 | src += 3; |
| 1168 | len -= 3; |
| 1169 | } else if (src[2] == ':') { |
Andy Parkins | c23290d | 2007-05-25 11:50:08 +0100 | [diff] [blame] | 1170 | /* |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1171 | * 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] | 1172 | * repository, we cope with that by stripping the expansion out. |
| 1173 | * This is probably not a good idea, since it will cause changes |
| 1174 | * on checkout, which won't go away by stash, but let's keep it |
| 1175 | * for git-style ids. |
Andy Parkins | c23290d | 2007-05-25 11:50:08 +0100 | [diff] [blame] | 1176 | */ |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1177 | dollar = memchr(src + 3, '$', len - 3); |
| 1178 | if (!dollar) { |
| 1179 | /* incomplete keyword, no more '$', so just quit the loop */ |
| 1180 | break; |
| 1181 | } |
| 1182 | |
Henrik Grubbström | a9f3049 | 2010-04-06 14:46:37 +0200 | [diff] [blame] | 1183 | if (memchr(src + 3, '\n', dollar - src - 3)) { |
| 1184 | /* Line break before the next dollar. */ |
| 1185 | continue; |
| 1186 | } |
| 1187 | |
Henrik Grubbström | 07814d9 | 2010-04-06 14:46:38 +0200 | [diff] [blame] | 1188 | spc = memchr(src + 4, ' ', dollar - src - 4); |
| 1189 | if (spc && spc < dollar-1) { |
| 1190 | /* There are spaces in unexpected places. |
| 1191 | * This is probably an id from some other |
| 1192 | * versioning system. Keep it for now. |
| 1193 | */ |
| 1194 | continue; |
| 1195 | } |
| 1196 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1197 | len -= dollar + 1 - src; |
| 1198 | src = dollar + 1; |
| 1199 | } else { |
| 1200 | /* it wasn't a "Id$" or "Id:xxxx$" */ |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1201 | continue; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1202 | } |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1203 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1204 | /* step 4: substitute */ |
| 1205 | strbuf_addstr(buf, "Id: "); |
Patryk Obara | f070fac | 2018-01-28 01:13:13 +0100 | [diff] [blame] | 1206 | strbuf_addstr(buf, oid_to_hex(&oid)); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1207 | strbuf_addstr(buf, " $"); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1208 | } |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1209 | strbuf_add(buf, src, len); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1210 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1211 | free(to_free); |
| 1212 | return 1; |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
Lars Schneider | 107642f | 2018-04-15 20:16:07 +0200 | [diff] [blame] | 1215 | static const char *git_path_check_encoding(struct attr_check_item *check) |
| 1216 | { |
| 1217 | const char *value = check->value; |
| 1218 | |
| 1219 | if (ATTR_UNSET(value) || !strlen(value)) |
| 1220 | return NULL; |
| 1221 | |
| 1222 | if (ATTR_TRUE(value) || ATTR_FALSE(value)) { |
| 1223 | die(_("true/false are no valid working-tree-encodings")); |
| 1224 | } |
| 1225 | |
| 1226 | /* Don't encode to the default encoding */ |
| 1227 | if (same_encoding(value, default_encoding)) |
| 1228 | return NULL; |
| 1229 | |
| 1230 | return value; |
| 1231 | } |
| 1232 | |
Junio C Hamano | 7bd1805 | 2017-01-27 18:01:54 -0800 | [diff] [blame] | 1233 | static enum crlf_action git_path_check_crlf(struct attr_check_item *check) |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 1234 | { |
Junio C Hamano | 6073ee8 | 2007-04-20 23:44:02 -0700 | [diff] [blame] | 1235 | const char *value = check->value; |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 1236 | |
Junio C Hamano | 6073ee8 | 2007-04-20 23:44:02 -0700 | [diff] [blame] | 1237 | if (ATTR_TRUE(value)) |
| 1238 | return CRLF_TEXT; |
| 1239 | else if (ATTR_FALSE(value)) |
| 1240 | return CRLF_BINARY; |
| 1241 | else if (ATTR_UNSET(value)) |
| 1242 | ; |
| 1243 | else if (!strcmp(value, "input")) |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 1244 | return CRLF_TEXT_INPUT; |
Eyvind Bernhardsen | fd6cce9 | 2010-05-19 22:43:10 +0200 | [diff] [blame] | 1245 | else if (!strcmp(value, "auto")) |
| 1246 | return CRLF_AUTO; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 1247 | return CRLF_UNDEFINED; |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
Junio C Hamano | 7bd1805 | 2017-01-27 18:01:54 -0800 | [diff] [blame] | 1250 | static enum eol git_path_check_eol(struct attr_check_item *check) |
Eyvind Bernhardsen | fd6cce9 | 2010-05-19 22:43:10 +0200 | [diff] [blame] | 1251 | { |
| 1252 | const char *value = check->value; |
| 1253 | |
| 1254 | if (ATTR_UNSET(value)) |
| 1255 | ; |
| 1256 | else if (!strcmp(value, "lf")) |
| 1257 | return EOL_LF; |
| 1258 | else if (!strcmp(value, "crlf")) |
| 1259 | return EOL_CRLF; |
| 1260 | return EOL_UNSET; |
| 1261 | } |
| 1262 | |
Junio C Hamano | 7bd1805 | 2017-01-27 18:01:54 -0800 | [diff] [blame] | 1263 | static struct convert_driver *git_path_check_convert(struct attr_check_item *check) |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 1264 | { |
| 1265 | const char *value = check->value; |
| 1266 | struct convert_driver *drv; |
| 1267 | |
| 1268 | if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value)) |
| 1269 | return NULL; |
| 1270 | for (drv = user_convert; drv; drv = drv->next) |
| 1271 | if (!strcmp(value, drv->name)) |
| 1272 | return drv; |
| 1273 | return NULL; |
| 1274 | } |
| 1275 | |
Junio C Hamano | 7bd1805 | 2017-01-27 18:01:54 -0800 | [diff] [blame] | 1276 | static int git_path_check_ident(struct attr_check_item *check) |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1277 | { |
| 1278 | const char *value = check->value; |
| 1279 | |
| 1280 | return !!ATTR_TRUE(value); |
| 1281 | } |
| 1282 | |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1283 | struct conv_attrs { |
| 1284 | struct convert_driver *drv; |
Torsten Bögershausen | bb211b4 | 2016-02-05 17:13:23 +0100 | [diff] [blame] | 1285 | enum crlf_action attr_action; /* What attr says */ |
| 1286 | 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] | 1287 | int ident; |
Lars Schneider | 107642f | 2018-04-15 20:16:07 +0200 | [diff] [blame] | 1288 | const char *working_tree_encoding; /* Supported encoding or default encoding if NULL */ |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1289 | }; |
| 1290 | |
brian m. carlson | 2c65d90 | 2019-09-02 22:39:44 +0000 | [diff] [blame] | 1291 | static struct attr_check *check; |
| 1292 | |
Nguyễn Thái Ngọc Duy | 7f944e2 | 2018-08-13 18:14:21 +0200 | [diff] [blame] | 1293 | static void convert_attrs(const struct index_state *istate, |
| 1294 | struct conv_attrs *ca, const char *path) |
Junio C Hamano | 8329596 | 2011-05-09 11:23:04 -0700 | [diff] [blame] | 1295 | { |
Torsten Bögershausen | d64324c | 2018-09-12 21:32:02 +0200 | [diff] [blame] | 1296 | struct attr_check_item *ccheck = NULL; |
Junio C Hamano | 8329596 | 2011-05-09 11:23:04 -0700 | [diff] [blame] | 1297 | |
Junio C Hamano | 2aef63d | 2017-01-27 18:01:57 -0800 | [diff] [blame] | 1298 | if (!check) { |
| 1299 | check = attr_check_initl("crlf", "ident", "filter", |
Lars Schneider | 107642f | 2018-04-15 20:16:07 +0200 | [diff] [blame] | 1300 | "eol", "text", "working-tree-encoding", |
| 1301 | NULL); |
Junio C Hamano | 8329596 | 2011-05-09 11:23:04 -0700 | [diff] [blame] | 1302 | user_convert_tail = &user_convert; |
| 1303 | git_config(read_convert_config, NULL); |
| 1304 | } |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1305 | |
Torsten Bögershausen | d64324c | 2018-09-12 21:32:02 +0200 | [diff] [blame] | 1306 | git_check_attr(istate, path, check); |
| 1307 | ccheck = check->items; |
| 1308 | ca->crlf_action = git_path_check_crlf(ccheck + 4); |
| 1309 | if (ca->crlf_action == CRLF_UNDEFINED) |
| 1310 | ca->crlf_action = git_path_check_crlf(ccheck + 0); |
| 1311 | ca->ident = git_path_check_ident(ccheck + 1); |
| 1312 | ca->drv = git_path_check_convert(ccheck + 2); |
| 1313 | if (ca->crlf_action != CRLF_BINARY) { |
| 1314 | enum eol eol_attr = git_path_check_eol(ccheck + 3); |
| 1315 | if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_LF) |
| 1316 | ca->crlf_action = CRLF_AUTO_INPUT; |
| 1317 | else if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_CRLF) |
| 1318 | ca->crlf_action = CRLF_AUTO_CRLF; |
| 1319 | else if (eol_attr == EOL_LF) |
| 1320 | ca->crlf_action = CRLF_TEXT_INPUT; |
| 1321 | else if (eol_attr == EOL_CRLF) |
| 1322 | ca->crlf_action = CRLF_TEXT_CRLF; |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1323 | } |
Torsten Bögershausen | d64324c | 2018-09-12 21:32:02 +0200 | [diff] [blame] | 1324 | ca->working_tree_encoding = git_path_check_encoding(ccheck + 5); |
Martin Ågren | 5c94c93 | 2017-08-21 19:43:45 +0200 | [diff] [blame] | 1325 | |
| 1326 | /* Save attr and make a decision for action */ |
| 1327 | ca->attr_action = ca->crlf_action; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 1328 | if (ca->crlf_action == CRLF_TEXT) |
| 1329 | ca->crlf_action = text_eol_is_crlf() ? CRLF_TEXT_CRLF : CRLF_TEXT_INPUT; |
| 1330 | if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_FALSE) |
| 1331 | ca->crlf_action = CRLF_BINARY; |
| 1332 | if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_TRUE) |
| 1333 | ca->crlf_action = CRLF_AUTO_CRLF; |
| 1334 | if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_INPUT) |
| 1335 | ca->crlf_action = CRLF_AUTO_INPUT; |
Junio C Hamano | 8329596 | 2011-05-09 11:23:04 -0700 | [diff] [blame] | 1336 | } |
| 1337 | |
brian m. carlson | 2c65d90 | 2019-09-02 22:39:44 +0000 | [diff] [blame] | 1338 | void reset_parsed_attributes(void) |
| 1339 | { |
| 1340 | struct convert_driver *drv, *next; |
| 1341 | |
| 1342 | attr_check_free(check); |
| 1343 | check = NULL; |
| 1344 | reset_merge_attributes(); |
| 1345 | |
| 1346 | for (drv = user_convert; drv; drv = next) { |
| 1347 | next = drv->next; |
| 1348 | free((void *)drv->name); |
| 1349 | free(drv); |
| 1350 | } |
| 1351 | user_convert = NULL; |
| 1352 | user_convert_tail = NULL; |
| 1353 | } |
| 1354 | |
Nguyễn Thái Ngọc Duy | 7f944e2 | 2018-08-13 18:14:21 +0200 | [diff] [blame] | 1355 | int would_convert_to_git_filter_fd(const struct index_state *istate, const char *path) |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 1356 | { |
| 1357 | struct conv_attrs ca; |
| 1358 | |
Nguyễn Thái Ngọc Duy | 7f944e2 | 2018-08-13 18:14:21 +0200 | [diff] [blame] | 1359 | convert_attrs(istate, &ca, path); |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 1360 | if (!ca.drv) |
| 1361 | return 0; |
| 1362 | |
| 1363 | /* |
| 1364 | * Apply a filter to an fd only if the filter is required to succeed. |
| 1365 | * We must die if the filter fails, because the original data before |
| 1366 | * filtering is not available. |
| 1367 | */ |
| 1368 | if (!ca.drv->required) |
| 1369 | return 0; |
| 1370 | |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 1371 | return apply_filter(path, NULL, 0, -1, NULL, ca.drv, CAP_CLEAN, NULL); |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 1372 | } |
| 1373 | |
Nguyễn Thái Ngọc Duy | 7f944e2 | 2018-08-13 18:14:21 +0200 | [diff] [blame] | 1374 | const char *get_convert_attr_ascii(const struct index_state *istate, const char *path) |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 1375 | { |
| 1376 | struct conv_attrs ca; |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 1377 | |
Nguyễn Thái Ngọc Duy | 7f944e2 | 2018-08-13 18:14:21 +0200 | [diff] [blame] | 1378 | convert_attrs(istate, &ca, path); |
Torsten Bögershausen | bb211b4 | 2016-02-05 17:13:23 +0100 | [diff] [blame] | 1379 | switch (ca.attr_action) { |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 1380 | case CRLF_UNDEFINED: |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 1381 | return ""; |
| 1382 | case CRLF_BINARY: |
| 1383 | return "-text"; |
| 1384 | case CRLF_TEXT: |
| 1385 | return "text"; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 1386 | case CRLF_TEXT_INPUT: |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 1387 | return "text eol=lf"; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 1388 | case CRLF_TEXT_CRLF: |
| 1389 | return "text eol=crlf"; |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 1390 | case CRLF_AUTO: |
| 1391 | return "text=auto"; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 1392 | case CRLF_AUTO_CRLF: |
Torsten Bögershausen | 6523728 | 2016-06-28 10:01:13 +0200 | [diff] [blame] | 1393 | return "text=auto eol=crlf"; |
Torsten Bögershausen | df747b8 | 2016-02-10 17:24:41 +0100 | [diff] [blame] | 1394 | case CRLF_AUTO_INPUT: |
Torsten Bögershausen | 6523728 | 2016-06-28 10:01:13 +0200 | [diff] [blame] | 1395 | return "text=auto eol=lf"; |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 1396 | } |
| 1397 | return ""; |
| 1398 | } |
| 1399 | |
Brandon Williams | 82b474e | 2017-06-12 15:13:55 -0700 | [diff] [blame] | 1400 | int convert_to_git(const struct index_state *istate, |
| 1401 | const char *path, const char *src, size_t len, |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 1402 | struct strbuf *dst, int conv_flags) |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 1403 | { |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1404 | int ret = 0; |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1405 | struct conv_attrs ca; |
Junio C Hamano | 6073ee8 | 2007-04-20 23:44:02 -0700 | [diff] [blame] | 1406 | |
Nguyễn Thái Ngọc Duy | 7f944e2 | 2018-08-13 18:14:21 +0200 | [diff] [blame] | 1407 | convert_attrs(istate, &ca, path); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1408 | |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 1409 | ret |= apply_filter(path, src, len, -1, dst, ca.drv, CAP_CLEAN, NULL); |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 1410 | if (!ret && ca.drv && ca.drv->required) |
Nguyễn Thái Ngọc Duy | d26a328 | 2018-07-21 09:49:29 +0200 | [diff] [blame] | 1411 | die(_("%s: clean filter '%s' failed"), path, ca.drv->name); |
Jehan Bing | 36daaac | 2012-02-16 17:19:03 -0800 | [diff] [blame] | 1412 | |
Jeff King | 92ac319 | 2012-02-24 17:02:37 -0500 | [diff] [blame] | 1413 | if (ret && dst) { |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1414 | src = dst->buf; |
| 1415 | len = dst->len; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 1416 | } |
Lars Schneider | 107642f | 2018-04-15 20:16:07 +0200 | [diff] [blame] | 1417 | |
| 1418 | ret |= encode_to_git(path, src, len, dst, ca.working_tree_encoding, conv_flags); |
| 1419 | if (ret && dst) { |
| 1420 | src = dst->buf; |
| 1421 | len = dst->len; |
| 1422 | } |
| 1423 | |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 1424 | if (!(conv_flags & CONV_EOL_KEEP_CRLF)) { |
| 1425 | ret |= crlf_to_git(istate, path, src, len, dst, ca.crlf_action, conv_flags); |
Torsten Bögershausen | 2fea9de | 2017-08-13 10:51:04 +0200 | [diff] [blame] | 1426 | if (ret && dst) { |
| 1427 | src = dst->buf; |
| 1428 | len = dst->len; |
| 1429 | } |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1430 | } |
Jeff King | 55ad152 | 2019-01-24 08:12:51 -0500 | [diff] [blame] | 1431 | return ret | ident_to_git(src, len, dst, ca.ident); |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
Brandon Williams | d6c41c2 | 2017-06-12 15:13:54 -0700 | [diff] [blame] | 1434 | void convert_to_git_filter_fd(const struct index_state *istate, |
| 1435 | const char *path, int fd, struct strbuf *dst, |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 1436 | int conv_flags) |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 1437 | { |
| 1438 | struct conv_attrs ca; |
Nguyễn Thái Ngọc Duy | 7f944e2 | 2018-08-13 18:14:21 +0200 | [diff] [blame] | 1439 | convert_attrs(istate, &ca, path); |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 1440 | |
| 1441 | assert(ca.drv); |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 1442 | assert(ca.drv->clean || ca.drv->process); |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 1443 | |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 1444 | if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN, NULL)) |
Nguyễn Thái Ngọc Duy | d26a328 | 2018-07-21 09:49:29 +0200 | [diff] [blame] | 1445 | die(_("%s: clean filter '%s' failed"), path, ca.drv->name); |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 1446 | |
Lars Schneider | 107642f | 2018-04-15 20:16:07 +0200 | [diff] [blame] | 1447 | encode_to_git(path, dst->buf, dst->len, dst, ca.working_tree_encoding, conv_flags); |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 1448 | crlf_to_git(istate, path, dst->buf, dst->len, dst, ca.crlf_action, conv_flags); |
Jeff King | 55ad152 | 2019-01-24 08:12:51 -0500 | [diff] [blame] | 1449 | ident_to_git(dst->buf, dst->len, dst, ca.ident); |
Steffen Prohaska | 9035d75 | 2014-08-26 17:23:25 +0200 | [diff] [blame] | 1450 | } |
| 1451 | |
Nguyễn Thái Ngọc Duy | 7f944e2 | 2018-08-13 18:14:21 +0200 | [diff] [blame] | 1452 | static int convert_to_working_tree_internal(const struct index_state *istate, |
| 1453 | const char *path, const char *src, |
Eyvind Bernhardsen | 43dd233 | 2010-07-02 21:20:49 +0200 | [diff] [blame] | 1454 | size_t len, struct strbuf *dst, |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 1455 | int normalizing, struct delayed_checkout *dco) |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 1456 | { |
Jehan Bing | 36daaac | 2012-02-16 17:19:03 -0800 | [diff] [blame] | 1457 | int ret = 0, ret_filter = 0; |
Junio C Hamano | 3bfba20 | 2011-05-09 13:58:31 -0700 | [diff] [blame] | 1458 | struct conv_attrs ca; |
Junio C Hamano | 6073ee8 | 2007-04-20 23:44:02 -0700 | [diff] [blame] | 1459 | |
Nguyễn Thái Ngọc Duy | 7f944e2 | 2018-08-13 18:14:21 +0200 | [diff] [blame] | 1460 | convert_attrs(istate, &ca, path); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1461 | |
Jeff King | 55ad152 | 2019-01-24 08:12:51 -0500 | [diff] [blame] | 1462 | ret |= ident_to_worktree(src, len, dst, ca.ident); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 1463 | if (ret) { |
| 1464 | src = dst->buf; |
| 1465 | len = dst->len; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 1466 | } |
Eyvind Bernhardsen | 43dd233 | 2010-07-02 21:20:49 +0200 | [diff] [blame] | 1467 | /* |
| 1468 | * CRLF conversion can be skipped if normalizing, unless there |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 1469 | * is a smudge or process filter (even if the process filter doesn't |
| 1470 | * support smudge). The filters might expect CRLFs. |
Eyvind Bernhardsen | 43dd233 | 2010-07-02 21:20:49 +0200 | [diff] [blame] | 1471 | */ |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 1472 | if ((ca.drv && (ca.drv->smudge || ca.drv->process)) || !normalizing) { |
Jeff King | 55ad152 | 2019-01-24 08:12:51 -0500 | [diff] [blame] | 1473 | ret |= crlf_to_worktree(src, len, dst, ca.crlf_action); |
Eyvind Bernhardsen | 43dd233 | 2010-07-02 21:20:49 +0200 | [diff] [blame] | 1474 | if (ret) { |
| 1475 | src = dst->buf; |
| 1476 | len = dst->len; |
| 1477 | } |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 1478 | } |
Jehan Bing | 36daaac | 2012-02-16 17:19:03 -0800 | [diff] [blame] | 1479 | |
Lars Schneider | 107642f | 2018-04-15 20:16:07 +0200 | [diff] [blame] | 1480 | ret |= encode_to_worktree(path, src, len, dst, ca.working_tree_encoding); |
| 1481 | if (ret) { |
| 1482 | src = dst->buf; |
| 1483 | len = dst->len; |
| 1484 | } |
| 1485 | |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 1486 | ret_filter = apply_filter( |
| 1487 | path, src, len, -1, dst, ca.drv, CAP_SMUDGE, dco); |
Lars Schneider | 234fa07 | 2016-10-16 16:20:36 -0700 | [diff] [blame] | 1488 | if (!ret_filter && ca.drv && ca.drv->required) |
Nguyễn Thái Ngọc Duy | d26a328 | 2018-07-21 09:49:29 +0200 | [diff] [blame] | 1489 | die(_("%s: smudge filter %s failed"), path, ca.drv->name); |
Jehan Bing | 36daaac | 2012-02-16 17:19:03 -0800 | [diff] [blame] | 1490 | |
| 1491 | return ret | ret_filter; |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 1492 | } |
Eyvind Bernhardsen | f217f0e | 2010-07-02 21:20:47 +0200 | [diff] [blame] | 1493 | |
Nguyễn Thái Ngọc Duy | 7f944e2 | 2018-08-13 18:14:21 +0200 | [diff] [blame] | 1494 | int async_convert_to_working_tree(const struct index_state *istate, |
| 1495 | const char *path, const char *src, |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 1496 | size_t len, struct strbuf *dst, |
| 1497 | void *dco) |
| 1498 | { |
Nguyễn Thái Ngọc Duy | 7f944e2 | 2018-08-13 18:14:21 +0200 | [diff] [blame] | 1499 | return convert_to_working_tree_internal(istate, path, src, len, dst, 0, dco); |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 1500 | } |
| 1501 | |
Nguyễn Thái Ngọc Duy | 7f944e2 | 2018-08-13 18:14:21 +0200 | [diff] [blame] | 1502 | int convert_to_working_tree(const struct index_state *istate, |
| 1503 | const char *path, const char *src, |
| 1504 | size_t len, struct strbuf *dst) |
Eyvind Bernhardsen | 43dd233 | 2010-07-02 21:20:49 +0200 | [diff] [blame] | 1505 | { |
Nguyễn Thái Ngọc Duy | 7f944e2 | 2018-08-13 18:14:21 +0200 | [diff] [blame] | 1506 | return convert_to_working_tree_internal(istate, path, src, len, dst, 0, NULL); |
Eyvind Bernhardsen | 43dd233 | 2010-07-02 21:20:49 +0200 | [diff] [blame] | 1507 | } |
| 1508 | |
Brandon Williams | a33e0b2 | 2017-06-12 15:13:56 -0700 | [diff] [blame] | 1509 | int renormalize_buffer(const struct index_state *istate, const char *path, |
| 1510 | const char *src, size_t len, struct strbuf *dst) |
Eyvind Bernhardsen | f217f0e | 2010-07-02 21:20:47 +0200 | [diff] [blame] | 1511 | { |
Nguyễn Thái Ngọc Duy | 7f944e2 | 2018-08-13 18:14:21 +0200 | [diff] [blame] | 1512 | int ret = convert_to_working_tree_internal(istate, path, src, len, dst, 1, NULL); |
Eyvind Bernhardsen | f217f0e | 2010-07-02 21:20:47 +0200 | [diff] [blame] | 1513 | if (ret) { |
| 1514 | src = dst->buf; |
| 1515 | len = dst->len; |
| 1516 | } |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 1517 | return ret | convert_to_git(istate, path, src, len, dst, CONV_EOL_RENORMALIZE); |
Eyvind Bernhardsen | f217f0e | 2010-07-02 21:20:47 +0200 | [diff] [blame] | 1518 | } |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 1519 | |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 1520 | /***************************************************************** |
| 1521 | * |
Ondřej Bílka | 749f763 | 2013-07-22 23:02:23 +0200 | [diff] [blame] | 1522 | * Streaming conversion support |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 1523 | * |
| 1524 | *****************************************************************/ |
| 1525 | |
| 1526 | typedef int (*filter_fn)(struct stream_filter *, |
| 1527 | const char *input, size_t *isize_p, |
| 1528 | char *output, size_t *osize_p); |
| 1529 | typedef void (*free_fn)(struct stream_filter *); |
| 1530 | |
| 1531 | struct stream_filter_vtbl { |
| 1532 | filter_fn filter; |
| 1533 | free_fn free; |
| 1534 | }; |
| 1535 | |
| 1536 | struct stream_filter { |
| 1537 | struct stream_filter_vtbl *vtbl; |
| 1538 | }; |
| 1539 | |
| 1540 | static int null_filter_fn(struct stream_filter *filter, |
| 1541 | const char *input, size_t *isize_p, |
| 1542 | char *output, size_t *osize_p) |
| 1543 | { |
Junio C Hamano | 4ae6670 | 2011-05-21 14:05:51 -0700 | [diff] [blame] | 1544 | size_t count; |
| 1545 | |
| 1546 | if (!input) |
| 1547 | return 0; /* we do not keep any states */ |
| 1548 | count = *isize_p; |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 1549 | if (*osize_p < count) |
| 1550 | count = *osize_p; |
| 1551 | if (count) { |
| 1552 | memmove(output, input, count); |
| 1553 | *isize_p -= count; |
| 1554 | *osize_p -= count; |
| 1555 | } |
| 1556 | return 0; |
| 1557 | } |
| 1558 | |
| 1559 | static void null_free_fn(struct stream_filter *filter) |
| 1560 | { |
| 1561 | ; /* nothing -- null instances are shared */ |
| 1562 | } |
| 1563 | |
| 1564 | static struct stream_filter_vtbl null_vtbl = { |
| 1565 | null_filter_fn, |
| 1566 | null_free_fn, |
| 1567 | }; |
| 1568 | |
| 1569 | static struct stream_filter null_filter_singleton = { |
| 1570 | &null_vtbl, |
| 1571 | }; |
| 1572 | |
| 1573 | int is_null_stream_filter(struct stream_filter *filter) |
| 1574 | { |
| 1575 | return filter == &null_filter_singleton; |
| 1576 | } |
| 1577 | |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1578 | |
| 1579 | /* |
| 1580 | * LF-to-CRLF filter |
| 1581 | */ |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1582 | |
| 1583 | struct lf_to_crlf_filter { |
| 1584 | struct stream_filter filter; |
Junio C Hamano | 8496f56 | 2011-12-16 15:44:18 -0800 | [diff] [blame] | 1585 | unsigned has_held:1; |
| 1586 | char held; |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1587 | }; |
| 1588 | |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1589 | static int lf_to_crlf_filter_fn(struct stream_filter *filter, |
| 1590 | const char *input, size_t *isize_p, |
| 1591 | char *output, size_t *osize_p) |
| 1592 | { |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1593 | size_t count, o = 0; |
| 1594 | 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] | 1595 | |
Junio C Hamano | 8496f56 | 2011-12-16 15:44:18 -0800 | [diff] [blame] | 1596 | /* |
| 1597 | * We may be holding onto the CR to see if it is followed by a |
| 1598 | * LF, in which case we would need to go to the main loop. |
| 1599 | * Otherwise, just emit it to the output stream. |
| 1600 | */ |
| 1601 | if (lf_to_crlf->has_held && (lf_to_crlf->held != '\r' || !input)) { |
| 1602 | output[o++] = lf_to_crlf->held; |
| 1603 | lf_to_crlf->has_held = 0; |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1604 | } |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1605 | |
Junio C Hamano | 87afe9a | 2011-12-16 14:39:37 -0800 | [diff] [blame] | 1606 | /* We are told to drain */ |
| 1607 | if (!input) { |
| 1608 | *osize_p -= o; |
| 1609 | return 0; |
| 1610 | } |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1611 | |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1612 | count = *isize_p; |
Junio C Hamano | 8496f56 | 2011-12-16 15:44:18 -0800 | [diff] [blame] | 1613 | if (count || lf_to_crlf->has_held) { |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1614 | size_t i; |
Junio C Hamano | 8496f56 | 2011-12-16 15:44:18 -0800 | [diff] [blame] | 1615 | int was_cr = 0; |
| 1616 | |
| 1617 | if (lf_to_crlf->has_held) { |
| 1618 | was_cr = 1; |
| 1619 | lf_to_crlf->has_held = 0; |
| 1620 | } |
| 1621 | |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1622 | for (i = 0; o < *osize_p && i < count; i++) { |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1623 | char ch = input[i]; |
Junio C Hamano | 8496f56 | 2011-12-16 15:44:18 -0800 | [diff] [blame] | 1624 | |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1625 | if (ch == '\n') { |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1626 | output[o++] = '\r'; |
Junio C Hamano | 8496f56 | 2011-12-16 15:44:18 -0800 | [diff] [blame] | 1627 | } else if (was_cr) { |
| 1628 | /* |
| 1629 | * Previous round saw CR and it is not followed |
| 1630 | * by a LF; emit the CR before processing the |
| 1631 | * current character. |
| 1632 | */ |
| 1633 | output[o++] = '\r'; |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1634 | } |
Junio C Hamano | 8496f56 | 2011-12-16 15:44:18 -0800 | [diff] [blame] | 1635 | |
| 1636 | /* |
| 1637 | * We may have consumed the last output slot, |
| 1638 | * in which case we need to break out of this |
| 1639 | * loop; hold the current character before |
| 1640 | * returning. |
| 1641 | */ |
| 1642 | if (*osize_p <= o) { |
| 1643 | lf_to_crlf->has_held = 1; |
| 1644 | lf_to_crlf->held = ch; |
| 1645 | continue; /* break but increment i */ |
| 1646 | } |
| 1647 | |
| 1648 | if (ch == '\r') { |
| 1649 | was_cr = 1; |
| 1650 | continue; |
| 1651 | } |
| 1652 | |
| 1653 | was_cr = 0; |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1654 | output[o++] = ch; |
| 1655 | } |
| 1656 | |
| 1657 | *osize_p -= o; |
| 1658 | *isize_p -= i; |
Junio C Hamano | 8496f56 | 2011-12-16 15:44:18 -0800 | [diff] [blame] | 1659 | |
| 1660 | if (!lf_to_crlf->has_held && was_cr) { |
| 1661 | lf_to_crlf->has_held = 1; |
| 1662 | lf_to_crlf->held = '\r'; |
| 1663 | } |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1664 | } |
| 1665 | return 0; |
| 1666 | } |
| 1667 | |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1668 | static void lf_to_crlf_free_fn(struct stream_filter *filter) |
| 1669 | { |
| 1670 | free(filter); |
| 1671 | } |
| 1672 | |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1673 | static struct stream_filter_vtbl lf_to_crlf_vtbl = { |
| 1674 | lf_to_crlf_filter_fn, |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1675 | lf_to_crlf_free_fn, |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1676 | }; |
| 1677 | |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1678 | static struct stream_filter *lf_to_crlf_filter(void) |
| 1679 | { |
Junio C Hamano | 87afe9a | 2011-12-16 14:39:37 -0800 | [diff] [blame] | 1680 | 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] | 1681 | |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1682 | lf_to_crlf->filter.vtbl = &lf_to_crlf_vtbl; |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1683 | return (struct stream_filter *)lf_to_crlf; |
| 1684 | } |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1685 | |
| 1686 | /* |
Junio C Hamano | a265a7f | 2011-05-21 18:28:41 -0700 | [diff] [blame] | 1687 | * Cascade filter |
| 1688 | */ |
| 1689 | #define FILTER_BUFFER 1024 |
| 1690 | struct cascade_filter { |
| 1691 | struct stream_filter filter; |
| 1692 | struct stream_filter *one; |
| 1693 | struct stream_filter *two; |
| 1694 | char buf[FILTER_BUFFER]; |
| 1695 | int end, ptr; |
| 1696 | }; |
| 1697 | |
| 1698 | static int cascade_filter_fn(struct stream_filter *filter, |
| 1699 | const char *input, size_t *isize_p, |
| 1700 | char *output, size_t *osize_p) |
| 1701 | { |
| 1702 | struct cascade_filter *cas = (struct cascade_filter *) filter; |
| 1703 | size_t filled = 0; |
| 1704 | size_t sz = *osize_p; |
| 1705 | size_t to_feed, remaining; |
| 1706 | |
| 1707 | /* |
| 1708 | * input -- (one) --> buf -- (two) --> output |
| 1709 | */ |
| 1710 | while (filled < sz) { |
| 1711 | remaining = sz - filled; |
| 1712 | |
| 1713 | /* do we already have something to feed two with? */ |
| 1714 | if (cas->ptr < cas->end) { |
| 1715 | to_feed = cas->end - cas->ptr; |
| 1716 | if (stream_filter(cas->two, |
| 1717 | cas->buf + cas->ptr, &to_feed, |
| 1718 | output + filled, &remaining)) |
| 1719 | return -1; |
| 1720 | cas->ptr += (cas->end - cas->ptr) - to_feed; |
| 1721 | filled = sz - remaining; |
| 1722 | continue; |
| 1723 | } |
| 1724 | |
| 1725 | /* feed one from upstream and have it emit into our buffer */ |
| 1726 | to_feed = input ? *isize_p : 0; |
| 1727 | if (input && !to_feed) |
| 1728 | break; |
| 1729 | remaining = sizeof(cas->buf); |
| 1730 | if (stream_filter(cas->one, |
| 1731 | input, &to_feed, |
| 1732 | cas->buf, &remaining)) |
| 1733 | return -1; |
| 1734 | cas->end = sizeof(cas->buf) - remaining; |
| 1735 | cas->ptr = 0; |
| 1736 | if (input) { |
| 1737 | size_t fed = *isize_p - to_feed; |
| 1738 | *isize_p -= fed; |
| 1739 | input += fed; |
| 1740 | } |
| 1741 | |
| 1742 | /* do we know that we drained one completely? */ |
| 1743 | if (input || cas->end) |
| 1744 | continue; |
| 1745 | |
| 1746 | /* tell two to drain; we have nothing more to give it */ |
| 1747 | to_feed = 0; |
| 1748 | remaining = sz - filled; |
| 1749 | if (stream_filter(cas->two, |
| 1750 | NULL, &to_feed, |
| 1751 | output + filled, &remaining)) |
| 1752 | return -1; |
| 1753 | if (remaining == (sz - filled)) |
| 1754 | break; /* completely drained two */ |
| 1755 | filled = sz - remaining; |
| 1756 | } |
| 1757 | *osize_p -= filled; |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
| 1761 | static void cascade_free_fn(struct stream_filter *filter) |
| 1762 | { |
| 1763 | struct cascade_filter *cas = (struct cascade_filter *)filter; |
| 1764 | free_stream_filter(cas->one); |
| 1765 | free_stream_filter(cas->two); |
| 1766 | free(filter); |
| 1767 | } |
| 1768 | |
| 1769 | static struct stream_filter_vtbl cascade_vtbl = { |
| 1770 | cascade_filter_fn, |
| 1771 | cascade_free_fn, |
| 1772 | }; |
| 1773 | |
| 1774 | static struct stream_filter *cascade_filter(struct stream_filter *one, |
| 1775 | struct stream_filter *two) |
| 1776 | { |
| 1777 | struct cascade_filter *cascade; |
| 1778 | |
| 1779 | if (!one || is_null_stream_filter(one)) |
| 1780 | return two; |
| 1781 | if (!two || is_null_stream_filter(two)) |
| 1782 | return one; |
| 1783 | |
| 1784 | cascade = xmalloc(sizeof(*cascade)); |
| 1785 | cascade->one = one; |
| 1786 | cascade->two = two; |
| 1787 | cascade->end = cascade->ptr = 0; |
| 1788 | cascade->filter.vtbl = &cascade_vtbl; |
| 1789 | return (struct stream_filter *)cascade; |
| 1790 | } |
| 1791 | |
| 1792 | /* |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1793 | * ident filter |
| 1794 | */ |
| 1795 | #define IDENT_DRAINING (-1) |
| 1796 | #define IDENT_SKIPPING (-2) |
| 1797 | struct ident_filter { |
| 1798 | struct stream_filter filter; |
| 1799 | struct strbuf left; |
| 1800 | int state; |
brian m. carlson | 1a75044 | 2018-03-12 02:27:56 +0000 | [diff] [blame] | 1801 | char ident[GIT_MAX_HEXSZ + 5]; /* ": x40 $" */ |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1802 | }; |
| 1803 | |
| 1804 | static int is_foreign_ident(const char *str) |
| 1805 | { |
| 1806 | int i; |
| 1807 | |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 1808 | if (!skip_prefix(str, "$Id: ", &str)) |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1809 | return 0; |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 1810 | for (i = 0; str[i]; i++) { |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1811 | if (isspace(str[i]) && str[i+1] != '$') |
| 1812 | return 1; |
| 1813 | } |
| 1814 | return 0; |
| 1815 | } |
| 1816 | |
| 1817 | static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p) |
| 1818 | { |
| 1819 | size_t to_drain = ident->left.len; |
| 1820 | |
| 1821 | if (*osize_p < to_drain) |
| 1822 | to_drain = *osize_p; |
| 1823 | if (to_drain) { |
| 1824 | memcpy(*output_p, ident->left.buf, to_drain); |
| 1825 | strbuf_remove(&ident->left, 0, to_drain); |
| 1826 | *output_p += to_drain; |
| 1827 | *osize_p -= to_drain; |
| 1828 | } |
| 1829 | if (!ident->left.len) |
| 1830 | ident->state = 0; |
| 1831 | } |
| 1832 | |
| 1833 | static int ident_filter_fn(struct stream_filter *filter, |
| 1834 | const char *input, size_t *isize_p, |
| 1835 | char *output, size_t *osize_p) |
| 1836 | { |
| 1837 | struct ident_filter *ident = (struct ident_filter *)filter; |
| 1838 | static const char head[] = "$Id"; |
| 1839 | |
| 1840 | if (!input) { |
| 1841 | /* drain upon eof */ |
| 1842 | switch (ident->state) { |
| 1843 | default: |
| 1844 | strbuf_add(&ident->left, head, ident->state); |
Jeff King | 1cf01a3 | 2017-09-21 02:25:41 -0400 | [diff] [blame] | 1845 | /* fallthrough */ |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1846 | case IDENT_SKIPPING: |
Jeff King | 1cf01a3 | 2017-09-21 02:25:41 -0400 | [diff] [blame] | 1847 | /* fallthrough */ |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1848 | case IDENT_DRAINING: |
| 1849 | ident_drain(ident, &output, osize_p); |
| 1850 | } |
| 1851 | return 0; |
| 1852 | } |
| 1853 | |
| 1854 | while (*isize_p || (ident->state == IDENT_DRAINING)) { |
| 1855 | int ch; |
| 1856 | |
| 1857 | if (ident->state == IDENT_DRAINING) { |
| 1858 | ident_drain(ident, &output, osize_p); |
| 1859 | if (!*osize_p) |
| 1860 | break; |
| 1861 | continue; |
| 1862 | } |
| 1863 | |
| 1864 | ch = *(input++); |
| 1865 | (*isize_p)--; |
| 1866 | |
| 1867 | if (ident->state == IDENT_SKIPPING) { |
| 1868 | /* |
| 1869 | * Skipping until '$' or LF, but keeping them |
| 1870 | * in case it is a foreign ident. |
| 1871 | */ |
| 1872 | strbuf_addch(&ident->left, ch); |
| 1873 | if (ch != '\n' && ch != '$') |
| 1874 | continue; |
| 1875 | if (ch == '$' && !is_foreign_ident(ident->left.buf)) { |
| 1876 | strbuf_setlen(&ident->left, sizeof(head) - 1); |
| 1877 | strbuf_addstr(&ident->left, ident->ident); |
| 1878 | } |
| 1879 | ident->state = IDENT_DRAINING; |
| 1880 | continue; |
| 1881 | } |
| 1882 | |
| 1883 | if (ident->state < sizeof(head) && |
| 1884 | head[ident->state] == ch) { |
| 1885 | ident->state++; |
| 1886 | continue; |
| 1887 | } |
| 1888 | |
| 1889 | if (ident->state) |
| 1890 | strbuf_add(&ident->left, head, ident->state); |
| 1891 | if (ident->state == sizeof(head) - 1) { |
| 1892 | if (ch != ':' && ch != '$') { |
| 1893 | strbuf_addch(&ident->left, ch); |
| 1894 | ident->state = 0; |
| 1895 | continue; |
| 1896 | } |
| 1897 | |
| 1898 | if (ch == ':') { |
| 1899 | strbuf_addch(&ident->left, ch); |
| 1900 | ident->state = IDENT_SKIPPING; |
| 1901 | } else { |
| 1902 | strbuf_addstr(&ident->left, ident->ident); |
| 1903 | ident->state = IDENT_DRAINING; |
| 1904 | } |
| 1905 | continue; |
| 1906 | } |
| 1907 | |
| 1908 | strbuf_addch(&ident->left, ch); |
| 1909 | ident->state = IDENT_DRAINING; |
| 1910 | } |
| 1911 | return 0; |
| 1912 | } |
| 1913 | |
| 1914 | static void ident_free_fn(struct stream_filter *filter) |
| 1915 | { |
| 1916 | struct ident_filter *ident = (struct ident_filter *)filter; |
| 1917 | strbuf_release(&ident->left); |
| 1918 | free(filter); |
| 1919 | } |
| 1920 | |
| 1921 | static struct stream_filter_vtbl ident_vtbl = { |
| 1922 | ident_filter_fn, |
| 1923 | ident_free_fn, |
| 1924 | }; |
| 1925 | |
brian m. carlson | 1a75044 | 2018-03-12 02:27:56 +0000 | [diff] [blame] | 1926 | static struct stream_filter *ident_filter(const struct object_id *oid) |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1927 | { |
| 1928 | struct ident_filter *ident = xmalloc(sizeof(*ident)); |
| 1929 | |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 1930 | xsnprintf(ident->ident, sizeof(ident->ident), |
brian m. carlson | 1a75044 | 2018-03-12 02:27:56 +0000 | [diff] [blame] | 1931 | ": %s $", oid_to_hex(oid)); |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1932 | strbuf_init(&ident->left, 0); |
| 1933 | ident->filter.vtbl = &ident_vtbl; |
| 1934 | ident->state = 0; |
| 1935 | return (struct stream_filter *)ident; |
| 1936 | } |
| 1937 | |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 1938 | /* |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 1939 | * Return an appropriately constructed filter for the path, or NULL if |
| 1940 | * the contents cannot be filtered without reading the whole thing |
| 1941 | * in-core. |
| 1942 | * |
| 1943 | * Note that you would be crazy to set CRLF, smuge/clean or ident to a |
| 1944 | * 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] | 1945 | */ |
Nguyễn Thái Ngọc Duy | 7f944e2 | 2018-08-13 18:14:21 +0200 | [diff] [blame] | 1946 | struct stream_filter *get_stream_filter(const struct index_state *istate, |
| 1947 | const char *path, |
| 1948 | const struct object_id *oid) |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 1949 | { |
| 1950 | struct conv_attrs ca; |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1951 | struct stream_filter *filter = NULL; |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 1952 | |
Nguyễn Thái Ngọc Duy | 7f944e2 | 2018-08-13 18:14:21 +0200 | [diff] [blame] | 1953 | convert_attrs(istate, &ca, path); |
Lars Schneider | edcc858 | 2016-10-16 16:20:37 -0700 | [diff] [blame] | 1954 | 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] | 1955 | return NULL; |
| 1956 | |
Lars Schneider | 107642f | 2018-04-15 20:16:07 +0200 | [diff] [blame] | 1957 | if (ca.working_tree_encoding) |
| 1958 | return NULL; |
| 1959 | |
Torsten Bögershausen | caa47ad | 2016-04-25 18:56:32 +0200 | [diff] [blame] | 1960 | if (ca.crlf_action == CRLF_AUTO || ca.crlf_action == CRLF_AUTO_CRLF) |
| 1961 | return NULL; |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1962 | |
| 1963 | if (ca.ident) |
brian m. carlson | 1a75044 | 2018-03-12 02:27:56 +0000 | [diff] [blame] | 1964 | filter = ident_filter(oid); |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 1965 | |
Torsten Bögershausen | caa47ad | 2016-04-25 18:56:32 +0200 | [diff] [blame] | 1966 | if (output_eol(ca.crlf_action) == EOL_CRLF) |
Carlos Martín Nieto | 284e3d2 | 2011-11-28 11:48:12 +0100 | [diff] [blame] | 1967 | filter = cascade_filter(filter, lf_to_crlf_filter()); |
Torsten Bögershausen | caa47ad | 2016-04-25 18:56:32 +0200 | [diff] [blame] | 1968 | else |
| 1969 | filter = cascade_filter(filter, &null_filter_singleton); |
Junio C Hamano | e322ee3 | 2011-05-20 16:47:56 -0700 | [diff] [blame] | 1970 | |
Junio C Hamano | b84c7839 | 2011-05-20 18:28:00 -0700 | [diff] [blame] | 1971 | return filter; |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | void free_stream_filter(struct stream_filter *filter) |
| 1975 | { |
| 1976 | filter->vtbl->free(filter); |
| 1977 | } |
| 1978 | |
| 1979 | int stream_filter(struct stream_filter *filter, |
| 1980 | const char *input, size_t *isize_p, |
| 1981 | char *output, size_t *osize_p) |
| 1982 | { |
| 1983 | return filter->vtbl->filter(filter, input, isize_p, output, osize_p); |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 1984 | } |