Raw
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "commit.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "hex.h"
8 #include "object-name.h"
9 #include "parse-options.h"
10 #include "commit-reach.h"
11
12 static int show_merge_base(struct commit **rev, size_t rev_nr, int show_all)
13 {
14 enum merge_base_flags flags = show_all ? MERGE_BASE_FIND_ALL : 0;
15 struct commit_list *result = NULL, *r;
16
17 if (repo_get_merge_bases_many_dirty(the_repository, rev[0],
18 rev_nr - 1, rev + 1,
19 flags, &result) < 0) {
20 commit_list_free(result);
21 return -1;
22 }
23
24 if (!result)
25 return 1;
26
27 for (r = result; r; r = r->next) {
28 printf("%s\n", oid_to_hex(&r->item->object.oid));
29 if (!show_all)
30 break;
31 }
32
33 commit_list_free(result);
34 return 0;
35 }
36
37 static const char * const merge_base_usage[] = {
38 N_("git merge-base [-a | --all] <commit> <commit>..."),
39 N_("git merge-base [-a | --all] --octopus <commit>..."),
40 N_("git merge-base --is-ancestor <commit> <commit>"),
41 N_("git merge-base --independent <commit>..."),
42 N_("git merge-base --fork-point <ref> [<commit>]"),
43 NULL
44 };
45
46 static struct commit *get_commit_reference(const char *arg)
47 {
48 struct object_id revkey;
49 struct commit *r;
50
51 if (repo_get_oid(the_repository, arg, &revkey))
52 die("Not a valid object name %s", arg);
53 r = lookup_commit_reference(the_repository, &revkey);
54 if (!r)
55 die("Not a valid commit name %s", arg);
56
57 return r;
58 }
59
60 static int handle_independent(int count, const char **args)
61 {
62 struct commit_list *revs = NULL, *rev;
63 int i;
64
65 for (i = count - 1; i >= 0; i--)
66 commit_list_insert(get_commit_reference(args[i]), &revs);
67
68 reduce_heads_replace(&revs);
69
70 if (!revs)
71 return 1;
72
73 for (rev = revs; rev; rev = rev->next)
74 printf("%s\n", oid_to_hex(&rev->item->object.oid));
75
76 commit_list_free(revs);
77 return 0;
78 }
79
80 static int handle_octopus(int count, const char **args, int show_all)
81 {
82 struct commit_list *revs = NULL;
83 struct commit_list *result = NULL, *rev;
84 int i;
85
86 for (i = count - 1; i >= 0; i--)
87 commit_list_insert(get_commit_reference(args[i]), &revs);
88
89 if (get_octopus_merge_bases(revs, &result) < 0) {
90 commit_list_free(revs);
91 commit_list_free(result);
92 return 128;
93 }
94 commit_list_free(revs);
95 reduce_heads_replace(&result);
96
97 if (!result)
98 return 1;
99
100 for (rev = result; rev; rev = rev->next) {
101 printf("%s\n", oid_to_hex(&rev->item->object.oid));
102 if (!show_all)
103 break;
104 }
105
106 commit_list_free(result);
107 return 0;
108 }
109
110 static int handle_is_ancestor(int argc, const char **argv)
111 {
112 struct commit *one, *two;
113 int ret;
114
115 if (argc != 2)
116 die("--is-ancestor takes exactly two commits");
117 one = get_commit_reference(argv[0]);
118 two = get_commit_reference(argv[1]);
119 ret = repo_in_merge_bases(the_repository, one, two);
120 if (ret < 0)
121 exit(128);
122 if (ret)
123 return 0;
124 else
125 return 1;
126 }
127
128 static int handle_fork_point(int argc, const char **argv)
129 {
130 struct object_id oid;
131 struct commit *derived, *fork_point;
132 const char *commitname;
133
134 commitname = (argc == 2) ? argv[1] : "HEAD";
135 if (repo_get_oid(the_repository, commitname, &oid))
136 die("Not a valid object name: '%s'", commitname);
137
138 derived = lookup_commit_reference(the_repository, &oid);
139
140 fork_point = get_fork_point(argv[0], derived);
141
142 if (!fork_point)
143 return 1;
144
145 printf("%s\n", oid_to_hex(&fork_point->object.oid));
146 return 0;
147 }
148
149 int cmd_merge_base(int argc,
150 const char **argv,
151 const char *prefix,
152 struct repository *repo UNUSED)
153 {
154 struct commit **rev;
155 size_t rev_nr = 0;
156 int show_all = 0;
157 int cmdmode = 0;
158 int ret;
159
160 struct option options[] = {
161 OPT_BOOL('a', "all", &show_all, N_("output all common ancestors")),
162 OPT_CMDMODE(0, "octopus", &cmdmode,
163 N_("find ancestors for a single n-way merge"), 'o'),
164 OPT_CMDMODE(0, "independent", &cmdmode,
165 N_("list revs not reachable from others"), 'r'),
166 OPT_CMDMODE(0, "is-ancestor", &cmdmode,
167 N_("is the first one ancestor of the other?"), 'a'),
168 OPT_CMDMODE(0, "fork-point", &cmdmode,
169 N_("find where <commit> forked from reflog of <ref>"), 'f'),
170 OPT_END()
171 };
172
173 repo_config(the_repository, git_default_config, NULL);
174 argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
175
176 if (cmdmode == 'a') {
177 if (argc < 2)
178 usage_with_options(merge_base_usage, options);
179 if (show_all)
180 die(_("options '%s' and '%s' cannot be used together"),
181 "--is-ancestor", "--all");
182 return handle_is_ancestor(argc, argv);
183 }
184
185 if (cmdmode == 'r' && show_all)
186 die(_("options '%s' and '%s' cannot be used together"),
187 "--independent", "--all");
188
189 if (cmdmode == 'o')
190 return handle_octopus(argc, argv, show_all);
191
192 if (cmdmode == 'r')
193 return handle_independent(argc, argv);
194
195 if (cmdmode == 'f') {
196 if (argc < 1 || 2 < argc)
197 usage_with_options(merge_base_usage, options);
198 return handle_fork_point(argc, argv);
199 }
200
201 if (argc < 2)
202 usage_with_options(merge_base_usage, options);
203
204 ALLOC_ARRAY(rev, argc);
205 while (argc-- > 0)
206 rev[rev_nr++] = get_commit_reference(*argv++);
207 ret = show_merge_base(rev, rev_nr, show_all);
208 free(rev);
209 return ret;
210 }