Commit f684970a1087abe34319d35a46f2e954681d5323

Russell Belfer 2012-11-29T22:53:34

Merge pull request #1108 from libgit2/ahead-behind-count Add API to calculate ahead/behind count

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
diff --git a/include/git2.h b/include/git2.h
index fe8b021..e138e27 100644
--- a/include/git2.h
+++ b/include/git2.h
@@ -23,6 +23,7 @@
 #include "git2/repository.h"
 #include "git2/revwalk.h"
 #include "git2/merge.h"
+#include "git2/graph.h"
 #include "git2/refs.h"
 #include "git2/reflog.h"
 #include "git2/revparse.h"
diff --git a/include/git2/graph.h b/include/git2/graph.h
new file mode 100644
index 0000000..c89efa6
--- /dev/null
+++ b/include/git2/graph.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2009-2012 the libgit2 contributors
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_git_graph_h__
+#define INCLUDE_git_graph_h__
+
+#include "common.h"
+#include "types.h"
+#include "oid.h"
+
+/**
+ * @file git2/graph.h
+ * @brief Git graph traversal routines
+ * @defgroup git_revwalk Git graph traversal routines
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+/**
+ * Count the number of unique commits between two commit objects
+ *
+ * @param ahead number of commits, starting at `one`, unique from commits in `two`
+ * @param behind number of commits, starting at `two`, unique from commits in `one`
+ * @param repo the repository where the commits exist
+ * @param one one of the commits
+ * @param two the other commit
+ */
+GIT_EXTERN(int) git_graph_ahead_behind(size_t *ahead, size_t *behind, git_repository *repo, const git_oid *one, const git_oid *two);
+
+/** @} */
+GIT_END_DECL
+#endif
diff --git a/src/commit_list.c b/src/commit_list.c
index c1a7b22..d79934a 100644
--- a/src/commit_list.c
+++ b/src/commit_list.c
@@ -127,7 +127,7 @@ static int commit_quick_parse(git_revwalk *walk, git_commit_list_node *commit, g
 		if (git_oid_fromstr(&oid, (char *)buffer + strlen("parent ")) < 0)
 			return -1;
 
-		commit->parents[i] = commit_lookup(walk, &oid);
+		commit->parents[i] = git_revwalk__commit_lookup(walk, &oid);
 		if (commit->parents[i] == NULL)
 			return -1;
 
@@ -181,9 +181,13 @@ int git_commit_list_parse(git_revwalk *walk, git_commit_list_node *commit)
 
 	if ((error = git_odb_read(&obj, walk->odb, &commit->oid)) < 0)
 		return error;
-	assert(obj->raw.type == GIT_OBJ_COMMIT);
 
-	error = commit_quick_parse(walk, commit, &obj->raw);
+	if (obj->raw.type != GIT_OBJ_COMMIT) {
+		giterr_set(GITERR_INVALID, "Object is no commit object");
+		error = -1;
+	} else
+		error = commit_quick_parse(walk, commit, &obj->raw);
+
 	git_odb_object_free(obj);
 	return error;
 }
