]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/merge-base.c
Merge branch 'nd/maint-clone-gitdir' into maint
[thirdparty/git.git] / builtin / merge-base.c
CommitLineData
baffc0e7 1#include "builtin.h"
6683463e 2#include "cache.h"
b5039db6 3#include "commit.h"
e5d1a4df 4#include "parse-options.h"
6683463e 5
53eda89b 6static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
52cab8a0 7{
53eda89b
CC
8 struct commit_list *result;
9
10 result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1, 0);
52cab8a0
JS
11
12 if (!result)
13 return 1;
14
9585e406 15 while (result) {
52cab8a0 16 printf("%s\n", sha1_to_hex(result->item->object.sha1));
9585e406
JH
17 if (!show_all)
18 return 0;
52cab8a0 19 result = result->next;
9585e406 20 }
52cab8a0 21
9585e406 22 return 0;
6683463e
LT
23}
24
e5d1a4df 25static const char * const merge_base_usage[] = {
57294824
VR
26 "git merge-base [-a|--all] <commit> <commit>...",
27 "git merge-base [-a|--all] --octopus <commit>...",
a1e0ad78 28 "git merge-base --independent <commit>...",
e5d1a4df
PH
29 NULL
30};
9585e406 31
df57accb
CC
32static struct commit *get_commit_reference(const char *arg)
33{
34 unsigned char revkey[20];
35 struct commit *r;
36
37 if (get_sha1(arg, revkey))
38 die("Not a valid object name %s", arg);
39 r = lookup_commit_reference(revkey);
40 if (!r)
41 die("Not a valid commit name %s", arg);
42
43 return r;
44}
45
a1e0ad78 46static int handle_octopus(int count, const char **args, int reduce, int show_all)
aa8f98c1
JN
47{
48 struct commit_list *revs = NULL;
49 struct commit_list *result;
50 int i;
51
a1e0ad78
JN
52 if (reduce)
53 show_all = 1;
54
55 for (i = count - 1; i >= 0; i--)
aa8f98c1 56 commit_list_insert(get_commit_reference(args[i]), &revs);
a1e0ad78
JN
57
58 result = reduce ? reduce_heads(revs) : get_octopus_merge_bases(revs);
aa8f98c1
JN
59
60 if (!result)
61 return 1;
62
63 while (result) {
64 printf("%s\n", sha1_to_hex(result->item->object.sha1));
65 if (!show_all)
66 return 0;
67 result = result->next;
68 }
69
70 return 0;
71}
72
71dfbf22 73int cmd_merge_base(int argc, const char **argv, const char *prefix)
6683463e 74{
53eda89b
CC
75 struct commit **rev;
76 int rev_nr = 0;
71dfbf22 77 int show_all = 0;
aa8f98c1 78 int octopus = 0;
a1e0ad78 79 int reduce = 0;
6683463e 80
e5d1a4df 81 struct option options[] = {
aa8f98c1
JN
82 OPT_BOOLEAN('a', "all", &show_all, "output all common ancestors"),
83 OPT_BOOLEAN(0, "octopus", &octopus, "find ancestors for a single n-way merge"),
a1e0ad78 84 OPT_BOOLEAN(0, "independent", &reduce, "list revs not reachable from others"),
e5d1a4df
PH
85 OPT_END()
86 };
53eda89b 87
e5d1a4df 88 git_config(git_default_config, NULL);
37782920 89 argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
a1e0ad78 90 if (!octopus && !reduce && argc < 2)
e5d1a4df 91 usage_with_options(merge_base_usage, options);
a1e0ad78
JN
92 if (reduce && (show_all || octopus))
93 die("--independent cannot be used with other options");
aa8f98c1 94
a1e0ad78
JN
95 if (octopus || reduce)
96 return handle_octopus(argc, argv, reduce, show_all);
aa8f98c1 97
e5d1a4df
PH
98 rev = xmalloc(argc * sizeof(*rev));
99 while (argc-- > 0)
100 rev[rev_nr++] = get_commit_reference(*argv++);
53eda89b 101 return show_merge_base(rev, rev_nr, show_all);
6683463e 102}