From e992d1eb39033206dd44143ff28214b837c3d89f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Thu, 10 Jul 2014 10:52:21 +0200 Subject: [PATCH 1/5] use strbuf_addbuf for adding strbufs Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- builtin/log.c | 2 +- pretty.c | 2 +- rerere.c | 4 ++-- sha1_name.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/builtin/log.c b/builtin/log.c index 27c1b65db4..4389722b4b 100644 --- a/builtin/log.c +++ b/builtin/log.c @@ -861,7 +861,7 @@ static void add_branch_description(struct strbuf *buf, const char *branch_name) read_branch_desc(&desc, branch_name); if (desc.len) { strbuf_addch(buf, '\n'); - strbuf_add(buf, desc.buf, desc.len); + strbuf_addbuf(buf, &desc); strbuf_addch(buf, '\n'); } } diff --git a/pretty.c b/pretty.c index 8d201f6bda..6e54934723 100644 --- a/pretty.c +++ b/pretty.c @@ -1376,7 +1376,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */ case trunc_none: break; } - strbuf_addstr(sb, local_sb.buf); + strbuf_addbuf(sb, &local_sb); } else { int sb_len = sb->len, offset = 0; if (c->flush_type == flush_left) diff --git a/rerere.c b/rerere.c index d55aa8a01b..04d923d8cf 100644 --- a/rerere.c +++ b/rerere.c @@ -207,11 +207,11 @@ static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_siz strbuf_reset(&one); strbuf_reset(&two); } else if (hunk == RR_SIDE_1) - strbuf_addstr(&one, buf.buf); + strbuf_addbuf(&one, &buf); else if (hunk == RR_ORIGINAL) ; /* discard */ else if (hunk == RR_SIDE_2) - strbuf_addstr(&two, buf.buf); + strbuf_addbuf(&two, &buf); else rerere_io_putstr(buf.buf, io); continue; diff --git a/sha1_name.c b/sha1_name.c index 5bfa841699..6ccd3a53f8 100644 --- a/sha1_name.c +++ b/sha1_name.c @@ -946,7 +946,7 @@ static int interpret_nth_prior_checkout(const char *name, int namelen, retval = 0; if (0 < for_each_reflog_ent_reverse("HEAD", grab_nth_branch_switch, &cb)) { strbuf_reset(buf); - strbuf_add(buf, cb.buf.buf, cb.buf.len); + strbuf_addbuf(buf, &cb.buf); retval = brace - name + 1; } From 294b2680cd89234618e329e090b68dc69cc41a37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Thu, 10 Jul 2014 10:54:24 +0200 Subject: [PATCH 2/5] use strbuf_addch for adding single characters Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- merge-recursive.c | 2 +- pathspec.c | 2 +- url.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/merge-recursive.c b/merge-recursive.c index b5c3c5314f..fad7b2c2b8 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -171,7 +171,7 @@ static void output(struct merge_options *o, int v, const char *fmt, ...) strbuf_vaddf(&o->obuf, fmt, ap); va_end(ap); - strbuf_add(&o->obuf, "\n", 1); + strbuf_addch(&o->obuf, '\n'); if (!o->buffer_output) flush_output(o); } diff --git a/pathspec.c b/pathspec.c index 8043099955..89f2c8ffff 100644 --- a/pathspec.c +++ b/pathspec.c @@ -338,7 +338,7 @@ static void NORETURN unsupported_magic(const char *pattern, if (!(magic & m->bit)) continue; if (sb.len) - strbuf_addstr(&sb, " "); + strbuf_addch(&sb, ' '); if (short_magic & m->bit) strbuf_addf(&sb, "'%c'", m->mnemonic); else diff --git a/url.c b/url.c index 335d97d3f7..7ca2a69e10 100644 --- a/url.c +++ b/url.c @@ -121,7 +121,7 @@ void end_url_with_slash(struct strbuf *buf, const char *url) { strbuf_addstr(buf, url); if (buf->len && buf->buf[buf->len - 1] != '/') - strbuf_addstr(buf, "/"); + strbuf_addch(buf, '/'); } void str_end_url_with_slash(const char *url, char **dest) { From 910a09a7350a556f7f367680294fca8d05ddc5f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Thu, 10 Jul 2014 11:41:40 +0200 Subject: [PATCH 3/5] merge: simplify merge_trivial() by using commit_list_append() Build the commit_list of parents by calling commit_list_append() twice instead of allocating and linking the items by hand. This makes the code shorter and simpler. Rename the commit_list from parent to parents (plural) while at it because there are two of them. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- builtin/merge.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/builtin/merge.c b/builtin/merge.c index b49c310866..f50270efb1 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -843,16 +843,14 @@ static void prepare_to_commit(struct commit_list *remoteheads) static int merge_trivial(struct commit *head, struct commit_list *remoteheads) { unsigned char result_tree[20], result_commit[20]; - struct commit_list *parent = xmalloc(sizeof(*parent)); + struct commit_list *parents, **pptr = &parents; write_tree_trivial(result_tree); printf(_("Wonderful.\n")); - parent->item = head; - parent->next = xmalloc(sizeof(*parent->next)); - parent->next->item = remoteheads->item; - parent->next->next = NULL; + pptr = commit_list_append(head, pptr); + pptr = commit_list_append(remoteheads->item, pptr); prepare_to_commit(remoteheads); - if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parent, + if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parents, result_commit, NULL, sign_commit)) die(_("failed to write commit object")); finish(head, remoteheads, result_commit, "In-index merge"); From cb979dbd8fead65e579442c25b620fdc401227b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Thu, 10 Jul 2014 11:47:47 +0200 Subject: [PATCH 4/5] commit: use commit_list_append() instead of duplicating its code Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- commit.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/commit.c b/commit.c index fb7897c2a4..61d2e13f48 100644 --- a/commit.c +++ b/commit.c @@ -447,12 +447,7 @@ struct commit_list *copy_commit_list(struct commit_list *list) struct commit_list *head = NULL; struct commit_list **pp = &head; while (list) { - struct commit_list *new; - new = xmalloc(sizeof(struct commit_list)); - new->item = list->item; - new->next = NULL; - *pp = new; - pp = &new->next; + pp = commit_list_append(list->item, pp); list = list->next; } return head; From 9d02150cf4d833935161ef265e4dc03807caa800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Thu, 10 Jul 2014 11:48:26 +0200 Subject: [PATCH 5/5] fsck: simplify fsck_commit_buffer() by using commit_list_count() fsck_commit_buffer() checks that the number of items in the parents list of a commit matches the number of parent lines in its buffer or -- if a graft is used -- the number of parents in that graft. Simplify the code by using commit_list_count() instead of counting by hand. Also use different variables for the number of lines and the number of list items, making it easier to compare them. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- fsck.c | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/fsck.c b/fsck.c index a4e8593e78..56156fff44 100644 --- a/fsck.c +++ b/fsck.c @@ -281,7 +281,7 @@ static int fsck_commit_buffer(struct commit *commit, const char *buffer, { unsigned char tree_sha1[20], sha1[20]; struct commit_graft *graft; - int parents = 0; + unsigned parent_count, parent_line_count = 0; int err; if (!skip_prefix(buffer, "tree ", &buffer)) @@ -293,27 +293,17 @@ static int fsck_commit_buffer(struct commit *commit, const char *buffer, if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n') return error_func(&commit->object, FSCK_ERROR, "invalid 'parent' line format - bad sha1"); buffer += 41; - parents++; + parent_line_count++; } graft = lookup_commit_graft(commit->object.sha1); + parent_count = commit_list_count(commit->parents); if (graft) { - struct commit_list *p = commit->parents; - parents = 0; - while (p) { - p = p->next; - parents++; - } - if (graft->nr_parent == -1 && !parents) + if (graft->nr_parent == -1 && !parent_count) ; /* shallow commit */ - else if (graft->nr_parent != parents) + else if (graft->nr_parent != parent_count) return error_func(&commit->object, FSCK_ERROR, "graft objects missing"); } else { - struct commit_list *p = commit->parents; - while (p && parents) { - p = p->next; - parents--; - } - if (p || parents) + if (parent_count != parent_line_count) return error_func(&commit->object, FSCK_ERROR, "parent objects missing"); } if (!skip_prefix(buffer, "author ", &buffer))