{"thread":{"id":"17687","subject":"[PATCH] Generalize and libify index_is_dirty() to index_differs_from(...)","startedAt":"2009-02-09T23:40:43Z","lastAt":"2009-02-10T14:30:35Z","messageCount":4,"participants":["Stephan Beyer","Jeff King","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"103930","messageId":"1234222843-15577-1-git-send-email-s-beyer@gmx.net","threadId":"17687","inReplyTo":null,"subject":"[PATCH] Generalize and libify index_is_dirty() to index_differs_from(...)","fromName":"Stephan Beyer","fromEmail":"s-beyer@gmx.net","sentAt":"2009-02-09T23:40:43Z","receivedAt":"2009-02-09T23:40:43Z","isPatch":true,"sender":{"key":"s-beyer@gmx.net","avatar":"https://avatars.githubusercontent.com/u/143889?v=4"},"body":"index_is_dirty() in builtin-revert.c checks if the index is dirty.\nThis patch generalizes this function to check if the index differs\nfrom a revision, i.e. the former index_is_dirty() behavior can now be\nachieved by index_differs_from(\"HEAD\", 0).\n\nThe second argument \"diff_flags\" allows to set further diff option\nflags like DIFF_OPT_IGNORE_SUBMODULES. See DIFF_OPT_* macros in diff.h\nfor a list.\n\nindex_differs_from() seems to be useful for more than builtin-revert.c,\nso it is moved into revision.c and also used in builtin-commit.c to\nremove code duplication.\n\nYet to mention:\n\n - \"rev.abbrev = 0;\" in builtin-commit.c can be safely removed.\n   This has no impact on performance or functioning of neither\n   setup_revisions() nor run_diff_index().\n\n - rev.pending.objects is free()d because this fixes a leak.\n   (Also see 295dd2ad \"Fix memory leak in traverse_commit_list\")\n\nMentored-by: Daniel Barkalow <barkalow@iabervon.org>\nMentored-by: Christian Couder <chriscool@tuxfamily.org>\nSigned-off-by: Stephan Beyer <s-beyer@gmx.net>\n---\n\n  This is one of the sequencer-preparing patches.\n  (The function is used in sequencer several times, most of the time\n   with diff_flags set to DIFF_OPT_IGNORE_SUBMODULES.)\n\n  Alex is on Cc because he introduced the \"Is commitable?\" (i.e.\n  \"Is index dirty?\") part in builtin-commit.c.\n\n  Peff is on Cc because he introduced index_is_dirty() in\n  builtin-revert.c.\n\n builtin-commit.c |   13 ++-----------\n builtin-revert.c |   13 +------------\n revision.c       |   15 +++++++++++++++\n revision.h       |    2 ++\n 4 files changed, 20 insertions(+), 23 deletions(-)\n\ndiff --git a/builtin-commit.c b/builtin-commit.c\nindex d6a3a62..46e649c 100644\n--- a/builtin-commit.c\n+++ b/builtin-commit.c\n@@ -561,7 +561,6 @@ static int prepare_to_commit(const char *index_file, const char *prefix)\n \t\tcommitable = run_status(fp, index_file, prefix, 1);\n \t\twt_status_use_color = saved_color_setting;\n \t} else {\n-\t\tstruct rev_info rev;\n \t\tunsigned char sha1[20];\n \t\tconst char *parent = \"HEAD\";\n \n@@ -573,16 +572,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix)\n \n \t\tif (get_sha1(parent, sha1))\n \t\t\tcommitable = !!active_nr;\n-\t\telse {\n-\t\t\tinit_revisions(&rev, \"\");\n-\t\t\trev.abbrev = 0;\n-\t\t\tsetup_revisions(0, NULL, &rev, parent);\n-\t\t\tDIFF_OPT_SET(&rev.diffopt, QUIET);\n-\t\t\tDIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);\n-\t\t\trun_diff_index(&rev, 1 /* cached */);\n-\n-\t\t\tcommitable = !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);\n-\t\t}\n+\t\telse\n+\t\t\tcommitable = index_differs_from(parent, 0);\n \t}\n \n \tfclose(fp);\ndiff --git a/builtin-revert.c b/builtin-revert.c\nindex d48313c..d210150 100644\n--- a/builtin-revert.c\n+++ b/builtin-revert.c\n@@ -223,17 +223,6 @@ static char *help_msg(const unsigned char *sha1)\n \treturn helpbuf;\n }\n \n-static int index_is_dirty(void)\n-{\n-\tstruct rev_info rev;\n-\tinit_revisions(&rev, NULL);\n-\tsetup_revisions(0, NULL, &rev, \"HEAD\");\n-\tDIFF_OPT_SET(&rev.diffopt, QUIET);\n-\tDIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);\n-\trun_diff_index(&rev, 1);\n-\treturn !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);\n-}\n-\n static struct tree *empty_tree(void)\n {\n \tstruct tree *tree = xcalloc(1, sizeof(struct tree));\n@@ -279,7 +268,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)\n \t} else {\n \t\tif (get_sha1(\"HEAD\", head))\n \t\t\tdie (\"You do not have a valid HEAD\");\n-\t\tif (index_is_dirty())\n+\t\tif (index_differs_from(\"HEAD\", 0))\n \t\t\tdie (\"Dirty index: cannot %s\", me);\n \t}\n \tdiscard_cache();\ndiff --git a/revision.c b/revision.c\nindex 8603c14..de489db 100644\n--- a/revision.c\n+++ b/revision.c\n@@ -1926,3 +1926,18 @@ struct commit *get_revision(struct rev_info *revs)\n \t\tgraph_update(revs->graph, c);\n \treturn c;\n }\n+\n+int index_differs_from(const char *def, int diff_flags)\n+{\n+\tstruct rev_info rev;\n+\n+\tinit_revisions(&rev, NULL);\n+\tsetup_revisions(0, NULL, &rev, def);\n+\tDIFF_OPT_SET(&rev.diffopt, QUIET);\n+\tDIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);\n+\trev.diffopt.flags |= diff_flags;\n+\trun_diff_index(&rev, 1);\n+\tif (rev.pending.alloc)\n+\t\tfree(rev.pending.objects);\n+\treturn (DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES) != 0);\n+}\ndiff --git a/revision.h b/revision.h\nindex 7cf8487..bc17949 100644\n--- a/revision.h\n+++ b/revision.h\n@@ -164,4 +164,6 @@ enum commit_action {\n \n extern enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit);\n \n+extern int index_differs_from(const char *def, int diff_flags);\n+\n #endif\n-- \n1.6.2.rc0.458.g97dd\n"},{"id":"103934","messageId":"20090210001220.GB5551@sigill.intra.peff.net","threadId":"17687","inReplyTo":"1234222843-15577-1-git-send-email-s-beyer@gmx.net","subject":"Re: [PATCH] Generalize and libify index_is_dirty() to index_differs_from(...)","fromName":"Jeff King","fromEmail":"peff@cc.gatech.edu","sentAt":"2009-02-10T00:12:20Z","receivedAt":"2009-02-10T00:12:20Z","isPatch":true,"sender":{"key":"peff@cc.gatech.edu","avatar":null},"body":"On Tue, Feb 10, 2009 at 12:40:43AM +0100, Stephan Beyer wrote:\n\n>   Peff is on Cc because he introduced index_is_dirty() in\n>   builtin-revert.c.\n\nCertainly it looks to me like a straight-forward libification of what I\nhad added before.\n\nAcked-by: Jeff King <peff@peff.net>\n\n-Peff\n"},{"id":"103948","messageId":"7v63jirhw8.fsf@gitster.siamese.dyndns.org","threadId":"17687","inReplyTo":"1234222843-15577-1-git-send-email-s-beyer@gmx.net","subject":"Re: [PATCH] Generalize and libify index_is_dirty() to index_differs_from(...)","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-02-10T05:16:23Z","receivedAt":"2009-02-10T05:16:23Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Stephan Beyer <s-beyer@gmx.net> writes:\n\n>   This is one of the sequencer-preparing patches.\n>   (The function is used in sequencer several times, most of the time\n>    with diff_flags set to DIFF_OPT_IGNORE_SUBMODULES.)\n>\n>   Alex is on Cc because he introduced the \"Is commitable?\" (i.e.\n>   \"Is index dirty?\") part in builtin-commit.c.\n>\n>   Peff is on Cc because he introduced index_is_dirty() in\n>   builtin-revert.c.\n>\n>  builtin-commit.c |   13 ++-----------\n>  builtin-revert.c |   13 +------------\n>  revision.c       |   15 +++++++++++++++\n>  revision.h       |    2 ++\n>  4 files changed, 20 insertions(+), 23 deletions(-)\n\nIt is a straightforward and clean restructuring, but please do not\ncontaminate revision.[ch] with this function about \"internally running\ndiff-index\".  \n\nrevision.[ch] is a library for revision/ancestry traversal and it is\nalready one of the largest library-ish files. It does not know nor care\nabout the index, and we want to keep it that way.  Please keep its focus\nto revision traversal.\n\nPerhaps diff-lib.c would be a better home for your helper function.\n"},{"id":"104014","messageId":"1234276235-21822-1-git-send-email-s-beyer@gmx.net","threadId":"17687","inReplyTo":"7v63jirhw8.fsf@gitster.siamese.dyndns.org","subject":"[PATCH v2] Generalize and libify index_is_dirty() to index_differs_from(...)","fromName":"Stephan Beyer","fromEmail":"s-beyer@gmx.net","sentAt":"2009-02-10T14:30:35Z","receivedAt":"2009-02-10T14:30:35Z","isPatch":true,"sender":{"key":"s-beyer@gmx.net","avatar":"https://avatars.githubusercontent.com/u/143889?v=4"},"body":"index_is_dirty() in builtin-revert.c checks if the index is dirty.\nThis patch generalizes this function to check if the index differs\nfrom a revision, i.e. the former index_is_dirty() behavior can now be\nachieved by index_differs_from(\"HEAD\", 0).\n\nThe second argument \"diff_flags\" allows to set further diff option\nflags like DIFF_OPT_IGNORE_SUBMODULES. See DIFF_OPT_* macros in diff.h\nfor a list.\n\nindex_differs_from() seems to be useful for more than builtin-revert.c,\nso it is moved into diff-lib.c and also used in builtin-commit.c.\n\nYet to mention:\n\n - \"rev.abbrev = 0;\" can be safely removed.\n   This has no impact on performance or functioning of neither\n   setup_revisions() nor run_diff_index().\n\n - rev.pending.objects is free()d because this fixes a leak.\n   (Also see 295dd2ad \"Fix memory leak in traverse_commit_list\")\n\nMentored-by: Daniel Barkalow <barkalow@iabervon.org>\nMentored-by: Christian Couder <chriscool@tuxfamily.org>\nSigned-off-by: Stephan Beyer <s-beyer@gmx.net>\n---\n\n  Err, this didn't get to the list, did it?\n\n builtin-commit.c |   13 ++-----------\n builtin-revert.c |   13 +------------\n diff-lib.c       |   15 +++++++++++++++\n diff.h           |    2 ++\n 4 files changed, 20 insertions(+), 23 deletions(-)\n\ndiff --git a/builtin-commit.c b/builtin-commit.c\nindex d6a3a62..46e649c 100644\n--- a/builtin-commit.c\n+++ b/builtin-commit.c\n@@ -561,7 +561,6 @@ static int prepare_to_commit(const char *index_file, const char *prefix)\n \t\tcommitable = run_status(fp, index_file, prefix, 1);\n \t\twt_status_use_color = saved_color_setting;\n \t} else {\n-\t\tstruct rev_info rev;\n \t\tunsigned char sha1[20];\n \t\tconst char *parent = \"HEAD\";\n \n@@ -573,16 +572,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix)\n \n \t\tif (get_sha1(parent, sha1))\n \t\t\tcommitable = !!active_nr;\n-\t\telse {\n-\t\t\tinit_revisions(&rev, \"\");\n-\t\t\trev.abbrev = 0;\n-\t\t\tsetup_revisions(0, NULL, &rev, parent);\n-\t\t\tDIFF_OPT_SET(&rev.diffopt, QUIET);\n-\t\t\tDIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);\n-\t\t\trun_diff_index(&rev, 1 /* cached */);\n-\n-\t\t\tcommitable = !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);\n-\t\t}\n+\t\telse\n+\t\t\tcommitable = index_differs_from(parent, 0);\n \t}\n \n \tfclose(fp);\ndiff --git a/builtin-revert.c b/builtin-revert.c\nindex d48313c..d210150 100644\n--- a/builtin-revert.c\n+++ b/builtin-revert.c\n@@ -223,17 +223,6 @@ static char *help_msg(const unsigned char *sha1)\n \treturn helpbuf;\n }\n \n-static int index_is_dirty(void)\n-{\n-\tstruct rev_info rev;\n-\tinit_revisions(&rev, NULL);\n-\tsetup_revisions(0, NULL, &rev, \"HEAD\");\n-\tDIFF_OPT_SET(&rev.diffopt, QUIET);\n-\tDIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);\n-\trun_diff_index(&rev, 1);\n-\treturn !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);\n-}\n-\n static struct tree *empty_tree(void)\n {\n \tstruct tree *tree = xcalloc(1, sizeof(struct tree));\n@@ -279,7 +268,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)\n \t} else {\n \t\tif (get_sha1(\"HEAD\", head))\n \t\t\tdie (\"You do not have a valid HEAD\");\n-\t\tif (index_is_dirty())\n+\t\tif (index_differs_from(\"HEAD\", 0))\n \t\t\tdie (\"Dirty index: cannot %s\", me);\n \t}\n \tdiscard_cache();\ndiff --git a/diff-lib.c b/diff-lib.c\nindex a41e1ec..79d0606 100644\n--- a/diff-lib.c\n+++ b/diff-lib.c\n@@ -513,3 +513,18 @@ int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)\n \t\texit(128);\n \treturn 0;\n }\n+\n+int index_differs_from(const char *def, int diff_flags)\n+{\n+\tstruct rev_info rev;\n+\n+\tinit_revisions(&rev, NULL);\n+\tsetup_revisions(0, NULL, &rev, def);\n+\tDIFF_OPT_SET(&rev.diffopt, QUIET);\n+\tDIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);\n+\trev.diffopt.flags |= diff_flags;\n+\trun_diff_index(&rev, 1);\n+\tif (rev.pending.alloc)\n+\t\tfree(rev.pending.objects);\n+\treturn (DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES) != 0);\n+}\ndiff --git a/diff.h b/diff.h\nindex 23cd90c..6703a4f 100644\n--- a/diff.h\n+++ b/diff.h\n@@ -265,4 +265,6 @@ extern int diff_result_code(struct diff_options *, int);\n \n extern void diff_no_index(struct rev_info *, int, const char **, int, const char *);\n \n+extern int index_differs_from(const char *def, int diff_flags);\n+\n #endif /* DIFF_H */\n-- \n1.6.2.rc0.464.g3ec3\n"}]}