Commit 4dcd87801972e1b880afa9cd0998842bae7af5b5

Russell Belfer 2013-04-19T17:17:44

Move refdb_backend to include/git2/sys This moves most of the refdb stuff over to the include/git2/sys directory, with some minor shifts in function organization. While I was making the necessary updates, I also removed the trailing whitespace in a few files that I modified just because I was there and it was bugging me.

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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
diff --git a/include/git2/refdb.h b/include/git2/refdb.h
index 0e3ec5e..9278b11 100644
--- a/include/git2/refdb.h
+++ b/include/git2/refdb.h
@@ -81,20 +81,6 @@ GIT_EXTERN(int) git_refdb_compress(git_refdb *refdb);
  */
 GIT_EXTERN(void) git_refdb_free(git_refdb *refdb);
 
-/**
- * Sets the custom backend to an existing reference DB
- *
- * Read <refdb_backends.h> for more information.
- *
- * @param refdb database to add the backend to
- * @param backend pointer to a git_refdb_backend instance
- * @param priority Value for ordering the backends queue
- * @return 0 on success; error code otherwise
- */
-GIT_EXTERN(int) git_refdb_set_backend(
-	git_refdb *refdb,
-	git_refdb_backend *backend);
-
 /** @} */
 GIT_END_DECL
 
