]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-merge-base.c
git-submodule: replace duplicated code with a module_list function
[thirdparty/git.git] / builtin-merge-base.c
CommitLineData
baffc0e7 1#include "builtin.h"
6683463e 2#include "cache.h"
b5039db6 3#include "commit.h"
6683463e 4
53eda89b 5static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
52cab8a0 6{
53eda89b
CC
7 struct commit_list *result;
8
9 result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1, 0);
52cab8a0
JS
10
11 if (!result)
12 return 1;
13
9585e406 14 while (result) {
52cab8a0 15 printf("%s\n", sha1_to_hex(result->item->object.sha1));
9585e406
JH
16 if (!show_all)
17 return 0;
52cab8a0 18 result = result->next;
9585e406 19 }
52cab8a0 20
9585e406 21 return 0;
6683463e
LT
22}
23
9585e406 24static const char merge_base_usage[] =
53eda89b 25"git merge-base [--all] <commit-id> <commit-id>...";
9585e406 26
df57accb
CC
27static struct commit *get_commit_reference(const char *arg)
28{
29 unsigned char revkey[20];
30 struct commit *r;
31
32 if (get_sha1(arg, revkey))
33 die("Not a valid object name %s", arg);
34 r = lookup_commit_reference(revkey);
35 if (!r)
36 die("Not a valid commit name %s", arg);
37
38 return r;
39}
40
71dfbf22 41int cmd_merge_base(int argc, const char **argv, const char *prefix)
6683463e 42{
53eda89b
CC
43 struct commit **rev;
44 int rev_nr = 0;
71dfbf22 45 int show_all = 0;
6683463e 46
ef90d6d4 47 git_config(git_default_config, NULL);
53228a5f 48
9585e406 49 while (1 < argc && argv[1][0] == '-') {
71dfbf22 50 const char *arg = argv[1];
9585e406
JH
51 if (!strcmp(arg, "-a") || !strcmp(arg, "--all"))
52 show_all = 1;
53 else
54 usage(merge_base_usage);
55 argc--; argv++;
56 }
53eda89b 57 if (argc < 3)
9585e406 58 usage(merge_base_usage);
df57accb 59
53eda89b
CC
60 rev = xmalloc((argc - 1) * sizeof(*rev));
61
62 do {
63 rev[rev_nr++] = get_commit_reference(argv[1]);
64 argc--; argv++;
65 } while (argc > 1);
66
67 return show_merge_base(rev, rev_nr, show_all);
6683463e 68}