]>
git.ipfire.org Git - thirdparty/git.git/blob - builtin/merge-base.c
8 #include "parse-options.h"
9 #include "repository.h"
10 #include "commit-reach.h"
12 static int show_merge_base(struct commit
**rev
, int rev_nr
, int show_all
)
14 struct commit_list
*result
, *r
;
16 result
= get_merge_bases_many_dirty(rev
[0], rev_nr
- 1, rev
+ 1);
21 for (r
= result
; r
; r
= r
->next
) {
22 printf("%s\n", oid_to_hex(&r
->item
->object
.oid
));
27 free_commit_list(result
);
31 static const char * const merge_base_usage
[] = {
32 N_("git merge-base [-a | --all] <commit> <commit>..."),
33 N_("git merge-base [-a | --all] --octopus <commit>..."),
34 N_("git merge-base --independent <commit>..."),
35 N_("git merge-base --is-ancestor <commit> <commit>"),
36 N_("git merge-base --fork-point <ref> [<commit>]"),
40 static struct commit
*get_commit_reference(const char *arg
)
42 struct object_id revkey
;
45 if (get_oid(arg
, &revkey
))
46 die("Not a valid object name %s", arg
);
47 r
= lookup_commit_reference(the_repository
, &revkey
);
49 die("Not a valid commit name %s", arg
);
54 static int handle_independent(int count
, const char **args
)
56 struct commit_list
*revs
= NULL
, *rev
;
59 for (i
= count
- 1; i
>= 0; i
--)
60 commit_list_insert(get_commit_reference(args
[i
]), &revs
);
62 reduce_heads_replace(&revs
);
67 for (rev
= revs
; rev
; rev
= rev
->next
)
68 printf("%s\n", oid_to_hex(&rev
->item
->object
.oid
));
70 free_commit_list(revs
);
74 static int handle_octopus(int count
, const char **args
, int show_all
)
76 struct commit_list
*revs
= NULL
;
77 struct commit_list
*result
, *rev
;
80 for (i
= count
- 1; i
>= 0; i
--)
81 commit_list_insert(get_commit_reference(args
[i
]), &revs
);
83 result
= get_octopus_merge_bases(revs
);
84 free_commit_list(revs
);
85 reduce_heads_replace(&result
);
90 for (rev
= result
; rev
; rev
= rev
->next
) {
91 printf("%s\n", oid_to_hex(&rev
->item
->object
.oid
));
96 free_commit_list(result
);
100 static int handle_is_ancestor(int argc
, const char **argv
)
102 struct commit
*one
, *two
;
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
))
114 static int handle_fork_point(int argc
, const char **argv
)
116 struct object_id oid
;
118 struct commit
*derived
, *fork_point
;
119 const char *commitname
;
121 switch (dwim_ref(argv
[0], strlen(argv
[0]), &oid
, &refname
)) {
123 die("No such ref: '%s'", argv
[0]);
127 die("Ambiguous refname: '%s'", argv
[0]);
130 commitname
= (argc
== 2) ? argv
[1] : "HEAD";
131 if (get_oid(commitname
, &oid
))
132 die("Not a valid object name: '%s'", commitname
);
134 derived
= lookup_commit_reference(the_repository
, &oid
);
136 fork_point
= get_fork_point(refname
, derived
);
141 printf("%s\n", oid_to_hex(&fork_point
->object
.oid
));
145 int cmd_merge_base(int argc
, const char **argv
, const char *prefix
)
152 struct option options
[] = {
153 OPT_BOOL('a', "all", &show_all
, N_("output all common ancestors")),
154 OPT_CMDMODE(0, "octopus", &cmdmode
,
155 N_("find ancestors for a single n-way merge"), 'o'),
156 OPT_CMDMODE(0, "independent", &cmdmode
,
157 N_("list revs not reachable from others"), 'r'),
158 OPT_CMDMODE(0, "is-ancestor", &cmdmode
,
159 N_("is the first one ancestor of the other?"), 'a'),
160 OPT_CMDMODE(0, "fork-point", &cmdmode
,
161 N_("find where <commit> forked from reflog of <ref>"), 'f'),
165 git_config(git_default_config
, NULL
);
166 argc
= parse_options(argc
, argv
, prefix
, options
, merge_base_usage
, 0);
168 if (cmdmode
== 'a') {
170 usage_with_options(merge_base_usage
, options
);
172 die("--is-ancestor cannot be used with --all");
173 return handle_is_ancestor(argc
, argv
);
176 if (cmdmode
== 'r' && show_all
)
177 die("--independent cannot be used with --all");
180 return handle_octopus(argc
, argv
, show_all
);
183 return handle_independent(argc
, argv
);
185 if (cmdmode
== 'f') {
186 if (argc
< 1 || 2 < argc
)
187 usage_with_options(merge_base_usage
, options
);
188 return handle_fork_point(argc
, argv
);
192 usage_with_options(merge_base_usage
, options
);
194 ALLOC_ARRAY(rev
, argc
);
196 rev
[rev_nr
++] = get_commit_reference(*argv
++);
197 return show_merge_base(rev
, rev_nr
, show_all
);