@matheusbsbs / matheus-bagliano / commits / df57accb46
Fork 0

merge-base: die with an error message if not passed a commit ref

Before this patch "git merge-base" just exited with error code 1 and without an error message in case it was passed a ref to an object that is not a commit (for example a tree). This patch makes it "die" in this case with an error message. While at it, this patch also refactors the code to get the commit reference from an argument into a new "get_commit_reference" function. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Jul 29, 2008 at 07:42 UTC df57accb46695b50e2db94424e9d4ebfed0694c8
1 file changed +17 -9
builtin-merge-base.c
+17 -9
@@ -22,10 +22,23 @@ static int show_merge_base(struct commit *rev1, struct commit *rev2, int show_al
22 static const char merge_base_usage[] =
23 "git merge-base [--all] <commit-id> <commit-id>";
24
25 +static struct commit *get_commit_reference(const char *arg)
26 +{
27 + unsigned char revkey[20];
28 + struct commit *r;
29 +
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);
35 +
36 + return r;
37 +}
38 +
39 int cmd_merge_base(int argc, const char **argv, const char *prefix)
40 {
41 struct commit *rev1, *rev2;
28 - unsigned char rev1key[20], rev2key[20];
42 int show_all = 0;
43
44 git_config(git_default_config, NULL);
@@ -40,13 +53,8 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
53 }
54 if (argc != 3)
55 usage(merge_base_usage);
43 - if (get_sha1(argv[1], rev1key))
44 - die("Not a valid object name %s", argv[1]);
45 - if (get_sha1(argv[2], rev2key))
46 - die("Not a valid object name %s", argv[2]);
47 - rev1 = lookup_commit_reference(rev1key);
48 - rev2 = lookup_commit_reference(rev2key);
49 - if (!rev1 || !rev2)
50 - return 1;
56 + rev1 = get_commit_reference(argv[1]);
57 + rev2 = get_commit_reference(argv[2]);
58 +
59 return show_merge_base(rev1, rev2, show_all);
60 }