refactor merge_bases() as preparation to libify merge-base
Signed-off-by: Junio C Hamano <junkio@cox.net>
Johannes Schindelin committed
Jun 29, 2006 at 15:16 UTC
52cab8a0848463d793a3fc819b6d604ffc077ac1
1 file changed
+46
-18
merge-base.c
+46
-18
@@ -124,8 +124,6 @@ static struct commit *interesting(struct commit_list *list)
124
* to contaminate D and E.
125
*/
126
127
-static int show_all = 0;
128
-
127
static void mark_reachable_commits(struct commit_list *result,
128
struct commit_list *list)
129
{
@@ -167,34 +165,33 @@ static void mark_reachable_commits(struct commit_list *result,
165
}
166
}
167
170
-static int merge_base(struct commit *rev1, struct commit *rev2)
168
+struct commit_list *get_merge_bases(struct commit *rev1, struct commit *rev2)
169
{
170
struct commit_list *list = NULL;
171
struct commit_list *result = NULL;
172
struct commit_list *tmp = NULL;
173
176
- if (rev1 == rev2) {
177
- printf("%s\n", sha1_to_hex(rev1->object.sha1));
178
- return 0;
179
- }
174
+ if (rev1 == rev2)
175
+ return commit_list_insert(rev1, &result);
176
177
parse_commit(rev1);
178
parse_commit(rev2);
179
184
- rev1->object.flags |= 1;
185
- rev2->object.flags |= 2;
180
+ rev1->object.flags |= PARENT1;
181
+ rev2->object.flags |= PARENT2;
182
insert_by_date(rev1, &list);
183
insert_by_date(rev2, &list);
184
185
while (interesting(list)) {
186
struct commit *commit = list->item;
187
struct commit_list *parents;
192
- int flags = commit->object.flags & 7;
188
+ int flags = commit->object.flags
189
+ & (PARENT1 | PARENT2 | UNINTERESTING);
190
191
tmp = list;
192
list = list->next;
193
free(tmp);
197
- if (flags == 3) {
194
+ if (flags == (PARENT1 | PARENT2)) {
195
insert_by_date(commit, &result);
196
197
/* Mark parents of a found merge uninteresting */
@@ -213,21 +210,52 @@ static int merge_base(struct commit *rev1, struct commit *rev2)
210
}
211
212
if (!result)
216
- return 1;
213
+ return NULL;
214
215
if (result->next && list)
216
mark_reachable_commits(result, list);
217
218
+ /* cull duplicates */
219
+ for (tmp = result, list = NULL; tmp; ) {
220
+ struct commit *commit = tmp->item;
221
+ struct commit_list *next = tmp->next;
222
+ if (commit->object.flags & UNINTERESTING) {
223
+ if (list != NULL)
224
+ list->next = next;
225
+ free(tmp);
226
+ } else {
227
+ if (list == NULL)
228
+ result = tmp;
229
+ list = tmp;
230
+ commit->object.flags |= UNINTERESTING;
231
+ }
232
+
233
+ tmp = next;
234
+ }
235
+
236
+ /* reset flags */
237
+ clear_commit_marks(rev1, PARENT1 | PARENT2 | UNINTERESTING);
238
+ clear_commit_marks(rev2, PARENT1 | PARENT2 | UNINTERESTING);
239
+
240
+ return result;
241
+}
242
+
243
+static int show_all = 0;
244
+
245
+static int merge_base(struct commit *rev1, struct commit *rev2)
246
+{
247
+ struct commit_list *result = get_merge_bases(rev1, rev2);
248
+
249
+ if (!result)
250
+ return 1;
251
+
252
while (result) {
222
- struct commit *commit = result->item;
223
- result = result->next;
224
- if (commit->object.flags & UNINTERESTING)
225
- continue;
226
- printf("%s\n", sha1_to_hex(commit->object.sha1));
253
+ printf("%s\n", sha1_to_hex(result->item->object.sha1));
254
if (!show_all)
255
return 0;
229
- commit->object.flags |= UNINTERESTING;
256
+ result = result->next;
257
}
258
+
259
return 0;
260
}
261