{"thread":{"id":"24758","subject":"[RFC/PATCH 1/2] commit: add parse_commit_repl() to replace commits at parsing time","startedAt":"2010-08-17T01:58:59Z","lastAt":"2010-08-20T04:04:41Z","messageCount":8,"participants":["Christian Couder","Junio C Hamano","Nguyen Thai Ngoc Duy","Jonathan Nieder"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"148211","messageId":"20100817015901.5592.25471.chriscool@tuxfamily.org","threadId":"24758","inReplyTo":null,"subject":"[RFC/PATCH 1/2] commit: add parse_commit_repl() to replace commits at parsing time","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2010-08-17T01:58:59Z","receivedAt":"2010-08-17T01:58:59Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"The function parse_commit() is not safe regarding replaced commits\nbecause it uses the buffer of the replacement commit but the object\npart of the commit struct stay the same. Especially the sha1 is not\nchanged so it doesn't match the content of the commit.\n\nTo fix that, this patch adds a new function that takes a\n\"struct commit **\" instead of a \"struct commit *\" so we can\nchange the commit pointer that is passed to us.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n commit.c |   43 +++++++++++++++++++++++++++++++++++++++++++\n commit.h |    6 ++++++\n 2 files changed, 49 insertions(+), 0 deletions(-)\n\ndiff --git a/commit.c b/commit.c\nindex 652c1ba..f170179 100644\n--- a/commit.c\n+++ b/commit.c\n@@ -316,6 +316,49 @@ int parse_commit(struct commit *item)\n \treturn ret;\n }\n \n+int parse_commit_repl(struct commit **commit)\n+{\n+\tenum object_type type;\n+\tvoid *buffer;\n+\tunsigned long size;\n+\tint ret;\n+\tconst unsigned char *repl;\n+\tstruct commit *item = *commit;\n+\n+\tif (!item)\n+\t\treturn -1;\n+\tif (item->object.parsed)\n+\t\treturn 0;\n+\tbuffer = read_sha1_file_repl(item->object.sha1, &type, &size, &repl);\n+\tif (!buffer)\n+\t\treturn error(\"Could not read %s\",\n+\t\t\t     sha1_to_hex(item->object.sha1));\n+\n+\tif (item->object.sha1 != repl) {\n+\t\tstruct commit *repl_item = lookup_commit(repl);\n+\t\tif (!repl_item) {\n+\t\t\tfree(buffer);\n+\t\t\treturn error(\"Bad replacement %s for commit %s\",\n+\t\t\t\t     sha1_to_hex(repl),\n+\t\t\t\t     sha1_to_hex(item->object.sha1));\n+\t\t}\n+\t\trepl_item->object.flags = item->object.flags;\n+\t\t*commit = item = repl_item;\n+\t} else if (type != OBJ_COMMIT) {\n+\t\tfree(buffer);\n+\t\treturn error(\"Object %s not a commit\",\n+\t\t\t     sha1_to_hex(item->object.sha1));\n+\t}\n+\n+\tret = parse_commit_buffer(item, buffer, size);\n+\tif (save_commit_buffer && !ret) {\n+\t\titem->buffer = buffer;\n+\t\treturn 0;\n+\t}\n+\tfree(buffer);\n+\treturn ret;\n+}\n+\n int find_commit_subject(const char *commit_buffer, const char **subject)\n {\n \tconst char *eol;\ndiff --git a/commit.h b/commit.h\nindex a3618f8..d3dfebb 100644\n--- a/commit.h\n+++ b/commit.h\n@@ -39,6 +39,12 @@ struct commit *lookup_commit_reference_gently(const unsigned char *sha1,\n \n int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size);\n \n+int parse_commit_repl(struct commit **item);\n+\n+/*\n+ * parse_commit() is deprecated, because it's buggy regarding replacements.\n+ * Use parse_commit_repl() instead.\n+ */\n int parse_commit(struct commit *item);\n \n /* Find beginning and length of commit subject. */\n-- \n1.7.2.1.351.g275bf\n"},{"id":"148307","messageId":"7vbp91aqfk.fsf@alter.siamese.dyndns.org","threadId":"24758","inReplyTo":"20100817015901.5592.25471.chriscool@tuxfamily.org","subject":"Re: [RFC/PATCH 1/2] commit: add parse_commit_repl() to replace commits at parsing time","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-08-17T21:18:39Z","receivedAt":"2010-08-17T21:18:39Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Christian Couder <chriscool@tuxfamily.org> writes:\n\n> The function parse_commit() is not safe regarding replaced commits\n> because it uses the buffer of the replacement commit but the object\n> part of the commit struct stay the same. Especially the sha1 is not\n> changed so it doesn't match the content of the commit.\n\nThis all sounds backwards to me, if I am reading the discussion correctly.\n\nIf a replace record says commit 0123 is replaced by commit 4567 (iow, 0123\nwas a mistake, and pretend that its content is what is recorded in 4567),\nand when we are honoring the replace records (iow, we are not fsck),\nshouldn't read_sha1(\"0123\") give us a piece of memory that stores what is\nrecorded in 4567, parse_object(\"0123\") return a struct commit whose buffer\npoints at a block of memory that has what is recorded in 4567 _while_ its\nobject.sha1[] say \"0123\"?\n\nWhat problem are you trying to solve?\n"},{"id":"148335","messageId":"AANLkTimu0r_3L7_YJgfMVb6saFOyOK-mHLiKyTG_6Q5O@mail.gmail.com","threadId":"24758","inReplyTo":"7vbp91aqfk.fsf@alter.siamese.dyndns.org","subject":"Re: [RFC/PATCH 1/2] commit: add parse_commit_repl() to replace commits at parsing time","fromName":"Nguyen Thai Ngoc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-08-18T03:17:52Z","receivedAt":"2010-08-18T03:17:52Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Wed, Aug 18, 2010 at 7:18 AM, Junio C Hamano <gitster@pobox.com> wrote:\n> Christian Couder <chriscool@tuxfamily.org> writes:\n>\n>> The function parse_commit() is not safe regarding replaced commits\n>> because it uses the buffer of the replacement commit but the object\n>> part of the commit struct stay the same. Especially the sha1 is not\n>> changed so it doesn't match the content of the commit.\n>\n> This all sounds backwards to me, if I am reading the discussion correctly.\n>\n> If a replace record says commit 0123 is replaced by commit 4567 (iow, 0123\n> was a mistake, and pretend that its content is what is recorded in 4567),\n> and when we are honoring the replace records (iow, we are not fsck),\n> shouldn't read_sha1(\"0123\") give us a piece of memory that stores what is\n> recorded in 4567, parse_object(\"0123\") return a struct commit whose buffer\n> points at a block of memory that has what is recorded in 4567 _while_ its\n> object.sha1[] say \"0123\"?\n\n1. parse_object() as it is now would return object.sha1[] = \"4567\".\n2. lookup_commit(), then parse_commit() would return object.sha1[] = \"0123\".\n\n> What problem are you trying to solve?\n\nInconsistency in replacing objects. I have no comments whether #1 or\n#2 is expected behavior. But at least it should stick to one behavior\nonly.\n-- \nDuy\n"},{"id":"148337","messageId":"201008180607.54675.chriscool@tuxfamily.org","threadId":"24758","inReplyTo":"AANLkTimu0r_3L7_YJgfMVb6saFOyOK-mHLiKyTG_6Q5O@mail.gmail.com","subject":"Re: [RFC/PATCH 1/2] commit: add parse_commit_repl() to replace commits at parsing time","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2010-08-18T04:07:54Z","receivedAt":"2010-08-18T04:07:54Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Wednesday 18 August 2010 05:17:52 Nguyen Thai Ngoc Duy wrote:\n> On Wed, Aug 18, 2010 at 7:18 AM, Junio C Hamano <gitster@pobox.com> wrote:\n> > Christian Couder <chriscool@tuxfamily.org> writes:\n> >> The function parse_commit() is not safe regarding replaced commits\n> >> because it uses the buffer of the replacement commit but the object\n> >> part of the commit struct stay the same. Especially the sha1 is not\n> >> changed so it doesn't match the content of the commit.\n> > \n> > This all sounds backwards to me, if I am reading the discussion\n> > correctly.\n> > \n> > If a replace record says commit 0123 is replaced by commit 4567 (iow,\n> > 0123 was a mistake, and pretend that its content is what is recorded in\n> > 4567), and when we are honoring the replace records (iow, we are not\n> > fsck), shouldn't read_sha1(\"0123\") give us a piece of memory that stores\n> > what is recorded in 4567, parse_object(\"0123\") return a struct commit\n> > whose buffer points at a block of memory that has what is recorded in\n> > 4567 _while_ its object.sha1[] say \"0 123\"?\n> \n> 1. parse_object() as it is now would return object.sha1[] = \"4567\".\n> 2. lookup_commit(), then parse_commit() would return object.sha1[] =\n> \"0123\".\n> \n> > What problem are you trying to solve?\n> \n> Inconsistency in replacing objects. I have no comments whether #1 or\n> #2 is expected behavior. But at least it should stick to one behavior\n> only.\n\nWe discussed this inconsistency in this thread:\n\nhttp://thread.gmane.org/gmane.comp.version-control.git/152321/ \n\nSo we can resolve the inconsistency with Duy's patch to make parse_object() \nreturn object.sha1[] = \"0123\".\n\nIt's simpler and probably safer. The downside is that the sha1 will not be \nconsistent with the content anymore and that it will be more difficult to \nrealize that an object has been replaced as there will be no sha1 change to be \nseen.\n\nBest regards,\nChristian.\n"},{"id":"148339","messageId":"20100818042459.GD21185@burratino","threadId":"24758","inReplyTo":"201008180607.54675.chriscool@tuxfamily.org","subject":"Re: [RFC/PATCH 1/2] commit: add parse_commit_repl() to replace commits at parsing time","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2010-08-18T04:24:59Z","receivedAt":"2010-08-18T04:24:59Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Christian Couder wrote:\n\n> The downside is that the sha1 will not be \n> consistent with the content anymore and that it will be more difficult to \n> realize that an object has been replaced as there will be no sha1 change to be \n> seen.\n\nMaybe in the long run it would make sense to keep a \"replaced\" flag\nand use it to mark the replace objects specially in some user-facing\ncommands (like log --format=medium).\n"},{"id":"148340","messageId":"AANLkTimZOOMnbbu_gHoeE2Bo_uDajN6Nw64eOtRFfVmM@mail.gmail.com","threadId":"24758","inReplyTo":"20100818042459.GD21185@burratino","subject":"Re: [RFC/PATCH 1/2] commit: add parse_commit_repl() to replace commits at parsing time","fromName":"Nguyen Thai Ngoc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2010-08-18T04:37:02Z","receivedAt":"2010-08-18T04:37:02Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Wed, Aug 18, 2010 at 2:24 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:\n> Christian Couder wrote:\n>\n>> The downside is that the sha1 will not be\n>> consistent with the content anymore and that it will be more difficult to\n>> realize that an object has been replaced as there will be no sha1 change to be\n>> seen.\n>\n> Maybe in the long run it would make sense to keep a \"replaced\" flag\n> and use it to mark the replace objects specially in some user-facing\n> commands (like log --format=medium).\n\nSounds good (if uses really need to know that). It can be used for\ncommit grafts too.\n\nYou would need to find an available bit flag first though. Currently\nobject.flags is used for different purposes and its bit definitions\nare not centralized. So it's hard to find a \"good\" bit that no one\nuses yet, to become the \"replaced\" bit. OK I'm getting off-topic now.\n-- \nDuy\n"},{"id":"148387","messageId":"7vr5hw9dqn.fsf@alter.siamese.dyndns.org","threadId":"24758","inReplyTo":"201008180607.54675.chriscool@tuxfamily.org","subject":"Re: [RFC/PATCH 1/2] commit: add parse_commit_repl() to replace commits at parsing time","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-08-18T14:50:24Z","receivedAt":"2010-08-18T14:50:24Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Christian Couder <chriscool@tuxfamily.org> writes:\n\n> On Wednesday 18 August 2010 05:17:52 Nguyen Thai Ngoc Duy wrote:\n>> On Wed, Aug 18, 2010 at 7:18 AM, Junio C Hamano <gitster@pobox.com> wrote:\n>> > Christian Couder <chriscool@tuxfamily.org> writes:\n>> >> The function parse_commit() is not safe regarding replaced commits\n>> >> because it uses the buffer of the replacement commit but the object\n>> >> part of the commit struct stay the same. Especially the sha1 is not\n>> >> changed so it doesn't match the content of the commit.\n>> > \n>> > This all sounds backwards to me, if I am reading the discussion\n>> > correctly.\n>> > \n>> > If a replace record says commit 0123 is replaced by commit 4567 (iow,\n>> > 0123 was a mistake, and pretend that its content is what is recorded in\n>> > 4567), and when we are honoring the replace records (iow, we are not\n>> > fsck), shouldn't read_sha1(\"0123\") give us a piece of memory that stores\n>> > what is recorded in 4567, parse_object(\"0123\") return a struct commit\n>> > whose buffer points at a block of memory that has what is recorded in\n>> > 4567 _while_ its object.sha1[] say \"0 123\"?\n>> \n>> 1. parse_object() as it is now would return object.sha1[] = \"4567\".\n>> 2. lookup_commit(), then parse_commit() would return object.sha1[] =\n>> \"0123\".\n>> \n>> > What problem are you trying to solve?\n>> \n>> Inconsistency in replacing objects. I have no comments whether #1 or\n>> #2 is expected behavior. But at least it should stick to one behavior\n>> only.\n>\n> We discussed this inconsistency in this thread:\n>\n> http://thread.gmane.org/gmane.comp.version-control.git/152321/ \n>\n> So we can resolve the inconsistency with Duy's patch to make parse_object() \n> return object.sha1[] = \"0123\".\n>\n> It's simpler and probably safer. The downside is that the sha1 will not be \n> consistent with the content anymore and that it will be more difficult to \n> realize that an object has been replaced as there will be no sha1 change to be \n> seen.\n\nI do not see it as a downside at all.\n\nIf the user wants to take replaced objects, they should be shown just like\nan ordinary objects at the machinery level.\n\nOf course, the user is free to add comments on the commit log to note the\nfact that a new commit is replacing some other commit and for what\npurpose.  Also if somebody really wants to, cat-file piped to hash-object\ncan be used to see the difference.\n"},{"id":"148500","messageId":"201008200604.41631.chriscool@tuxfamily.org","threadId":"24758","inReplyTo":"7vr5hw9dqn.fsf@alter.siamese.dyndns.org","subject":"Re: [RFC/PATCH 1/2] commit: add parse_commit_repl() to replace commits at parsing time","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2010-08-20T04:04:41Z","receivedAt":"2010-08-20T04:04:41Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Wednesday 18 August 2010 16:50:24 Junio C Hamano wrote:\n> Christian Couder <chriscool@tuxfamily.org> writes:\n> > On Wednesday 18 August 2010 05:17:52 Nguyen Thai Ngoc Duy wrote:\n> >> On Wed, Aug 18, 2010 at 7:18 AM, Junio C Hamano <gitster@pobox.com> \nwrote:\n> >> > Christian Couder <chriscool@tuxfamily.org> writes:\n> >> >> The function parse_commit() is not safe regarding replaced commits\n> >> >> because it uses the buffer of the replacement commit but the object\n> >> >> part of the commit struct stay the same. Especially the sha1 is not\n> >> >> changed so it doesn't match the content of the commit.\n> >> > \n> >> > This all sounds backwards to me, if I am reading the discussion\n> >> > correctly.\n> >> > \n> >> > If a replace record says commit 0123 is replaced by commit 4567 (iow,\n> >> > 0123 was a mistake, and pretend that its content is what is recorded\n> >> > in 4567), and when we are honoring the replace records (iow, we are\n> >> > not fsck), shouldn't read_sha1(\"0123\") give us a piece of memory that\n> >> > stores what is recorded in 4567, parse_object(\"0123\") return a struct\n> >> > commit whose buffer points at a block of memory that has what is\n> >> > recorded in 4567 _while_ its object.sha1[] say \"0 123\"?\n> >> \n> >> 1. parse_object() as it is now would return object.sha1[] = \"4567\".\n> >> 2. lookup_commit(), then parse_commit() would return object.sha1[] =\n> >> \"0123\".\n> >> \n> >> > What problem are you trying to solve?\n> >> \n> >> Inconsistency in replacing objects. I have no comments whether #1 or\n> >> #2 is expected behavior. But at least it should stick to one behavior\n> >> only.\n> > \n> > We discussed this inconsistency in this thread:\n> > \n> > http://thread.gmane.org/gmane.comp.version-control.git/152321/\n> > \n> > So we can resolve the inconsistency with Duy's patch to make\n> > parse_object() return object.sha1[] = \"0123\".\n> > \n> > It's simpler and probably safer. The downside is that the sha1 will not\n> > be consistent with the content anymore and that it will be more\n> > difficult to realize that an object has been replaced as there will be\n> > no sha1 change to be seen.\n> \n> I do not see it as a downside at all.\n> \n> If the user wants to take replaced objects, they should be shown just like\n> an ordinary objects at the machinery level.\n> \n> Of course, the user is free to add comments on the commit log to note the\n> fact that a new commit is replacing some other commit and for what\n> purpose.  Also if somebody really wants to, cat-file piped to hash-object\n> can be used to see the difference.\n\nOk so please apply Duy's patch perhaps with an improved commit message.\n\nThanks,\nChristian.\n"}]}