{"thread":{"id":"11634","subject":"Make builtin-commit.c more careful about parenthood","startedAt":"2008-01-16T00:12:33Z","lastAt":"2008-01-16T00:12:33Z","messageCount":1,"participants":["Linus Torvalds"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"65440","messageId":"alpine.LFD.1.00.0801151608520.2806@woody.linux-foundation.org","threadId":"11634","inReplyTo":null,"subject":"Make builtin-commit.c more careful about parenthood","fromName":"Linus Torvalds","fromEmail":"torvalds@linux-foundation.org","sentAt":"2008-01-16T00:12:33Z","receivedAt":"2008-01-16T00:12:33Z","isPatch":false,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\nWhen creating the commit object, be a whole lot more careful about making \nsure that the parent lines really are valid parent lines. Check things \nlike MERGE_HEAD having proper SHA1 lines in it, and double-check that all \nthe parents exist and are actually commits.\n\n\n\n---\nThis is related to the previous patch, in that it is meant to get us \nbetter error messages and behaviour with a corrupt repository where the \nHEAD is not a proper commit.\n\n builtin-commit.c |   24 +++++++++++++++++++-----\n 1 files changed, 19 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin-commit.c b/builtin-commit.c\nindex 6d2ca80..30586a1 100644\n--- a/builtin-commit.c\n+++ b/builtin-commit.c\n@@ -756,6 +756,17 @@ static const char commit_utf8_warn[] =\n \"You may want to amend it after fixing the message, or set the config\\n\"\n \"variable i18n.commitencoding to the encoding your project uses.\\n\";\n \n+static void add_parent(struct strbuf *sb, const unsigned char *sha1)\n+{\n+\tstruct object *obj = parse_object(sha1);\n+\tconst char *parent = sha1_to_hex(sha1);\n+\tif (!obj)\n+\t\tdie(\"Unable to find commit parent %s\", parent);\n+\tif (obj->type != OBJ_COMMIT)\n+\t\tdie(\"Parent %s isn't a proper commit\", parent);\n+\tstrbuf_addf(sb, \"parent %s\\n\", parent);\n+}\n+\n int cmd_commit(int argc, const char **argv, const char *prefix)\n {\n \tint header_len;\n@@ -818,21 +829,24 @@ int cmd_commit(int argc, const char **argv, const char *prefix)\n \t\t\tdie(\"could not parse HEAD commit\");\n \n \t\tfor (c = commit->parents; c; c = c->next)\n-\t\t\tstrbuf_addf(&sb, \"parent %s\\n\",\n-\t\t\t\t      sha1_to_hex(c->item->object.sha1));\n+\t\t\tadd_parent(&sb, c->item->object.sha1);\n \t} else if (in_merge) {\n \t\tstruct strbuf m;\n \t\tFILE *fp;\n \n \t\treflog_msg = \"commit (merge)\";\n-\t\tstrbuf_addf(&sb, \"parent %s\\n\", sha1_to_hex(head_sha1));\n+\t\tadd_parent(&sb, head_sha1);\n \t\tstrbuf_init(&m, 0);\n \t\tfp = fopen(git_path(\"MERGE_HEAD\"), \"r\");\n \t\tif (fp == NULL)\n \t\t\tdie(\"could not open %s for reading: %s\",\n \t\t\t    git_path(\"MERGE_HEAD\"), strerror(errno));\n-\t\twhile (strbuf_getline(&m, fp, '\\n') != EOF)\n-\t\t\tstrbuf_addf(&sb, \"parent %s\\n\", m.buf);\n+\t\twhile (strbuf_getline(&m, fp, '\\n') != EOF) {\n+\t\t\tunsigned char sha1[20];\n+\t\t\tif (get_sha1_hex(m.buf, sha1) < 0)\n+\t\t\t\tdie(\"Corrupt MERGE_HEAD file (%s)\", m.buf);\n+\t\t\tadd_parent(&sb, sha1);\n+\t\t}\n \t\tfclose(fp);\n \t\tstrbuf_release(&m);\n \t} else {\n"}]}