Skip to content

Commit 5907cda

Browse files
committed
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 <[email protected]>
1 parent 94f0ced commit 5907cda

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

Documentation/git-merge-base.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ SYNOPSIS
1111
[verse]
1212
'git merge-base' [-a|--all] <commit> <commit>...
1313
'git merge-base' [-a|--all] --octopus <commit>...
14+
'git merge-base' --is-ancestor <commit> <commit>
1415
'git merge-base' --independent <commit>...
1516

1617
DESCRIPTION
@@ -50,6 +51,12 @@ from linkgit:git-show-branch[1] when used with the `--merge-base` option.
5051
from any other. This mimics the behavior of 'git show-branch
5152
--independent'.
5253

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+
5360
OPTIONS
5461
-------
5562
-a::
@@ -110,6 +117,27 @@ both '1' and '2' are merge-bases of A and B. Neither one is better than
110117
the other (both are 'best' merge bases). When the `--all` option is not given,
111118
it is unspecified which best one is output.
112119

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+
113141
See also
114142
--------
115143
linkgit:git-rev-list[1],

builtin/merge-base.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ static const char * const merge_base_usage[] = {
2626
"git merge-base [-a|--all] <commit> <commit>...",
2727
"git merge-base [-a|--all] --octopus <commit>...",
2828
"git merge-base --independent <commit>...",
29+
"git merge-base --is-ancestor <commit> <commit>",
2930
NULL
3031
};
3132

@@ -70,25 +71,46 @@ static int handle_octopus(int count, const char **args, int reduce, int show_all
7071
return 0;
7172
}
7273

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+
7388
int cmd_merge_base(int argc, const char **argv, const char *prefix)
7489
{
7590
struct commit **rev;
7691
int rev_nr = 0;
7792
int show_all = 0;
7893
int octopus = 0;
7994
int reduce = 0;
95+
int is_ancestor = 0;
8096

8197
struct option options[] = {
8298
OPT_BOOLEAN('a', "all", &show_all, "output all common ancestors"),
8399
OPT_BOOLEAN(0, "octopus", &octopus, "find ancestors for a single n-way merge"),
84100
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?"),
85103
OPT_END()
86104
};
87105

88106
git_config(git_default_config, NULL);
89107
argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
90108
if (!octopus && !reduce && argc < 2)
91109
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);
92114
if (reduce && (show_all || octopus))
93115
die("--independent cannot be used with other options");
94116

0 commit comments

Comments
 (0)