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