Simplify calling of CR/LF conversion routines
Signed-off-by: Alex Riesen <raa.lkml@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
Alex Riesen committed
Apr 19, 2007 at 02:05 UTC
ac78e548049f4e86b38368d2c4b4dbb546c64ac6
6 files changed
+51
-60
builtin-apply.c
+7
-11
@@ -1475,8 +1475,8 @@ static int read_old_data(struct stat *st, const char *path, char **buf_p, unsign
1475
}
1476
close(fd);
1477
nsize = got;
1478
- nbuf = buf;
1479
- if (convert_to_git(path, &nbuf, &nsize)) {
1478
+ nbuf = convert_to_git(path, buf, &nsize);
1479
+ if (nbuf) {
1480
free(buf);
1481
*buf_p = nbuf;
1482
*alloc_p = nsize;
@@ -2355,9 +2355,8 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
2355
2356
static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
2357
{
2358
- int fd, converted;
2358
+ int fd;
2359
char *nbuf;
2360
- unsigned long nsize;
2360
2361
if (has_symlinks && S_ISLNK(mode))
2362
/* Although buf:size is counted string, it also is NUL
@@ -2369,13 +2368,10 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
2368
if (fd < 0)
2369
return -1;
2370
2372
- nsize = size;
2373
- nbuf = (char *) buf;
2374
- converted = convert_to_working_tree(path, &nbuf, &nsize);
2375
- if (converted) {
2371
+ nbuf = convert_to_working_tree(path, buf, &size);
2372
+ if (nbuf)
2373
buf = nbuf;
2377
- size = nsize;
2378
- }
2374
+
2375
while (size) {
2376
int written = xwrite(fd, buf, size);
2377
if (written < 0)
@@ -2387,7 +2383,7 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
2383
}
2384
if (close(fd) < 0)
2385
die("closing file %s: %s", path, strerror(errno));
2390
- if (converted)
2386
+ if (nbuf)
2387
free(nbuf);
2388
return 0;
2389
}
cache.h
+2
-2
@@ -496,8 +496,8 @@ extern void trace_printf(const char *format, ...);
496
extern void trace_argv_printf(const char **argv, int count, const char *format, ...);
497
498
/* convert.c */
499
-extern int convert_to_git(const char *path, char **bufp, unsigned long *sizep);
500
-extern int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep);
499
+extern char *convert_to_git(const char *path, const char *src, unsigned long *sizep);
500
+extern char *convert_to_working_tree(const char *path, const char *src, unsigned long *sizep);
501
502
/* match-trees.c */
503
void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, int);
convert.c
+35
-36
@@ -79,25 +79,24 @@ static int is_binary(unsigned long size, struct text_stat *stats)
79
return 0;
80
}
81
82
-static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int action)
82
+static char *crlf_to_git(const char *path, const char *src, unsigned long *sizep, int action)
83
{
84
- char *buffer, *nbuf;
84
+ char *buffer, *dst;
85
unsigned long size, nsize;
86
struct text_stat stats;
87
88
if ((action == CRLF_BINARY) || (action == CRLF_GUESS && !auto_crlf))
89
- return 0;
89
+ return NULL;
90
91
size = *sizep;
92
if (!size)
93
- return 0;
94
- buffer = *bufp;
93
+ return NULL;
94
96
- gather_stats(buffer, size, &stats);
95
+ gather_stats(src, size, &stats);
96
97
/* No CR? Nothing to convert, regardless. */
98
if (!stats.cr)
100
- return 0;
99
+ return NULL;
100
101
if (action == CRLF_GUESS) {
102
/*
@@ -106,13 +105,13 @@ static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int
105
* stuff?
106
*/
107
if (stats.cr != stats.crlf)
109
- return 0;
108
+ return NULL;
109
110
/*
111
* And add some heuristics for binary vs text, of course...
112
*/
113
if (is_binary(size, &stats))
115
- return 0;
114
+ return NULL;
115
}
116
117
/*
@@ -120,10 +119,10 @@ static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int
119
* to let the caller know that we switched buffers on it.
120
*/
121
nsize = size - stats.crlf;
123
- nbuf = xmalloc(nsize);
124
- *bufp = nbuf;
122
+ buffer = xmalloc(nsize);
123
*sizep = nsize;
124
125
+ dst = buffer;
126
if (action == CRLF_GUESS) {
127
/*
128
* If we guessed, we already know we rejected a file with
@@ -131,54 +130,53 @@ static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int
130
* follow it.
131
*/
132
do {
134
- unsigned char c = *buffer++;
133
+ unsigned char c = *src++;
134
if (c != '\r')
136
- *nbuf++ = c;
135
+ *dst++ = c;
136
} while (--size);
137
} else {
138
do {
140
- unsigned char c = *buffer++;
139
+ unsigned char c = *src++;
140
if (! (c == '\r' && (1 < size && *buffer == '\n')))
142
- *nbuf++ = c;
141
+ *dst++ = c;
142
} while (--size);
143
}
144
146
- return 1;
145
+ return buffer;
146
}
147
149
-static int crlf_to_worktree(const char *path, char **bufp, unsigned long *sizep, int action)
148
+static char *crlf_to_worktree(const char *path, const char *src, unsigned long *sizep, int action)
149
{
151
- char *buffer, *nbuf;
150
+ char *buffer, *dst;
151
unsigned long size, nsize;
152
struct text_stat stats;
153
unsigned char last;
154
155
if ((action == CRLF_BINARY) || (action == CRLF_INPUT) ||
156
(action == CRLF_GUESS && auto_crlf <= 0))
158
- return 0;
157
+ return NULL;
158
159
size = *sizep;
160
if (!size)
162
- return 0;
163
- buffer = *bufp;
161
+ return NULL;
162
165
- gather_stats(buffer, size, &stats);
163
+ gather_stats(src, size, &stats);
164
165
/* No LF? Nothing to convert, regardless. */
166
if (!stats.lf)
169
- return 0;
167
+ return NULL;
168
169
/* Was it already in CRLF format? */
170
if (stats.lf == stats.crlf)
173
- return 0;
171
+ return NULL;
172
173
if (action == CRLF_GUESS) {
174
/* If we have any bare CR characters, we're not going to touch it */
175
if (stats.cr != stats.crlf)
178
- return 0;
176
+ return NULL;
177
178
if (is_binary(size, &stats))
181
- return 0;
179
+ return NULL;
180
}
181
182
/*
@@ -186,19 +184,20 @@ static int crlf_to_worktree(const char *path, char **bufp, unsigned long *sizep,
184
* to let the caller know that we switched buffers on it.
185
*/
186
nsize = size + stats.lf - stats.crlf;
189
- nbuf = xmalloc(nsize);
190
- *bufp = nbuf;
187
+ buffer = xmalloc(nsize);
188
*sizep = nsize;
189
last = 0;
190
+
191
+ dst = buffer;
192
do {
194
- unsigned char c = *buffer++;
193
+ unsigned char c = *src++;
194
if (c == '\n' && last != '\r')
196
- *nbuf++ = '\r';
197
- *nbuf++ = c;
195
+ *dst++ = '\r';
196
+ *dst++ = c;
197
last = c;
198
} while (--size);
199
201
- return 1;
200
+ return buffer;
201
}
202
203
static void setup_crlf_check(struct git_attr_check *check)
@@ -231,12 +230,12 @@ static int git_path_check_crlf(const char *path)
230
return CRLF_GUESS;
231
}
232
234
-int convert_to_git(const char *path, char **bufp, unsigned long *sizep)
233
+char *convert_to_git(const char *path, const char *src, unsigned long *sizep)
234
{
236
- return crlf_to_git(path, bufp, sizep, git_path_check_crlf(path));
235
+ return crlf_to_git(path, src, sizep, git_path_check_crlf(path));
236
}
237
239
-int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep)
238
+char *convert_to_working_tree(const char *path, const char *src, unsigned long *sizep)
239
{
241
- return crlf_to_worktree(path, bufp, sizep, git_path_check_crlf(path));
240
+ return crlf_to_worktree(path, src, sizep, git_path_check_crlf(path));
241
}
diff.c
+2
-2
@@ -1493,9 +1493,9 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
1493
/*
1494
* Convert from working tree format to canonical git format
1495
*/
1496
- buf = s->data;
1496
size = s->size;
1498
- if (convert_to_git(s->path, &buf, &size)) {
1497
+ buf = convert_to_git(s->path, s->data, &size);
1498
+ if (buf) {
1499
munmap(s->data, s->size);
1500
s->should_munmap = 0;
1501
s->data = buf;
entry.c
+2
-5
@@ -79,7 +79,6 @@ static int write_entry(struct cache_entry *ce, char *path, struct checkout *stat
79
}
80
switch (ntohl(ce->ce_mode) & S_IFMT) {
81
char *buf;
82
- unsigned long nsize;
82
83
case S_IFREG:
84
if (to_tempfile) {
@@ -96,12 +95,10 @@ static int write_entry(struct cache_entry *ce, char *path, struct checkout *stat
95
/*
96
* Convert from git internal format to working tree format
97
*/
99
- buf = new;
100
- nsize = size;
101
- if (convert_to_working_tree(ce->name, &buf, &nsize)) {
98
+ buf = convert_to_working_tree(ce->name, new, &size);
99
+ if (buf) {
100
free(new);
101
new = buf;
104
- size = nsize;
102
}
103
104
wrote = write_in_full(fd, new, size);
sha1_file.c
+3
-4
@@ -2277,10 +2277,9 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
2277
*/
2278
if ((type == OBJ_BLOB) && S_ISREG(st->st_mode)) {
2279
unsigned long nsize = size;
2280
- char *nbuf = buf;
2281
- if (convert_to_git(path, &nbuf, &nsize)) {
2282
- if (size)
2283
- munmap(buf, size);
2280
+ char *nbuf = convert_to_git(path, buf, &nsize);
2281
+ if (nbuf) {
2282
+ munmap(buf, size);
2283
size = nsize;
2284
buf = nbuf;
2285
re_allocated = 1;