{"thread":{"id":"6304","subject":"[PATCH 3/6] git-merge-base: --check-ancestry option","startedAt":"2007-01-09T10:41:28Z","lastAt":"2007-01-09T10:41:28Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":6},"messages":[{"id":"31230","messageId":"7vodp8o52f.fsf@assigned-by-dhcp.cox.net","threadId":"6304","inReplyTo":null,"subject":"[PATCH 3/6] git-merge-base: --check-ancestry option","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2007-01-09T10:41:28Z","receivedAt":"2007-01-09T10:41:28Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"A new option drives underlying in_merge_bases() function to\ndetermine if a commit is reachable from any of the other commits\nsupplied on the command line.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n---\n Documentation/git-merge-base.txt |   13 +++++++++++-\n merge-base.c                     |   39 ++++++++++++++++++++++++++++++++++++-\n 2 files changed, 49 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/git-merge-base.txt b/Documentation/git-merge-base.txt\nindex 6099be2..7d4d027 100644\n--- a/Documentation/git-merge-base.txt\n+++ b/Documentation/git-merge-base.txt\n@@ -8,7 +8,9 @@ git-merge-base - Finds as good a common ancestor as possible for a merge\n \n SYNOPSIS\n --------\n+[verse]\n 'git-merge-base' [--all] <commit> <commit>\n+'git-merge-base' --check-ancestry <commit> <commit>...\n \n DESCRIPTION\n -----------\n@@ -21,7 +23,10 @@ the parent relationship.\n Given a selection of equally good common ancestors it should not be\n relied on to decide in any particular way.\n \n-The \"git-merge-base\" algorithm is still in flux - use the source...\n+With `--check-ancestry` option, the command can be used to check\n+if the first commit is an ancestor of any of the remaining\n+commits.\n+\n \n OPTIONS\n -------\n@@ -29,6 +34,12 @@ OPTIONS\n \tOutput all common ancestors for the two commits instead of\n \tjust one.\n \n+--check-ancestry::\n+\n+\tCheck if the first commit is an ancestor of any of the\n+\tremaining commits.  If so, exit with status 0, otherwise\n+\tnon-zero.\n+\n Author\n ------\n Written by Linus Torvalds <torvalds@osdl.org>\ndiff --git a/merge-base.c b/merge-base.c\nindex 385f4ba..bc478eb 100644\n--- a/merge-base.c\n+++ b/merge-base.c\n@@ -2,6 +2,8 @@\n #include \"commit.h\"\n \n static int show_all;\n+static const char merge_base_usage[] =\n+\"git-merge-base [--all] <commit> <commit> | --check-ancestry <commit> <commit>...\";\n \n static int merge_base(struct commit *rev1, struct commit *rev2)\n {\n@@ -20,13 +22,37 @@ static int merge_base(struct commit *rev1, struct commit *rev2)\n \treturn 0;\n }\n \n-static const char merge_base_usage[] =\n-\"git-merge-base [--all] <commit-id> <commit-id>\";\n+static int check_ancestry(int argc, char **argv)\n+{\n+\tstruct commit *one, *two, **reference;\n+\tunsigned char sha1[20];\n+\tint i;\n+\n+\tif (argc < 2)\n+\t\tusage(merge_base_usage);\n+\tif (get_sha1(argv[0], sha1))\n+\t\tdie(\"Not a valid object name %s\", argv[0]);\n+\tone = lookup_commit_reference(sha1);\n+\tif (!one)\n+\t\treturn 1;\n+\n+\treference = xcalloc(argc - 1, sizeof(struct commit *));\n+\tfor (i = 1; i < argc; i++) {\n+\t\tif (get_sha1(argv[i], sha1))\n+\t\t\tdie(\"Not a valid object name %s\", argv[i]);\n+\t\ttwo = lookup_commit_reference(sha1);\n+\t\tif (!two)\n+\t\t\treturn 1;\n+\t\treference[i - 1] = two;\n+\t}\n+\treturn !!in_merge_bases(one, reference, argc - 1);\n+}\n \n int main(int argc, char **argv)\n {\n \tstruct commit *rev1, *rev2;\n \tunsigned char rev1key[20], rev2key[20];\n+\tint check = 0;\n \n \tsetup_git_directory();\n \tgit_config(git_default_config);\n@@ -35,10 +61,19 @@ int main(int argc, char **argv)\n \t\tchar *arg = argv[1];\n \t\tif (!strcmp(arg, \"-a\") || !strcmp(arg, \"--all\"))\n \t\t\tshow_all = 1;\n+\t\telse if (!strcmp(arg, \"-c\") || !strcmp(arg, \"--check-ancestry\"))\n+\t\t\tcheck = 1;\n \t\telse\n \t\t\tusage(merge_base_usage);\n \t\targc--; argv++;\n \t}\n+\n+\tif (check) {\n+\t\tif (show_all)\n+\t\t\tdie(\"--all and --check-ancestry are mutually incompatible\");\n+\t\treturn check_ancestry(argc - 1, argv + 1);\n+\t}\n+\n \tif (argc != 3)\n \t\tusage(merge_base_usage);\n \tif (get_sha1(argv[1], rev1key))\n-- \n1.4.4.4.g564d\n"}]}