Skip to content

Commit 1fc8345

Browse files
committed
Merge branch 'rs/code-cleaning'
* rs/code-cleaning: fsck: simplify fsck_commit_buffer() by using commit_list_count() commit: use commit_list_append() instead of duplicating its code merge: simplify merge_trivial() by using commit_list_append() use strbuf_addch for adding single characters use strbuf_addbuf for adding strbufs
2 parents f357797 + 9d02150 commit 1fc8345

File tree

10 files changed

+19
-36
lines changed

10 files changed

+19
-36
lines changed

builtin/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ static void add_branch_description(struct strbuf *buf, const char *branch_name)
861861
read_branch_desc(&desc, branch_name);
862862
if (desc.len) {
863863
strbuf_addch(buf, '\n');
864-
strbuf_add(buf, desc.buf, desc.len);
864+
strbuf_addbuf(buf, &desc);
865865
strbuf_addch(buf, '\n');
866866
}
867867
}

builtin/merge.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -839,16 +839,14 @@ static void prepare_to_commit(struct commit_list *remoteheads)
839839
static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
840840
{
841841
unsigned char result_tree[20], result_commit[20];
842-
struct commit_list *parent = xmalloc(sizeof(*parent));
842+
struct commit_list *parents, **pptr = &parents;
843843

844844
write_tree_trivial(result_tree);
845845
printf(_("Wonderful.\n"));
846-
parent->item = head;
847-
parent->next = xmalloc(sizeof(*parent->next));
848-
parent->next->item = remoteheads->item;
849-
parent->next->next = NULL;
846+
pptr = commit_list_append(head, pptr);
847+
pptr = commit_list_append(remoteheads->item, pptr);
850848
prepare_to_commit(remoteheads);
851-
if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parent,
849+
if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parents,
852850
result_commit, NULL, sign_commit))
853851
die(_("failed to write commit object"));
854852
finish(head, remoteheads, result_commit, "In-index merge");

commit.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -447,12 +447,7 @@ struct commit_list *copy_commit_list(struct commit_list *list)
447447
struct commit_list *head = NULL;
448448
struct commit_list **pp = &head;
449449
while (list) {
450-
struct commit_list *new;
451-
new = xmalloc(sizeof(struct commit_list));
452-
new->item = list->item;
453-
new->next = NULL;
454-
*pp = new;
455-
pp = &new->next;
450+
pp = commit_list_append(list->item, pp);
456451
list = list->next;
457452
}
458453
return head;

fsck.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ static int fsck_commit_buffer(struct commit *commit, const char *buffer,
281281
{
282282
unsigned char tree_sha1[20], sha1[20];
283283
struct commit_graft *graft;
284-
int parents = 0;
284+
unsigned parent_count, parent_line_count = 0;
285285
int err;
286286

287287
if (!skip_prefix(buffer, "tree ", &buffer))
@@ -293,27 +293,17 @@ static int fsck_commit_buffer(struct commit *commit, const char *buffer,
293293
if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n')
294294
return error_func(&commit->object, FSCK_ERROR, "invalid 'parent' line format - bad sha1");
295295
buffer += 41;
296-
parents++;
296+
parent_line_count++;
297297
}
298298
graft = lookup_commit_graft(commit->object.sha1);
299+
parent_count = commit_list_count(commit->parents);
299300
if (graft) {
300-
struct commit_list *p = commit->parents;
301-
parents = 0;
302-
while (p) {
303-
p = p->next;
304-
parents++;
305-
}
306-
if (graft->nr_parent == -1 && !parents)
301+
if (graft->nr_parent == -1 && !parent_count)
307302
; /* shallow commit */
308-
else if (graft->nr_parent != parents)
303+
else if (graft->nr_parent != parent_count)
309304
return error_func(&commit->object, FSCK_ERROR, "graft objects missing");
310305
} else {
311-
struct commit_list *p = commit->parents;
312-
while (p && parents) {
313-
p = p->next;
314-
parents--;
315-
}
316-
if (p || parents)
306+
if (parent_count != parent_line_count)
317307
return error_func(&commit->object, FSCK_ERROR, "parent objects missing");
318308
}
319309
if (!skip_prefix(buffer, "author ", &buffer))

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ static void output(struct merge_options *o, int v, const char *fmt, ...)
171171
strbuf_vaddf(&o->obuf, fmt, ap);
172172
va_end(ap);
173173

174-
strbuf_add(&o->obuf, "\n", 1);
174+
strbuf_addch(&o->obuf, '\n');
175175
if (!o->buffer_output)
176176
flush_output(o);
177177
}

pathspec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ static void NORETURN unsupported_magic(const char *pattern,
338338
if (!(magic & m->bit))
339339
continue;
340340
if (sb.len)
341-
strbuf_addstr(&sb, " ");
341+
strbuf_addch(&sb, ' ');
342342
if (short_magic & m->bit)
343343
strbuf_addf(&sb, "'%c'", m->mnemonic);
344344
else

pretty.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
13761376
case trunc_none:
13771377
break;
13781378
}
1379-
strbuf_addstr(sb, local_sb.buf);
1379+
strbuf_addbuf(sb, &local_sb);
13801380
} else {
13811381
int sb_len = sb->len, offset = 0;
13821382
if (c->flush_type == flush_left)

rerere.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@ static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_siz
207207
strbuf_reset(&one);
208208
strbuf_reset(&two);
209209
} else if (hunk == RR_SIDE_1)
210-
strbuf_addstr(&one, buf.buf);
210+
strbuf_addbuf(&one, &buf);
211211
else if (hunk == RR_ORIGINAL)
212212
; /* discard */
213213
else if (hunk == RR_SIDE_2)
214-
strbuf_addstr(&two, buf.buf);
214+
strbuf_addbuf(&two, &buf);
215215
else
216216
rerere_io_putstr(buf.buf, io);
217217
continue;

sha1_name.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ static int interpret_nth_prior_checkout(const char *name, int namelen,
946946
retval = 0;
947947
if (0 < for_each_reflog_ent_reverse("HEAD", grab_nth_branch_switch, &cb)) {
948948
strbuf_reset(buf);
949-
strbuf_add(buf, cb.buf.buf, cb.buf.len);
949+
strbuf_addbuf(buf, &cb.buf);
950950
retval = brace - name + 1;
951951
}
952952

url.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ void end_url_with_slash(struct strbuf *buf, const char *url)
121121
{
122122
strbuf_addstr(buf, url);
123123
if (buf->len && buf->buf[buf->len - 1] != '/')
124-
strbuf_addstr(buf, "/");
124+
strbuf_addch(buf, '/');
125125
}
126126

127127
void str_end_url_with_slash(const char *url, char **dest) {

0 commit comments

Comments
 (0)