merge-base: separate "--independent" codepath into its own helper

It piggybacks on an unrelated handle_octopus() function only because there are some similarities between the way they need to preprocess their input and output their result. There is nothing similar in the true logic between these two operations. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Dec 30, 2013 at 11:37 UTC e2f5df42449ace700e5a237f604c34b4657c0edf
1 file changed +25 -6
builtin/merge-base.c
+25 -6
@@ -44,19 +44,36 @@ static struct commit *get_commit_reference(const char *arg)
44 return r;
45 }
46
47 -static int handle_octopus(int count, const char **args, int reduce, int show_all)
47 +static int handle_independent(int count, const char **args)
48 {
49 struct commit_list *revs = NULL;
50 struct commit_list *result;
51 int i;
52
53 - if (reduce)
54 - show_all = 1;
53 + for (i = count - 1; i >= 0; i--)
54 + commit_list_insert(get_commit_reference(args[i]), &revs);
55 +
56 + result = reduce_heads(revs);
57 + if (!result)
58 + return 1;
59 +
60 + while (result) {
61 + printf("%s\n", sha1_to_hex(result->item->object.sha1));
62 + result = result->next;
63 + }
64 + return 0;
65 +}
66 +
67 +static int handle_octopus(int count, const char **args, int show_all)
68 +{
69 + struct commit_list *revs = NULL;
70 + struct commit_list *result;
71 + int i;
72
73 for (i = count - 1; i >= 0; i--)
74 commit_list_insert(get_commit_reference(args[i]), &revs);
75
59 - result = reduce ? reduce_heads(revs) : get_octopus_merge_bases(revs);
76 + result = get_octopus_merge_bases(revs);
77
78 if (!result)
79 return 1;
@@ -114,8 +131,10 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
131 if (reduce && (show_all || octopus))
132 die("--independent cannot be used with other options");
133
117 - if (octopus || reduce)
118 - return handle_octopus(argc, argv, reduce, show_all);
134 + if (octopus)
135 + return handle_octopus(argc, argv, show_all);
136 + else if (reduce)
137 + return handle_independent(argc, argv);
138
139 rev = xmalloc(argc * sizeof(*rev));
140 while (argc-- > 0)