@matheusbsbs / matheus-bagliano / commits / 6716027108
Fork 0

Teach core.autocrlf to 'git apply'

This teaches git-apply that the data read from and written to the filesystem might need to get converted to adjust for local line-ending convention. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Feb 17, 2007 at 12:37 UTC 6716027108f426c83038b05baf3f20ceefe6fbd1
2 files changed +43 -10
builtin-apply.c
+28 -6
@@ -1393,28 +1393,39 @@ static void show_stats(struct patch *patch)
1393 free(qname);
1394 }
1395
1396 -static int read_old_data(struct stat *st, const char *path, void *buf, unsigned long size)
1396 +static int read_old_data(struct stat *st, const char *path, char **buf_p, unsigned long *alloc_p, unsigned long *size_p)
1397 {
1398 int fd;
1399 unsigned long got;
1400 + unsigned long nsize;
1401 + char *nbuf;
1402 + unsigned long size = *size_p;
1403 + char *buf = *buf_p;
1404
1405 switch (st->st_mode & S_IFMT) {
1406 case S_IFLNK:
1403 - return readlink(path, buf, size);
1407 + return readlink(path, buf, size) != size;
1408 case S_IFREG:
1409 fd = open(path, O_RDONLY);
1410 if (fd < 0)
1411 return error("unable to open %s", path);
1412 got = 0;
1413 for (;;) {
1410 - int ret = xread(fd, (char *) buf + got, size - got);
1414 + int ret = xread(fd, buf + got, size - got);
1415 if (ret <= 0)
1416 break;
1417 got += ret;
1418 }
1419 close(fd);
1416 - return got;
1417 -
1420 + nsize = got;
1421 + nbuf = buf;
1422 + if (convert_to_git(path, &nbuf, &nsize)) {
1423 + free(buf);
1424 + *buf_p = nbuf;
1425 + *alloc_p = nsize;
1426 + *size_p = nsize;
1427 + }
1428 + return got != size;
1429 default:
1430 return -1;
1431 }
@@ -1910,7 +1921,7 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
1921 size = st->st_size;
1922 alloc = size + 8192;
1923 buf = xmalloc(alloc);
1913 - if (read_old_data(st, patch->old_name, buf, alloc) != size)
1924 + if (read_old_data(st, patch->old_name, &buf, &alloc, &size))
1925 return error("read of %s failed", patch->old_name);
1926 }
1927
@@ -2282,12 +2293,22 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
2293 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
2294 {
2295 int fd;
2296 + char *nbuf;
2297 + unsigned long nsize;
2298
2299 if (S_ISLNK(mode))
2300 /* Although buf:size is counted string, it also is NUL
2301 * terminated.
2302 */
2303 return symlink(buf, path);
2304 + nsize = size;
2305 + nbuf = (char *) buf;
2306 + if (convert_to_working_tree(path, &nbuf, &nsize)) {
2307 + free((char *) buf);
2308 + buf = nbuf;
2309 + size = nsize;
2310 + }
2311 +
2312 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
2313 if (fd < 0)
2314 return -1;
@@ -2598,6 +2619,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
2619
2620 const char *whitespace_option = NULL;
2621
2622 +
2623 for (i = 1; i < argc; i++) {
2624 const char *arg = argv[i];
2625 char *end;
t/t0020-crlf.sh
+15 -4
@@ -180,11 +180,8 @@ test_expect_success 'apply patch (autocrlf=true)' '
180 git repo-config core.autocrlf true &&
181 git read-tree --reset -u HEAD &&
182
183 - # Sore thumb
184 - remove_cr one >tmp && mv -f tmp one &&
185 -
183 git apply patch.file &&
187 - test "$patched" = "`git hash-object --stdin <one`" || {
184 + test "$patched" = "`remove_cr one | git hash-object --stdin`" || {
185 echo "Eh? apply without index"
186 false
187 }
@@ -203,4 +200,18 @@ test_expect_success 'apply patch --cached (autocrlf=true)' '
200 }
201 '
202
203 +test_expect_success 'apply patch --index (autocrlf=true)' '
204 +
205 + rm -f tmp one dir/two &&
206 + git repo-config core.autocrlf true &&
207 + git read-tree --reset -u HEAD &&
208 +
209 + git apply --index patch.file &&
210 + test "$patched" = `git rev-parse :one` &&
211 + test "$patched" = "`remove_cr one | git hash-object --stdin`" || {
212 + echo "Eh? apply with --index"
213 + false
214 + }
215 +'
216 +
217 test_done