]> asedeno.scripts.mit.edu Git - git.git/blobdiff - diff.c
Drop strbuf's 'eof' marker, and make read_line a first class citizen.
[git.git] / diff.c
diff --git a/diff.c b/diff.c
index 1aca5df522d1c5249c9d1ce8d030849d59a5eb6a..56b672c4f0cb6287f03230e11b18ebf8fd81b744 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -1545,26 +1545,16 @@ static int reuse_worktree_file(const char *name, const unsigned char *sha1, int
 
 static int populate_from_stdin(struct diff_filespec *s)
 {
-#define INCREMENT 1024
-       char *buf;
-       unsigned long size;
-       ssize_t got;
-
-       size = 0;
-       buf = NULL;
-       while (1) {
-               buf = xrealloc(buf, size + INCREMENT);
-               got = xread(0, buf + size, INCREMENT);
-               if (!got)
-                       break; /* EOF */
-               if (got < 0)
-                       return error("error while reading from stdin %s",
+       struct strbuf buf;
+
+       strbuf_init(&buf, 0);
+       if (strbuf_read(&buf, 0, 0) < 0)
+               return error("error while reading from stdin %s",
                                     strerror(errno));
-               size += got;
-       }
+
        s->should_munmap = 0;
-       s->data = buf;
-       s->size = size;
+       s->size = buf.len;
+       s->data = strbuf_detach(&buf);
        s->should_free = 1;
        return 0;
 }
@@ -1609,10 +1599,9 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
 
        if (!s->sha1_valid ||
            reuse_worktree_file(s->path, s->sha1, 0)) {
+               struct strbuf buf;
                struct stat st;
                int fd;
-               char *buf;
-               unsigned long size;
 
                if (!strcmp(s->path, "-"))
                        return populate_from_stdin(s);
@@ -1653,13 +1642,12 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
                /*
                 * Convert from working tree format to canonical git format
                 */
-               size = s->size;
-               buf = convert_to_git(s->path, s->data, &size);
-               if (buf) {
+               strbuf_init(&buf, 0);
+               if (convert_to_git(s->path, s->data, s->size, &buf)) {
                        munmap(s->data, s->size);
                        s->should_munmap = 0;
-                       s->data = buf;
-                       s->size = size;
+                       s->data = buf.buf;
+                       s->size = buf.len;
                        s->should_free = 1;
                }
        }