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