From af6eb82262e35687aa8f00d688e327cb845973fa Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Thu, 6 Sep 2007 13:20:09 +0200 Subject: Use strbuf API in apply, blame, commit-tree and diff Signed-off-by: Pierre Habouzit Signed-off-by: Junio C Hamano --- builtin-blame.c | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) (limited to 'builtin-blame.c') diff --git a/builtin-blame.c b/builtin-blame.c index dc88a953a5..1b1e6da853 100644 --- a/builtin-blame.c +++ b/builtin-blame.c @@ -18,6 +18,7 @@ #include "cache-tree.h" #include "path-list.h" #include "mailmap.h" +#include "strbuf.h" static char blame_usage[] = "git-blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-p] [-w] [-L n,m] [-S ] [-M] [-C] [-C] [--contents ] [--incremental] [commit] [--] file\n" @@ -2001,11 +2002,10 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con struct commit *commit; struct origin *origin; unsigned char head_sha1[20]; - char *buf; + struct strbuf buf; const char *ident; int fd; time_t now; - unsigned long fin_size; int size, len; struct cache_entry *ce; unsigned mode; @@ -2023,9 +2023,11 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con origin = make_origin(commit, path); + strbuf_init(&buf); if (!contents_from || strcmp("-", contents_from)) { struct stat st; const char *read_from; + unsigned long fin_size; if (contents_from) { if (stat(contents_from, &st) < 0) @@ -2038,19 +2040,19 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con read_from = path; } fin_size = xsize_t(st.st_size); - buf = xmalloc(fin_size+1); mode = canon_mode(st.st_mode); switch (st.st_mode & S_IFMT) { case S_IFREG: fd = open(read_from, O_RDONLY); if (fd < 0) die("cannot open %s", read_from); - if (read_in_full(fd, buf, fin_size) != fin_size) + if (strbuf_read(&buf, fd) != xsize_t(st.st_size)) die("cannot read %s", read_from); break; case S_IFLNK: - if (readlink(read_from, buf, fin_size+1) != fin_size) + if (readlink(read_from, buf.buf, buf.alloc) != fin_size) die("cannot readlink %s", read_from); + buf.len = fin_size; break; default: die("unsupported file type %s", read_from); @@ -2059,26 +2061,13 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con else { /* Reading from stdin */ contents_from = "standard input"; - buf = NULL; - fin_size = 0; mode = 0; - while (1) { - ssize_t cnt = 8192; - buf = xrealloc(buf, fin_size + cnt); - cnt = xread(0, buf + fin_size, cnt); - if (cnt < 0) - die("read error %s from stdin", - strerror(errno)); - if (!cnt) - break; - fin_size += cnt; - } - buf = xrealloc(buf, fin_size + 1); + if (strbuf_read(&buf, 0) < 0) + die("read error %s from stdin", strerror(errno)); } - buf[fin_size] = 0; - origin->file.ptr = buf; - origin->file.size = fin_size; - pretend_sha1_file(buf, fin_size, OBJ_BLOB, origin->blob_sha1); + origin->file.ptr = buf.buf; + origin->file.size = buf.len; + pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1); commit->util = origin; /* -- cgit v1.2.1 From f1696ee398e92bcea3cdc7b3da85d8e0f77f6c50 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Mon, 10 Sep 2007 12:35:04 +0200 Subject: Strbuf API extensions and fixes. * Add strbuf_rtrim to remove trailing spaces. * Add strbuf_insert to insert data at a given position. * Off-by one fix in strbuf_addf: strbuf_avail() does not counts the final \0 so the overflow test for snprintf is the strict comparison. This is not critical as the growth mechanism chosen will always allocate _more_ memory than asked, so the second test will not fail. It's some kind of miracle though. * Add size extension hints for strbuf_init and strbuf_read. If 0, default applies, else: + initial buffer has the given size for strbuf_init. + first growth checks it has at least this size rather than the default 8192. Signed-off-by: Pierre Habouzit Signed-off-by: Junio C Hamano --- builtin-blame.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'builtin-blame.c') diff --git a/builtin-blame.c b/builtin-blame.c index 1b1e6da853..b004f06cd8 100644 --- a/builtin-blame.c +++ b/builtin-blame.c @@ -2023,7 +2023,7 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con origin = make_origin(commit, path); - strbuf_init(&buf); + strbuf_init(&buf, 0); if (!contents_from || strcmp("-", contents_from)) { struct stat st; const char *read_from; @@ -2046,7 +2046,7 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con fd = open(read_from, O_RDONLY); if (fd < 0) die("cannot open %s", read_from); - if (strbuf_read(&buf, fd) != xsize_t(st.st_size)) + if (strbuf_read(&buf, fd, 0) != xsize_t(st.st_size)) die("cannot read %s", read_from); break; case S_IFLNK: @@ -2062,7 +2062,7 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con /* Reading from stdin */ contents_from = "standard input"; mode = 0; - if (strbuf_read(&buf, 0) < 0) + if (strbuf_read(&buf, 0, 0) < 0) die("read error %s from stdin", strerror(errno)); } origin->file.ptr = buf.buf; -- cgit v1.2.1 From ba3ed09728cb25e004d3b732de14fca8aeb602f6 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sat, 15 Sep 2007 15:56:50 +0200 Subject: Now that cache.h needs strbuf.h, remove useless includes. Signed-off-by: Pierre Habouzit Acked-by: Linus Torvalds Signed-off-by: Junio C Hamano --- builtin-blame.c | 1 - 1 file changed, 1 deletion(-) (limited to 'builtin-blame.c') diff --git a/builtin-blame.c b/builtin-blame.c index b004f06cd8..e364b6c6c0 100644 --- a/builtin-blame.c +++ b/builtin-blame.c @@ -18,7 +18,6 @@ #include "cache-tree.h" #include "path-list.h" #include "mailmap.h" -#include "strbuf.h" static char blame_usage[] = "git-blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-p] [-w] [-L n,m] [-S ] [-M] [-C] [-C] [--contents ] [--incremental] [commit] [--] file\n" -- cgit v1.2.1 From 663af3422a648e87945e4d8c0cc3e13671f2bbde Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Thu, 20 Sep 2007 00:42:15 +0200 Subject: Full rework of quote_c_style and write_name_quoted. * quote_c_style works on a strbuf instead of a wild buffer. * quote_c_style is now clever enough to not add double quotes if not needed. * write_name_quoted inherits those advantages, but also take a different set of arguments. Now instead of asking for quotes or not, you pass a "terminator". If it's \0 then we assume you don't want to escape, else C escaping is performed. In any case, the terminator is also appended to the stream. It also no longer takes the prefix/prefix_len arguments, as it's seldomly used, and makes some optimizations harder. * write_name_quotedpfx is created to work like write_name_quoted and take the prefix/prefix_len arguments. Thanks to those API changes, diff.c has somehow lost weight, thanks to the removal of functions that were wrappers around the old write_name_quoted trying to give it a semantics like the new one, but performing a lot of allocations for this goal. Now we always write directly to the stream, no intermediate allocation is performed. As a side effect of the refactor in builtin-apply.c, the length of the bar graphs in diffstats are not affected anymore by the fact that the path was clipped. Signed-off-by: Pierre Habouzit --- builtin-blame.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'builtin-blame.c') diff --git a/builtin-blame.c b/builtin-blame.c index e364b6c6c0..16c0ca8206 100644 --- a/builtin-blame.c +++ b/builtin-blame.c @@ -1430,8 +1430,7 @@ static void get_commit_info(struct commit *commit, static void write_filename_info(const char *path) { printf("filename "); - write_name_quoted(NULL, 0, path, 1, stdout); - putchar('\n'); + write_name_quoted(path, stdout, '\n'); } /* -- cgit v1.2.1 From 387e7e19d7eb5444be8da8e99ed7491989dc1cbb Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Thu, 27 Sep 2007 15:25:55 +0200 Subject: strbuf_read_file enhancement, and use it. * make strbuf_read_file take a size hint (works like strbuf_read) * use it in a couple of places. Signed-off-by: Pierre Habouzit Signed-off-by: Junio C Hamano --- builtin-blame.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'builtin-blame.c') diff --git a/builtin-blame.c b/builtin-blame.c index 16c0ca8206..e3112a2d5b 100644 --- a/builtin-blame.c +++ b/builtin-blame.c @@ -2002,7 +2002,6 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con unsigned char head_sha1[20]; struct strbuf buf; const char *ident; - int fd; time_t now; int size, len; struct cache_entry *ce; @@ -2041,11 +2040,8 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con mode = canon_mode(st.st_mode); switch (st.st_mode & S_IFMT) { case S_IFREG: - fd = open(read_from, O_RDONLY); - if (fd < 0) - die("cannot open %s", read_from); - if (strbuf_read(&buf, fd, 0) != xsize_t(st.st_size)) - die("cannot read %s", read_from); + if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size) + die("cannot open or read %s", read_from); break; case S_IFLNK: if (readlink(read_from, buf.buf, buf.alloc) != fin_size) -- cgit v1.2.1