diff --git a/include/git2/refdb_backend.h b/include/git2/refdb_backend.h
deleted file mode 100644
index 20eb6a9..0000000
--- a/include/git2/refdb_backend.h
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * Copyright (C) the libgit2 contributors. All rights reserved.
- *
- * 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_refdb_backend_h__
-#define INCLUDE_git_refdb_backend_h__
-
-#include "common.h"
-#include "types.h"
-#include "oid.h"
-
-/**
- * @file git2/refdb_backend.h
- * @brief Git custom refs backend functions
- * @defgroup git_refdb_backend Git custom refs backend API
- * @ingroup Git
- * @{
- */
-GIT_BEGIN_DECL
-
-/** An instance for a custom backend */
-struct git_refdb_backend {
-    unsigned int version;
-
-	/**
-	 * Queries the refdb backend to determine if the given ref_name
-	 * exists.  A refdb implementation must provide this function.
-	 */
-	int (*exists)(
-		int *exists,
-		struct git_refdb_backend *backend,
-		const char *ref_name);
-
-	/**
-	 * Queries the refdb backend for a given reference.  A refdb
-	 * implementation must provide this function.
-	 */
-	int (*lookup)(
-		git_reference **out,
-		struct git_refdb_backend *backend,
-		const char *ref_name);
-
-	/**
-	 * Enumerates each reference in the refdb.  A refdb implementation must
-	 * provide this function.
-	 */
-	int (*foreach)(
-		struct git_refdb_backend *backend,
-		unsigned int list_flags,
-		git_reference_foreach_cb callback,
-		void *payload);
-
-	/**
-	 * Enumerates each reference in the refdb that matches the given
-	 * glob string.  A refdb implementation may provide this function;
-	 * if it is not provided, foreach will be used and the results filtered
-	 * against the glob.
-	 */
-	int (*foreach_glob)(
-		struct git_refdb_backend *backend,
-		const char *glob,
-		unsigned int list_flags,
-		git_reference_foreach_cb callback,
-		void *payload);
-
-	/**
-	 * Writes the given reference to the refdb.  A refdb implementation
-	 * must provide this function.
-	 */
-	int (*write)(struct git_refdb_backend *backend, const git_reference *ref);
-
-	/**
-	 * Deletes the given reference from the refdb.  A refdb implementation
-	 * must provide this function.
-	 */
-	int (*delete)(struct git_refdb_backend *backend, const git_reference *ref);
-
-	/**
-	 * Suggests that the given refdb compress or optimize its references.
-	 * This mechanism is implementation specific.  (For on-disk reference
-	 * databases, this may pack all loose references.)    A refdb
-	 * implementation may provide this function; if it is not provided,
-	 * nothing will be done.
-	 */
-	int (*compress)(struct git_refdb_backend *backend);
-
-	/**
-	 * Frees any resources held by the refdb.  A refdb implementation may
-	 * provide this function; if it is not provided, nothing will be done.
-	 */
-	void (*free)(struct git_refdb_backend *backend);
-};
-
-#define GIT_ODB_BACKEND_VERSION 1
-#define GIT_ODB_BACKEND_INIT {GIT_ODB_BACKEND_VERSION}
-
-/**
- * Constructors for default refdb backends.
- */
-GIT_EXTERN(int) git_refdb_backend_fs(
-	struct git_refdb_backend **backend_out,
-	git_repository *repo);
-
-GIT_END_DECL
-
-#endif
diff --git a/include/git2/sys/refdb_backend.h b/include/git2/sys/refdb_backend.h
new file mode 100644
index 0000000..dcdf7bc
--- /dev/null
+++ b/include/git2/sys/refdb_backend.h
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * 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_refdb_backend_h__
+#define INCLUDE_git_refdb_backend_h__
+
+#include "git2/common.h"
+#include "git2/types.h"
+#include "git2/oid.h"
+
+/**
+ * @file git2/refdb_backend.h
+ * @brief Git custom refs backend functions
+ * @defgroup git_refdb_backend Git custom refs backend API
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+/** An instance for a custom backend */
+struct git_refdb_backend {
+    unsigned int version;
+
+	/**
+	 * Queries the refdb backend to determine if the given ref_name
+	 * exists.  A refdb implementation must provide this function.
+	 */
+	int (*exists)(
+		int *exists,
+		struct git_refdb_backend *backend,
+		const char *ref_name);
+
+	/**
+	 * Queries the refdb backend for a given reference.  A refdb
+	 * implementation must provide this function.
+	 */
+	int (*lookup)(
+		git_reference **out,
+		struct git_refdb_backend *backend,
+		const char *ref_name);
+
+	/**
+	 * Enumerates each reference in the refdb.  A refdb implementation must
+	 * provide this function.
+	 */
+	int (*foreach)(
+		struct git_refdb_backend *backend,
+		unsigned int list_flags,
+		git_reference_foreach_cb callback,
+		void *payload);
+
+	/**
+	 * Enumerates each reference in the refdb that matches the given
+	 * glob string.  A refdb implementation may provide this function;
+	 * if it is not provided, foreach will be used and the results filtered
+	 * against the glob.
+	 */
+	int (*foreach_glob)(
+		struct git_refdb_backend *backend,
+		const char *glob,
+		unsigned int list_flags,
+		git_reference_foreach_cb callback,
+		void *payload);
+
+	/**
+	 * Writes the given reference to the refdb.  A refdb implementation
+	 * must provide this function.
+	 */
+	int (*write)(struct git_refdb_backend *backend, const git_reference *ref);
+
+	/**
+	 * Deletes the given reference from the refdb.  A refdb implementation
+	 * must provide this function.
+	 */
+	int (*delete)(struct git_refdb_backend *backend, const git_reference *ref);
+
+	/**
+	 * Suggests that the given refdb compress or optimize its references.
+	 * This mechanism is implementation specific.  (For on-disk reference
+	 * databases, this may pack all loose references.)    A refdb
+	 * implementation may provide this function; if it is not provided,
+	 * nothing will be done.
+	 */
+	int (*compress)(struct git_refdb_backend *backend);
+
+	/**
+	 * Frees any resources held by the refdb.  A refdb implementation may
+	 * provide this function; if it is not provided, nothing will be done.
+	 */
+	void (*free)(struct git_refdb_backend *backend);
+};
+
+#define GIT_ODB_BACKEND_VERSION 1
+#define GIT_ODB_BACKEND_INIT {GIT_ODB_BACKEND_VERSION}
+
+/**
+ * Constructors for default refdb backends.
+ */
+GIT_EXTERN(int) git_refdb_backend_fs(
+	struct git_refdb_backend **backend_out,
+	git_repository *repo);
+
+/**
+ * Sets the custom backend to an existing reference DB
+ *
+ * @param refdb database to add the backend to
+ * @param backend pointer to a git_refdb_backend instance
+ * @param priority Value for ordering the backends queue
+ * @return 0 on success; error code otherwise
+ */
+GIT_EXTERN(int) git_refdb_set_backend(
+	git_refdb *refdb,
+	git_refdb_backend *backend);
+
+GIT_END_DECL
+
+#endif
diff --git a/src/refdb.c b/src/refdb.c
index 2a0fd70..0675be6 100644
--- a/src/refdb.c
+++ b/src/refdb.c
@@ -7,15 +7,16 @@
 
 #include "common.h"
 #include "posix.h"
