]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/merge-base.c
Merge branch 'js/t5563-portability-fix'
[thirdparty/git.git] / builtin / merge-base.c
CommitLineData
baffc0e7 1#include "builtin.h"
6683463e 2#include "cache.h"
b2141fc1 3#include "config.h"
b5039db6 4#include "commit.h"
41771fa4 5#include "hex.h"
d96855ff
JH
6#include "refs.h"
7#include "diff.h"
8#include "revision.h"
e5d1a4df 9#include "parse-options.h"
2122f675 10#include "repository.h"
64043556 11#include "commit-reach.h"
6683463e 12
53eda89b 13static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
52cab8a0 14{
a452d0f4 15 struct commit_list *result, *r;
53eda89b 16
2ce406cc 17 result = get_merge_bases_many_dirty(rev[0], rev_nr - 1, rev + 1);
52cab8a0
JS
18
19 if (!result)
20 return 1;
21
a452d0f4
22 for (r = result; r; r = r->next) {
23 printf("%s\n", oid_to_hex(&r->item->object.oid));
9585e406 24 if (!show_all)
a452d0f4 25 break;
9585e406 26 }
52cab8a0 27
a452d0f4 28 free_commit_list(result);
9585e406 29 return 0;
6683463e
LT
30}
31
e5d1a4df 32static const char * const merge_base_usage[] = {
9c9b4f2f
AH
33 N_("git merge-base [-a | --all] <commit> <commit>..."),
34 N_("git merge-base [-a | --all] --octopus <commit>..."),
34f5130a 35 N_("git merge-base --is-ancestor <commit> <commit>"),
8f5f2f64 36 N_("git merge-base --independent <commit>..."),
d96855ff 37 N_("git merge-base --fork-point <ref> [<commit>]"),
e5d1a4df
PH
38 NULL
39};
9585e406 40
df57accb
CC
41static struct commit *get_commit_reference(const char *arg)
42{
d0ae910a 43 struct object_id revkey;
df57accb
CC
44 struct commit *r;
45
d0ae910a 46 if (get_oid(arg, &revkey))
df57accb 47 die("Not a valid object name %s", arg);
2122f675 48 r = lookup_commit_reference(the_repository, &revkey);
df57accb
CC
49 if (!r)
50 die("Not a valid commit name %s", arg);
51
52 return r;
53}
54
e2f5df42 55static int handle_independent(int count, const char **args)
aa8f98c1 56{
a452d0f4 57 struct commit_list *revs = NULL, *rev;
aa8f98c1
JN
58 int i;
59
e2f5df42
JH
60 for (i = count - 1; i >= 0; i--)
61 commit_list_insert(get_commit_reference(args[i]), &revs);
62
4da72644 63 reduce_heads_replace(&revs);
a452d0f4
64
65 if (!revs)
e2f5df42
JH
66 return 1;
67
a452d0f4
68 for (rev = revs; rev; rev = rev->next)
69 printf("%s\n", oid_to_hex(&rev->item->object.oid));
70
71 free_commit_list(revs);
e2f5df42
JH
72 return 0;
73}
74
75static int handle_octopus(int count, const char **args, int show_all)
76{
77 struct commit_list *revs = NULL;
a452d0f4 78 struct commit_list *result, *rev;
e2f5df42 79 int i;
a1e0ad78
JN
80
81 for (i = count - 1; i >= 0; i--)
aa8f98c1 82 commit_list_insert(get_commit_reference(args[i]), &revs);
a1e0ad78 83
4da72644
84 result = get_octopus_merge_bases(revs);
85 free_commit_list(revs);
86 reduce_heads_replace(&result);
aa8f98c1
JN
87
88 if (!result)
89 return 1;
90
a452d0f4
91 for (rev = result; rev; rev = rev->next) {
92 printf("%s\n", oid_to_hex(&rev->item->object.oid));
aa8f98c1 93 if (!show_all)
a452d0f4 94 break;
aa8f98c1
JN
95 }
96
a452d0f4 97 free_commit_list(result);
aa8f98c1
JN
98 return 0;
99}
100
5907cda1
JH
101static int handle_is_ancestor(int argc, const char **argv)
102{
103 struct commit *one, *two;
104
105 if (argc != 2)
106 die("--is-ancestor takes exactly two commits");
107 one = get_commit_reference(argv[0]);
108 two = get_commit_reference(argv[1]);
109 if (in_merge_bases(one, two))
110 return 0;
111 else
112 return 1;
113}
114
d96855ff
JH
115static int handle_fork_point(int argc, const char **argv)
116{
d0ae910a 117 struct object_id oid;
103148aa 118 struct commit *derived, *fork_point;
d96855ff 119 const char *commitname;
d96855ff 120
d96855ff 121 commitname = (argc == 2) ? argv[1] : "HEAD";
d0ae910a 122 if (get_oid(commitname, &oid))
d96855ff
JH
123 die("Not a valid object name: '%s'", commitname);
124
2122f675 125 derived = lookup_commit_reference(the_repository, &oid);
d96855ff 126
f08132f8 127 fork_point = get_fork_point(argv[0], derived);
4f21454b 128
103148aa
PK
129 if (!fork_point)
130 return 1;
d96855ff 131
103148aa
PK
132 printf("%s\n", oid_to_hex(&fork_point->object.oid));
133 return 0;
d96855ff
JH
134}
135
71dfbf22 136int cmd_merge_base(int argc, const char **argv, const char *prefix)
6683463e 137{
53eda89b
CC
138 struct commit **rev;
139 int rev_nr = 0;
71dfbf22 140 int show_all = 0;
16e57aec 141 int cmdmode = 0;
e69fe2e4 142 int ret;
6683463e 143
e5d1a4df 144 struct option options[] = {
d5d09d47 145 OPT_BOOL('a', "all", &show_all, N_("output all common ancestors")),
16e57aec
JH
146 OPT_CMDMODE(0, "octopus", &cmdmode,
147 N_("find ancestors for a single n-way merge"), 'o'),
148 OPT_CMDMODE(0, "independent", &cmdmode,
149 N_("list revs not reachable from others"), 'r'),
150 OPT_CMDMODE(0, "is-ancestor", &cmdmode,
151 N_("is the first one ancestor of the other?"), 'a'),
d96855ff
JH
152 OPT_CMDMODE(0, "fork-point", &cmdmode,
153 N_("find where <commit> forked from reflog of <ref>"), 'f'),
e5d1a4df
PH
154 OPT_END()
155 };
53eda89b 156
e5d1a4df 157 git_config(git_default_config, NULL);
37782920 158 argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
16e57aec
JH
159
160 if (cmdmode == 'a') {
161 if (argc < 2)
162 usage_with_options(merge_base_usage, options);
163 if (show_all)
a699367b
JNA
164 die(_("options '%s' and '%s' cannot be used together"),
165 "--is-ancestor", "--all");
5907cda1 166 return handle_is_ancestor(argc, argv);
16e57aec 167 }
aa8f98c1 168
16e57aec 169 if (cmdmode == 'r' && show_all)
a699367b
JNA
170 die(_("options '%s' and '%s' cannot be used together"),
171 "--independent", "--all");
16e57aec 172
d5d1678b 173 if (cmdmode == 'o')
e2f5df42 174 return handle_octopus(argc, argv, show_all);
d5d1678b
JH
175
176 if (cmdmode == 'r')
e2f5df42 177 return handle_independent(argc, argv);
16e57aec 178
d96855ff
JH
179 if (cmdmode == 'f') {
180 if (argc < 1 || 2 < argc)
181 usage_with_options(merge_base_usage, options);
182 return handle_fork_point(argc, argv);
183 }
184
16e57aec
JH
185 if (argc < 2)
186 usage_with_options(merge_base_usage, options);
aa8f98c1 187
b32fa95f 188 ALLOC_ARRAY(rev, argc);
e5d1a4df
PH
189 while (argc-- > 0)
190 rev[rev_nr++] = get_commit_reference(*argv++);
e69fe2e4
ÆAB
191 ret = show_merge_base(rev, rev_nr, show_all);
192 free(rev);
193 return ret;
6683463e 194}