built-in rebase --autostash: leave the current branch alone if possible
[git/debian.git] / builtin / merge-base.c
blob790ceaeed68fae3f8696d0d59fb97aa67f88dbce
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"
11 static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
13 struct commit_list *result, *r;
15 result = get_merge_bases_many_dirty(rev[0], rev_nr - 1, rev + 1);
17 if (!result)
18 return 1;
20 for (r = result; r; r = r->next) {
21 printf("%s\n", oid_to_hex(&r->item->object.oid));
22 if (!show_all)
23 break;
26 free_commit_list(result);
27 return 0;
30 static const char * const merge_base_usage[] = {
31 N_("git merge-base [-a | --all] <commit> <commit>..."),
32 N_("git merge-base [-a | --all] --octopus <commit>..."),
33 N_("git merge-base --independent <commit>..."),
34 N_("git merge-base --is-ancestor <commit> <commit>"),
35 N_("git merge-base --fork-point <ref> [<commit>]"),
36 NULL
39 static struct commit *get_commit_reference(const char *arg)
41 struct object_id revkey;
42 struct commit *r;
44 if (get_oid(arg, &revkey))
45 die("Not a valid object name %s", arg);
46 r = lookup_commit_reference(the_repository, &revkey);
47 if (!r)
48 die("Not a valid commit name %s", arg);
50 return r;
53 static int handle_independent(int count, const char **args)
55 struct commit_list *revs = NULL, *rev;
56 int i;
58 for (i = count - 1; i >= 0; i--)
59 commit_list_insert(get_commit_reference(args[i]), &revs);
61 reduce_heads_replace(&revs);
63 if (!revs)
64 return 1;
66 for (rev = revs; rev; rev = rev->next)
67 printf("%s\n", oid_to_hex(&rev->item->object.oid));
69 free_commit_list(revs);
70 return 0;
73 static int handle_octopus(int count, const char **args, int show_all)
75 struct commit_list *revs = NULL;
76 struct commit_list *result, *rev;
77 int i;
79 for (i = count - 1; i >= 0; i--)
80 commit_list_insert(get_commit_reference(args[i]), &revs);
82 result = get_octopus_merge_bases(revs);
83 free_commit_list(revs);
84 reduce_heads_replace(&result);
86 if (!result)
87 return 1;
89 for (rev = result; rev; rev = rev->next) {
90 printf("%s\n", oid_to_hex(&rev->item->object.oid));
91 if (!show_all)
92 break;
95 free_commit_list(result);
96 return 0;
99 static int handle_is_ancestor(int argc, const char **argv)
101 struct commit *one, *two;
103 if (argc != 2)
104 die("--is-ancestor takes exactly two commits");
105 one = get_commit_reference(argv[0]);
106 two = get_commit_reference(argv[1]);
107 if (in_merge_bases(one, two))
108 return 0;
109 else
110 return 1;
113 static int handle_fork_point(int argc, const char **argv)
115 struct object_id oid;
116 char *refname;
117 struct commit *derived, *fork_point;
118 const char *commitname;
120 switch (dwim_ref(argv[0], strlen(argv[0]), &oid, &refname)) {
121 case 0:
122 die("No such ref: '%s'", argv[0]);
123 case 1:
124 break; /* good */
125 default:
126 die("Ambiguous refname: '%s'", argv[0]);
129 commitname = (argc == 2) ? argv[1] : "HEAD";
130 if (get_oid(commitname, &oid))
131 die("Not a valid object name: '%s'", commitname);
133 derived = lookup_commit_reference(the_repository, &oid);
135 fork_point = get_fork_point(refname, derived);
137 if (!fork_point)
138 return 1;
140 printf("%s\n", oid_to_hex(&fork_point->object.oid));
141 return 0;
144 int cmd_merge_base(int argc, const char **argv, const char *prefix)
146 struct commit **rev;
147 int rev_nr = 0;
148 int show_all = 0;
149 int cmdmode = 0;
151 struct option options[] = {
152 OPT_BOOL('a', "all", &show_all, N_("output all common ancestors")),
153 OPT_CMDMODE(0, "octopus", &cmdmode,
154 N_("find ancestors for a single n-way merge"), 'o'),
155 OPT_CMDMODE(0, "independent", &cmdmode,
156 N_("list revs not reachable from others"), 'r'),
157 OPT_CMDMODE(0, "is-ancestor", &cmdmode,
158 N_("is the first one ancestor of the other?"), 'a'),
159 OPT_CMDMODE(0, "fork-point", &cmdmode,
160 N_("find where <commit> forked from reflog of <ref>"), 'f'),
161 OPT_END()
164 git_config(git_default_config, NULL);
165 argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
167 if (cmdmode == 'a') {
168 if (argc < 2)
169 usage_with_options(merge_base_usage, options);
170 if (show_all)
171 die("--is-ancestor cannot be used with --all");
172 return handle_is_ancestor(argc, argv);
175 if (cmdmode == 'r' && show_all)
176 die("--independent cannot be used with --all");
178 if (cmdmode == 'o')
179 return handle_octopus(argc, argv, show_all);
181 if (cmdmode == 'r')
182 return handle_independent(argc, argv);
184 if (cmdmode == 'f') {
185 if (argc < 1 || 2 < argc)
186 usage_with_options(merge_base_usage, options);
187 return handle_fork_point(argc, argv);
190 if (argc < 2)
191 usage_with_options(merge_base_usage, options);
193 ALLOC_ARRAY(rev, argc);
194 while (argc-- > 0)
195 rev[rev_nr++] = get_commit_reference(*argv++);
196 return show_merge_base(rev, rev_nr, show_all);