+
 #include "git2/object.h"
 #include "git2/refs.h"
 #include "git2/refdb.h"
+#include "git2/sys/refdb_backend.h"
+
 #include "hash.h"
 #include "refdb.h"
 #include "refs.h"
 
-#include "git2/refdb_backend.h"
-
 int git_refdb_new(git_refdb **out, git_repository *repo)
 {
 	git_refdb *db;
@@ -74,11 +75,11 @@ int git_refdb_set_backend(git_refdb *db, git_refdb_backend *backend)
 int git_refdb_compress(git_refdb *db)
 {
 	assert(db);
-	
+
 	if (db->backend->compress) {
 		return db->backend->compress(db->backend);
 	}
-	
+
 	return 0;
 }
 
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 56b2b6a..4d5d600 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -18,7 +18,7 @@
 #include <git2/tag.h>
 #include <git2/object.h>
 #include <git2/refdb.h>
-#include <git2/refdb_backend.h>
+#include <git2/sys/refdb_backend.h>
 
 GIT__USE_STRMAP;
 
@@ -61,7 +61,7 @@ static int reference_read(
 	/* Determine the full path of the file */
 	if (git_buf_joinpath(&path, repo_path, ref_name) < 0)
 		return -1;
-	
+
 	result = git_futils_readbuffer_updated(file_content, path.ptr, mtime, NULL, updated);
 	git_buf_free(&path);
 
@@ -174,7 +174,7 @@ static int packed_load(refdb_fs_backend *backend)
 		ref_cache->packfile = git_strmap_alloc();
 		GITERR_CHECK_ALLOC(ref_cache->packfile);
 	}
-	
+
 	result = reference_read(&packfile, &ref_cache->packfile_time,
 		backend->path, GIT_PACKEDREFS_FILE, &updated);
 
@@ -192,7 +192,7 @@ static int packed_load(refdb_fs_backend *backend)
 
 	if (result < 0)
 		return -1;
-	
+
 	if (!updated)
 		return 0;
 
@@ -433,7 +433,7 @@ static int loose_lookup(
 	} else {
 		if ((error = loose_parse_oid(&oid, &ref_file)) < 0)
 			goto done;
-		
+
 		*out = git_reference__alloc(ref_name, &oid, NULL);
 	}
 
@@ -455,19 +455,19 @@ static int packed_map_entry(
 
 	if (packed_load(backend) < 0)
 		return -1;
-	
+
 	/* Look up on the packfile */
 	packfile_refs = backend->refcache.packfile;
 
 	*pos = git_strmap_lookup_index(packfile_refs, ref_name);
-	
+
 	if (!git_strmap_valid_index(packfile_refs, *pos)) {
 		giterr_set(GITERR_REFERENCE, "Reference '%s' not found", ref_name);
 		return GIT_ENOTFOUND;
 	}
 
 	*entry = git_strmap_value_at(packfile_refs, *pos);
-	
+
 	return 0;
 }
 
@@ -479,14 +479,14 @@ static int packed_lookup(
 	struct packref *entry;
 	khiter_t pos;
 	int error = 0;
-	
+
 	if ((error = packed_map_entry(&entry, &pos, backend, ref_name)) < 0)
 		return error;
 
 	if ((*out = git_reference__alloc(ref_name,
 		&entry->oid, &entry->peel)) == NULL)
 		return -1;
-	
+
 	return 0;
 }
 
@@ -582,7 +582,7 @@ static int refdb_fs_backend__foreach(
 	git_buf refs_path = GIT_BUF_INIT;
 	const char *ref_name;
 	void *ref = NULL;
-	
+
 	GIT_UNUSED(ref);
 
 	assert(_backend);
@@ -590,7 +590,7 @@ static int refdb_fs_backend__foreach(
 
 	if (packed_load(backend) < 0)
 		return -1;
-	
+
 	/* list all the packed references first */
 	if (list_type & GIT_REF_OID) {
 		git_strmap_foreach(backend->refcache.packfile, ref_name, ref, {
@@ -924,7 +924,7 @@ static int refdb_fs_backend__delete(
 	repo = backend->repo;
 
 	/* If a loose reference exists, remove it from the filesystem */
-	
+
 	if (git_buf_joinpath(&loose_path, repo->path_repository, ref->name) < 0)
 		return -1;
 
@@ -932,7 +932,7 @@ static int refdb_fs_backend__delete(
 		error = p_unlink(loose_path.ptr);
 		loose_deleted = 1;
 	}
-	
+
 	git_buf_free(&loose_path);
 
 	if (error != 0)
@@ -946,7 +946,7 @@ static int refdb_fs_backend__delete(
 
 		error = packed_write(backend);
 	}
-	
+
 	if (pack_error == GIT_ENOTFOUND)
 		error = loose_deleted ? 0 : GIT_ENOTFOUND;
 	else
diff --git a/src/refs.c b/src/refs.c
index 5b5812a..d9291e5 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -19,7 +19,6 @@
 #include <git2/branch.h>
 #include <git2/refs.h>
 #include <git2/refdb.h>
-#include <git2/refdb_backend.h>
 
 GIT__USE_STRMAP;
 
@@ -255,10 +254,10 @@ int git_reference_lookup_resolved(
 		max_nesting = MAX_NESTING_LEVEL;
 	else if (max_nesting < 0)
 		max_nesting = DEFAULT_NESTING_LEVEL;
-	
+
 	strncpy(scan_name, name, GIT_REFNAME_MAX);
 	scan_type = GIT_REF_SYMBOLIC;
-	
+
 	if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
 		return -1;
 
@@ -276,7 +275,7 @@ int git_reference_lookup_resolved(
 
 		if ((error = git_refdb_lookup(&ref, refdb, scan_name)) < 0)
 			return error;
-		
+
 		scan_type = ref->type;
 	}
 
@@ -354,7 +353,7 @@ static int reference__create(
 	git_refdb *refdb;
 	git_reference *ref = NULL;
 	int error = 0;
-	
+
 	if (ref_out)
 		*ref_out = NULL;
 
@@ -369,7 +368,7 @@ static int reference__create(
 	} else {
 		ref = git_reference__alloc_symbolic(name, symbolic);
 	}
-	
+
 	GITERR_CHECK_ALLOC(ref);
 	ref->db = refdb;
 
@@ -377,7 +376,7 @@ static int reference__create(
 		git_reference_free(ref);
 		return error;
 	}
-	
+
 	if (ref_out == NULL)
 		git_reference_free(ref);
 	else
@@ -397,17 +396,17 @@ int git_reference_create(
 	int error = 0;
 
 	assert(repo && name && oid);
-	
+
 	/* Sanity check the reference being created - target must exist. */
 	if ((error = git_repository_odb__weakptr(&odb, repo)) < 0)
 		return error;
-	
+
 	if (!git_odb_exists(odb, oid)) {
 		giterr_set(GITERR_REFERENCE,
 			"Target OID for the reference doesn't exist on the repository");
 		return -1;
 	}
-	
+
 	return reference__create(ref_out, repo, name, oid, NULL, force);
 }
 
@@ -422,7 +421,7 @@ int git_reference_symbolic_create(
 	int error = 0;
 
 	assert(repo && name && target);
-	
+
 	if ((error = git_reference__normalize_name_lax(
 		normalized, sizeof(normalized), target)) < 0)
 		return error;
@@ -436,7 +435,7 @@ int git_reference_set_target(
 	const git_oid *id)
 {
 	assert(out && ref && id);
-	
+
 	if (ref->type != GIT_REF_OID) {
 		giterr_set(GITERR_REFERENCE, "Cannot set OID on symbolic reference");
 		return -1;
@@ -451,13 +450,13 @@ int git_reference_symbolic_set_target(
 	const char *target)
 {
 	assert(out && ref && target);
-	
+
 	if (ref->type != GIT_REF_SYMBOLIC) {
 		giterr_set(GITERR_REFERENCE,
 			"Cannot set symbolic target on a direct reference");
 		return -1;
 	}
-	
+
 	return git_reference_symbolic_create(out, ref->db->repo, ref->name, target, 1);
 }
 
@@ -473,7 +472,7 @@ int git_reference_rename(
 	git_reference *result = NULL;
 	int error = 0;
 	int reference_has_log;
-	
+
 	*out = NULL;
 
 	normalization_flags = ref->type == GIT_REF_SYMBOLIC ?
@@ -488,7 +487,7 @@ int git_reference_rename(
 	 * Create the new reference.
 	 */
 	if (ref->type == GIT_REF_OID) {
-		result = git_reference__alloc(new_name, &ref->target.oid, &ref->peel); 
+		result = git_reference__alloc(new_name, &ref->target.oid, &ref->peel);
 	} else if (ref->type == GIT_REF_SYMBOLIC) {
 		result = git_reference__alloc_symbolic(new_name, ref->target.symbolic);
 	} else {
@@ -509,11 +508,11 @@ int git_reference_rename(
 	/* Now delete the old ref and save the new one. */
 	if ((error = git_refdb_delete(ref->db, ref)) < 0)
 		goto on_error;
-	
+
 	/* Save the new reference. */
 	if ((error = git_refdb_write(ref->db, result)) < 0)
 		goto rollback;
-	
+
 	/* Update HEAD it was poiting to the reference being renamed. */
 	if (should_head_be_updated && (error = git_repository_set_head(ref->db->repo, new_name)) < 0) {
 		giterr_set(GITERR_REFERENCE, "Failed to update HEAD after renaming reference");
@@ -547,7 +546,7 @@ int git_reference_resolve(git_reference **ref_out, const git_reference *ref)
 	switch (git_reference_type(ref)) {
 	case GIT_REF_OID:
 		return git_reference_lookup(ref_out, ref->db->repo, ref->name);
-	
+
 	case GIT_REF_SYMBOLIC:
 		return git_reference_lookup_resolved(ref_out, ref->db->repo, ref->target.symbolic, -1);
 
@@ -846,7 +845,7 @@ static int reference__update_terminal(
 
 	if (nesting > MAX_NESTING_LEVEL)
 		return GIT_ENOTFOUND;
-	
+
 	error = git_reference_lookup(&ref, repo, ref_name);
 
 	/* If we haven't found the reference at all, create a new reference. */
@@ -854,10 +853,10 @@ static int reference__update_terminal(
 		giterr_clear();
 		return git_reference_create(NULL, repo, ref_name, oid, 0);
 	}
-	
+
 	if (error < 0)
 		return error;
-	
+
 	/* If the ref is a symbolic reference, follow its target. */
 	if (git_reference_type(ref) == GIT_REF_SYMBOLIC) {
 		error = reference__update_terminal(repo, git_reference_symbolic_target(ref), oid,
@@ -867,7 +866,7 @@ static int reference__update_terminal(
 		git_reference_free(ref);
 		error = git_reference_create(NULL, repo, ref_name, oid, 1);
 	}
-	
+
 	return error;
 }
 
diff --git a/src/util.h b/src/util.h
index c0f2719..af3ef0b 100644
--- a/src/util.h
+++ b/src/util.h
@@ -7,6 +7,8 @@
 #ifndef INCLUDE_util_h__
 #define INCLUDE_util_h__
 
+#include "common.h"
+
 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
 #define bitsizeof(x) (CHAR_BIT * sizeof(x))
 #define MSB(x, bits) ((x) & (~0ULL << (bitsizeof(x) - (bits))))
diff --git a/tests-clar/refdb/inmemory.c b/tests-clar/refdb/inmemory.c
index ea76172..2c76d41 100644
--- a/tests-clar/refdb/inmemory.c
+++ b/tests-clar/refdb/inmemory.c
@@ -1,12 +1,15 @@
 #include "clar_libgit2.h"
-#include "refdb.h"
-#include "repository.h"
+
+#include "buffer.h"
+#include "posix.h"
+#include "path.h"
+#include "refs.h"
+
 #include "testdb.h"
 
 #define TEST_REPO_PATH "testrepo"
 
 static git_repository *repo;
-static git_refdb_backend *refdb_backend;
 
 int unlink_ref(void *payload, git_buf *file)
 {
@@ -26,7 +29,7 @@ int ref_file_foreach(git_repository *repo, int (* cb)(void *payload, git_buf *fi
 	const char *repo_path;
 	git_buf repo_refs_dir = GIT_BUF_INIT;
 	int error = 0;
-	
+
 	repo_path = git_repository_path(repo);
 
 	git_buf_joinpath(&repo_refs_dir, repo_path, "HEAD");
@@ -38,7 +41,7 @@ int ref_file_foreach(git_repository *repo, int (* cb)(void *payload, git_buf *fi
 	git_buf_joinpath(&repo_refs_dir, git_buf_cstr(&repo_refs_dir), "heads");
 	if (git_path_direach(&repo_refs_dir, cb, NULL) != 0)
 		return -1;
-	
+
 	git_buf_joinpath(&repo_refs_dir, repo_path, "packed-refs");
 	if (git_path_exists(git_buf_cstr(&repo_refs_dir)) &&
 		cb(NULL, &repo_refs_dir) < 0)
@@ -51,16 +54,17 @@ int ref_file_foreach(git_repository *repo, int (* cb)(void *payload, git_buf *fi
 
 void test_refdb_inmemory__initialize(void)
 {
-	git_refdb *refdb;
-
 	git_buf repo_refs_dir = GIT_BUF_INIT;
+	git_refdb *refdb;
+	git_refdb_backend *refdb_backend;
 
 	repo = cl_git_sandbox_init(TEST_REPO_PATH);
 
 	cl_git_pass(git_repository_refdb(&refdb, repo));
 	cl_git_pass(refdb_backend_test(&refdb_backend, repo));
 	cl_git_pass(git_refdb_set_backend(refdb, refdb_backend));
-	
+	git_refdb_free(refdb);
+
 	ref_file_foreach(repo, unlink_ref);
 
 	git_buf_free(&repo_refs_dir);
@@ -76,10 +80,10 @@ void test_refdb_inmemory__doesnt_write_ref_file(void)
 {
 	git_reference *ref;
 	git_oid oid;
-	
+
 	cl_git_pass(git_oid_fromstr(&oid, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
 	cl_git_pass(git_reference_create(&ref, repo, GIT_REFS_HEADS_DIR "test1", &oid, 0));
-	
+
 	ref_file_foreach(repo, empty);
 
 	git_reference_free(ref);
@@ -89,10 +93,10 @@ void test_refdb_inmemory__read(void)
 {
 	git_reference *write1, *write2, *write3, *read1, *read2, *read3;
 	git_oid oid1, oid2, oid3;
-	
+
 	cl_git_pass(git_oid_fromstr(&oid1, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
 	cl_git_pass(git_reference_create(&write1, repo, GIT_REFS_HEADS_DIR "test1", &oid1, 0));
-	
+
 	cl_git_pass(git_oid_fromstr(&oid2, "e90810b8df3e80c413d903f631643c716887138d"));
 	cl_git_pass(git_reference_create(&write2, repo, GIT_REFS_HEADS_DIR "test2", &oid2, 0));
 
@@ -139,7 +143,7 @@ int foreach_test(const char *ref_name, void *payload)
 	cl_assert(git_oid_cmp(&expected, git_reference_target(ref)) == 0);
 
 	++(*i);
-	
+
 	git_reference_free(ref);
 
 	return 0;
@@ -150,19 +154,19 @@ void test_refdb_inmemory__foreach(void)
 	git_reference *write1, *write2, *write3;
 	git_oid oid1, oid2, oid3;
 	size_t i = 0;
-	
+
 	cl_git_pass(git_oid_fromstr(&oid1, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
 	cl_git_pass(git_reference_create(&write1, repo, GIT_REFS_HEADS_DIR "test1", &oid1, 0));
-	
+
 	cl_git_pass(git_oid_fromstr(&oid2, "e90810b8df3e80c413d903f631643c716887138d"));
 	cl_git_pass(git_reference_create(&write2, repo, GIT_REFS_HEADS_DIR "test2", &oid2, 0));
-	
+
 	cl_git_pass(git_oid_fromstr(&oid3, "763d71aadf09a7951596c9746c024e7eece7c7af"));
 	cl_git_pass(git_reference_create(&write3, repo, GIT_REFS_HEADS_DIR "test3", &oid3, 0));
-	
+
 	cl_git_pass(git_reference_foreach(repo, GIT_REF_LISTALL, foreach_test, &i));
 	cl_assert_equal_i(3, (int)i);
-	
+
 	git_reference_free(write1);
 	git_reference_free(write2);
 	git_reference_free(write3);
@@ -175,14 +179,14 @@ int delete_test(const char *ref_name, void *payload)
 	size_t *i = payload;
 
 	cl_git_pass(git_reference_lookup(&ref, repo, ref_name));
-	
-	cl_git_pass(git_oid_fromstr(&expected, "e90810b8df3e80c413d903f631643c716887138d"));	
+
+	cl_git_pass(git_oid_fromstr(&expected, "e90810b8df3e80c413d903f631643c716887138d"));
 	cl_assert(git_oid_cmp(&expected, git_reference_target(ref)) == 0);
-	
+
 	++(*i);
-	
+
 	git_reference_free(ref);
-	
+
 	return 0;
 }
 
@@ -191,22 +195,22 @@ void test_refdb_inmemory__delete(void)
 	git_reference *write1, *write2, *write3;
 	git_oid oid1, oid2, oid3;
 	size_t i = 0;
-	
+
 	cl_git_pass(git_oid_fromstr(&oid1, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
 	cl_git_pass(git_reference_create(&write1, repo, GIT_REFS_HEADS_DIR "test1", &oid1, 0));
-	
+
 	cl_git_pass(git_oid_fromstr(&oid2, "e90810b8df3e80c413d903f631643c716887138d"));
 	cl_git_pass(git_reference_create(&write2, repo, GIT_REFS_HEADS_DIR "test2", &oid2, 0));
-	
+
 	cl_git_pass(git_oid_fromstr(&oid3, "763d71aadf09a7951596c9746c024e7eece7c7af"));
 	cl_git_pass(git_reference_create(&write3, repo, GIT_REFS_HEADS_DIR "test3", &oid3, 0));
-	
+
 	git_reference_delete(write1);
 	git_reference_free(write1);
-	
+
 	git_reference_delete(write3);
 	git_reference_free(write3);
-	
+
 	cl_git_pass(git_reference_foreach(repo, GIT_REF_LISTALL, delete_test, &i));
 	cl_assert_equal_i(1, (int)i);
 
diff --git a/tests-clar/refdb/testdb.c b/tests-clar/refdb/testdb.c
index 4bca398..627254e 100644
--- a/tests-clar/refdb/testdb.c
+++ b/tests-clar/refdb/testdb.c
@@ -1,14 +1,10 @@
-#include "common.h"
 #include "vector.h"
 #include "util.h"
-#include <git2/refdb.h>
-#include <git2/refdb_backend.h>
-#include <git2/errors.h>
-#include <git2/repository.h>
+#include "testdb.h"
 
 typedef struct refdb_test_backend {
 	git_refdb_backend parent;
-	
+
 	git_repository *repo;
 	git_vector refs;
 } refdb_test_backend;
@@ -16,7 +12,7 @@ typedef struct refdb_test_backend {
 typedef struct refdb_test_entry {
 	char *name;
 	git_ref_t type;
-	
+
 	union {
 		git_oid oid;
 		char *symbolic;
@@ -37,19 +33,19 @@ static int refdb_test_backend__exists(
 	refdb_test_backend *backend;
 	refdb_test_entry *entry;
 	size_t i;
-	
+
 	assert(_backend);
 	backend = (refdb_test_backend *)_backend;
-	
+
 	*exists = 0;
-	
+
 	git_vector_foreach(&backend->refs, i, entry) {
 		if (strcmp(entry->name, ref_name) == 0) {
 			*exists = 1;
 			break;
 		}
 	}
-	
+
 	return 0;
 }
 
@@ -59,18 +55,18 @@ static int refdb_test_backend__write(
 {
 	refdb_test_backend *backend;
 	refdb_test_entry *entry;
-	
+
 	assert(_backend);
 	backend = (refdb_test_backend *)_backend;
 
 	entry = git__calloc(1, sizeof(refdb_test_entry));
 	GITERR_CHECK_ALLOC(entry);
-	
+
 	entry->name = git__strdup(git_reference_name(ref));
 	GITERR_CHECK_ALLOC(entry->name);
-	
+
 	entry->type = git_reference_type(ref);
-	
+
 	if (entry->type == GIT_REF_OID)
 		git_oid_cpy(&entry->target.oid, git_reference_target(ref));
 	else {
@@ -79,7 +75,7 @@ static int refdb_test_backend__write(
 	}
 
 	git_vector_insert(&backend->refs, entry);
-	
+
 	return 0;
 }
 
@@ -94,7 +90,7 @@ static int refdb_test_backend__lookup(
 
 	assert(_backend);
 	backend = (refdb_test_backend *)_backend;
-	
+
 	git_vector_foreach(&backend->refs, i, entry) {
 		if (strcmp(entry->name, ref_name) == 0) {
 
@@ -108,7 +104,7 @@ static int refdb_test_backend__lookup(
 
 			if (*out == NULL)
 				return -1;
-			
+
 			return 0;
 		}
 	}
@@ -125,21 +121,21 @@ static int refdb_test_backend__foreach(
 	refdb_test_backend *backend;
 	refdb_test_entry *entry;
 	size_t i;
-	
+
 	assert(_backend);
 	backend = (refdb_test_backend *)_backend;
 
 	git_vector_foreach(&backend->refs, i, entry) {
 		if (entry->type == GIT_REF_OID && (list_flags & GIT_REF_OID) == 0)
 			continue;
-		
+
 		if (entry->type == GIT_REF_SYMBOLIC && (list_flags & GIT_REF_SYMBOLIC) == 0)
 			continue;
-		
+
 		if (callback(entry->name, payload) != 0)
 			return GIT_EUSER;
 	}
-	
+
 	return 0;
 }
 
@@ -147,7 +143,7 @@ static void refdb_test_entry_free(refdb_test_entry *entry)
 {
 	if (entry->type == GIT_REF_SYMBOLIC)
 		git__free(entry->target.symbolic);
-	
+
 	git__free(entry->name);
 	git__free(entry);
 }
@@ -178,14 +174,14 @@ static void refdb_test_backend__free(git_refdb_backend *_backend)
 	refdb_test_backend *backend;
 	refdb_test_entry *entry;
 	size_t i;
-	
+
 	assert(_backend);
 	backend = (refdb_test_backend *)_backend;
 
 	git_vector_foreach(&backend->refs, i, entry)
 		refdb_test_entry_free(entry);
 
-	git_vector_free(&backend->refs);	
+	git_vector_free(&backend->refs);
 	git__free(backend);
 }
 
@@ -197,7 +193,7 @@ int refdb_backend_test(
 
 	backend = git__calloc(1, sizeof(refdb_test_backend));
 	GITERR_CHECK_ALLOC(backend);
-	
+
 	git_vector_init(&backend->refs, 0, ref_name_cmp);
 
 	backend->repo = repo;
diff --git a/tests-clar/refdb/testdb.h b/tests-clar/refdb/testdb.h
index e38abd9..49d1cb1 100644
--- a/tests-clar/refdb/testdb.h
+++ b/tests-clar/refdb/testdb.h
@@ -1,3 +1,8 @@
+#include <git2/errors.h>
+#include <git2/repository.h>
+#include <git2/refdb.h>
+#include <git2/sys/refdb_backend.h>
+
 int refdb_backend_test(
 	git_refdb_backend **backend_out,
 	git_repository *repo);