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