commit: simplify building parents list
Push pptr down into the FROM_MERGE branch of the if/else statement, where it's actually used, and call commit_list_append() for appending elements instead of playing tricks with commit_list_insert(). Call copy_commit_list() in the amend branch instead of open-coding it. Don't bother setting pptr in the final branch as it's not used thereafter. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Oct 29, 2016 at 14:55 UTC
de9f7fa3b08ae0554e57f6a6f5de2559d5bcb871
1 file changed
+6
-8
builtin/commit.c
+6
-8
@@ -1639,7 +1639,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1639
const char *index_file, *reflog_msg;
1640
char *nl;
1641
unsigned char sha1[20];
1642
- struct commit_list *parents = NULL, **pptr = &parents;
1642
+ struct commit_list *parents = NULL;
1643
struct stat statbuf;
1644
struct commit *current_head = NULL;
1645
struct commit_extra_header *extra = NULL;
@@ -1681,20 +1681,18 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1681
if (!reflog_msg)
1682
reflog_msg = "commit (initial)";
1683
} else if (amend) {
1684
- struct commit_list *c;
1685
-
1684
if (!reflog_msg)
1685
reflog_msg = "commit (amend)";
1688
- for (c = current_head->parents; c; c = c->next)
1689
- pptr = &commit_list_insert(c->item, pptr)->next;
1686
+ parents = copy_commit_list(current_head->parents);
1687
} else if (whence == FROM_MERGE) {
1688
struct strbuf m = STRBUF_INIT;
1689
FILE *fp;
1690
int allow_fast_forward = 1;
1691
+ struct commit_list **pptr = &parents;
1692
1693
if (!reflog_msg)
1694
reflog_msg = "commit (merge)";
1697
- pptr = &commit_list_insert(current_head, pptr)->next;
1695
+ pptr = commit_list_append(current_head, pptr);
1696
fp = fopen(git_path_merge_head(), "r");
1697
if (fp == NULL)
1698
die_errno(_("could not open '%s' for reading"),
@@ -1705,7 +1703,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1703
parent = get_merge_parent(m.buf);
1704
if (!parent)
1705
die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
1708
- pptr = &commit_list_insert(parent, pptr)->next;
1706
+ pptr = commit_list_append(parent, pptr);
1707
}
1708
fclose(fp);
1709
strbuf_release(&m);
@@ -1722,7 +1720,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1720
reflog_msg = (whence == FROM_CHERRY_PICK)
1721
? "commit (cherry-pick)"
1722
: "commit";
1725
- pptr = &commit_list_insert(current_head, pptr)->next;
1723
+ commit_list_insert(current_head, &parents);
1724
}
1725
1726
/* Finally, get the commit message */