Daniel Barkalow | b5039db | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 1 | #include <stdlib.h> |
Linus Torvalds | 6683463 | 2005-04-17 12:18:17 -0700 | [diff] [blame] | 2 | #include "cache.h" |
Daniel Barkalow | b5039db | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 3 | #include "commit.h" |
Linus Torvalds | 6683463 | 2005-04-17 12:18:17 -0700 | [diff] [blame] | 4 | |
Johannes Schindelin | 52cab8a | 2006-06-29 15:16:46 +0200 | [diff] [blame] | 5 | static int show_all = 0; |
| 6 | |
| 7 | static int merge_base(struct commit *rev1, struct commit *rev2) |
| 8 | { |
Rene Scharfe | c0fa825 | 2006-07-02 11:49:38 +0200 | [diff] [blame] | 9 | struct commit_list *result = get_merge_bases(rev1, rev2, 0); |
Johannes Schindelin | 52cab8a | 2006-06-29 15:16:46 +0200 | [diff] [blame] | 10 | |
| 11 | if (!result) |
| 12 | return 1; |
| 13 | |
Junio C Hamano | 9585e40 | 2005-08-23 21:08:59 -0700 | [diff] [blame] | 14 | while (result) { |
Johannes Schindelin | 52cab8a | 2006-06-29 15:16:46 +0200 | [diff] [blame] | 15 | printf("%s\n", sha1_to_hex(result->item->object.sha1)); |
Junio C Hamano | 9585e40 | 2005-08-23 21:08:59 -0700 | [diff] [blame] | 16 | if (!show_all) |
| 17 | return 0; |
Johannes Schindelin | 52cab8a | 2006-06-29 15:16:46 +0200 | [diff] [blame] | 18 | result = result->next; |
Junio C Hamano | 9585e40 | 2005-08-23 21:08:59 -0700 | [diff] [blame] | 19 | } |
Johannes Schindelin | 52cab8a | 2006-06-29 15:16:46 +0200 | [diff] [blame] | 20 | |
Junio C Hamano | 9585e40 | 2005-08-23 21:08:59 -0700 | [diff] [blame] | 21 | return 0; |
Linus Torvalds | 6683463 | 2005-04-17 12:18:17 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Junio C Hamano | 9585e40 | 2005-08-23 21:08:59 -0700 | [diff] [blame] | 24 | static const char merge_base_usage[] = |
| 25 | "git-merge-base [--all] <commit-id> <commit-id>"; |
| 26 | |
Linus Torvalds | 6683463 | 2005-04-17 12:18:17 -0700 | [diff] [blame] | 27 | int main(int argc, char **argv) |
| 28 | { |
Junio C Hamano | 9585e40 | 2005-08-23 21:08:59 -0700 | [diff] [blame] | 29 | struct commit *rev1, *rev2; |
Daniel Barkalow | b5039db | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 30 | unsigned char rev1key[20], rev2key[20]; |
Linus Torvalds | 6683463 | 2005-04-17 12:18:17 -0700 | [diff] [blame] | 31 | |
Junio C Hamano | 53228a5 | 2005-11-26 00:50:02 -0800 | [diff] [blame] | 32 | setup_git_directory(); |
Junio C Hamano | 84a9b58 | 2006-03-23 23:41:18 -0800 | [diff] [blame] | 33 | git_config(git_default_config); |
Junio C Hamano | 53228a5 | 2005-11-26 00:50:02 -0800 | [diff] [blame] | 34 | |
Junio C Hamano | 9585e40 | 2005-08-23 21:08:59 -0700 | [diff] [blame] | 35 | while (1 < argc && argv[1][0] == '-') { |
| 36 | char *arg = argv[1]; |
| 37 | if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) |
| 38 | show_all = 1; |
| 39 | else |
| 40 | usage(merge_base_usage); |
| 41 | argc--; argv++; |
| 42 | } |
Dmitry V. Levin | 31fff30 | 2006-05-09 01:43:38 +0400 | [diff] [blame] | 43 | if (argc != 3) |
Junio C Hamano | 9585e40 | 2005-08-23 21:08:59 -0700 | [diff] [blame] | 44 | usage(merge_base_usage); |
Dmitry V. Levin | 31fff30 | 2006-05-09 01:43:38 +0400 | [diff] [blame] | 45 | if (get_sha1(argv[1], rev1key)) |
| 46 | die("Not a valid object name %s", argv[1]); |
| 47 | if (get_sha1(argv[2], rev2key)) |
| 48 | die("Not a valid object name %s", argv[2]); |
Linus Torvalds | 9b632be | 2005-05-18 16:16:51 -0700 | [diff] [blame] | 49 | rev1 = lookup_commit_reference(rev1key); |
| 50 | rev2 = lookup_commit_reference(rev2key); |
Linus Torvalds | 4f7eb2e | 2005-07-30 15:10:20 -0700 | [diff] [blame] | 51 | if (!rev1 || !rev2) |
| 52 | return 1; |
Junio C Hamano | 9585e40 | 2005-08-23 21:08:59 -0700 | [diff] [blame] | 53 | return merge_base(rev1, rev2); |
Linus Torvalds | 6683463 | 2005-04-17 12:18:17 -0700 | [diff] [blame] | 54 | } |