]>
git.ipfire.org Git - thirdparty/git.git/blob - builtin/merge-base.c
9 #include "parse-options.h"
10 #include "repository.h"
11 #include "commit-reach.h"
13 static int show_merge_base(struct commit
**rev
, int rev_nr
, int show_all
)
15 struct commit_list
*result
, *r
;
17 result
= get_merge_bases_many_dirty(rev
[0], rev_nr
- 1, rev
+ 1);
22 for (r
= result
; r
; r
= r
->next
) {
23 printf("%s\n", oid_to_hex(&r
->item
->object
.oid
));
28 free_commit_list(result
);
32 static const char * const merge_base_usage
[] = {
33 N_("git merge-base [-a | --all] <commit> <commit>..."),
34 N_("git merge-base [-a | --all] --octopus <commit>..."),
35 N_("git merge-base --is-ancestor <commit> <commit>"),
36 N_("git merge-base --independent <commit>..."),
37 N_("git merge-base --fork-point <ref> [<commit>]"),
41 static struct commit
*get_commit_reference(const char *arg
)
43 struct object_id revkey
;
46 if (get_oid(arg
, &revkey
))
47 die("Not a valid object name %s", arg
);
48 r
= lookup_commit_reference(the_repository
, &revkey
);
50 die("Not a valid commit name %s", arg
);
55 static int handle_independent(int count
, const char **args
)
57 struct commit_list
*revs
= NULL
, *rev
;
60 for (i
= count
- 1; i
>= 0; i
--)
61 commit_list_insert(get_commit_reference(args
[i
]), &revs
);
63 reduce_heads_replace(&revs
);
68 for (rev
= revs
; rev
; rev
= rev
->next
)
69 printf("%s\n", oid_to_hex(&rev
->item
->object
.oid
));
71 free_commit_list(revs
);
75 static int handle_octopus(int count
, const char **args
, int show_all
)
77 struct commit_list
*revs
= NULL
;
78 struct commit_list
*result
, *rev
;
81 for (i
= count
- 1; i
>= 0; i
--)
82 commit_list_insert(get_commit_reference(args
[i
]), &revs
);
84 result
= get_octopus_merge_bases(revs
);
85 free_commit_list(revs
);
86 reduce_heads_replace(&result
);
91 for (rev
= result
; rev
; rev
= rev
->next
) {
92 printf("%s\n", oid_to_hex(&rev
->item
->object
.oid
));
97 free_commit_list(result
);
101 static int handle_is_ancestor(int argc
, const char **argv
)
103 struct commit
*one
, *two
;
106 die("--is-ancestor takes exactly two commits");
107 one
= get_commit_reference(argv
[0]);
108 two
= get_commit_reference(argv
[1]);
109 if (in_merge_bases(one
, two
))
115 static int handle_fork_point(int argc
, const char **argv
)
117 struct object_id oid
;
118 struct commit
*derived
, *fork_point
;
119 const char *commitname
;
121 commitname
= (argc
== 2) ? argv
[1] : "HEAD";
122 if (get_oid(commitname
, &oid
))
123 die("Not a valid object name: '%s'", commitname
);
125 derived
= lookup_commit_reference(the_repository
, &oid
);
127 fork_point
= get_fork_point(argv
[0], derived
);
132 printf("%s\n", oid_to_hex(&fork_point
->object
.oid
));
136 int cmd_merge_base(int argc
, const char **argv
, const char *prefix
)
144 struct option options
[] = {
145 OPT_BOOL('a', "all", &show_all
, N_("output all common ancestors")),
146 OPT_CMDMODE(0, "octopus", &cmdmode
,
147 N_("find ancestors for a single n-way merge"), 'o'),
148 OPT_CMDMODE(0, "independent", &cmdmode
,
149 N_("list revs not reachable from others"), 'r'),
150 OPT_CMDMODE(0, "is-ancestor", &cmdmode
,
151 N_("is the first one ancestor of the other?"), 'a'),
152 OPT_CMDMODE(0, "fork-point", &cmdmode
,
153 N_("find where <commit> forked from reflog of <ref>"), 'f'),
157 git_config(git_default_config
, NULL
);
158 argc
= parse_options(argc
, argv
, prefix
, options
, merge_base_usage
, 0);
160 if (cmdmode
== 'a') {
162 usage_with_options(merge_base_usage
, options
);
164 die(_("options '%s' and '%s' cannot be used together"),
165 "--is-ancestor", "--all");
166 return handle_is_ancestor(argc
, argv
);
169 if (cmdmode
== 'r' && show_all
)
170 die(_("options '%s' and '%s' cannot be used together"),
171 "--independent", "--all");
174 return handle_octopus(argc
, argv
, show_all
);
177 return handle_independent(argc
, argv
);
179 if (cmdmode
== 'f') {
180 if (argc
< 1 || 2 < argc
)
181 usage_with_options(merge_base_usage
, options
);
182 return handle_fork_point(argc
, argv
);
186 usage_with_options(merge_base_usage
, options
);
188 ALLOC_ARRAY(rev
, argc
);
190 rev
[rev_nr
++] = get_commit_reference(*argv
++);
191 ret
= show_merge_base(rev
, rev_nr
, show_all
);