8 #include "parse-options.h"
10 static int show_merge_base(struct commit
**rev
, int rev_nr
, int show_all
)
12 struct commit_list
*result
, *r
;
14 result
= get_merge_bases_many_dirty(rev
[0], rev_nr
- 1, rev
+ 1);
19 for (r
= result
; r
; r
= r
->next
) {
20 printf("%s\n", oid_to_hex(&r
->item
->object
.oid
));
25 free_commit_list(result
);
29 static const char * const merge_base_usage
[] = {
30 N_("git merge-base [-a | --all] <commit> <commit>..."),
31 N_("git merge-base [-a | --all] --octopus <commit>..."),
32 N_("git merge-base --independent <commit>..."),
33 N_("git merge-base --is-ancestor <commit> <commit>"),
34 N_("git merge-base --fork-point <ref> [<commit>]"),
38 static struct commit
*get_commit_reference(const char *arg
)
40 struct object_id revkey
;
43 if (get_oid(arg
, &revkey
))
44 die("Not a valid object name %s", arg
);
45 r
= lookup_commit_reference(&revkey
);
47 die("Not a valid commit name %s", arg
);
52 static int handle_independent(int count
, const char **args
)
54 struct commit_list
*revs
= NULL
, *rev
;
57 for (i
= count
- 1; i
>= 0; i
--)
58 commit_list_insert(get_commit_reference(args
[i
]), &revs
);
60 reduce_heads_replace(&revs
);
65 for (rev
= revs
; rev
; rev
= rev
->next
)
66 printf("%s\n", oid_to_hex(&rev
->item
->object
.oid
));
68 free_commit_list(revs
);
72 static int handle_octopus(int count
, const char **args
, int show_all
)
74 struct commit_list
*revs
= NULL
;
75 struct commit_list
*result
, *rev
;
78 for (i
= count
- 1; i
>= 0; i
--)
79 commit_list_insert(get_commit_reference(args
[i
]), &revs
);
81 result
= get_octopus_merge_bases(revs
);
82 free_commit_list(revs
);
83 reduce_heads_replace(&result
);
88 for (rev
= result
; rev
; rev
= rev
->next
) {
89 printf("%s\n", oid_to_hex(&rev
->item
->object
.oid
));
94 free_commit_list(result
);
98 static int handle_is_ancestor(int argc
, const char **argv
)
100 struct commit
*one
, *two
;
103 die("--is-ancestor takes exactly two commits");
104 one
= get_commit_reference(argv
[0]);
105 two
= get_commit_reference(argv
[1]);
106 if (in_merge_bases(one
, two
))
113 struct commit
**commit
;
116 unsigned int initial
: 1;
119 static void add_one_commit(struct object_id
*oid
, struct rev_collect
*revs
)
121 struct commit
*commit
;
123 if (is_null_oid(oid
))
126 commit
= lookup_commit(oid
);
128 (commit
->object
.flags
& TMP_MARK
) ||
129 parse_commit(commit
))
132 ALLOC_GROW(revs
->commit
, revs
->nr
+ 1, revs
->alloc
);
133 revs
->commit
[revs
->nr
++] = commit
;
134 commit
->object
.flags
|= TMP_MARK
;
137 static int collect_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
138 const char *ident
, timestamp_t timestamp
,
139 int tz
, const char *message
, void *cbdata
)
141 struct rev_collect
*revs
= cbdata
;
145 add_one_commit(ooid
, revs
);
147 add_one_commit(noid
, revs
);
151 static int handle_fork_point(int argc
, const char **argv
)
153 struct object_id oid
;
155 const char *commitname
;
156 struct rev_collect revs
;
157 struct commit
*derived
;
158 struct commit_list
*bases
;
161 switch (dwim_ref(argv
[0], strlen(argv
[0]), &oid
, &refname
)) {
163 die("No such ref: '%s'", argv
[0]);
167 die("Ambiguous refname: '%s'", argv
[0]);
170 commitname
= (argc
== 2) ? argv
[1] : "HEAD";
171 if (get_oid(commitname
, &oid
))
172 die("Not a valid object name: '%s'", commitname
);
174 derived
= lookup_commit_reference(&oid
);
175 memset(&revs
, 0, sizeof(revs
));
177 for_each_reflog_ent(refname
, collect_one_reflog_ent
, &revs
);
179 if (!revs
.nr
&& !get_oid(refname
, &oid
))
180 add_one_commit(&oid
, &revs
);
182 for (i
= 0; i
< revs
.nr
; i
++)
183 revs
.commit
[i
]->object
.flags
&= ~TMP_MARK
;
185 bases
= get_merge_bases_many_dirty(derived
, revs
.nr
, revs
.commit
);
188 * There should be one and only one merge base, when we found
189 * a common ancestor among reflog entries.
191 if (!bases
|| bases
->next
) {
196 /* And the found one must be one of the reflog entries */
197 for (i
= 0; i
< revs
.nr
; i
++)
198 if (&bases
->item
->object
== &revs
.commit
[i
]->object
)
201 ret
= 1; /* not found */
205 printf("%s\n", oid_to_hex(&bases
->item
->object
.oid
));
208 free_commit_list(bases
);
212 int cmd_merge_base(int argc
, const char **argv
, const char *prefix
)
219 struct option options
[] = {
220 OPT_BOOL('a', "all", &show_all
, N_("output all common ancestors")),
221 OPT_CMDMODE(0, "octopus", &cmdmode
,
222 N_("find ancestors for a single n-way merge"), 'o'),
223 OPT_CMDMODE(0, "independent", &cmdmode
,
224 N_("list revs not reachable from others"), 'r'),
225 OPT_CMDMODE(0, "is-ancestor", &cmdmode
,
226 N_("is the first one ancestor of the other?"), 'a'),
227 OPT_CMDMODE(0, "fork-point", &cmdmode
,
228 N_("find where <commit> forked from reflog of <ref>"), 'f'),
232 git_config(git_default_config
, NULL
);
233 argc
= parse_options(argc
, argv
, prefix
, options
, merge_base_usage
, 0);
235 if (cmdmode
== 'a') {
237 usage_with_options(merge_base_usage
, options
);
239 die("--is-ancestor cannot be used with --all");
240 return handle_is_ancestor(argc
, argv
);
243 if (cmdmode
== 'r' && show_all
)
244 die("--independent cannot be used with --all");
247 return handle_octopus(argc
, argv
, show_all
);
250 return handle_independent(argc
, argv
);
252 if (cmdmode
== 'f') {
253 if (argc
< 1 || 2 < argc
)
254 usage_with_options(merge_base_usage
, options
);
255 return handle_fork_point(argc
, argv
);
259 usage_with_options(merge_base_usage
, options
);
261 ALLOC_ARRAY(rev
, argc
);
263 rev
[rev_nr
++] = get_commit_reference(*argv
++);
264 return show_merge_base(rev
, rev_nr
, show_all
);