4 #include "parse-options.h"
6 static int show_merge_base(struct commit
**rev
, int rev_nr
, int show_all
)
8 struct commit_list
*result
;
10 result
= get_merge_bases_many(rev
[0], rev_nr
- 1, rev
+ 1, 0);
16 printf("%s\n", sha1_to_hex(result
->item
->object
.sha1
));
19 result
= result
->next
;
25 static const char * const merge_base_usage
[] = {
26 N_("git merge-base [-a|--all] <commit> <commit>..."),
27 N_("git merge-base [-a|--all] --octopus <commit>..."),
28 N_("git merge-base --independent <commit>..."),
29 N_("git merge-base --is-ancestor <commit> <commit>"),
33 static struct commit
*get_commit_reference(const char *arg
)
35 unsigned char revkey
[20];
38 if (get_sha1(arg
, revkey
))
39 die("Not a valid object name %s", arg
);
40 r
= lookup_commit_reference(revkey
);
42 die("Not a valid commit name %s", arg
);
47 static int handle_independent(int count
, const char **args
)
49 struct commit_list
*revs
= NULL
;
50 struct commit_list
*result
;
53 for (i
= count
- 1; i
>= 0; i
--)
54 commit_list_insert(get_commit_reference(args
[i
]), &revs
);
56 result
= reduce_heads(revs
);
61 printf("%s\n", sha1_to_hex(result
->item
->object
.sha1
));
62 result
= result
->next
;
67 static int handle_octopus(int count
, const char **args
, int show_all
)
69 struct commit_list
*revs
= NULL
;
70 struct commit_list
*result
;
73 for (i
= count
- 1; i
>= 0; i
--)
74 commit_list_insert(get_commit_reference(args
[i
]), &revs
);
76 result
= get_octopus_merge_bases(revs
);
82 printf("%s\n", sha1_to_hex(result
->item
->object
.sha1
));
85 result
= result
->next
;
91 static int handle_is_ancestor(int argc
, const char **argv
)
93 struct commit
*one
, *two
;
96 die("--is-ancestor takes exactly two commits");
97 one
= get_commit_reference(argv
[0]);
98 two
= get_commit_reference(argv
[1]);
99 if (in_merge_bases(one
, two
))
105 int cmd_merge_base(int argc
, const char **argv
, const char *prefix
)
114 struct option options
[] = {
115 OPT_BOOLEAN('a', "all", &show_all
, N_("output all common ancestors")),
116 OPT_BOOLEAN(0, "octopus", &octopus
, N_("find ancestors for a single n-way merge")),
117 OPT_BOOLEAN(0, "independent", &reduce
, N_("list revs not reachable from others")),
118 OPT_BOOLEAN(0, "is-ancestor", &is_ancestor
,
119 N_("is the first one ancestor of the other?")),
123 git_config(git_default_config
, NULL
);
124 argc
= parse_options(argc
, argv
, prefix
, options
, merge_base_usage
, 0);
125 if (!octopus
&& !reduce
&& argc
< 2)
126 usage_with_options(merge_base_usage
, options
);
127 if (is_ancestor
&& (show_all
|| octopus
|| reduce
))
128 die("--is-ancestor cannot be used with other options");
130 return handle_is_ancestor(argc
, argv
);
131 if (reduce
&& (show_all
|| octopus
))
132 die("--independent cannot be used with other options");
135 return handle_octopus(argc
, argv
, show_all
);
137 return handle_independent(argc
, argv
);
139 rev
= xmalloc(argc
* sizeof(*rev
));
141 rev
[rev_nr
++] = get_commit_reference(*argv
++);
142 return show_merge_base(rev
, rev_nr
, show_all
);