git-p4: chdir now properly sets PWD environment variable in msysGit
[git/git-p4.git] / builtin-merge-base.c
blob3382b1382a7dcbd525126a35209072da4b4d8041
1 #include "builtin.h"
2 #include "cache.h"
3 #include "commit.h"
5 static int show_merge_base(struct commit *rev1, struct commit *rev2, int show_all)
7 struct commit_list *result = get_merge_bases(rev1, rev2, 0);
9 if (!result)
10 return 1;
12 while (result) {
13 printf("%s\n", sha1_to_hex(result->item->object.sha1));
14 if (!show_all)
15 return 0;
16 result = result->next;
19 return 0;
22 static const char merge_base_usage[] =
23 "git merge-base [--all] <commit-id> <commit-id>";
25 static struct commit *get_commit_reference(const char *arg)
27 unsigned char revkey[20];
28 struct commit *r;
30 if (get_sha1(arg, revkey))
31 die("Not a valid object name %s", arg);
32 r = lookup_commit_reference(revkey);
33 if (!r)
34 die("Not a valid commit name %s", arg);
36 return r;
39 int cmd_merge_base(int argc, const char **argv, const char *prefix)
41 struct commit *rev1, *rev2;
42 int show_all = 0;
44 git_config(git_default_config, NULL);
46 while (1 < argc && argv[1][0] == '-') {
47 const char *arg = argv[1];
48 if (!strcmp(arg, "-a") || !strcmp(arg, "--all"))
49 show_all = 1;
50 else
51 usage(merge_base_usage);
52 argc--; argv++;
54 if (argc != 3)
55 usage(merge_base_usage);
56 rev1 = get_commit_reference(argv[1]);
57 rev2 = get_commit_reference(argv[2]);
59 return show_merge_base(rev1, rev2, show_all);