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);
20 for (r
= result
; r
; r
= r
->next
) {
21 printf("%s\n", oid_to_hex(&r
->item
->object
.oid
));
26 free_commit_list(result
);
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>]"),
39 static struct commit
*get_commit_reference(const char *arg
)
41 struct object_id revkey
;
44 if (get_oid(arg
, &revkey
))
45 die("Not a valid object name %s", arg
);
46 r
= lookup_commit_reference(the_repository
, &revkey
);
48 die("Not a valid commit name %s", arg
);
53 static int handle_independent(int count
, const char **args
)
55 struct commit_list
*revs
= NULL
, *rev
;
58 for (i
= count
- 1; i
>= 0; i
--)
59 commit_list_insert(get_commit_reference(args
[i
]), &revs
);
61 reduce_heads_replace(&revs
);
66 for (rev
= revs
; rev
; rev
= rev
->next
)
67 printf("%s\n", oid_to_hex(&rev
->item
->object
.oid
));
69 free_commit_list(revs
);
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
;
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
);
89 for (rev
= result
; rev
; rev
= rev
->next
) {
90 printf("%s\n", oid_to_hex(&rev
->item
->object
.oid
));
95 free_commit_list(result
);
99 static int handle_is_ancestor(int argc
, const char **argv
)
101 struct commit
*one
, *two
;
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
))
114 struct commit
**commit
;
117 unsigned int initial
: 1;
120 static void add_one_commit(struct object_id
*oid
, struct rev_collect
*revs
)
122 struct commit
*commit
;
124 if (is_null_oid(oid
))
127 commit
= lookup_commit(the_repository
, oid
);
129 (commit
->object
.flags
& TMP_MARK
) ||
130 parse_commit(commit
))
133 ALLOC_GROW(revs
->commit
, revs
->nr
+ 1, revs
->alloc
);
134 revs
->commit
[revs
->nr
++] = commit
;
135 commit
->object
.flags
|= TMP_MARK
;
138 static int collect_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
139 const char *ident
, timestamp_t timestamp
,
140 int tz
, const char *message
, void *cbdata
)
142 struct rev_collect
*revs
= cbdata
;
146 add_one_commit(ooid
, revs
);
148 add_one_commit(noid
, revs
);
152 static int handle_fork_point(int argc
, const char **argv
)
154 struct object_id oid
;
156 const char *commitname
;
157 struct rev_collect revs
;
158 struct commit
*derived
;
159 struct commit_list
*bases
;
162 switch (dwim_ref(argv
[0], strlen(argv
[0]), &oid
, &refname
)) {
164 die("No such ref: '%s'", argv
[0]);
168 die("Ambiguous refname: '%s'", argv
[0]);
171 commitname
= (argc
== 2) ? argv
[1] : "HEAD";
172 if (get_oid(commitname
, &oid
))
173 die("Not a valid object name: '%s'", commitname
);
175 derived
= lookup_commit_reference(the_repository
, &oid
);
176 memset(&revs
, 0, sizeof(revs
));
178 for_each_reflog_ent(refname
, collect_one_reflog_ent
, &revs
);
180 if (!revs
.nr
&& !get_oid(refname
, &oid
))
181 add_one_commit(&oid
, &revs
);
183 for (i
= 0; i
< revs
.nr
; i
++)
184 revs
.commit
[i
]->object
.flags
&= ~TMP_MARK
;
186 bases
= get_merge_bases_many_dirty(derived
, revs
.nr
, revs
.commit
);
189 * There should be one and only one merge base, when we found
190 * a common ancestor among reflog entries.
192 if (!bases
|| bases
->next
) {
197 /* And the found one must be one of the reflog entries */
198 for (i
= 0; i
< revs
.nr
; i
++)
199 if (&bases
->item
->object
== &revs
.commit
[i
]->object
)
202 ret
= 1; /* not found */
206 printf("%s\n", oid_to_hex(&bases
->item
->object
.oid
));
209 free_commit_list(bases
);
213 int cmd_merge_base(int argc
, const char **argv
, const char *prefix
)
220 struct option options
[] = {
221 OPT_BOOL('a', "all", &show_all
, N_("output all common ancestors")),
222 OPT_CMDMODE(0, "octopus", &cmdmode
,
223 N_("find ancestors for a single n-way merge"), 'o'),
224 OPT_CMDMODE(0, "independent", &cmdmode
,
225 N_("list revs not reachable from others"), 'r'),
226 OPT_CMDMODE(0, "is-ancestor", &cmdmode
,
227 N_("is the first one ancestor of the other?"), 'a'),
228 OPT_CMDMODE(0, "fork-point", &cmdmode
,
229 N_("find where <commit> forked from reflog of <ref>"), 'f'),
233 git_config(git_default_config
, NULL
);
234 argc
= parse_options(argc
, argv
, prefix
, options
, merge_base_usage
, 0);
236 if (cmdmode
== 'a') {
238 usage_with_options(merge_base_usage
, options
);
240 die("--is-ancestor cannot be used with --all");
241 return handle_is_ancestor(argc
, argv
);
244 if (cmdmode
== 'r' && show_all
)
245 die("--independent cannot be used with --all");
248 return handle_octopus(argc
, argv
, show_all
);
251 return handle_independent(argc
, argv
);
253 if (cmdmode
== 'f') {
254 if (argc
< 1 || 2 < argc
)
255 usage_with_options(merge_base_usage
, options
);
256 return handle_fork_point(argc
, argv
);
260 usage_with_options(merge_base_usage
, options
);
262 ALLOC_ARRAY(rev
, argc
);
264 rev
[rev_nr
++] = get_commit_reference(*argv
++);
265 return show_merge_base(rev
, rev_nr
, show_all
);