{"thread":{"id":"7734","subject":"[PATCH] Simplify calling of CR/LF conversion routines","startedAt":"2007-04-19T00:05:03Z","lastAt":"2007-04-19T00:05:03Z","messageCount":1,"participants":["Alex Riesen"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"39847","messageId":"20070419000503.GB14247@steel.home","threadId":"7734","inReplyTo":null,"subject":"[PATCH] Simplify calling of CR/LF conversion routines","fromName":"Alex Riesen","fromEmail":"raa.lkml@gmail.com","sentAt":"2007-04-19T00:05:03Z","receivedAt":"2007-04-19T00:05:03Z","isPatch":true,"sender":{"key":"raa.lkml@gmail.com","avatar":"https://avatars.githubusercontent.com/u/324101?v=4"},"body":"Signed-off-by: Alex Riesen <raa.lkml@gmail.com>\n---\n\nThis is against next.\n\n builtin-apply.c |   18 +++++++-----------\n cache.h         |    4 ++--\n convert.c       |   50 +++++++++++++++++++++++---------------------------\n diff.c          |    4 ++--\n entry.c         |    8 +++-----\n sha1_file.c     |    7 +++----\n 6 files changed, 40 insertions(+), 51 deletions(-)\n\ndiff --git a/builtin-apply.c b/builtin-apply.c\nindex fd92ef7..ccd342c 100644\n--- a/builtin-apply.c\n+++ b/builtin-apply.c\n@@ -1475,8 +1475,8 @@ static int read_old_data(struct stat *st, const char *path, char **buf_p, unsign\n \t\t}\n \t\tclose(fd);\n \t\tnsize = got;\n-\t\tnbuf = buf;\n-\t\tif (convert_to_git(path, &nbuf, &nsize)) {\n+\t\tnbuf = convert_to_git(path, buf, &nsize);\n+\t\tif (nbuf) {\n \t\t\tfree(buf);\n \t\t\t*buf_p = nbuf;\n \t\t\t*alloc_p = nsize;\n@@ -2355,9 +2355,8 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned\n \n static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)\n {\n-\tint fd, converted;\n+\tint fd;\n \tchar *nbuf;\n-\tunsigned long nsize;\n \n \tif (has_symlinks && S_ISLNK(mode))\n \t\t/* Although buf:size is counted string, it also is NUL\n@@ -2369,13 +2368,10 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,\n \tif (fd < 0)\n \t\treturn -1;\n \n-\tnsize = size;\n-\tnbuf = (char *) buf;\n-\tconverted = convert_to_working_tree(path, &nbuf, &nsize);\n-\tif (converted) {\n+\tnbuf = convert_to_working_tree(path, buf, &size);\n+\tif (nbuf)\n \t\tbuf = nbuf;\n-\t\tsize = nsize;\n-\t}\n+\n \twhile (size) {\n \t\tint written = xwrite(fd, buf, size);\n \t\tif (written < 0)\n@@ -2387,7 +2383,7 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,\n \t}\n \tif (close(fd) < 0)\n \t\tdie(\"closing file %s: %s\", path, strerror(errno));\n-\tif (converted)\n+\tif (nbuf)\n \t\tfree(nbuf);\n \treturn 0;\n }\ndiff --git a/cache.h b/cache.h\nindex 6d462bd..39b53e5 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -512,8 +512,8 @@ extern void trace_printf(const char *format, ...);\n extern void trace_argv_printf(const char **argv, int count, const char *format, ...);\n \n /* convert.c */\n-extern int convert_to_git(const char *path, char **bufp, unsigned long *sizep);\n-extern int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep);\n+extern char *convert_to_git(const char *path, const char *bufp, unsigned long *sizep);\n+extern char *convert_to_working_tree(const char *path, const char *bufp, unsigned long *sizep);\n \n /* match-trees.c */\n void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, int);\ndiff --git a/convert.c b/convert.c\nindex 68bb70f..17d7324 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -74,7 +74,7 @@ static int is_binary(unsigned long size, struct text_stat *stats)\n \treturn 0;\n }\n \n-static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int guess)\n+char *crlf_to_git(const char *path, const char *bufp, unsigned long *sizep, int guess)\n {\n \tchar *buffer, *nbuf;\n \tunsigned long size, nsize;\n@@ -85,14 +85,13 @@ static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int\n \n \tsize = *sizep;\n \tif (!size)\n-\t\treturn 0;\n-\tbuffer = *bufp;\n+\t\treturn NULL;\n \n-\tgather_stats(buffer, size, &stats);\n+\tgather_stats(bufp, size, &stats);\n \n \t/* No CR? Nothing to convert, regardless. */\n \tif (!stats.cr)\n-\t\treturn 0;\n+\t\treturn NULL;\n \n \tif (guess) {\n \t\t/*\n@@ -115,38 +114,37 @@ static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int\n \t * to let the caller know that we switched buffers on it.\n \t */\n \tnsize = size - stats.crlf;\n-\tnbuf = xmalloc(nsize);\n-\t*bufp = nbuf;\n+\tbuffer = nbuf = xmalloc(nsize);\n \t*sizep = nsize;\n \n \tif (guess) {\n \t\tdo {\n-\t\t\tunsigned char c = *buffer++;\n+\t\t\tunsigned char c = *bufp++;\n \t\t\tif (c != '\\r')\n \t\t\t\t*nbuf++ = c;\n \t\t} while (--size);\n \t} else {\n \t\tdo {\n-\t\t\tunsigned char c = *buffer++;\n-\t\t\tif (! (c == '\\r' && (1 < size && *buffer == '\\n')))\n+\t\t\tunsigned char c = *bufp++;\n+\t\t\tif (! (c == '\\r' && (1 < size && *bufp == '\\n')))\n \t\t\t\t*nbuf++ = c;\n \t\t} while (--size);\n \t}\n \n-\treturn 1;\n+\treturn buffer;\n }\n \n-static int autocrlf_to_git(const char *path, char **bufp, unsigned long *sizep)\n+static char *autocrlf_to_git(const char *path, const char *bufp, unsigned long *sizep)\n {\n \treturn crlf_to_git(path, bufp, sizep, 1);\n }\n \n-static int forcecrlf_to_git(const char *path, char **bufp, unsigned long *sizep)\n+static char *forcecrlf_to_git(const char *path, const char *bufp, unsigned long *sizep)\n {\n \treturn crlf_to_git(path, bufp, sizep, 0);\n }\n \n-static int crlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep, int guess)\n+static char *crlf_to_working_tree(const char *path, const char *bufp, unsigned long *sizep, int guess)\n {\n \tchar *buffer, *nbuf;\n \tunsigned long size, nsize;\n@@ -158,18 +156,17 @@ static int crlf_to_working_tree(const char *path, char **bufp, unsigned long *si\n \n \tsize = *sizep;\n \tif (!size)\n-\t\treturn 0;\n-\tbuffer = *bufp;\n+\t\treturn NULL;\n \n-\tgather_stats(buffer, size, &stats);\n+\tgather_stats(bufp, size, &stats);\n \n \t/* No LF? Nothing to convert, regardless. */\n \tif (!stats.lf)\n-\t\treturn 0;\n+\t\treturn NULL;\n \n \t/* Was it already in CRLF format? */\n \tif (stats.lf == stats.crlf)\n-\t\treturn 0;\n+\t\treturn NULL;\n \n \tif (guess) {\n \t\t/* If we have any bare CR characters, we're not going to touch it */\n@@ -185,27 +182,26 @@ static int crlf_to_working_tree(const char *path, char **bufp, unsigned long *si\n \t * to let the caller know that we switched buffers on it.\n \t */\n \tnsize = size + stats.lf - stats.crlf;\n-\tnbuf = xmalloc(nsize);\n-\t*bufp = nbuf;\n+\tbuffer = nbuf = xmalloc(nsize);\n \t*sizep = nsize;\n \tlast = 0;\n \tdo {\n-\t\tunsigned char c = *buffer++;\n+\t\tunsigned char c = *bufp++;\n \t\tif (c == '\\n' && last != '\\r')\n \t\t\t*nbuf++ = '\\r';\n \t\t*nbuf++ = c;\n \t\tlast = c;\n \t} while (--size);\n \n-\treturn 1;\n+\treturn buffer;\n }\n \n-static int autocrlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep)\n+static char *autocrlf_to_working_tree(const char *path, const char *bufp, unsigned long *sizep)\n {\n \treturn crlf_to_working_tree(path, bufp, sizep, 1);\n }\n \n-static int forcecrlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep)\n+static char *forcecrlf_to_working_tree(const char *path, const char *bufp, unsigned long *sizep)\n {\n \treturn crlf_to_working_tree(path, bufp, sizep, 0);\n }\n@@ -240,7 +236,7 @@ static int git_path_check_crlf(const char *path)\n \treturn -1;\n }\n \n-int convert_to_git(const char *path, char **bufp, unsigned long *sizep)\n+char *convert_to_git(const char *path, const char *bufp, unsigned long *sizep)\n {\n \tswitch (git_path_check_crlf(path)) {\n \tcase 0:\n@@ -252,7 +248,7 @@ int convert_to_git(const char *path, char **bufp, unsigned long *sizep)\n \t}\n }\n \n-int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep)\n+char *convert_to_working_tree(const char *path, const char *bufp, unsigned long *sizep)\n {\n \tswitch (git_path_check_crlf(path)) {\n \tcase 0:\ndiff --git a/diff.c b/diff.c\nindex 294183b..3d36ce1 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -1513,9 +1513,9 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)\n \t\t/*\n \t\t * Convert from working tree format to canonical git format\n \t\t */\n-\t\tbuf = s->data;\n \t\tsize = s->size;\n-\t\tif (convert_to_git(s->path, &buf, &size)) {\n+\t\tbuf = convert_to_git(s->path, s->data, &size);\n+\t\tif (buf) {\n \t\t\tmunmap(s->data, s->size);\n \t\t\ts->should_munmap = 0;\n \t\t\ts->data = buf;\ndiff --git a/entry.c b/entry.c\nindex 50ffae4..84f7802 100644\n--- a/entry.c\n+++ b/entry.c\n@@ -82,7 +82,7 @@ static int write_entry(struct cache_entry *ce, char *path, struct checkout *stat\n \n \tswitch (ntohl(ce->ce_mode) & S_IFMT) {\n \t\tchar *buf, *new;\n-\t\tunsigned long size, nsize;\n+\t\tunsigned long size;\n \n \tcase S_IFREG:\n \t\tnew = read_blob_entry(ce, path, &size);\n@@ -103,12 +103,10 @@ static int write_entry(struct cache_entry *ce, char *path, struct checkout *stat\n \t\t/*\n \t\t * Convert from git internal format to working tree format\n \t\t */\n-\t\tbuf = new;\n-\t\tnsize = size;\n-\t\tif (convert_to_working_tree(ce->name, &buf, &nsize)) {\n+\t\tbuf = convert_to_working_tree(ce->name, new, &size);\n+\t\tif (buf) {\n \t\t\tfree(new);\n \t\t\tnew = buf;\n-\t\t\tsize = nsize;\n \t\t}\n \n \t\twrote = write_in_full(fd, new, size);\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 0c0fcc5..0642664 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -2338,10 +2338,9 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,\n \t */\n \tif ((type == OBJ_BLOB) && S_ISREG(st->st_mode)) {\n \t\tunsigned long nsize = size;\n-\t\tchar *nbuf = buf;\n-\t\tif (convert_to_git(path, &nbuf, &nsize)) {\n-\t\t\tif (size)\n-\t\t\t\tmunmap(buf, size);\n+\t\tchar *nbuf = convert_to_git(path, buf, &nsize);\n+\t\tif (nbuf) {\n+\t\t\tmunmap(buf, size);\n \t\t\tsize = nsize;\n \t\t\tbuf = nbuf;\n \t\t\tre_allocated = 1;\n-- \n1.5.1.1.916.g7d4b\n"}]}