]>
Commit | Line | Data |
---|---|---|
baffc0e7 | 1 | #include "builtin.h" |
6683463e | 2 | #include "cache.h" |
b2141fc1 | 3 | #include "config.h" |
b5039db6 | 4 | #include "commit.h" |
d96855ff JH |
5 | #include "refs.h" |
6 | #include "diff.h" | |
7 | #include "revision.h" | |
e5d1a4df | 8 | #include "parse-options.h" |
2122f675 | 9 | #include "repository.h" |
64043556 | 10 | #include "commit-reach.h" |
6683463e | 11 | |
53eda89b | 12 | static int show_merge_base(struct commit **rev, int rev_nr, int show_all) |
52cab8a0 | 13 | { |
a452d0f4 | 14 | struct commit_list *result, *r; |
53eda89b | 15 | |
2ce406cc | 16 | result = get_merge_bases_many_dirty(rev[0], rev_nr - 1, rev + 1); |
52cab8a0 JS |
17 | |
18 | if (!result) | |
19 | return 1; | |
20 | ||
a452d0f4 MÅ |
21 | for (r = result; r; r = r->next) { |
22 | printf("%s\n", oid_to_hex(&r->item->object.oid)); | |
9585e406 | 23 | if (!show_all) |
a452d0f4 | 24 | break; |
9585e406 | 25 | } |
52cab8a0 | 26 | |
a452d0f4 | 27 | free_commit_list(result); |
9585e406 | 28 | return 0; |
6683463e LT |
29 | } |
30 | ||
e5d1a4df | 31 | static const char * const merge_base_usage[] = { |
9c9b4f2f AH |
32 | N_("git merge-base [-a | --all] <commit> <commit>..."), |
33 | N_("git merge-base [-a | --all] --octopus <commit>..."), | |
f037dbf4 | 34 | N_("git merge-base --independent <commit>..."), |
34f5130a | 35 | N_("git merge-base --is-ancestor <commit> <commit>"), |
d96855ff | 36 | N_("git merge-base --fork-point <ref> [<commit>]"), |
e5d1a4df PH |
37 | NULL |
38 | }; | |
9585e406 | 39 | |
df57accb CC |
40 | static struct commit *get_commit_reference(const char *arg) |
41 | { | |
d0ae910a | 42 | struct object_id revkey; |
df57accb CC |
43 | struct commit *r; |
44 | ||
d0ae910a | 45 | if (get_oid(arg, &revkey)) |
df57accb | 46 | die("Not a valid object name %s", arg); |
2122f675 | 47 | r = lookup_commit_reference(the_repository, &revkey); |
df57accb CC |
48 | if (!r) |
49 | die("Not a valid commit name %s", arg); | |
50 | ||
51 | return r; | |
52 | } | |
53 | ||
e2f5df42 | 54 | static int handle_independent(int count, const char **args) |
aa8f98c1 | 55 | { |
a452d0f4 | 56 | struct commit_list *revs = NULL, *rev; |
aa8f98c1 JN |
57 | int i; |
58 | ||
e2f5df42 JH |
59 | for (i = count - 1; i >= 0; i--) |
60 | commit_list_insert(get_commit_reference(args[i]), &revs); | |
61 | ||
4da72644 | 62 | reduce_heads_replace(&revs); |
a452d0f4 MÅ |
63 | |
64 | if (!revs) | |
e2f5df42 JH |
65 | return 1; |
66 | ||
a452d0f4 MÅ |
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); | |
e2f5df42 JH |
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; | |
a452d0f4 | 77 | struct commit_list *result, *rev; |
e2f5df42 | 78 | int i; |
a1e0ad78 JN |
79 | |
80 | for (i = count - 1; i >= 0; i--) | |
aa8f98c1 | 81 | commit_list_insert(get_commit_reference(args[i]), &revs); |
a1e0ad78 | 82 | |
4da72644 MÅ |
83 | result = get_octopus_merge_bases(revs); |
84 | free_commit_list(revs); | |
85 | reduce_heads_replace(&result); | |
aa8f98c1 JN |
86 | |
87 | if (!result) | |
88 | return 1; | |
89 | ||
a452d0f4 MÅ |
90 | for (rev = result; rev; rev = rev->next) { |
91 | printf("%s\n", oid_to_hex(&rev->item->object.oid)); | |
aa8f98c1 | 92 | if (!show_all) |
a452d0f4 | 93 | break; |
aa8f98c1 JN |
94 | } |
95 | ||
a452d0f4 | 96 | free_commit_list(result); |
aa8f98c1 JN |
97 | return 0; |
98 | } | |
99 | ||
5907cda1 JH |
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 | ||
d96855ff JH |
114 | static int handle_fork_point(int argc, const char **argv) |
115 | { | |
d0ae910a | 116 | struct object_id oid; |
103148aa | 117 | struct commit *derived, *fork_point; |
d96855ff | 118 | const char *commitname; |
d96855ff | 119 | |
d96855ff | 120 | commitname = (argc == 2) ? argv[1] : "HEAD"; |
d0ae910a | 121 | if (get_oid(commitname, &oid)) |
d96855ff JH |
122 | die("Not a valid object name: '%s'", commitname); |
123 | ||
2122f675 | 124 | derived = lookup_commit_reference(the_repository, &oid); |
d96855ff | 125 | |
f08132f8 | 126 | fork_point = get_fork_point(argv[0], derived); |
4f21454b | 127 | |
103148aa PK |
128 | if (!fork_point) |
129 | return 1; | |
d96855ff | 130 | |
103148aa PK |
131 | printf("%s\n", oid_to_hex(&fork_point->object.oid)); |
132 | return 0; | |
d96855ff JH |
133 | } |
134 | ||
71dfbf22 | 135 | int cmd_merge_base(int argc, const char **argv, const char *prefix) |
6683463e | 136 | { |
53eda89b CC |
137 | struct commit **rev; |
138 | int rev_nr = 0; | |
71dfbf22 | 139 | int show_all = 0; |
16e57aec | 140 | int cmdmode = 0; |
6683463e | 141 | |
e5d1a4df | 142 | struct option options[] = { |
d5d09d47 | 143 | OPT_BOOL('a', "all", &show_all, N_("output all common ancestors")), |
16e57aec JH |
144 | OPT_CMDMODE(0, "octopus", &cmdmode, |
145 | N_("find ancestors for a single n-way merge"), 'o'), | |
146 | OPT_CMDMODE(0, "independent", &cmdmode, | |
147 | N_("list revs not reachable from others"), 'r'), | |
148 | OPT_CMDMODE(0, "is-ancestor", &cmdmode, | |
149 | N_("is the first one ancestor of the other?"), 'a'), | |
d96855ff JH |
150 | OPT_CMDMODE(0, "fork-point", &cmdmode, |
151 | N_("find where <commit> forked from reflog of <ref>"), 'f'), | |
e5d1a4df PH |
152 | OPT_END() |
153 | }; | |
53eda89b | 154 | |
e5d1a4df | 155 | git_config(git_default_config, NULL); |
37782920 | 156 | argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0); |
16e57aec JH |
157 | |
158 | if (cmdmode == 'a') { | |
159 | if (argc < 2) | |
160 | usage_with_options(merge_base_usage, options); | |
161 | if (show_all) | |
162 | die("--is-ancestor cannot be used with --all"); | |
5907cda1 | 163 | return handle_is_ancestor(argc, argv); |
16e57aec | 164 | } |
aa8f98c1 | 165 | |
16e57aec JH |
166 | if (cmdmode == 'r' && show_all) |
167 | die("--independent cannot be used with --all"); | |
168 | ||
d5d1678b | 169 | if (cmdmode == 'o') |
e2f5df42 | 170 | return handle_octopus(argc, argv, show_all); |
d5d1678b JH |
171 | |
172 | if (cmdmode == 'r') | |
e2f5df42 | 173 | return handle_independent(argc, argv); |
16e57aec | 174 | |
d96855ff JH |
175 | if (cmdmode == 'f') { |
176 | if (argc < 1 || 2 < argc) | |
177 | usage_with_options(merge_base_usage, options); | |
178 | return handle_fork_point(argc, argv); | |
179 | } | |
180 | ||
16e57aec JH |
181 | if (argc < 2) |
182 | usage_with_options(merge_base_usage, options); | |
aa8f98c1 | 183 | |
b32fa95f | 184 | ALLOC_ARRAY(rev, argc); |
e5d1a4df PH |
185 | while (argc-- > 0) |
186 | rev[rev_nr++] = get_commit_reference(*argv++); | |
53eda89b | 187 | return show_merge_base(rev, rev_nr, show_all); |
6683463e | 188 | } |