builtin-commit: use commit_tree()
First, it adds less code than removes, second this allows us to use recuce_heads() for parents, so that the parents of a merge will be always the same with or without a conflict. Signed-off-by: Miklos Vajna <vmiklos@frugalware.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Miklos Vajna committed
Sep 10, 2008 at 22:10 UTC
6bb6b0341af26739ba305b20c62a433f15029049
1 file changed
+16
-51
builtin-commit.c
+16
-51
@@ -667,14 +667,14 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
667
}
668
669
/*
670
- * Find out if the message starting at position 'start' in the strbuf
671
- * contains only whitespace and Signed-off-by lines.
670
+ * Find out if the message in the strbuf contains only whitespace and
671
+ * Signed-off-by lines.
672
*/
673
-static int message_is_empty(struct strbuf *sb, int start)
673
+static int message_is_empty(struct strbuf *sb)
674
{
675
struct strbuf tmpl;
676
const char *nl;
677
- int eol, i;
677
+ int eol, i, start = 0;
678
679
if (cleanup_mode == CLEANUP_NONE && sb->len)
680
return 0;
@@ -929,34 +929,14 @@ static const char commit_utf8_warn[] =
929
"You may want to amend it after fixing the message, or set the config\n"
930
"variable i18n.commitencoding to the encoding your project uses.\n";
931
932
-static void add_parent(struct strbuf *sb, const unsigned char *sha1)
933
-{
934
- struct object *obj = parse_object(sha1);
935
- const char *parent = sha1_to_hex(sha1);
936
- const char *cp;
937
-
938
- if (!obj)
939
- die("Unable to find commit parent %s", parent);
940
- if (obj->type != OBJ_COMMIT)
941
- die("Parent %s isn't a proper commit", parent);
942
-
943
- for (cp = sb->buf; cp && (cp = strstr(cp, "\nparent ")); cp += 8) {
944
- if (!memcmp(cp + 8, parent, 40) && cp[48] == '\n') {
945
- error("duplicate parent %s ignored", parent);
946
- return;
947
- }
948
- }
949
- strbuf_addf(sb, "parent %s\n", parent);
950
-}
951
-
932
int cmd_commit(int argc, const char **argv, const char *prefix)
933
{
954
- int header_len;
934
struct strbuf sb;
935
const char *index_file, *reflog_msg;
936
char *nl, *p;
937
unsigned char commit_sha1[20];
938
struct ref_lock *ref_lock;
939
+ struct commit_list *parents = NULL, **pptr = &parents;
940
941
git_config(git_commit_config, NULL);
942
@@ -971,13 +951,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
951
return 1;
952
}
953
974
- /*
975
- * The commit object
976
- */
977
- strbuf_init(&sb, 0);
978
- strbuf_addf(&sb, "tree %s\n",
979
- sha1_to_hex(active_cache_tree->sha1));
980
-
954
/* Determine parents */
955
if (initial_commit) {
956
reflog_msg = "commit (initial)";
@@ -991,13 +964,13 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
964
die("could not parse HEAD commit");
965
966
for (c = commit->parents; c; c = c->next)
994
- add_parent(&sb, c->item->object.sha1);
967
+ pptr = &commit_list_insert(c->item, pptr)->next;
968
} else if (in_merge) {
969
struct strbuf m;
970
FILE *fp;
971
972
reflog_msg = "commit (merge)";
1000
- add_parent(&sb, head_sha1);
973
+ pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next;
974
strbuf_init(&m, 0);
975
fp = fopen(git_path("MERGE_HEAD"), "r");
976
if (fp == NULL)
@@ -1007,24 +980,18 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
980
unsigned char sha1[20];
981
if (get_sha1_hex(m.buf, sha1) < 0)
982
die("Corrupt MERGE_HEAD file (%s)", m.buf);
1010
- add_parent(&sb, sha1);
983
+ pptr = &commit_list_insert(lookup_commit(sha1), pptr)->next;
984
}
985
fclose(fp);
986
strbuf_release(&m);
987
} else {
988
reflog_msg = "commit";
1016
- strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1));
989
+ pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next;
990
}
1018
-
1019
- strbuf_addf(&sb, "author %s\n",
1020
- fmt_ident(author_name, author_email, author_date, IDENT_ERROR_ON_NO_NAME));
1021
- strbuf_addf(&sb, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
1022
- if (!is_encoding_utf8(git_commit_encoding))
1023
- strbuf_addf(&sb, "encoding %s\n", git_commit_encoding);
1024
- strbuf_addch(&sb, '\n');
991
+ parents = reduce_heads(parents);
992
993
/* Finally, get the commit message */
1027
- header_len = sb.len;
994
+ strbuf_init(&sb, 0);
995
if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
996
rollback_index_files();
997
die("could not read commit message");
@@ -1037,16 +1004,15 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1004
1005
if (cleanup_mode != CLEANUP_NONE)
1006
stripspace(&sb, cleanup_mode == CLEANUP_ALL);
1040
- if (sb.len < header_len || message_is_empty(&sb, header_len)) {
1007
+ if (message_is_empty(&sb)) {
1008
rollback_index_files();
1009
fprintf(stderr, "Aborting commit due to empty commit message.\n");
1010
exit(1);
1011
}
1045
- strbuf_addch(&sb, '\0');
1046
- if (is_encoding_utf8(git_commit_encoding) && !is_utf8(sb.buf))
1047
- fprintf(stderr, commit_utf8_warn);
1012
1049
- if (write_sha1_file(sb.buf, sb.len - 1, commit_type, commit_sha1)) {
1013
+ if (commit_tree(sb.buf, active_cache_tree->sha1, parents, commit_sha1,
1014
+ fmt_ident(author_name, author_email, author_date,
1015
+ IDENT_ERROR_ON_NO_NAME))) {
1016
rollback_index_files();
1017
die("failed to write commit object");
1018
}
@@ -1055,12 +1021,11 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1021
initial_commit ? NULL : head_sha1,
1022
0);
1023
1058
- nl = strchr(sb.buf + header_len, '\n');
1024
+ nl = strchr(sb.buf, '\n');
1025
if (nl)
1026
strbuf_setlen(&sb, nl + 1 - sb.buf);
1027
else
1028
strbuf_addch(&sb, '\n');
1063
- strbuf_remove(&sb, 0, header_len);
1029
strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
1030
strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
1031