builtin/show: do not prune by pathspec
[git/mjg.git] / builtin / merge-base.c
bloba20c93b11aaa0b9f380b843469da2a3b5db10d00
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "commit.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "object-name.h"
8 #include "parse-options.h"
9 #include "commit-reach.h"
11 static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
13 struct commit_list *result = NULL, *r;
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;
21 if (!result)
22 return 1;
24 for (r = result; r; r = r->next) {
25 printf("%s\n", oid_to_hex(&r->item->object.oid));
26 if (!show_all)
27 break;
30 free_commit_list(result);
31 return 0;
34 static const char * const merge_base_usage[] = {
35 N_("git merge-base [-a | --all] <commit> <commit>..."),
36 N_("git merge-base [-a | --all] --octopus <commit>..."),
37 N_("git merge-base --is-ancestor <commit> <commit>"),
38 N_("git merge-base --independent <commit>..."),
39 N_("git merge-base --fork-point <ref> [<commit>]"),
40 NULL
43 static struct commit *get_commit_reference(const char *arg)
45 struct object_id revkey;
46 struct commit *r;
48 if (repo_get_oid(the_repository, arg, &revkey))
49 die("Not a valid object name %s", arg);
50 r = lookup_commit_reference(the_repository, &revkey);
51 if (!r)
52 die("Not a valid commit name %s", arg);
54 return r;
57 static int handle_independent(int count, const char **args)
59 struct commit_list *revs = NULL, *rev;
60 int i;
62 for (i = count - 1; i >= 0; i--)
63 commit_list_insert(get_commit_reference(args[i]), &revs);
65 reduce_heads_replace(&revs);
67 if (!revs)
68 return 1;
70 for (rev = revs; rev; rev = rev->next)
71 printf("%s\n", oid_to_hex(&rev->item->object.oid));
73 free_commit_list(revs);
74 return 0;
77 static int handle_octopus(int count, const char **args, int show_all)
79 struct commit_list *revs = NULL;
80 struct commit_list *result = NULL, *rev;
81 int i;
83 for (i = count - 1; i >= 0; i--)
84 commit_list_insert(get_commit_reference(args[i]), &revs);
86 if (get_octopus_merge_bases(revs, &result) < 0) {
87 free_commit_list(revs);
88 free_commit_list(result);
89 return 128;
91 free_commit_list(revs);
92 reduce_heads_replace(&result);
94 if (!result)
95 return 1;
97 for (rev = result; rev; rev = rev->next) {
98 printf("%s\n", oid_to_hex(&rev->item->object.oid));
99 if (!show_all)
100 break;
103 free_commit_list(result);
104 return 0;
107 static int handle_is_ancestor(int argc, const char **argv)
109 struct commit *one, *two;
110 int ret;
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]);
116 ret = repo_in_merge_bases(the_repository, one, two);
117 if (ret < 0)
118 exit(128);
119 if (ret)
120 return 0;
121 else
122 return 1;
125 static int handle_fork_point(int argc, const char **argv)
127 struct object_id oid;
128 struct commit *derived, *fork_point;
129 const char *commitname;
131 commitname = (argc == 2) ? argv[1] : "HEAD";
132 if (repo_get_oid(the_repository, commitname, &oid))
133 die("Not a valid object name: '%s'", commitname);
135 derived = lookup_commit_reference(the_repository, &oid);
137 fork_point = get_fork_point(argv[0], derived);
139 if (!fork_point)
140 return 1;
142 printf("%s\n", oid_to_hex(&fork_point->object.oid));
143 return 0;
146 int cmd_merge_base(int argc,
147 const char **argv,
148 const char *prefix,
149 struct repository *repo UNUSED)
151 struct commit **rev;
152 int rev_nr = 0;
153 int show_all = 0;
154 int cmdmode = 0;
155 int ret;
157 struct option options[] = {
158 OPT_BOOL('a', "all", &show_all, N_("output all common ancestors")),
159 OPT_CMDMODE(0, "octopus", &cmdmode,
160 N_("find ancestors for a single n-way merge"), 'o'),
161 OPT_CMDMODE(0, "independent", &cmdmode,
162 N_("list revs not reachable from others"), 'r'),
163 OPT_CMDMODE(0, "is-ancestor", &cmdmode,
164 N_("is the first one ancestor of the other?"), 'a'),
165 OPT_CMDMODE(0, "fork-point", &cmdmode,
166 N_("find where <commit> forked from reflog of <ref>"), 'f'),
167 OPT_END()
170 git_config(git_default_config, NULL);
171 argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
173 if (cmdmode == 'a') {
174 if (argc < 2)
175 usage_with_options(merge_base_usage, options);
176 if (show_all)
177 die(_("options '%s' and '%s' cannot be used together"),
178 "--is-ancestor", "--all");
179 return handle_is_ancestor(argc, argv);
182 if (cmdmode == 'r' && show_all)
183 die(_("options '%s' and '%s' cannot be used together"),
184 "--independent", "--all");
186 if (cmdmode == 'o')
187 return handle_octopus(argc, argv, show_all);
189 if (cmdmode == 'r')
190 return handle_independent(argc, argv);
192 if (cmdmode == 'f') {
193 if (argc < 1 || 2 < argc)
194 usage_with_options(merge_base_usage, options);
195 return handle_fork_point(argc, argv);
198 if (argc < 2)
199 usage_with_options(merge_base_usage, options);
201 ALLOC_ARRAY(rev, argc);
202 while (argc-- > 0)
203 rev[rev_nr++] = get_commit_reference(*argv++);
204 ret = show_merge_base(rev, rev_nr, show_all);
205 free(rev);
206 return ret;