merge-base: "--is-ancestor A B"

In many scripted Porcelain commands, we find this idiom: if test "$(git rev-parse --verify A)" = "$(git merge-base A B)" then ... A is an ancestor of B ... fi But you do not have to compute exact merge-base only to see if A is an ancestor of B. Give them a more direct way to use the underlying machinery. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 30, 2012 at 14:52 UTC 5907cda1b2e0bd7dc5c26466f60b9cbb60288cc5
2 files changed +50
Documentation/git-merge-base.txt
+28
@@ -11,6 +11,7 @@ SYNOPSIS
11 [verse]
12 'git merge-base' [-a|--all] <commit> <commit>...
13 'git merge-base' [-a|--all] --octopus <commit>...
14 +'git merge-base' --is-ancestor <commit> <commit>
15 'git merge-base' --independent <commit>...
16
17 DESCRIPTION
@@ -50,6 +51,12 @@ from linkgit:git-show-branch[1] when used with the `--merge-base` option.
51 from any other. This mimics the behavior of 'git show-branch
52 --independent'.
53
54 +--is-ancestor::
55 + Check if the first <commit> is an ancestor of the second <commit>,
56 + and exit with status 0 if true, or with status 1 if not.
57 + Errors are signaled by a non-zero status that is not 1.
58 +
59 +
60 OPTIONS
61 -------
62 -a::
@@ -110,6 +117,27 @@ both '1' and '2' are merge-bases of A and B. Neither one is better than
117 the other (both are 'best' merge bases). When the `--all` option is not given,
118 it is unspecified which best one is output.
119
120 +A common idiom to check "fast-forward-ness" between two commits A
121 +and B is (or at least used to be) to compute the merge base between
122 +A and B, and check if it is the same as A, in which case, A is an
123 +ancestor of B. You will see this idiom used often in older scripts.
124 +
125 + A=$(git rev-parse --verify A)
126 + if test "$A" = "$(git merge-base A B)"
127 + then
128 + ... A is an ancestor of B ...
129 + fi
130 +
131 +In modern git, you can say this in a more direct way:
132 +
133 + if git merge-base --is-ancestor A B
134 + then
135 + ... A is an ancestor of B ...
136 + fi
137 +
138 +instead.
139 +
140 +
141 See also
142 --------
143 linkgit:git-rev-list[1],
builtin/merge-base.c
+22
@@ -26,6 +26,7 @@ static const char * const merge_base_usage[] = {
26 "git merge-base [-a|--all] <commit> <commit>...",
27 "git merge-base [-a|--all] --octopus <commit>...",
28 "git merge-base --independent <commit>...",
29 + "git merge-base --is-ancestor <commit> <commit>",
30 NULL
31 };
32
@@ -70,6 +71,20 @@ static int handle_octopus(int count, const char **args, int reduce, int show_all
71 return 0;
72 }
73
74 +static int handle_is_ancestor(int argc, const char **argv)
75 +{
76 + struct commit *one, *two;
77 +
78 + if (argc != 2)
79 + die("--is-ancestor takes exactly two commits");
80 + one = get_commit_reference(argv[0]);
81 + two = get_commit_reference(argv[1]);
82 + if (in_merge_bases(one, two))
83 + return 0;
84 + else
85 + return 1;
86 +}
87 +
88 int cmd_merge_base(int argc, const char **argv, const char *prefix)
89 {
90 struct commit **rev;
@@ -77,11 +92,14 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
92 int show_all = 0;
93 int octopus = 0;
94 int reduce = 0;
95 + int is_ancestor = 0;
96
97 struct option options[] = {
98 OPT_BOOLEAN('a', "all", &show_all, "output all common ancestors"),
99 OPT_BOOLEAN(0, "octopus", &octopus, "find ancestors for a single n-way merge"),
100 OPT_BOOLEAN(0, "independent", &reduce, "list revs not reachable from others"),
101 + OPT_BOOLEAN(0, "is-ancestor", &is_ancestor,
102 + "is the first one ancestor of the other?"),
103 OPT_END()
104 };
105
@@ -89,6 +107,10 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
107 argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
108 if (!octopus && !reduce && argc < 2)
109 usage_with_options(merge_base_usage, options);
110 + if (is_ancestor && (show_all | octopus | reduce))
111 + die("--is-ancestor cannot be used with other options");
112 + if (is_ancestor)
113 + return handle_is_ancestor(argc, argv);
114 if (reduce && (show_all || octopus))
115 die("--independent cannot be used with other options");
116