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