1
#include "cache.h"
2
#include "attr.h"
3
#include "run-command.h"
4
+#include "strbuf.h"
5
6
/*
7
* convert.c - convert a file when checking it out and checking it in.
81
return 0;
82
}
83
83
-static char *crlf_to_git(const char *path, const char *src, unsigned long *sizep, int action)
84
+static int crlf_to_git(const char *path, const char *src, size_t len,
85
+ struct strbuf *buf, int action)
86
{
85
- char *buffer, *dst;
86
- unsigned long size, nsize;
87
struct text_stat stats;
88
+ char *dst;
89
89
- if ((action == CRLF_BINARY) || !auto_crlf)
90
- return NULL;
91
-
92
- size = *sizep;
93
- if (!size)
94
- return NULL;
95
-
96
- gather_stats(src, size, &stats);
90
+ if ((action == CRLF_BINARY) || !auto_crlf || !len)
91
+ return 0;
92
93
+ gather_stats(src, len, &stats);
94
/* No CR? Nothing to convert, regardless. */
95
if (!stats.cr)
100
- return NULL;
96
+ return 0;
97
98
if (action == CRLF_GUESS) {
99
/*
102
* stuff?
103
*/
104
if (stats.cr != stats.crlf)
109
- return NULL;
105
+ return 0;
106
107
/*
108
* And add some heuristics for binary vs text, of course...
109
*/
114
- if (is_binary(size, &stats))
115
- return NULL;
110
+ if (is_binary(len, &stats))
111
+ return 0;
112
}
113
118
- /*
119
- * Ok, allocate a new buffer, fill it in, and return it
120
- * to let the caller know that we switched buffers.
121
- */
122
- nsize = size - stats.crlf;
123
- buffer = xmalloc(nsize);
124
- *sizep = nsize;
125
-
126
- dst = buffer;
114
+ strbuf_grow(buf, len);
115
+ dst = buf->buf;
116
if (action == CRLF_GUESS) {
117
/*
118
* If we guessed, we already know we rejected a file with
123
unsigned char c = *src++;
124
if (c != '\r')
125
*dst++ = c;
137
- } while (--size);
126
+ } while (--len);
127
} else {
128
do {
129
unsigned char c = *src++;
141
- if (! (c == '\r' && (1 < size && *src == '\n')))
130
+ if (! (c == '\r' && (1 < len && *src == '\n')))
131
*dst++ = c;
143
- } while (--size);
132
+ } while (--len);
133
}
145
-
146
- return buffer;
134
+ strbuf_setlen(buf, dst - buf->buf);
135
+ return 1;
136
}
137
149
-static char *crlf_to_worktree(const char *path, const char *src, unsigned long *sizep, int action)
138
+static int crlf_to_worktree(const char *path, const char *src, size_t len,
139
+ struct strbuf *buf, int action)
140
{
151
- char *buffer, *dst;
152
- unsigned long size, nsize;
141
+ char *to_free = NULL;
142
struct text_stat stats;
154
- unsigned char last;
143
144
if ((action == CRLF_BINARY) || (action == CRLF_INPUT) ||
145
auto_crlf <= 0)
158
- return NULL;
146
+ return 0;
147
160
- size = *sizep;
161
- if (!size)
162
- return NULL;
148
+ if (!len)
149
+ return 0;
150
164
- gather_stats(src, size, &stats);
151
+ gather_stats(src, len, &stats);
152
153
/* No LF? Nothing to convert, regardless. */
154
if (!stats.lf)
168
- return NULL;
155
+ return 0;
156
157
/* Was it already in CRLF format? */
158
if (stats.lf == stats.crlf)
172
- return NULL;
159
+ return 0;
160
161
if (action == CRLF_GUESS) {
162
/* If we have any bare CR characters, we're not going to touch it */
163
if (stats.cr != stats.crlf)
177
- return NULL;
164
+ return 0;
165
179
- if (is_binary(size, &stats))
180
- return NULL;
166
+ if (is_binary(len, &stats))
167
+ return 0;
168
}
169
183
- /*
184
- * Ok, allocate a new buffer, fill it in, and return it
185
- * to let the caller know that we switched buffers.
186
- */
187
- nsize = size + stats.lf - stats.crlf;
188
- buffer = xmalloc(nsize);
189
- *sizep = nsize;
190
- last = 0;
191
-
192
- dst = buffer;
193
- do {
194
- unsigned char c = *src++;
195
- if (c == '\n' && last != '\r')
196
- *dst++ = '\r';
197
- *dst++ = c;
198
- last = c;
199
- } while (--size);
200
-
201
- return buffer;
170
+ /* are we "faking" in place editing ? */
171
+ if (src == buf->buf)
172
+ to_free = strbuf_detach(buf);
173
+
174
+ strbuf_grow(buf, len + stats.lf - stats.crlf);
175
+ for (;;) {
176
+ const char *nl = memchr(src, '\n', len);
177
+ if (!nl)
178
+ break;
179
+ if (nl > src && nl[-1] == '\r') {
180
+ strbuf_add(buf, src, nl + 1 - src);
181
+ } else {
182
+ strbuf_add(buf, src, nl - src);
183
+ strbuf_addstr(buf, "\r\n");
184
+ }
185
+ len -= nl + 1 - src;
186
+ src = nl + 1;
187
+ }
188
+ strbuf_add(buf, src, len);
189
+
190
+ free(to_free);
191
+ return 1;
192
}
193
194
static int filter_buffer(const char *path, const char *src,
236
return (write_err || status);
237
}
238
249
-static char *apply_filter(const char *path, const char *src,
250
- unsigned long *sizep, const char *cmd)
239
+static int apply_filter(const char *path, const char *src, size_t len,
240
+ struct strbuf *dst, const char *cmd)
241
{
242
/*
243
* Create a pipeline to have the command filter the buffer's
245
*
246
* (child --> cmd) --> us
247
*/
258
- const int SLOP = 4096;
248
int pipe_feed[2];
260
- int status;
261
- char *dst;
262
- unsigned long dstsize, dstalloc;
249
+ int status, ret = 1;
250
struct child_process child_process;
251
+ struct strbuf nbuf;
252
253
if (!cmd)
266
- return NULL;
254
+ return 0;
255
256
memset(&child_process, 0, sizeof(child_process));
257
258
if (pipe(pipe_feed) < 0) {
259
error("cannot create pipe to run external filter %s", cmd);
272
- return NULL;
260
+ return 0;
261
}
262
263
fflush(NULL);
266
error("cannot fork to run external filter %s", cmd);
267
close(pipe_feed[0]);
268
close(pipe_feed[1]);
281
- return NULL;
269
+ return 0;
270
}
271
if (!child_process.pid) {
272
dup2(pipe_feed[1], 1);
273
close(pipe_feed[0]);
274
close(pipe_feed[1]);
287
- exit(filter_buffer(path, src, *sizep, cmd));
275
+ exit(filter_buffer(path, src, len, cmd));
276
}
277
close(pipe_feed[1]);
278
291
- dstalloc = *sizep;
292
- dst = xmalloc(dstalloc);
293
- dstsize = 0;
294
-
295
- while (1) {
296
- ssize_t numread = xread(pipe_feed[0], dst + dstsize,
297
- dstalloc - dstsize);
298
-
299
- if (numread <= 0) {
300
- if (!numread)
301
- break;
302
- error("read from external filter %s failed", cmd);
303
- free(dst);
304
- dst = NULL;
305
- break;
306
- }
307
- dstsize += numread;
308
- if (dstalloc <= dstsize + SLOP) {
309
- dstalloc = dstsize + SLOP;
310
- dst = xrealloc(dst, dstalloc);
311
- }
279
+ strbuf_init(&nbuf, 0);
280
+ if (strbuf_read(&nbuf, pipe_feed[0], len) < 0) {
281
+ error("read from external filter %s failed", cmd);
282
+ ret = 0;
283
}
284
if (close(pipe_feed[0])) {
314
- error("read from external filter %s failed", cmd);
315
- free(dst);
316
- dst = NULL;
285
+ ret = error("read from external filter %s failed", cmd);
286
+ ret = 0;
287
}
318
-
288
status = finish_command(&child_process);
289
if (status) {
321
- error("external filter %s failed %d", cmd, -status);
322
- free(dst);
323
- dst = NULL;
290
+ ret = error("external filter %s failed %d", cmd, -status);
291
+ ret = 0;
292
}
293
326
- if (dst)
327
- *sizep = dstsize;
328
- return dst;
294
+ if (ret) {
295
+ *dst = nbuf;
296
+ } else {
297
+ strbuf_release(&nbuf);
298
+ }
299
+ return ret;
300
}
301
302
static struct convert_driver {
420
return cnt;
421
}
422
452
-static char *ident_to_git(const char *path, const char *src, unsigned long *sizep, int ident)
423
+static int ident_to_git(const char *path, const char *src, size_t len,
424
+ struct strbuf *buf, int ident)
425
{
454
- int cnt;
455
- unsigned long size;
456
- char *dst, *buf;
426
+ char *dst, *dollar;
427
458
- if (!ident)
459
- return NULL;
460
- size = *sizep;
461
- cnt = count_ident(src, size);
462
- if (!cnt)
463
- return NULL;
464
- buf = xmalloc(size);
465
-
466
- for (dst = buf; size; size--) {
467
- char ch = *src++;
468
- *dst++ = ch;
469
- if ((ch == '$') && (3 <= size) &&
470
- !memcmp("Id:", src, 3)) {
471
- unsigned long rem = size - 3;
472
- const char *cp = src + 3;
473
- do {
474
- ch = *cp++;
475
- if (ch == '$')
476
- break;
477
- rem--;
478
- } while (rem);
479
- if (!rem)
480
- continue;
428
+ if (!ident || !count_ident(src, len))
429
+ return 0;
430
+
431
+ strbuf_grow(buf, len);
432
+ dst = buf->buf;
433
+ for (;;) {
434
+ dollar = memchr(src, '$', len);
435
+ if (!dollar)
436
+ break;
437
+ memcpy(dst, src, dollar + 1 - src);
438
+ dst += dollar + 1 - src;
439
+ len -= dollar + 1 - src;
440
+ src = dollar + 1;
441
+
442
+ if (len > 3 && !memcmp(src, "Id:", 3)) {
443
+ dollar = memchr(src + 3, '$', len - 3);
444
+ if (!dollar)
445
+ break;
446
memcpy(dst, "Id$", 3);
447
dst += 3;
483
- size -= (cp - src);
484
- src = cp;
448
+ len -= dollar + 1 - src;
449
+ src = dollar + 1;
450
}
451
}
487
-
488
- *sizep = dst - buf;
489
- return buf;
452
+ memcpy(dst, src, len);
453
+ strbuf_setlen(buf, dst + len - buf->buf);
454
+ return 1;
455
}
456
492
-static char *ident_to_worktree(const char *path, const char *src, unsigned long *sizep, int ident)
457
+static int ident_to_worktree(const char *path, const char *src, size_t len,
458
+ struct strbuf *buf, int ident)
459
{
494
- int cnt;
495
- unsigned long size;
496
- char *dst, *buf;
460
unsigned char sha1[20];
461
+ char *to_free = NULL, *dollar;
462
+ int cnt;
463
464
if (!ident)
500
- return NULL;
465
+ return 0;
466
502
- size = *sizep;
503
- cnt = count_ident(src, size);
467
+ cnt = count_ident(src, len);
468
if (!cnt)
505
- return NULL;
469
+ return 0;
470
507
- hash_sha1_file(src, size, "blob", sha1);
508
- buf = xmalloc(size + cnt * 43);
509
-
510
- for (dst = buf; size; size--) {
511
- const char *cp;
512
- /* Fetch next source character, move the pointer on */
513
- char ch = *src++;
514
- /* Copy the current character to the destination */
515
- *dst++ = ch;
516
- /* If the current character is "$" or there are less than three
517
- * remaining bytes or the two bytes following this one are not
518
- * "Id", then simply read the next character */
519
- if ((ch != '$') || (size < 3) || memcmp("Id", src, 2))
520
- continue;
521
- /*
522
- * Here when
523
- * - There are more than 2 bytes remaining
524
- * - The current three bytes are "$Id"
525
- * with
526
- * - ch == "$"
527
- * - src[0] == "I"
528
- */
471
+ /* are we "faking" in place editing ? */
472
+ if (src == buf->buf)
473
+ to_free = strbuf_detach(buf);
474
+ hash_sha1_file(src, len, "blob", sha1);
475
530
- /*
531
- * It's possible that an expanded Id has crept its way into the
532
- * repository, we cope with that by stripping the expansion out
533
- */
534
- if (src[2] == ':') {
535
- /* Expanded keywords have "$Id:" at the front */
476
+ strbuf_grow(buf, len + cnt * 43);
477
+ for (;;) {
478
+ /* step 1: run to the next '$' */
479
+ dollar = memchr(src, '$', len);
480
+ if (!dollar)
481
+ break;
482
+ strbuf_add(buf, src, dollar + 1 - src);
483
+ len -= dollar + 1 - src;
484
+ src = dollar + 1;
485
537
- /* discard up to but not including the closing $ */
538
- unsigned long rem = size - 3;
539
- /* Point at first byte after the ":" */
540
- cp = src + 3;
541
- /*
542
- * Throw away characters until either
543
- * - we reach a "$"
544
- * - we run out of bytes (rem == 0)
545
- */
546
- do {
547
- ch = *cp;
548
- if (ch == '$')
549
- break;
550
- cp++;
551
- rem--;
552
- } while (rem);
553
- /* If the above finished because it ran out of characters, then
554
- * this is an incomplete keyword, so don't run the expansion */
555
- if (!rem)
556
- continue;
557
- } else if (src[2] == '$')
558
- cp = src + 2;
559
- else
560
- /* Anything other than "$Id:XXX$" or $Id$ and we skip the
561
- * expansion */
486
+ /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
487
+ if (len < 3 || memcmp("Id", src, 2))
488
continue;
489
564
- /* cp is now pointing at the last $ of the keyword */
565
-
566
- memcpy(dst, "Id: ", 4);
567
- dst += 4;
568
- memcpy(dst, sha1_to_hex(sha1), 40);
569
- dst += 40;
570
- *dst++ = ' ';
490
+ /* step 3: skip over Id$ or Id:xxxxx$ */
491
+ if (src[2] == '$') {
492
+ src += 3;
493
+ len -= 3;
494
+ } else if (src[2] == ':') {
495
+ /*
496
+ * It's possible that an expanded Id has crept its way into the
497
+ * repository, we cope with that by stripping the expansion out
498
+ */
499
+ dollar = memchr(src + 3, '$', len - 3);
500
+ if (!dollar) {
501
+ /* incomplete keyword, no more '$', so just quit the loop */
502
+ break;
503
+ }
504
572
- /* Adjust for the characters we've discarded */
573
- size -= (cp - src);
574
- src = cp;
505
+ len -= dollar + 1 - src;
506
+ src = dollar + 1;
507
+ } else {
508
+ /* it wasn't a "Id$" or "Id:xxxx$" */
509
+ continue;
510
+ }
511
576
- /* Copy the final "$" */
577
- *dst++ = *src++;
578
- size--;
512
+ /* step 4: substitute */
513
+ strbuf_addstr(buf, "Id: ");
514
+ strbuf_add(buf, sha1_to_hex(sha1), 40);
515
+ strbuf_addstr(buf, " $");
516
}
517
+ strbuf_add(buf, src, len);
518
581
- *sizep = dst - buf;
582
- return buf;
519
+ free(to_free);
520
+ return 1;
521
}
522
523
static int git_path_check_crlf(const char *path, struct git_attr_check *check)
556
return !!ATTR_TRUE(value);
557
}
558
621
-char *convert_to_git(const char *path, const char *src, unsigned long *sizep)
559
+int convert_to_git(const char *path, const char *src, size_t len, struct strbuf *dst)
560
{
561
struct git_attr_check check[3];
562
int crlf = CRLF_GUESS;
625
- int ident = 0;
563
+ int ident = 0, ret = 0;
564
char *filter = NULL;
627
- char *buf, *buf2;
565
566
setup_convert_check(check);
567
if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
573
filter = drv->clean;
574
}
575
639
- buf = apply_filter(path, src, sizep, filter);
640
-
641
- buf2 = crlf_to_git(path, buf ? buf : src, sizep, crlf);
642
- if (buf2) {
643
- free(buf);
644
- buf = buf2;
576
+ ret |= apply_filter(path, src, len, dst, filter);
577
+ if (ret) {
578
+ src = dst->buf;
579
+ len = dst->len;
580
}
646
-
647
- buf2 = ident_to_git(path, buf ? buf : src, sizep, ident);
648
- if (buf2) {
649
- free(buf);
650
- buf = buf2;
581
+ ret |= crlf_to_git(path, src, len, dst, crlf);
582
+ if (ret) {
583
+ src = dst->buf;
584
+ len = dst->len;
585
}
652
-
653
- return buf;
586
+ return ret | ident_to_git(path, src, len, dst, ident);
587
}
588
656
-char *convert_to_working_tree(const char *path, const char *src, unsigned long *sizep)
589
+int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
590
{
591
struct git_attr_check check[3];
592
int crlf = CRLF_GUESS;
660
- int ident = 0;
593
+ int ident = 0, ret = 0;
594
char *filter = NULL;
662
- char *buf, *buf2;
595
596
setup_convert_check(check);
597
if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
603
filter = drv->smudge;
604
}
605
674
- buf = ident_to_worktree(path, src, sizep, ident);
675
-
676
- buf2 = crlf_to_worktree(path, buf ? buf : src, sizep, crlf);
677
- if (buf2) {
678
- free(buf);
679
- buf = buf2;
606
+ ret |= ident_to_worktree(path, src, len, dst, ident);
607
+ if (ret) {
608
+ src = dst->buf;
609
+ len = dst->len;
610
}
681
-
682
- buf2 = apply_filter(path, buf ? buf : src, sizep, filter);
683
- if (buf2) {
684
- free(buf);
685
- buf = buf2;
611
+ ret |= crlf_to_worktree(path, src, len, dst, crlf);
612
+ if (ret) {
613
+ src = dst->buf;
614
+ len = dst->len;
615
}
687
-
688
- return buf;
616
+ return ret | apply_filter(path, src, len, dst, filter);
617
}