#include "cache.h"
-#include "revision.h"
+#include "commit.h"
-/*
- * This is stupid. We could have much better heurstics, I bet.
- */
-static int better(struct revision *new, struct revision *old)
-{
- return new->date > old->date;
-}
+static int show_all;
-static struct revision *common_parent(struct revision *rev1, struct revision *rev2)
+static int merge_base(struct commit *rev1, struct commit *rev2)
{
- int i;
- struct revision *best = NULL;
-
- mark_reachable(rev1, 1);
- mark_reachable(rev2, 2);
- for (i = 0; i < nr_revs ;i++) {
- struct revision *rev = revs[i];
- if ((rev->flags & 3) != 3)
- continue;
- if (!best) {
- best = rev;
- continue;
- }
- if (better(rev, best))
- best = rev;
+ struct commit_list *result = get_merge_bases(rev1, rev2, 0);
+
+ if (!result)
+ return 1;
+
+ while (result) {
+ printf("%s\n", sha1_to_hex(result->item->object.sha1));
+ if (!show_all)
+ return 0;
+ result = result->next;
}
- return best;
+
+ return 0;
}
+static const char merge_base_usage[] =
+"git-merge-base [--all] <commit-id> <commit-id>";
+
int main(int argc, char **argv)
{
- unsigned char rev1[20], rev2[20];
- struct revision *common;
-
- if (argc != 3 || get_sha1_hex(argv[1], rev1) || get_sha1_hex(argv[2], rev2))
- usage("merge-base <commit1> <commit2>");
-
- /*
- * We will eventually want to include a revision cache file
- * that "rev-tree.c" has generated, since this is going to
- * otherwise be quite expensive for big trees..
- *
- * That's some time off, though, and in the meantime we know
- * that we have a solution to the eventual expense.
- */
- common = common_parent(parse_commit(rev1), parse_commit(rev2));
- if (!common)
- die("no common parent found");
- printf("%s\n", sha1_to_hex(common->sha1));
- return 0;
+ struct commit *rev1, *rev2;
+ unsigned char rev1key[20], rev2key[20];
+
+ setup_git_directory();
+ git_config(git_default_config);
+
+ while (1 < argc && argv[1][0] == '-') {
+ char *arg = argv[1];
+ if (!strcmp(arg, "-a") || !strcmp(arg, "--all"))
+ show_all = 1;
+ else
+ usage(merge_base_usage);
+ argc--; argv++;
+ }
+ if (argc != 3)
+ usage(merge_base_usage);
+ if (get_sha1(argv[1], rev1key))
+ die("Not a valid object name %s", argv[1]);
+ if (get_sha1(argv[2], rev2key))
+ die("Not a valid object name %s", argv[2]);
+ rev1 = lookup_commit_reference(rev1key);
+ rev2 = lookup_commit_reference(rev2key);
+ if (!rev1 || !rev2)
+ return 1;
+ return merge_base(rev1, rev2);
}