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