blob: 54e7ec22370ce63150ddc93ebe252bea09f5064a [file] [log] [blame]
Peter Hagervallbaffc0e2007-07-15 01:14:45 +02001#include "builtin.h"
Linus Torvalds66834632005-04-17 12:18:17 -07002#include "cache.h"
Daniel Barkalowb5039db2005-04-18 11:39:48 -07003#include "commit.h"
Pierre Habouzite5d1a4d2008-10-02 14:59:19 +02004#include "parse-options.h"
Linus Torvalds66834632005-04-17 12:18:17 -07005
Christian Couder53eda892008-07-30 07:04:14 +02006static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
Johannes Schindelin52cab8a2006-06-29 15:16:46 +02007{
Christian Couder53eda892008-07-30 07:04:14 +02008 struct commit_list *result;
9
10 result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1, 0);
Johannes Schindelin52cab8a2006-06-29 15:16:46 +020011
12 if (!result)
13 return 1;
14
Junio C Hamano9585e402005-08-23 21:08:59 -070015 while (result) {
Johannes Schindelin52cab8a2006-06-29 15:16:46 +020016 printf("%s\n", sha1_to_hex(result->item->object.sha1));
Junio C Hamano9585e402005-08-23 21:08:59 -070017 if (!show_all)
18 return 0;
Johannes Schindelin52cab8a2006-06-29 15:16:46 +020019 result = result->next;
Junio C Hamano9585e402005-08-23 21:08:59 -070020 }
Johannes Schindelin52cab8a2006-06-29 15:16:46 +020021
Junio C Hamano9585e402005-08-23 21:08:59 -070022 return 0;
Linus Torvalds66834632005-04-17 12:18:17 -070023}
24
Pierre Habouzite5d1a4d2008-10-02 14:59:19 +020025static const char * const merge_base_usage[] = {
Michael J Gruber995bdc72009-08-05 09:59:19 +020026 "git merge-base [-a|--all] <commit> <commit>...",
Pierre Habouzite5d1a4d2008-10-02 14:59:19 +020027 NULL
28};
Junio C Hamano9585e402005-08-23 21:08:59 -070029
Christian Couderdf57acc2008-07-29 07:42:53 +020030static struct commit *get_commit_reference(const char *arg)
31{
32 unsigned char revkey[20];
33 struct commit *r;
34
35 if (get_sha1(arg, revkey))
36 die("Not a valid object name %s", arg);
37 r = lookup_commit_reference(revkey);
38 if (!r)
39 die("Not a valid commit name %s", arg);
40
41 return r;
42}
43
Junio C Hamano71dfbf22007-01-09 00:50:02 -080044int cmd_merge_base(int argc, const char **argv, const char *prefix)
Linus Torvalds66834632005-04-17 12:18:17 -070045{
Christian Couder53eda892008-07-30 07:04:14 +020046 struct commit **rev;
47 int rev_nr = 0;
Junio C Hamano71dfbf22007-01-09 00:50:02 -080048 int show_all = 0;
Linus Torvalds66834632005-04-17 12:18:17 -070049
Pierre Habouzite5d1a4d2008-10-02 14:59:19 +020050 struct option options[] = {
51 OPT_BOOLEAN('a', "all", &show_all, "outputs all common ancestors"),
52 OPT_END()
53 };
54
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010055 git_config(git_default_config, NULL);
Stephen Boyd37782922009-05-23 11:53:12 -070056 argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
Pierre Habouzite5d1a4d2008-10-02 14:59:19 +020057 if (argc < 2)
58 usage_with_options(merge_base_usage, options);
59 rev = xmalloc(argc * sizeof(*rev));
60 while (argc-- > 0)
61 rev[rev_nr++] = get_commit_reference(*argv++);
Christian Couder53eda892008-07-30 07:04:14 +020062 return show_merge_base(rev, rev_nr, show_all);
Linus Torvalds66834632005-04-17 12:18:17 -070063}