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