Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 1 | #include "cache.h" |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 2 | #include "attr.h" |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 3 | #include "run-command.h" |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 4 | |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 5 | /* |
| 6 | * convert.c - convert a file when checking it out and checking it in. |
| 7 | * |
| 8 | * This should use the pathname to decide on whether it wants to do some |
| 9 | * more interesting conversions (automatic gzip/unzip, general format |
| 10 | * conversions etc etc), but by default it just does automatic CRLF<->LF |
| 11 | * translation when the "auto_crlf" option is set. |
| 12 | */ |
| 13 | |
Junio C Hamano | 163b959 | 2007-04-19 22:37:19 -0700 | [diff] [blame] | 14 | #define CRLF_GUESS (-1) |
| 15 | #define CRLF_BINARY 0 |
| 16 | #define CRLF_TEXT 1 |
| 17 | #define CRLF_INPUT 2 |
| 18 | |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 19 | struct text_stat { |
Dmitry Potapov | 2862419 | 2008-01-16 04:59:12 +0300 | [diff] [blame] | 20 | /* NUL, CR, LF and CRLF counts */ |
| 21 | unsigned nul, cr, lf, crlf; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 22 | |
| 23 | /* These are just approximations! */ |
| 24 | unsigned printable, nonprintable; |
| 25 | }; |
| 26 | |
| 27 | static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats) |
| 28 | { |
| 29 | unsigned long i; |
| 30 | |
| 31 | memset(stats, 0, sizeof(*stats)); |
| 32 | |
| 33 | for (i = 0; i < size; i++) { |
| 34 | unsigned char c = buf[i]; |
| 35 | if (c == '\r') { |
| 36 | stats->cr++; |
| 37 | if (i+1 < size && buf[i+1] == '\n') |
| 38 | stats->crlf++; |
| 39 | continue; |
| 40 | } |
| 41 | if (c == '\n') { |
| 42 | stats->lf++; |
| 43 | continue; |
| 44 | } |
| 45 | if (c == 127) |
| 46 | /* DEL */ |
| 47 | stats->nonprintable++; |
| 48 | else if (c < 32) { |
| 49 | switch (c) { |
| 50 | /* BS, HT, ESC and FF */ |
| 51 | case '\b': case '\t': case '\033': case '\014': |
| 52 | stats->printable++; |
| 53 | break; |
Dmitry Potapov | 2862419 | 2008-01-16 04:59:12 +0300 | [diff] [blame] | 54 | case 0: |
| 55 | stats->nul++; |
| 56 | /* fall through */ |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 57 | default: |
| 58 | stats->nonprintable++; |
| 59 | } |
| 60 | } |
| 61 | else |
| 62 | stats->printable++; |
| 63 | } |
Dmitry Kakurin | f9dd4bf | 2008-07-11 18:48:16 +0200 | [diff] [blame] | 64 | |
| 65 | /* If file ends with EOF then don't count this EOF as non-printable. */ |
| 66 | if (size >= 1 && buf[size-1] == '\032') |
| 67 | stats->nonprintable--; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /* |
| 71 | * The same heuristics as diff.c::mmfile_is_binary() |
| 72 | */ |
| 73 | static int is_binary(unsigned long size, struct text_stat *stats) |
| 74 | { |
| 75 | |
Dmitry Potapov | 2862419 | 2008-01-16 04:59:12 +0300 | [diff] [blame] | 76 | if (stats->nul) |
| 77 | return 1; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 78 | if ((stats->printable >> 7) < stats->nonprintable) |
| 79 | return 1; |
| 80 | /* |
| 81 | * Other heuristics? Average line length might be relevant, |
| 82 | * as might LF vs CR vs CRLF counts.. |
| 83 | * |
| 84 | * NOTE! It might be normal to have a low ratio of CRLF to LF |
| 85 | * (somebody starts with a LF-only file and edits it with an editor |
| 86 | * that adds CRLF only to lines that are added..). But do we |
| 87 | * want to support CR-only? Probably not. |
| 88 | */ |
| 89 | return 0; |
| 90 | } |
| 91 | |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 92 | static void check_safe_crlf(const char *path, int action, |
| 93 | struct text_stat *stats, enum safe_crlf checksafe) |
| 94 | { |
| 95 | if (!checksafe) |
| 96 | return; |
| 97 | |
| 98 | if (action == CRLF_INPUT || auto_crlf <= 0) { |
| 99 | /* |
| 100 | * CRLFs would not be restored by checkout: |
| 101 | * check if we'd remove CRLFs |
| 102 | */ |
| 103 | if (stats->crlf) { |
| 104 | if (checksafe == SAFE_CRLF_WARN) |
| 105 | warning("CRLF will be replaced by LF in %s.", path); |
| 106 | else /* i.e. SAFE_CRLF_FAIL */ |
| 107 | die("CRLF would be replaced by LF in %s.", path); |
| 108 | } |
| 109 | } else if (auto_crlf > 0) { |
| 110 | /* |
| 111 | * CRLFs would be added by checkout: |
| 112 | * check if we have "naked" LFs |
| 113 | */ |
| 114 | if (stats->lf != stats->crlf) { |
| 115 | if (checksafe == SAFE_CRLF_WARN) |
| 116 | warning("LF will be replaced by CRLF in %s", path); |
| 117 | else /* i.e. SAFE_CRLF_FAIL */ |
| 118 | die("LF would be replaced by CRLF in %s", path); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 123 | static int crlf_to_git(const char *path, const char *src, size_t len, |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 124 | struct strbuf *buf, int action, enum safe_crlf checksafe) |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 125 | { |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 126 | struct text_stat stats; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 127 | char *dst; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 128 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 129 | if ((action == CRLF_BINARY) || !auto_crlf || !len) |
| 130 | return 0; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 131 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 132 | gather_stats(src, len, &stats); |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 133 | |
Junio C Hamano | 163b959 | 2007-04-19 22:37:19 -0700 | [diff] [blame] | 134 | if (action == CRLF_GUESS) { |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 135 | /* |
| 136 | * We're currently not going to even try to convert stuff |
| 137 | * that has bare CR characters. Does anybody do that crazy |
| 138 | * stuff? |
| 139 | */ |
| 140 | if (stats.cr != stats.crlf) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 141 | return 0; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 142 | |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 143 | /* |
| 144 | * And add some heuristics for binary vs text, of course... |
| 145 | */ |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 146 | if (is_binary(len, &stats)) |
| 147 | return 0; |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 148 | } |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 149 | |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 150 | check_safe_crlf(path, action, &stats, checksafe); |
| 151 | |
| 152 | /* Optimization: No CR? Nothing to convert, regardless. */ |
| 153 | if (!stats.cr) |
| 154 | return 0; |
| 155 | |
Pierre Habouzit | 90d16ec | 2007-10-05 10:11:59 +0200 | [diff] [blame] | 156 | /* only grow if not in place */ |
| 157 | if (strbuf_avail(buf) + buf->len < len) |
| 158 | strbuf_grow(buf, len - buf->len); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 159 | dst = buf->buf; |
Junio C Hamano | 163b959 | 2007-04-19 22:37:19 -0700 | [diff] [blame] | 160 | if (action == CRLF_GUESS) { |
| 161 | /* |
| 162 | * If we guessed, we already know we rejected a file with |
| 163 | * lone CR, and we can strip a CR without looking at what |
| 164 | * follow it. |
| 165 | */ |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 166 | do { |
Alex Riesen | ac78e54 | 2007-04-19 02:05:03 +0200 | [diff] [blame] | 167 | unsigned char c = *src++; |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 168 | if (c != '\r') |
Alex Riesen | ac78e54 | 2007-04-19 02:05:03 +0200 | [diff] [blame] | 169 | *dst++ = c; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 170 | } while (--len); |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 171 | } else { |
| 172 | do { |
Alex Riesen | ac78e54 | 2007-04-19 02:05:03 +0200 | [diff] [blame] | 173 | unsigned char c = *src++; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 174 | if (! (c == '\r' && (1 < len && *src == '\n'))) |
Alex Riesen | ac78e54 | 2007-04-19 02:05:03 +0200 | [diff] [blame] | 175 | *dst++ = c; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 176 | } while (--len); |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 177 | } |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 178 | strbuf_setlen(buf, dst - buf->buf); |
| 179 | return 1; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 182 | static int crlf_to_worktree(const char *path, const char *src, size_t len, |
| 183 | struct strbuf *buf, int action) |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 184 | { |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 185 | char *to_free = NULL; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 186 | struct text_stat stats; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 187 | |
Junio C Hamano | 163b959 | 2007-04-19 22:37:19 -0700 | [diff] [blame] | 188 | if ((action == CRLF_BINARY) || (action == CRLF_INPUT) || |
Andy Parkins | 760f0c6 | 2007-05-18 13:33:32 +0100 | [diff] [blame] | 189 | auto_crlf <= 0) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 190 | return 0; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 191 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 192 | if (!len) |
| 193 | return 0; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 194 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 195 | gather_stats(src, len, &stats); |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 196 | |
| 197 | /* No LF? Nothing to convert, regardless. */ |
| 198 | if (!stats.lf) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 199 | return 0; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 200 | |
| 201 | /* Was it already in CRLF format? */ |
| 202 | if (stats.lf == stats.crlf) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 203 | return 0; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 204 | |
Junio C Hamano | 163b959 | 2007-04-19 22:37:19 -0700 | [diff] [blame] | 205 | if (action == CRLF_GUESS) { |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 206 | /* If we have any bare CR characters, we're not going to touch it */ |
| 207 | if (stats.cr != stats.crlf) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 208 | return 0; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 209 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 210 | if (is_binary(len, &stats)) |
| 211 | return 0; |
Junio C Hamano | 201ac8e | 2007-04-15 13:35:45 -0700 | [diff] [blame] | 212 | } |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 213 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 214 | /* are we "faking" in place editing ? */ |
| 215 | if (src == buf->buf) |
Pierre Habouzit | b315c5c | 2007-09-27 12:58:23 +0200 | [diff] [blame] | 216 | to_free = strbuf_detach(buf, NULL); |
Alex Riesen | ac78e54 | 2007-04-19 02:05:03 +0200 | [diff] [blame] | 217 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 218 | strbuf_grow(buf, len + stats.lf - stats.crlf); |
| 219 | for (;;) { |
| 220 | const char *nl = memchr(src, '\n', len); |
| 221 | if (!nl) |
| 222 | break; |
| 223 | if (nl > src && nl[-1] == '\r') { |
| 224 | strbuf_add(buf, src, nl + 1 - src); |
| 225 | } else { |
| 226 | strbuf_add(buf, src, nl - src); |
| 227 | strbuf_addstr(buf, "\r\n"); |
| 228 | } |
| 229 | len -= nl + 1 - src; |
| 230 | src = nl + 1; |
| 231 | } |
| 232 | strbuf_add(buf, src, len); |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 233 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 234 | free(to_free); |
| 235 | return 1; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 236 | } |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 237 | |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 238 | struct filter_params { |
| 239 | const char *src; |
| 240 | unsigned long size; |
| 241 | const char *cmd; |
| 242 | }; |
| 243 | |
| 244 | static int filter_buffer(int fd, void *data) |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 245 | { |
| 246 | /* |
| 247 | * Spawn cmd and feed the buffer contents through its stdin. |
| 248 | */ |
| 249 | struct child_process child_process; |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 250 | struct filter_params *params = (struct filter_params *)data; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 251 | int write_err, status; |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 252 | const char *argv[] = { "sh", "-c", params->cmd, NULL }; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 253 | |
| 254 | memset(&child_process, 0, sizeof(child_process)); |
Johannes Sixt | dc1bfdc | 2007-10-19 21:47:55 +0200 | [diff] [blame] | 255 | child_process.argv = argv; |
| 256 | child_process.in = -1; |
Johannes Sixt | 7683b6e | 2007-10-19 21:48:05 +0200 | [diff] [blame] | 257 | child_process.out = fd; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 258 | |
Johannes Sixt | dc1bfdc | 2007-10-19 21:47:55 +0200 | [diff] [blame] | 259 | if (start_command(&child_process)) |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 260 | return error("cannot fork to run external filter %s", params->cmd); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 261 | |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 262 | write_err = (write_in_full(child_process.in, params->src, params->size) < 0); |
Johannes Sixt | dc1bfdc | 2007-10-19 21:47:55 +0200 | [diff] [blame] | 263 | if (close(child_process.in)) |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 264 | write_err = 1; |
| 265 | if (write_err) |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 266 | error("cannot feed the input to external filter %s", params->cmd); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 267 | |
| 268 | status = finish_command(&child_process); |
| 269 | if (status) |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 270 | error("external filter %s failed %d", params->cmd, -status); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 271 | return (write_err || status); |
| 272 | } |
| 273 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 274 | static int apply_filter(const char *path, const char *src, size_t len, |
| 275 | struct strbuf *dst, const char *cmd) |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 276 | { |
| 277 | /* |
| 278 | * Create a pipeline to have the command filter the buffer's |
| 279 | * contents. |
| 280 | * |
| 281 | * (child --> cmd) --> us |
| 282 | */ |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 283 | int ret = 1; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 284 | struct strbuf nbuf = STRBUF_INIT; |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 285 | struct async async; |
| 286 | struct filter_params params; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 287 | |
| 288 | if (!cmd) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 289 | return 0; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 290 | |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 291 | memset(&async, 0, sizeof(async)); |
| 292 | async.proc = filter_buffer; |
| 293 | async.data = ¶ms; |
| 294 | params.src = src; |
| 295 | params.size = len; |
| 296 | params.cmd = cmd; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 297 | |
| 298 | fflush(NULL); |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 299 | if (start_async(&async)) |
| 300 | return 0; /* error was already reported */ |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 301 | |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 302 | if (strbuf_read(&nbuf, async.out, len) < 0) { |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 303 | error("read from external filter %s failed", cmd); |
| 304 | ret = 0; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 305 | } |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 306 | if (close(async.out)) { |
Pierre Habouzit | 90d16ec | 2007-10-05 10:11:59 +0200 | [diff] [blame] | 307 | error("read from external filter %s failed", cmd); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 308 | ret = 0; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 309 | } |
Johannes Sixt | 546bb58 | 2007-10-19 21:48:06 +0200 | [diff] [blame] | 310 | if (finish_async(&async)) { |
| 311 | error("external filter %s failed", cmd); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 312 | ret = 0; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 315 | if (ret) { |
Pierre Habouzit | 90d16ec | 2007-10-05 10:11:59 +0200 | [diff] [blame] | 316 | strbuf_swap(dst, &nbuf); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 317 | } |
Pierre Habouzit | 90d16ec | 2007-10-05 10:11:59 +0200 | [diff] [blame] | 318 | strbuf_release(&nbuf); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 319 | return ret; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static struct convert_driver { |
| 323 | const char *name; |
| 324 | struct convert_driver *next; |
Brian Hetro | cd8be6c | 2008-07-05 01:24:42 -0400 | [diff] [blame] | 325 | const char *smudge; |
| 326 | const char *clean; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 327 | } *user_convert, **user_convert_tail; |
| 328 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 329 | 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] | 330 | { |
| 331 | const char *ep, *name; |
| 332 | int namelen; |
| 333 | struct convert_driver *drv; |
| 334 | |
| 335 | /* |
| 336 | * External conversion drivers are configured using |
| 337 | * "filter.<name>.variable". |
| 338 | */ |
| 339 | if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6) |
| 340 | return 0; |
| 341 | name = var + 7; |
| 342 | namelen = ep - name; |
| 343 | for (drv = user_convert; drv; drv = drv->next) |
| 344 | if (!strncmp(drv->name, name, namelen) && !drv->name[namelen]) |
| 345 | break; |
| 346 | if (!drv) { |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 347 | drv = xcalloc(1, sizeof(struct convert_driver)); |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 348 | drv->name = xmemdupz(name, namelen); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 349 | *user_convert_tail = drv; |
| 350 | user_convert_tail = &(drv->next); |
| 351 | } |
| 352 | |
| 353 | ep++; |
| 354 | |
| 355 | /* |
| 356 | * filter.<name>.smudge and filter.<name>.clean specifies |
| 357 | * the command line: |
| 358 | * |
| 359 | * command-line |
| 360 | * |
| 361 | * The command-line will not be interpolated in any way. |
| 362 | */ |
| 363 | |
Brian Hetro | cd8be6c | 2008-07-05 01:24:42 -0400 | [diff] [blame] | 364 | if (!strcmp("smudge", ep)) |
| 365 | return git_config_string(&drv->smudge, var, value); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 366 | |
Brian Hetro | cd8be6c | 2008-07-05 01:24:42 -0400 | [diff] [blame] | 367 | if (!strcmp("clean", ep)) |
| 368 | return git_config_string(&drv->clean, var, value); |
| 369 | |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 370 | return 0; |
| 371 | } |
| 372 | |
Junio C Hamano | 6073ee8 | 2007-04-20 23:44:02 -0700 | [diff] [blame] | 373 | static void setup_convert_check(struct git_attr_check *check) |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 374 | { |
| 375 | static struct git_attr *attr_crlf; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 376 | static struct git_attr *attr_ident; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 377 | static struct git_attr *attr_filter; |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 378 | |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 379 | if (!attr_crlf) { |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 380 | attr_crlf = git_attr("crlf", 4); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 381 | attr_ident = git_attr("ident", 5); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 382 | attr_filter = git_attr("filter", 6); |
| 383 | user_convert_tail = &user_convert; |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 384 | git_config(read_convert_config, NULL); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 385 | } |
| 386 | check[0].attr = attr_crlf; |
| 387 | check[1].attr = attr_ident; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 388 | check[2].attr = attr_filter; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | static int count_ident(const char *cp, unsigned long size) |
| 392 | { |
| 393 | /* |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 394 | * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$" |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 395 | */ |
| 396 | int cnt = 0; |
| 397 | char ch; |
| 398 | |
| 399 | while (size) { |
| 400 | ch = *cp++; |
| 401 | size--; |
| 402 | if (ch != '$') |
| 403 | continue; |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 404 | if (size < 3) |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 405 | break; |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 406 | if (memcmp("Id", cp, 2)) |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 407 | continue; |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 408 | ch = cp[2]; |
| 409 | cp += 3; |
| 410 | size -= 3; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 411 | if (ch == '$') |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 412 | cnt++; /* $Id$ */ |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 413 | if (ch != ':') |
| 414 | continue; |
| 415 | |
| 416 | /* |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 417 | * "$Id: ... "; scan up to the closing dollar sign and discard. |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 418 | */ |
| 419 | while (size) { |
| 420 | ch = *cp++; |
| 421 | size--; |
| 422 | if (ch == '$') { |
| 423 | cnt++; |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | return cnt; |
| 429 | } |
| 430 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 431 | static int ident_to_git(const char *path, const char *src, size_t len, |
| 432 | struct strbuf *buf, int ident) |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 433 | { |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 434 | char *dst, *dollar; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 435 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 436 | if (!ident || !count_ident(src, len)) |
| 437 | return 0; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 438 | |
Pierre Habouzit | 90d16ec | 2007-10-05 10:11:59 +0200 | [diff] [blame] | 439 | /* only grow if not in place */ |
| 440 | if (strbuf_avail(buf) + buf->len < len) |
| 441 | strbuf_grow(buf, len - buf->len); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 442 | dst = buf->buf; |
| 443 | for (;;) { |
| 444 | dollar = memchr(src, '$', len); |
| 445 | if (!dollar) |
| 446 | break; |
| 447 | memcpy(dst, src, dollar + 1 - src); |
| 448 | dst += dollar + 1 - src; |
| 449 | len -= dollar + 1 - src; |
| 450 | src = dollar + 1; |
| 451 | |
| 452 | if (len > 3 && !memcmp(src, "Id:", 3)) { |
| 453 | dollar = memchr(src + 3, '$', len - 3); |
| 454 | if (!dollar) |
| 455 | break; |
Andy Parkins | af9b54b | 2007-05-14 14:37:25 +0100 | [diff] [blame] | 456 | memcpy(dst, "Id$", 3); |
| 457 | dst += 3; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 458 | len -= dollar + 1 - src; |
| 459 | src = dollar + 1; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 462 | memcpy(dst, src, len); |
| 463 | strbuf_setlen(buf, dst + len - buf->buf); |
| 464 | return 1; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 467 | static int ident_to_worktree(const char *path, const char *src, size_t len, |
| 468 | struct strbuf *buf, int ident) |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 469 | { |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 470 | unsigned char sha1[20]; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 471 | char *to_free = NULL, *dollar; |
| 472 | int cnt; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 473 | |
| 474 | if (!ident) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 475 | return 0; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 476 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 477 | cnt = count_ident(src, len); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 478 | if (!cnt) |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 479 | return 0; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 480 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 481 | /* are we "faking" in place editing ? */ |
| 482 | if (src == buf->buf) |
Pierre Habouzit | b315c5c | 2007-09-27 12:58:23 +0200 | [diff] [blame] | 483 | to_free = strbuf_detach(buf, NULL); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 484 | hash_sha1_file(src, len, "blob", sha1); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 485 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 486 | strbuf_grow(buf, len + cnt * 43); |
| 487 | for (;;) { |
| 488 | /* step 1: run to the next '$' */ |
| 489 | dollar = memchr(src, '$', len); |
| 490 | if (!dollar) |
| 491 | break; |
| 492 | strbuf_add(buf, src, dollar + 1 - src); |
| 493 | len -= dollar + 1 - src; |
| 494 | src = dollar + 1; |
| 495 | |
| 496 | /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */ |
| 497 | if (len < 3 || memcmp("Id", src, 2)) |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 498 | continue; |
| 499 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 500 | /* step 3: skip over Id$ or Id:xxxxx$ */ |
| 501 | if (src[2] == '$') { |
| 502 | src += 3; |
| 503 | len -= 3; |
| 504 | } else if (src[2] == ':') { |
Andy Parkins | c23290d | 2007-05-25 11:50:08 +0100 | [diff] [blame] | 505 | /* |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 506 | * It's possible that an expanded Id has crept its way into the |
| 507 | * repository, we cope with that by stripping the expansion out |
Andy Parkins | c23290d | 2007-05-25 11:50:08 +0100 | [diff] [blame] | 508 | */ |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 509 | dollar = memchr(src + 3, '$', len - 3); |
| 510 | if (!dollar) { |
| 511 | /* incomplete keyword, no more '$', so just quit the loop */ |
| 512 | break; |
| 513 | } |
| 514 | |
| 515 | len -= dollar + 1 - src; |
| 516 | src = dollar + 1; |
| 517 | } else { |
| 518 | /* it wasn't a "Id$" or "Id:xxxx$" */ |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 519 | continue; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 520 | } |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 521 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 522 | /* step 4: substitute */ |
| 523 | strbuf_addstr(buf, "Id: "); |
| 524 | strbuf_add(buf, sha1_to_hex(sha1), 40); |
| 525 | strbuf_addstr(buf, " $"); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 526 | } |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 527 | strbuf_add(buf, src, len); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 528 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 529 | free(to_free); |
| 530 | return 1; |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Junio C Hamano | 6073ee8 | 2007-04-20 23:44:02 -0700 | [diff] [blame] | 533 | static int git_path_check_crlf(const char *path, struct git_attr_check *check) |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 534 | { |
Junio C Hamano | 6073ee8 | 2007-04-20 23:44:02 -0700 | [diff] [blame] | 535 | const char *value = check->value; |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 536 | |
Junio C Hamano | 6073ee8 | 2007-04-20 23:44:02 -0700 | [diff] [blame] | 537 | if (ATTR_TRUE(value)) |
| 538 | return CRLF_TEXT; |
| 539 | else if (ATTR_FALSE(value)) |
| 540 | return CRLF_BINARY; |
| 541 | else if (ATTR_UNSET(value)) |
| 542 | ; |
| 543 | else if (!strcmp(value, "input")) |
| 544 | return CRLF_INPUT; |
Junio C Hamano | 163b959 | 2007-04-19 22:37:19 -0700 | [diff] [blame] | 545 | return CRLF_GUESS; |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 548 | static struct convert_driver *git_path_check_convert(const char *path, |
| 549 | struct git_attr_check *check) |
| 550 | { |
| 551 | const char *value = check->value; |
| 552 | struct convert_driver *drv; |
| 553 | |
| 554 | if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value)) |
| 555 | return NULL; |
| 556 | for (drv = user_convert; drv; drv = drv->next) |
| 557 | if (!strcmp(value, drv->name)) |
| 558 | return drv; |
| 559 | return NULL; |
| 560 | } |
| 561 | |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 562 | static int git_path_check_ident(const char *path, struct git_attr_check *check) |
| 563 | { |
| 564 | const char *value = check->value; |
| 565 | |
| 566 | return !!ATTR_TRUE(value); |
| 567 | } |
| 568 | |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 569 | int convert_to_git(const char *path, const char *src, size_t len, |
| 570 | struct strbuf *dst, enum safe_crlf checksafe) |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 571 | { |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 572 | struct git_attr_check check[3]; |
Junio C Hamano | 6073ee8 | 2007-04-20 23:44:02 -0700 | [diff] [blame] | 573 | int crlf = CRLF_GUESS; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 574 | int ident = 0, ret = 0; |
Brian Hetro | cd8be6c | 2008-07-05 01:24:42 -0400 | [diff] [blame] | 575 | const char *filter = NULL; |
Junio C Hamano | 6073ee8 | 2007-04-20 23:44:02 -0700 | [diff] [blame] | 576 | |
| 577 | setup_convert_check(check); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 578 | if (!git_checkattr(path, ARRAY_SIZE(check), check)) { |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 579 | struct convert_driver *drv; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 580 | crlf = git_path_check_crlf(path, check + 0); |
| 581 | ident = git_path_check_ident(path, check + 1); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 582 | drv = git_path_check_convert(path, check + 2); |
| 583 | if (drv && drv->clean) |
| 584 | filter = drv->clean; |
Junio C Hamano | 6073ee8 | 2007-04-20 23:44:02 -0700 | [diff] [blame] | 585 | } |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 586 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 587 | ret |= apply_filter(path, src, len, dst, filter); |
| 588 | if (ret) { |
| 589 | src = dst->buf; |
| 590 | len = dst->len; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 591 | } |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 592 | ret |= crlf_to_git(path, src, len, dst, crlf, checksafe); |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 593 | if (ret) { |
| 594 | src = dst->buf; |
| 595 | len = dst->len; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 596 | } |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 597 | return ret | ident_to_git(path, src, len, dst, ident); |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 600 | int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst) |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 601 | { |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 602 | struct git_attr_check check[3]; |
Junio C Hamano | 6073ee8 | 2007-04-20 23:44:02 -0700 | [diff] [blame] | 603 | int crlf = CRLF_GUESS; |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 604 | int ident = 0, ret = 0; |
Brian Hetro | cd8be6c | 2008-07-05 01:24:42 -0400 | [diff] [blame] | 605 | const char *filter = NULL; |
Junio C Hamano | 6073ee8 | 2007-04-20 23:44:02 -0700 | [diff] [blame] | 606 | |
| 607 | setup_convert_check(check); |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 608 | if (!git_checkattr(path, ARRAY_SIZE(check), check)) { |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 609 | struct convert_driver *drv; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 610 | crlf = git_path_check_crlf(path, check + 0); |
| 611 | ident = git_path_check_ident(path, check + 1); |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 612 | drv = git_path_check_convert(path, check + 2); |
| 613 | if (drv && drv->smudge) |
| 614 | filter = drv->smudge; |
Junio C Hamano | 6073ee8 | 2007-04-20 23:44:02 -0700 | [diff] [blame] | 615 | } |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 616 | |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 617 | ret |= ident_to_worktree(path, src, len, dst, ident); |
| 618 | if (ret) { |
| 619 | src = dst->buf; |
| 620 | len = dst->len; |
Junio C Hamano | 3fed15f | 2007-04-21 19:09:02 -0700 | [diff] [blame] | 621 | } |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 622 | ret |= crlf_to_worktree(path, src, len, dst, crlf); |
| 623 | if (ret) { |
| 624 | src = dst->buf; |
| 625 | len = dst->len; |
Junio C Hamano | aa4ed40 | 2007-04-21 03:14:13 -0700 | [diff] [blame] | 626 | } |
Pierre Habouzit | 5ecd293 | 2007-09-16 15:51:04 +0200 | [diff] [blame] | 627 | return ret | apply_filter(path, src, len, dst, filter); |
Junio C Hamano | 35ebfd6 | 2007-04-12 22:30:05 -0700 | [diff] [blame] | 628 | } |