diff --git a/src/graph.c b/src/graph.c
new file mode 100644
index 0000000..28026d4
--- /dev/null
+++ b/src/graph.c
@@ -0,0 +1,93 @@
+
+/*
+ * Copyright (C) 2009-2012 the libgit2 contributors
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "revwalk.h"
+#include "merge.h"
+#include "git2/graph.h"
+
+static int ahead_behind(git_commit_list_node *one, git_commit_list_node *two,
+	size_t *ahead, size_t *behind)
+{
+	git_commit_list_node *commit;
+	git_pqueue pq;
+	int i;
+	*ahead = 0;
+	*behind = 0;
+
+	if (git_pqueue_init(&pq, 2, git_commit_list_time_cmp) < 0)
+		return -1;
+	if (git_pqueue_insert(&pq, one) < 0)
+		return -1;
+	if (git_pqueue_insert(&pq, two) < 0)
+		return -1;
+
+	while ((commit = git_pqueue_pop(&pq)) != NULL) {
+		if (commit->flags & RESULT ||
+			(commit->flags & (PARENT1 | PARENT2)) == (PARENT1 | PARENT2))
+			continue;
+		else if (commit->flags & PARENT1)
+			(*behind)++;
+		else if (commit->flags & PARENT2)
+			(*ahead)++;
+
+		for (i = 0; i < commit->out_degree; i++) {
+			git_commit_list_node *p = commit->parents[i];
+			if (git_pqueue_insert(&pq, p) < 0)
+				return -1;
+		}
+		commit->flags |= RESULT;
+	}
+
+	return 0;
+}
+
+int git_graph_ahead_behind(size_t *ahead, size_t *behind, git_repository *repo,
+	const git_oid *one, const git_oid *two)
+{
+	git_revwalk *walk;
+	git_vector list;
+	struct git_commit_list *result = NULL;
+	git_commit_list_node *commit1, *commit2;
+	void *contents[1];
+
+	if (git_revwalk_new(&walk, repo) < 0)
+		return -1;
+
+	commit2 = git_revwalk__commit_lookup(walk, two);
+	if (commit2 == NULL)
+		goto on_error;
+
+	/* This is just one value, so we can do it on the stack */
+	memset(&list, 0x0, sizeof(git_vector));
+	contents[0] = commit2;
+	list.length = 1;
+	list.contents = contents;
+
+	commit1 = git_revwalk__commit_lookup(walk, one);
+	if (commit1 == NULL)
+		goto on_error;
+
+	if (git_merge__bases_many(&result, walk, commit1, &list) < 0)
+		goto on_error;
+	if (ahead_behind(commit1, commit2, ahead, behind) < 0)
+		goto on_error;
+
+	if (!result) {
+		git_revwalk_free(walk);
+		return GIT_ENOTFOUND;
+	}
+
+	git_commit_list_free(&result);
+	git_revwalk_free(walk);
+
+	return 0;
+
+on_error:
+	git_revwalk_free(walk);
+	return -1;
+}
diff --git a/src/merge.c b/src/merge.c
index c795b80..1386d09 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -71,14 +71,14 @@ int git_merge_base_many(git_oid *out, git_repository *repo, const git_oid input_
 		goto cleanup;
 
 	for (i = 1; i < length; i++) {
-		commit = commit_lookup(walk, &input_array[i]);
+		commit = git_revwalk__commit_lookup(walk, &input_array[i]);
 		if (commit == NULL)
 			goto cleanup;
 
 		git_vector_insert(&list, commit);
 	}
 
-	commit = commit_lookup(walk, &input_array[0]);
+	commit = git_revwalk__commit_lookup(walk, &input_array[0]);
 	if (commit == NULL)
 		goto cleanup;
 
@@ -112,7 +112,7 @@ int git_merge_base(git_oid *out, git_repository *repo, const git_oid *one, const
 	if (git_revwalk_new(&walk, repo) < 0)
 		return -1;
 
-	commit = commit_lookup(walk, two);
+	commit = git_revwalk__commit_lookup(walk, two);
 	if (commit == NULL)
 		goto on_error;
 
@@ -122,7 +122,7 @@ int git_merge_base(git_oid *out, git_repository *repo, const git_oid *one, const
 	list.length = 1;
 	list.contents = contents;
 
-	commit = commit_lookup(walk, one);
+	commit = git_revwalk__commit_lookup(walk, one);
 	if (commit == NULL)
 		goto on_error;
 
@@ -241,4 +241,3 @@ int git_merge__bases_many(git_commit_list **out, git_revwalk *walk, git_commit_l
 	*out = result;
 	return 0;
 }
-
diff --git a/src/revwalk.c b/src/revwalk.c
index bdbbdbd..cad2f09 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -15,7 +15,8 @@
 
 #include <regex.h>
 
-git_commit_list_node *commit_lookup(git_revwalk *walk, const git_oid *oid)
+git_commit_list_node *git_revwalk__commit_lookup(
+	git_revwalk *walk, const git_oid *oid)
 {
 	git_commit_list_node *commit;
 	khiter_t pos;
@@ -101,7 +102,7 @@ static int push_commit(git_revwalk *walk, const git_oid *oid, int uninteresting)
 		return -1;
 	}
 
-	commit = commit_lookup(walk, oid);
+	commit = git_revwalk__commit_lookup(walk, oid);
 	if (commit == NULL)
 		return -1; /* error already reported by failed lookup */
 
diff --git a/src/revwalk.h b/src/revwalk.h
index 2d482cf..6146eaf 100644
--- a/src/revwalk.h
+++ b/src/revwalk.h
@@ -39,6 +39,6 @@ struct git_revwalk {
 	git_vector twos;
 };
 
-git_commit_list_node *commit_lookup(git_revwalk *walk, const git_oid *oid);
+git_commit_list_node *git_revwalk__commit_lookup(git_revwalk *walk, const git_oid *oid);
 
 #endif
diff --git a/tests-clar/resources/twowaymerge.git/HEAD b/tests-clar/resources/twowaymerge.git/HEAD
new file mode 100644
index 0000000..cb089cd
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/HEAD
@@ -0,0 +1 @@
+ref: refs/heads/master
diff --git a/tests-clar/resources/twowaymerge.git/config b/tests-clar/resources/twowaymerge.git/config
new file mode 100644
index 0000000..c53d818
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/config
@@ -0,0 +1,5 @@
+[core]
+	repositoryformatversion = 0
+	filemode = true
+	bare = true
+	ignorecase = true
diff --git a/tests-clar/resources/twowaymerge.git/description b/tests-clar/resources/twowaymerge.git/description
new file mode 100644
index 0000000..498b267
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/description
@@ -0,0 +1 @@
+Unnamed repository; edit this file 'description' to name the repository.
diff --git a/tests-clar/resources/twowaymerge.git/info/exclude b/tests-clar/resources/twowaymerge.git/info/exclude
new file mode 100644
index 0000000..a5196d1
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/info/exclude
@@ -0,0 +1,6 @@
+# git ls-files --others --exclude-from=.git/info/exclude
+# Lines that start with '#' are comments.
+# For a project mostly in C, the following would be a good set of
+# exclude patterns (uncomment them if you want to use them):
+# *.[oa]
+# *~
diff --git a/tests-clar/resources/twowaymerge.git/objects/0c/8a3f1f3d5f421cf83048c7c73ee3b55a5e0f29 b/tests-clar/resources/twowaymerge.git/objects/0c/8a3f1f3d5f421cf83048c7c73ee3b55a5e0f29
new file mode 100644
index 0000000..12698af
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/0c/8a3f1f3d5f421cf83048c7c73ee3b55a5e0f29 differ
diff --git a/tests-clar/resources/twowaymerge.git/objects/10/2dce8e3081f398e4bdd9fd894dc85ac3ca6a67 b/tests-clar/resources/twowaymerge.git/objects/10/2dce8e3081f398e4bdd9fd894dc85ac3ca6a67
new file mode 100644
index 0000000..3806ee7
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/10/2dce8e3081f398e4bdd9fd894dc85ac3ca6a67 differ
diff --git a/tests-clar/resources/twowaymerge.git/objects/17/7d8634a28e26ec7819284752757ebe01a479d5 b/tests-clar/resources/twowaymerge.git/objects/17/7d8634a28e26ec7819284752757ebe01a479d5
new file mode 100644
index 0000000..e91e06d
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/17/7d8634a28e26ec7819284752757ebe01a479d5 differ
diff --git a/tests-clar/resources/twowaymerge.git/objects/1c/30b88f5f3ee66d78df6520a7de9e89b890818b b/tests-clar/resources/twowaymerge.git/objects/1c/30b88f5f3ee66d78df6520a7de9e89b890818b
new file mode 100644
index 0000000..57d1a0f
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/objects/1c/30b88f5f3ee66d78df6520a7de9e89b890818b
@@ -0,0 +1,3 @@
+xM
+0F]s	zIi^{<>TKy4`6,y9jGJ8b\f5/
^8v'˜`SƝ%[
+T[[,psL6oK5;n-=D
\ No newline at end of file
diff --git a/tests-clar/resources/twowaymerge.git/objects/1f/4c0311a24b63f6fc209a59a1e404942d4a5006 b/tests-clar/resources/twowaymerge.git/objects/1f/4c0311a24b63f6fc209a59a1e404942d4a5006
new file mode 100644
index 0000000..99288fd
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/objects/1f/4c0311a24b63f6fc209a59a1e404942d4a5006
@@ -0,0 +1,2 @@
+x=0@aM6BlH m!RqO7[
r5gNXű)Eg]DY2c	R8x7
+TRo8~Ӣ[#uj;`7
\ No newline at end of file
diff --git a/tests-clar/resources/twowaymerge.git/objects/22/24e191514cb4bd8c566d80dac22dfcb1e9bb83 b/tests-clar/resources/twowaymerge.git/objects/22/24e191514cb4bd8c566d80dac22dfcb1e9bb83
new file mode 100644
index 0000000..48466ea
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/objects/22/24e191514cb4bd8c566d80dac22dfcb1e9bb83
@@ -0,0 +1,3 @@
+xK
+0@]sK&	z4HMobY1t[JblɈ4vɡL
'՛V`B .
+Im 1ZǠxcKh^^+\>?2a.M,tTBp^kucjV_sFh
\ No newline at end of file
diff --git a/tests-clar/resources/twowaymerge.git/objects/29/6e56023cdc034d2735fee8c0d85a659d1b07f4 b/tests-clar/resources/twowaymerge.git/objects/29/6e56023cdc034d2735fee8c0d85a659d1b07f4
new file mode 100644
index 0000000..aa3fccd
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/29/6e56023cdc034d2735fee8c0d85a659d1b07f4 differ
diff --git a/tests-clar/resources/twowaymerge.git/objects/31/51880ae2b363f1c262cf98b750c1f169a0d432 b/tests-clar/resources/twowaymerge.git/objects/31/51880ae2b363f1c262cf98b750c1f169a0d432
new file mode 100644
index 0000000..235d42b
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/31/51880ae2b363f1c262cf98b750c1f169a0d432 differ
diff --git a/tests-clar/resources/twowaymerge.git/objects/3b/287f8730c81d0b763c2d294618a5e32b67b4f8 b/tests-clar/resources/twowaymerge.git/objects/3b/287f8730c81d0b763c2d294618a5e32b67b4f8
new file mode 100644
index 0000000..56ddac5
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/3b/287f8730c81d0b763c2d294618a5e32b67b4f8 differ
diff --git a/tests-clar/resources/twowaymerge.git/objects/42/b7311aa626e712891940c1ec5d5cba201946a4 b/tests-clar/resources/twowaymerge.git/objects/42/b7311aa626e712891940c1ec5d5cba201946a4
new file mode 100644
index 0000000..a8e6581
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/objects/42/b7311aa626e712891940c1ec5d5cba201946a4
@@ -0,0 +1,3 @@
+xν
0@ajOqc"!DDK1
+h_ZʣAC[saȞpI#lBq׼4Z"(yGFd#y[
+X[[J/psL6oAXሂo7ԸEc
\ No newline at end of file
diff --git a/tests-clar/resources/twowaymerge.git/objects/49/6d6428b9cf92981dc9495211e6e1120fb6f2ba b/tests-clar/resources/twowaymerge.git/objects/49/6d6428b9cf92981dc9495211e6e1120fb6f2ba
new file mode 100644
index 0000000..978bc34
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/49/6d6428b9cf92981dc9495211e6e1120fb6f2ba differ
diff --git a/tests-clar/resources/twowaymerge.git/objects/59/b0cf7d74659e1cdb13305319d6d4ce2733c118 b/tests-clar/resources/twowaymerge.git/objects/59/b0cf7d74659e1cdb13305319d6d4ce2733c118
new file mode 100644
index 0000000..30b507c
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/59/b0cf7d74659e1cdb13305319d6d4ce2733c118 differ
diff --git a/tests-clar/resources/twowaymerge.git/objects/6a/b5d28acbf3c3bdff276f7ccfdf29c1520e542f b/tests-clar/resources/twowaymerge.git/objects/6a/b5d28acbf3c3bdff276f7ccfdf29c1520e542f
new file mode 100644
index 0000000..ff6a386
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/objects/6a/b5d28acbf3c3bdff276f7ccfdf29c1520e542f
@@ -0,0 +1 @@
+xM0@a=\@2cܙeRܾŗǵRԶ@(i$uO19Ro"
9x-
@cc3;-KvH+9Fe{O{]b	+\>oܦ}踖+Hm
z(zl7 F-
\ No newline at end of file
diff --git a/tests-clar/resources/twowaymerge.git/objects/6c/fca542b55b8b37017e6125a4b8f59a6eae6f11 b/tests-clar/resources/twowaymerge.git/objects/6c/fca542b55b8b37017e6125a4b8f59a6eae6f11
new file mode 100644
index 0000000..9a969a2
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/6c/fca542b55b8b37017e6125a4b8f59a6eae6f11 differ
diff --git a/tests-clar/resources/twowaymerge.git/objects/76/5b32c65d38f04c4f287abda055818ec0f26912 b/tests-clar/resources/twowaymerge.git/objects/76/5b32c65d38f04c4f287abda055818ec0f26912
new file mode 100644
index 0000000..493bbc0
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/76/5b32c65d38f04c4f287abda055818ec0f26912 differ
diff --git a/tests-clar/resources/twowaymerge.git/objects/7b/8c336c45fc6895c1c60827260fe5d798e5d247 b/tests-clar/resources/twowaymerge.git/objects/7b/8c336c45fc6895c1c60827260fe5d798e5d247
new file mode 100644
index 0000000..19e7ef4
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/objects/7b/8c336c45fc6895c1c60827260fe5d798e5d247
@@ -0,0 +1,3 @@
+xA @Qלb.`3S(1LzB[TzܾϏe
+D|kJC3fȵu	L>YGMVeMK9Z5H#{EJ:
+ϲp:ne>6-sHGծfMS}ZE
\ No newline at end of file
diff --git a/tests-clar/resources/twowaymerge.git/objects/82/bf9a1a10a4b25c1f14c9607b60970705e92545 b/tests-clar/resources/twowaymerge.git/objects/82/bf9a1a10a4b25c1f14c9607b60970705e92545
new file mode 100644
index 0000000..89b0b9f
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/objects/82/bf9a1a10a4b25c1f14c9607b60970705e92545
@@ -0,0 +1 @@
+x
09mh_BܐRןc1-pyPKy4RږDGFJvFE>1#q
joimbvSYSbErQ"e{+ޖ=6b+>?/-;3hC#gXyF
\ No newline at end of file
diff --git a/tests-clar/resources/twowaymerge.git/objects/8b/82fb1794cb1c8c7f172ec730a4c2db0ae3e650 b/tests-clar/resources/twowaymerge.git/objects/8b/82fb1794cb1c8c7f172ec730a4c2db0ae3e650
new file mode 100644
index 0000000..8e9b758
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/objects/8b/82fb1794cb1c8c7f172ec730a4c2db0ae3e650
@@ -0,0 +1,3 @@
+xν
0@ajOq7BtHLp%A8FٟW<=
x"ʎ$%1dcDNL:Yv=7yic
+l$\b{DbOd9x+
+6T[{??yqnӖ:cQZ]͖7H
\ No newline at end of file
diff --git a/tests-clar/resources/twowaymerge.git/objects/9a/40a2f11c191f180c47e54b11567cb3c1e89b30 b/tests-clar/resources/twowaymerge.git/objects/9a/40a2f11c191f180c47e54b11567cb3c1e89b30
new file mode 100644
index 0000000..1de1224
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/9a/40a2f11c191f180c47e54b11567cb3c1e89b30 differ
diff --git a/tests-clar/resources/twowaymerge.git/objects/9b/219343610c88a1187c996d0dc58330b55cee28 b/tests-clar/resources/twowaymerge.git/objects/9b/219343610c88a1187c996d0dc58330b55cee28
new file mode 100644
index 0000000..8b64b43
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/objects/9b/219343610c88a1187c996d0dc58330b55cee28
@@ -0,0 +1,2 @@
+xKj1ЬFj}Z3	VdFA#?\ zu]FSo"J&^,9$GEd)7|&[6(FU"&h<Fc4Aƿ>"ZQ;m9\;KP%1b9k93Gkwni[uZh"RYC[]=0IrKpO:
+pʯ
_(c
\ No newline at end of file
diff --git a/tests-clar/resources/twowaymerge.git/objects/9f/e06a50f4d1634d6c6879854d01d80857388706 b/tests-clar/resources/twowaymerge.git/objects/9f/e06a50f4d1634d6c6879854d01d80857388706
new file mode 100644
index 0000000..055de01
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/9f/e06a50f4d1634d6c6879854d01d80857388706 differ
diff --git a/tests-clar/resources/twowaymerge.git/objects/a4/1a49f8f5cd9b6cb14a076bf8394881ed0b4d19 b/tests-clar/resources/twowaymerge.git/objects/a4/1a49f8f5cd9b6cb14a076bf8394881ed0b4d19
new file mode 100644
index 0000000..cb4d34e
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/objects/a4/1a49f8f5cd9b6cb14a076bf8394881ed0b4d19
@@ -0,0 +1,3 @@
+xν
0@ajOq7'!DD	A$FٟW<=<5Z
+8N(CzDž$'2!>[):#Dzǵ,
z	M	
d=tNŭ=
+wk}9.p9^Ʃ=I@Y=ulD
\ No newline at end of file
diff --git a/tests-clar/resources/twowaymerge.git/objects/a9/53a018c5b10b20c86e69fef55ebc8ad4c5a417 b/tests-clar/resources/twowaymerge.git/objects/a9/53a018c5b10b20c86e69fef55ebc8ad4c5a417
new file mode 100644
index 0000000..8235f18
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/objects/a9/53a018c5b10b20c86e69fef55ebc8ad4c5a417
@@ -0,0 +1 @@
+xJ0])nV3$is"ONŶɼ\|8!dzdXG/ޫϹp*CX@Z8|f[V0HDHE]6gI#g*9UEHH!MḦhRuo.{zSײ|үwȾ>14C8;rn8qۿ7kNui~M^
\ No newline at end of file
diff --git a/tests-clar/resources/twowaymerge.git/objects/a9/cce3cd1b3efbda5b1f4a6dcc3f1570b2d3d74c b/tests-clar/resources/twowaymerge.git/objects/a9/cce3cd1b3efbda5b1f4a6dcc3f1570b2d3d74c
new file mode 100644
index 0000000..4da7e82
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/objects/a9/cce3cd1b3efbda5b1f4a6dcc3f1570b2d3d74c
@@ -0,0 +1 @@
+x+)JMU044c040031QHdx6M9{wk+qIODd6>|X%>9j
\ No newline at end of file
diff --git a/tests-clar/resources/twowaymerge.git/objects/bd/1732c43c68d712ad09e1d872b9be6d4b9efdc4 b/tests-clar/resources/twowaymerge.git/objects/bd/1732c43c68d712ad09e1d872b9be6d4b9efdc4
new file mode 100644
index 0000000..b9b6012
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/bd/1732c43c68d712ad09e1d872b9be6d4b9efdc4 differ
diff --git a/tests-clar/resources/twowaymerge.git/objects/c3/7a783c20d92ac92362a78a32860f7eebf938ef b/tests-clar/resources/twowaymerge.git/objects/c3/7a783c20d92ac92362a78a32860f7eebf938ef
new file mode 100644
index 0000000..041e890
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/c3/7a783c20d92ac92362a78a32860f7eebf938ef differ
diff --git a/tests-clar/resources/twowaymerge.git/objects/cb/dd40facab1682754eb67f7a43f29e672903cf6 b/tests-clar/resources/twowaymerge.git/objects/cb/dd40facab1682754eb67f7a43f29e672903cf6
new file mode 100644
index 0000000..ccb156d
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/cb/dd40facab1682754eb67f7a43f29e672903cf6 differ
diff --git a/tests-clar/resources/twowaymerge.git/objects/cd/f97fd3bb48eb3827638bb33d208f5fd32d0aa6 b/tests-clar/resources/twowaymerge.git/objects/cd/f97fd3bb48eb3827638bb33d208f5fd32d0aa6
new file mode 100644
index 0000000..0e028dc
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/cd/f97fd3bb48eb3827638bb33d208f5fd32d0aa6 differ
diff --git a/tests-clar/resources/twowaymerge.git/objects/d6/f10d549cb335b9e6d38afc1f0088be69b50494 b/tests-clar/resources/twowaymerge.git/objects/d6/f10d549cb335b9e6d38afc1f0088be69b50494
new file mode 100644
index 0000000..b298c52
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/d6/f10d549cb335b9e6d38afc1f0088be69b50494 differ
diff --git a/tests-clar/resources/twowaymerge.git/objects/d9/acdc7ae7632adfeec67fa73c1e343cf4d1f47e b/tests-clar/resources/twowaymerge.git/objects/d9/acdc7ae7632adfeec67fa73c1e343cf4d1f47e
new file mode 100644
index 0000000..de94528
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/objects/d9/acdc7ae7632adfeec67fa73c1e343cf4d1f47e
@@ -0,0 +1 @@
+x+)JMU044c040031QHdx6M9{wk+qIODd>4|X%:79U
\ No newline at end of file
diff --git a/tests-clar/resources/twowaymerge.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 b/tests-clar/resources/twowaymerge.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391
new file mode 100644
index 0000000..7112238
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 differ
diff --git a/tests-clar/resources/twowaymerge.git/objects/ef/0488f0b722f0be8bcb90a7730ac7efafd1d694 b/tests-clar/resources/twowaymerge.git/objects/ef/0488f0b722f0be8bcb90a7730ac7efafd1d694
new file mode 100644
index 0000000..00f7d36
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/objects/ef/0488f0b722f0be8bcb90a7730ac7efafd1d694
@@ -0,0 +1 @@
+xM F]s0_cܙ@H1_ۗ}yh@mK	8Y4Ѩt^'`lPىڠ<gyY1*mF
5#2?ڲ!rZ5ƎpD(:-} #E
\ No newline at end of file
diff --git a/tests-clar/resources/twowaymerge.git/objects/fc/f7e3f51c11d199ab7a78403ee4f9ccd028da25 b/tests-clar/resources/twowaymerge.git/objects/fc/f7e3f51c11d199ab7a78403ee4f9ccd028da25
new file mode 100644
index 0000000..54989ea
Binary files /dev/null and b/tests-clar/resources/twowaymerge.git/objects/fc/f7e3f51c11d199ab7a78403ee4f9ccd028da25 differ
diff --git a/tests-clar/resources/twowaymerge.git/refs/heads/first-branch b/tests-clar/resources/twowaymerge.git/refs/heads/first-branch
new file mode 100644
index 0000000..ef0dead
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/refs/heads/first-branch
@@ -0,0 +1 @@
+2224e191514cb4bd8c566d80dac22dfcb1e9bb83
diff --git a/tests-clar/resources/twowaymerge.git/refs/heads/master b/tests-clar/resources/twowaymerge.git/refs/heads/master
new file mode 100644
index 0000000..ebf18f5
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/refs/heads/master
@@ -0,0 +1 @@
+1c30b88f5f3ee66d78df6520a7de9e89b890818b
diff --git a/tests-clar/resources/twowaymerge.git/refs/heads/second-branch b/tests-clar/resources/twowaymerge.git/refs/heads/second-branch
new file mode 100644
index 0000000..586a14a
--- /dev/null
+++ b/tests-clar/resources/twowaymerge.git/refs/heads/second-branch
@@ -0,0 +1 @@
+9b219343610c88a1187c996d0dc58330b55cee28
diff --git a/tests-clar/revwalk/mergebase.c b/tests-clar/revwalk/mergebase.c
index 268574e..9707d42 100644
--- a/tests-clar/revwalk/mergebase.c
+++ b/tests-clar/revwalk/mergebase.c
@@ -3,10 +3,12 @@
 #include <stdarg.h>
 
 static git_repository *_repo;
+static git_repository *_repo2;
 
 void test_revwalk_mergebase__initialize(void)
 {
 	cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
+	cl_git_pass(git_repository_open(&_repo2, cl_fixture("twowaymerge.git")));
 }
 
 void test_revwalk_mergebase__cleanup(void)
@@ -18,6 +20,7 @@ void test_revwalk_mergebase__cleanup(void)
 void test_revwalk_mergebase__single1(void)
 {
 	git_oid result, one, two, expected;
+	size_t ahead, behind;
 
 	cl_git_pass(git_oid_fromstr(&one, "c47800c7266a2be04c571c04d5a6614691ea99bd "));
 	cl_git_pass(git_oid_fromstr(&two, "9fd738e8f7967c078dceed8190330fc8648ee56a"));
@@ -25,11 +28,20 @@ void test_revwalk_mergebase__single1(void)
 
 	cl_git_pass(git_merge_base(&result, _repo, &one, &two));
 	cl_assert(git_oid_cmp(&result, &expected) == 0);
+
+	cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &one, &two));
+	cl_assert_equal_i(ahead, 2);
+	cl_assert_equal_i(behind, 1);
+
+	cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &two, &one));
+	cl_assert_equal_i(ahead,  1);
+	cl_assert_equal_i(behind,  2);
 }
 
 void test_revwalk_mergebase__single2(void)
 {
 	git_oid result, one, two, expected;
+	size_t ahead, behind;
 
 	cl_git_pass(git_oid_fromstr(&one, "763d71aadf09a7951596c9746c024e7eece7c7af"));
 	cl_git_pass(git_oid_fromstr(&two, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
@@ -37,11 +49,20 @@ void test_revwalk_mergebase__single2(void)
 
 	cl_git_pass(git_merge_base(&result, _repo, &one, &two));
 	cl_assert(git_oid_cmp(&result, &expected) == 0);
+
+	cl_git_pass(git_graph_ahead_behind( &ahead, &behind, _repo, &one, &two));
+	cl_assert_equal_i(ahead,  4);
+	cl_assert_equal_i(behind,  1);
+
+	cl_git_pass(git_graph_ahead_behind( &ahead, &behind, _repo, &two, &one));
+	cl_assert_equal_i(ahead,  1);
+	cl_assert_equal_i(behind,  4);
 }
 
 void test_revwalk_mergebase__merged_branch(void)
 {
 	git_oid result, one, two, expected;
+	size_t ahead, behind;
 
 	cl_git_pass(git_oid_fromstr(&one, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
 	cl_git_pass(git_oid_fromstr(&two, "9fd738e8f7967c078dceed8190330fc8648ee56a"));
@@ -52,11 +73,38 @@ void test_revwalk_mergebase__merged_branch(void)
 
 	cl_git_pass(git_merge_base(&result, _repo, &two, &one));
 	cl_assert(git_oid_cmp(&result, &expected) == 0);
+
+	cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &one, &two));
+	cl_assert_equal_i(ahead,  0);
+	cl_assert_equal_i(behind,  3);
+
+	cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &two, &one));
+	cl_assert_equal_i(ahead,  3);
+	cl_assert_equal_i(behind,  0);
+}
+
+void test_revwalk_mergebase__two_way_merge(void)
+{
+	git_oid one, two;
+	size_t ahead, behind;
+
+	cl_git_pass(git_oid_fromstr(&one, "9b219343610c88a1187c996d0dc58330b55cee28"));
+	cl_git_pass(git_oid_fromstr(&two, "a953a018c5b10b20c86e69fef55ebc8ad4c5a417"));
+	cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo2, &one, &two));
+
+	cl_assert_equal_i(ahead,  2);
+	cl_assert_equal_i(behind,  8);
+
+	cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo2, &two, &one));
+
+	cl_assert_equal_i(ahead,  8);
+	cl_assert_equal_i(behind,  2);
 }
 
 void test_revwalk_mergebase__no_common_ancestor_returns_ENOTFOUND(void)
 {
 	git_oid result, one, two;
+	size_t ahead, behind;
 	int error;
 
 	cl_git_pass(git_oid_fromstr(&one, "763d71aadf09a7951596c9746c024e7eece7c7af"));
@@ -66,6 +114,11 @@ void test_revwalk_mergebase__no_common_ancestor_returns_ENOTFOUND(void)
 	cl_git_fail(error);
 
 	cl_assert_equal_i(GIT_ENOTFOUND, error);
+
+	cl_git_fail(git_graph_ahead_behind(&ahead, &behind, _repo, &one, &two));
+	cl_git_fail(error);
+
+	cl_assert_equal_i(GIT_ENOTFOUND, error);
 }
 
 void test_revwalk_mergebase__no_off_by_one_missing(void)
@@ -143,7 +196,7 @@ void test_revwalk_mergebase__many_merge_branch(void)
 }
 
 /*
- * $ git log --graph --all
+ * testrepo.git $ git log --graph --all
  * * commit 763d71aadf09a7951596c9746c024e7eece7c7af
  * | Author: nulltoken <emeric.fermas@gmail.com>
  * | Date:   Sun Oct 9 12:54:47 2011 +0200
@@ -222,3 +275,104 @@ void test_revwalk_mergebase__many_merge_branch(void)
  * 
  *       packed commit one
  */
+
+/*
+ * twowaymerge.git $ git log --graph --all
+ * *   commit 9b219343610c88a1187c996d0dc58330b55cee28
+ * |\  Merge: c37a783 2224e19
+ * | | Author: Scott J. Goldman <scottjg@github.com>
+ * | | Date:   Tue Nov 27 20:31:04 2012 -0800
+ * | | 
+ * | |     Merge branch 'first-branch' into second-branch
+ * | |   
+ * | * commit 2224e191514cb4bd8c566d80dac22dfcb1e9bb83
+ * | | Author: Scott J. Goldman <scottjg@github.com>
+ * | | Date:   Tue Nov 27 20:28:51 2012 -0800
+ * | | 
+ * | |     j
+ * | |   
+ * | * commit a41a49f8f5cd9b6cb14a076bf8394881ed0b4d19
+ * | | Author: Scott J. Goldman <scottjg@github.com>
+ * | | Date:   Tue Nov 27 20:28:39 2012 -0800
+ * | | 
+ * | |     i
+ * | |   
+ * | * commit 82bf9a1a10a4b25c1f14c9607b60970705e92545
+ * | | Author: Scott J. Goldman <scottjg@github.com>
+ * | | Date:   Tue Nov 27 20:28:28 2012 -0800
+ * | | 
+ * | |     h
+ * | |   
+ * * | commit c37a783c20d92ac92362a78a32860f7eebf938ef
+ * | | Author: Scott J. Goldman <scottjg@github.com>
+ * | | Date:   Tue Nov 27 20:30:57 2012 -0800
+ * | | 
+ * | |     n
+ * | |   
+ * * | commit 8b82fb1794cb1c8c7f172ec730a4c2db0ae3e650
+ * | | Author: Scott J. Goldman <scottjg@github.com>
+ * | | Date:   Tue Nov 27 20:30:43 2012 -0800
+ * | | 
+ * | |     m
+ * | |   
+ * * | commit 6ab5d28acbf3c3bdff276f7ccfdf29c1520e542f
+ * | | Author: Scott J. Goldman <scottjg@github.com>
+ * | | Date:   Tue Nov 27 20:30:38 2012 -0800
+ * | | 
+ * | |     l
+ * | |   
+ * * | commit 7b8c336c45fc6895c1c60827260fe5d798e5d247
+ * | | Author: Scott J. Goldman <scottjg@github.com>
+ * | | Date:   Tue Nov 27 20:30:24 2012 -0800
+ * | | 
+ * | |     k
+ * | |     
+ * | | * commit 1c30b88f5f3ee66d78df6520a7de9e89b890818b
+ * | | | Author: Scott J. Goldman <scottjg@github.com>
+ * | | | Date:   Tue Nov 27 20:28:10 2012 -0800
+ * | | | 
+ * | | |     e
+ * | | |    
+ * | | * commit 42b7311aa626e712891940c1ec5d5cba201946a4
+ * | | | Author: Scott J. Goldman <scottjg@github.com>
+ * | | | Date:   Tue Nov 27 20:28:06 2012 -0800
+ * | | | 
+ * | | |     d
+ * | | |      
+ * | | *   commit a953a018c5b10b20c86e69fef55ebc8ad4c5a417
+ * | | |\  Merge: bd1732c cdf97fd
+ * | | |/  Author: Scott J. Goldman <scottjg@github.com>
+ * | |/|   Date:   Tue Nov 27 20:26:43 2012 -0800
+ * | | |   
+ * | | |       Merge branch 'first-branch'
+ * | | |    
+ * | * | commit cdf97fd3bb48eb3827638bb33d208f5fd32d0aa6
+ * | | | Author: Scott J. Goldman <scottjg@github.com>
+ * | | | Date:   Tue Nov 27 20:24:46 2012 -0800
+ * | | | 
+ * | | |     g
+ * | | |    
+ * | * | commit ef0488f0b722f0be8bcb90a7730ac7efafd1d694
+ * | | | Author: Scott J. Goldman <scottjg@github.com>
+ * | | | Date:   Tue Nov 27 20:24:39 2012 -0800
+ * | | | 
+ * | | |     f
+ * | | |    
+ * | | * commit bd1732c43c68d712ad09e1d872b9be6d4b9efdc4
+ * | |/  Author: Scott J. Goldman <scottjg@github.com>
+ * | |   Date:   Tue Nov 27 17:43:58 2012 -0800
+ * | |   
+ * | |       c
+ * | |   
+ * | * commit 0c8a3f1f3d5f421cf83048c7c73ee3b55a5e0f29
+ * |/  Author: Scott J. Goldman <scottjg@github.com>
+ * |   Date:   Tue Nov 27 17:43:48 2012 -0800
+ * |   
+ * |       b
+ * |  
+ * * commit 1f4c0311a24b63f6fc209a59a1e404942d4a5006
+ *   Author: Scott J. Goldman <scottjg@github.com>
+ *   Date:   Tue Nov 27 17:43:41 2012 -0800
+ *
+ *       a
+ */