Commit a46ec45746f965f2895098e058979225d92d66e5

schu 2011-08-10T16:19:42

refs: split internal and external references Currently libgit2 shares pointers to its internal reference cache with the user. This leads to several problems like invalidation of reference pointers when reordering the cache or manipulation of the cache from user side. Give each user its own git_reference instead of leaking the internal representation (struct reference). Add the following new API functions: * git_reference_free * git_reference_is_packed Signed-off-by: schu <schu-github@schulog.org>

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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
diff --git a/include/git2/refs.h b/include/git2/refs.h
index c319bfb..773ae44 100644
--- a/include/git2/refs.h
+++ b/include/git2/refs.h
@@ -23,8 +23,7 @@ GIT_BEGIN_DECL
 /**
  * Lookup a reference by its name in a repository.
  *
- * The generated reference is owned by the repository and
- * should not be freed by the user.
+ * The generated reference must be freed by the user.
  *
  * @param reference_out pointer to the looked-up reference
  * @param repo the repository to look up the reference
@@ -39,8 +38,7 @@ GIT_EXTERN(int) git_reference_lookup(git_reference **reference_out, git_reposito
  * The reference will be created in the repository and written
  * to the disk.
  *
- * This reference is owned by the repository and shall not
- * be free'd by the user.
+ * The generated reference must be freed by the user.
  *
  * If `force` is true and there already exists a reference
  * with the same name, it will be overwritten.
@@ -60,8 +58,7 @@ GIT_EXTERN(int) git_reference_create_symbolic(git_reference **ref_out, git_repos
  * The reference will be created in the repository and written
  * to the disk.
  *
- * This reference is owned by the repository and shall not
- * be free'd by the user.
+ * The generated reference must be freed by the user.
  *
  * If `force` is true and there already exists a reference
  * with the same name, it will be overwritten.
@@ -173,7 +170,9 @@ GIT_EXTERN(int) git_reference_set_oid(git_reference *ref, const git_oid *id);
  * The new name will be checked for validity and may be
  * modified into a normalized form.
  *
- * The refernece will be immediately renamed in-memory
+ * The given git_reference will be updated.
+ *
+ * The reference will be immediately renamed in-memory
  * and on disk.
  *
  */
@@ -200,9 +199,6 @@ GIT_EXTERN(int) git_reference_delete(git_reference *ref);
  * Once the `packed-refs` file has been written properly,
  * the loose references will be removed from disk.
  *
- * WARNING: calling this method may invalidate any existing
- * references previously loaded on the cache.
- *
  * @param repo Repository where the loose refs will be packed
  * @return GIT_SUCCESS or an error code
  */
@@ -253,6 +249,21 @@ GIT_EXTERN(int) git_reference_listall(git_strarray *array, git_repository *repo,
  */
 GIT_EXTERN(int) git_reference_foreach(git_repository *repo, unsigned int list_flags, int (*callback)(const char *, void *), void *payload);
 
+/**
+ * Check if a reference is packed
+ *
+ * @param ref git_reference
+ * @return 0 in case it's not packed; 1 otherwise
+ */
+GIT_EXTERN(int) git_reference_is_packed(git_reference *ref);
+
+/**
+ * Free the given reference
+ *
+ * @param ref git_reference
+ */
+GIT_EXTERN(void) git_reference_free(git_reference *ref);
+
 /** @} */
 GIT_END_DECL
 #endif
diff --git a/src/commit.c b/src/commit.c
index b9eb365..1010fdc 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -137,12 +137,13 @@ int git_commit_create(
 
 	if (error == GIT_SUCCESS && update_ref != NULL) {
 		git_reference *head;
+		git_reference *target;
 
 		error = git_reference_lookup(&head, repo, update_ref);
 		if (error < GIT_SUCCESS)
 			return git__rethrow(error, "Failed to create commit");
 
-		error = git_reference_resolve(&head, head);
+		error = git_reference_resolve(&target, head);
 		if (error < GIT_SUCCESS) {
 			if (error != GIT_ENOTFOUND)
 				return git__rethrow(error, "Failed to create commit");
@@ -156,7 +157,7 @@ int git_commit_create(
 			return git_reference_create_oid(&head, repo, git_reference_target(head), oid, 1);
 		}
 
-		error = git_reference_set_oid(head, oid);
+		error = git_reference_set_oid(target, oid);
 	}
 
 	if (error < GIT_SUCCESS)
diff --git a/src/refs.c b/src/refs.c
index 679d7bb..1c33f73 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -17,13 +17,20 @@
 #define MAX_NESTING_LEVEL 5
 
 typedef struct {
-	git_reference ref;
+	git_repository *owner;
+	char *name;
+	unsigned int type;
+	time_t mtime;
+} reference;
+
+typedef struct {
+	reference ref;
 	git_oid oid;
 	git_oid peel_target;
 } reference_oid;
 
 typedef struct {
-	git_reference ref;
+	reference ref;
 	char *target;
 } reference_symbolic;
 
@@ -40,16 +47,21 @@ static uint32_t reftable_hash(const void *key, int hash_id)
 	return git__hash(key, strlen((const char *)key), hash_seeds[hash_id]);
 }
 
-static void reference_free(git_reference *reference);
-static int reference_create(git_reference **ref_out, git_repository *repo, const char *name, git_rtype type);
+static void reference_free(reference *reference);
+static int reference_create(reference **ref_out, git_repository *repo, const char *name, git_rtype type);
 static int reference_read(git_fbuffer *file_content, time_t *mtime, const char *repo_path, const char *ref_name, int *updated);
 
+static int reference_set_target(reference *ref, const char *target);
+static int reference_set_oid(reference *ref, const git_oid *oid);
+
+static int reference_delete(reference *ref);
+
 /* loose refs */
-static int loose_parse_symbolic(git_reference *ref, git_fbuffer *file_content);
-static int loose_parse_oid(git_reference *ref, git_fbuffer *file_content);
-static int loose_lookup(git_reference **ref_out, git_repository *repo, const char *name, int skip_symbolic);
-static int loose_write(git_reference *ref);
-static int loose_update(git_reference *ref);
+static int loose_parse_symbolic(reference *ref, git_fbuffer *file_content);
+static int loose_parse_oid(reference *ref, git_fbuffer *file_content);
+static int loose_lookup(reference **ref_out, git_repository *repo, const char *name, int skip_symbolic);
+static int loose_write(reference *ref);
+static int loose_update(reference *ref);
 
 /* packed refs */
 static int packed_parse_peel(reference_oid *tag_ref, const char **buffer_out, const char *buffer_end);
@@ -64,15 +76,19 @@ static int packed_write(git_repository *repo);
 
 /* internal helpers */
 static int reference_available(git_repository *repo, const char *ref, const char *old_ref);
+static int reference_lookup(reference **out, git_repository *repo, const char *name);
 
 /* name normalization */
 static int check_valid_ref_char(char ch);
 static int normalize_name(char *buffer_out, size_t out_size, const char *name, int is_oid_ref);
 
-/*****************************************
- * Internal methods - Constructor/destructor
- *****************************************/
-static void reference_free(git_reference *reference)
+/* reference transition */
+static git_reference * reference_create_external(reference *ref);
+static reference * reference_get_internal(git_reference *ref);
+
+
+
+static void reference_free(reference *reference)
 {
 	if (reference == NULL)
 		return;
@@ -87,14 +103,14 @@ static void reference_free(git_reference *reference)
 }
 
 static int reference_create(
-	git_reference **ref_out,
+	reference **ref_out,
 	git_repository *repo,
 	const char *name,
 	git_rtype type)
 {
 	char normalized[GIT_REFNAME_MAX];
 	int error = GIT_SUCCESS, size;
-	git_reference *reference = NULL;
+	reference *reference = NULL;
 
 	assert(ref_out && repo && name);
 
@@ -145,13 +161,38 @@ static int reference_read(git_fbuffer *file_content, time_t *mtime, const char *
 	return git_futils_readbuffer_updated(file_content, path, mtime, updated);
 }
 
+static git_reference * reference_create_external(reference *ref)
+{
+	size_t size = sizeof(git_reference);
+	git_reference *out;
+
+	out = git__malloc(size);
+	if (out == NULL)
+		return NULL;
+
+	memset(out, 0x0, size);
+
+	out->owner = ref->owner;
+
+	out->name = git__strdup(ref->name);
+	if (out->name == NULL) {
+		git__free(out);
+		return NULL;
+	}
+
+	return out;
+}
+
+static reference * reference_get_internal(git_reference *ref)
+{
+	reference *out = NULL;
 
+	reference_lookup(&out, ref->owner, ref->name);
 
+	return out;
+}
 
-/*****************************************
- * Internal methods - Loose references
- *****************************************/
-static int loose_update(git_reference *ref)
+static int loose_update(reference *ref)
 {
 	int error, updated;
 	git_fbuffer ref_file = GIT_FBUFFER_INIT;
@@ -159,6 +200,7 @@ static int loose_update(git_reference *ref)
 	if (ref->type & GIT_REF_PACKED)
 		return packed_load(ref->owner);
 
+
 /*	error = reference_read(NULL, &ref_time, ref->owner->path_repository, ref->name);
 	if (error < GIT_SUCCESS)
 		goto cleanup;
@@ -181,7 +223,6 @@ static int loose_update(git_reference *ref)
 		error = git__throw(GIT_EOBJCORRUPTED,
 			"Invalid reference type (%d) for loose reference", ref->type);
 
-
 cleanup:
 	git_futils_freebuffer(&ref_file);
 	if (error != GIT_SUCCESS) {
@@ -192,7 +233,7 @@ cleanup:
 	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to update loose reference");
 }
 
-static int loose_parse_symbolic(git_reference *ref, git_fbuffer *file_content)
+static int loose_parse_symbolic(reference *ref, git_fbuffer *file_content)
 {
 	const unsigned int header_len = strlen(GIT_SYMREF);
 	const char *refname_start;
@@ -231,7 +272,7 @@ static int loose_parse_symbolic(git_reference *ref, git_fbuffer *file_content)
 	return GIT_SUCCESS;
 }
 
-static int loose_parse_oid(git_reference *ref, git_fbuffer *file_content)
+static int loose_parse_oid(reference *ref, git_fbuffer *file_content)
 {
 	int error;
 	reference_oid *ref_oid;
@@ -279,14 +320,14 @@ static git_rtype loose_guess_rtype(const char *full_path)
 }
 
 static int loose_lookup(
-		git_reference **ref_out,
+		reference **ref_out,
 		git_repository *repo,
 		const char *name,
 		int skip_symbolic)
 {
 	int error = GIT_SUCCESS;
 	git_fbuffer ref_file = GIT_FBUFFER_INIT;
-	git_reference *ref = NULL;
+	reference *ref = NULL;
 	time_t ref_time = 0;
 
 	*ref_out = NULL;
@@ -326,7 +367,7 @@ cleanup:
 	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to lookup loose reference");
 }
 
-static int loose_write(git_reference *ref)
+static int loose_write(reference *ref)
 {
 	git_filebuf file;
 	char ref_path[GIT_PATH_MAX];
@@ -370,15 +411,6 @@ unlock:
 	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to write loose reference");
 }
 
-
-
-
-
-
-/*****************************************
- * Internal methods - Packed references
- *****************************************/
-
 static int packed_parse_peel(
 		reference_oid *tag_ref,
 		const char **buffer_out,
@@ -458,7 +490,7 @@ static int packed_parse_oid(
 	if (refname[refname_len - 1] == '\r')
 		refname[refname_len - 1] = 0;
 
-	error = reference_create(&_ref, repo, refname, GIT_REF_OID);
+	error = reference_create((reference **)&_ref, repo, refname, GIT_REF_OID);
 	if (error < GIT_SUCCESS)
 		goto cleanup;
 
@@ -473,7 +505,7 @@ static int packed_parse_oid(
 	return GIT_SUCCESS;
 
 cleanup:
-	reference_free((git_reference *)ref);
+	reference_free((reference *)ref);
 	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to parse OID of packed reference");
 }
 
@@ -550,7 +582,7 @@ static int packed_load(git_repository *repo)
 
 		error = git_hashtable_insert(ref_cache->packfile, ref->ref.name, ref);
 		if (error < GIT_SUCCESS) {
-			reference_free((git_reference *)ref);
+			reference_free((reference *)ref);
 			goto cleanup;
 		}
 	}
@@ -566,8 +598,6 @@ cleanup:
 }
 
 
-
-
 struct dirent_list_data {
 	git_repository *repo;
 	size_t repo_path_len;
@@ -601,8 +631,8 @@ static int _dirent_loose_listall(void *_data, char *full_path)
 static int _dirent_loose_load(void *data, char *full_path)
 {
 	git_repository *repository = (git_repository *)data;
-	git_reference *reference;
 	void *old_ref = NULL;
+	reference *ref;
 	char *file_path;
 	int error;
 
@@ -610,17 +640,17 @@ static int _dirent_loose_load(void *data, char *full_path)
 		return git_futils_direach(full_path, GIT_PATH_MAX, _dirent_loose_load, repository);
 
 	file_path = full_path + strlen(repository->path_repository);
-	error = loose_lookup(&reference, repository, file_path, 1);
-	if (error == GIT_SUCCESS && reference != NULL) {
-		reference->type |= GIT_REF_PACKED;
+	error = loose_lookup(&ref, repository, file_path, 1);
+	if (error == GIT_SUCCESS && ref != NULL) {
+		ref->type |= GIT_REF_PACKED;
 
-		if (git_hashtable_insert2(repository->references.packfile, reference->name, reference, &old_ref) < GIT_SUCCESS) {
-			reference_free(reference);
+		if (git_hashtable_insert2(repository->references.packfile, ref->name, ref, &old_ref) < GIT_SUCCESS) {
+			reference_free(ref);
 			return GIT_ENOMEM;
 		}
 
 		if (old_ref != NULL)
-			reference_free((git_reference *)old_ref);
+			reference_free((reference *)old_ref);
 	}
 
 	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to load loose dirent");
@@ -644,7 +674,7 @@ static int packed_loadloose(git_repository *repository)
 	/* Remove any loose references from the cache */
 	{
 		const void *GIT_UNUSED(_unused);
-		git_reference *reference;
+		reference *reference;
 
 		GIT_HASHTABLE_FOREACH(repository->references.loose_cache, _unused, reference,
 			reference_free(reference);
@@ -767,16 +797,16 @@ static int packed_remove_loose(git_repository *repo, git_vector *packing_list)
 	unsigned int i;
 	char full_path[GIT_PATH_MAX];
 	int error = GIT_SUCCESS;
-	git_reference *reference;
+	reference *r;
 
 	for (i = 0; i < packing_list->length; ++i) {
-		git_reference *ref = git_vector_get(packing_list, i);
+		reference *ref = git_vector_get(packing_list, i);
 
 		/* Ensure the packed reference doesn't exist
 		 * in a (more up-to-date?) state as a loose reference
 		 */
-		reference = git_hashtable_lookup(ref->owner->references.loose_cache, ref->name);
-		if (reference != NULL)
+		r = git_hashtable_lookup(ref->owner->references.loose_cache, ref->name);
+		if (r != NULL)
 			continue;
 
 		git_path_join(full_path, repo->path_repository, ref->name);
@@ -801,8 +831,8 @@ static int packed_remove_loose(git_repository *repo, git_vector *packing_list)
 
 static int packed_sort(const void *a, const void *b)
 {
-	const git_reference *ref_a = (const git_reference *)a;
-	const git_reference *ref_b = (const git_reference *)b;
+	const reference *ref_a = (const reference *)a;
+	const reference *ref_b = (const reference *)b;
 
 	return strcmp(ref_a->name, ref_b->name);
 }
@@ -828,7 +858,7 @@ static int packed_write(git_repository *repo)
 
 	/* Load all the packfile into a vector */
 	{
-		git_reference *reference;
+		reference *reference;
 		const void *GIT_UNUSED(_unused);
 
 		GIT_HASHTABLE_FOREACH(repo->references.packfile, _unused, reference,
@@ -935,14 +965,7 @@ static int reference_available(git_repository *repo, const char *ref, const char
 	return error == GIT_SUCCESS ? GIT_SUCCESS : git__throw(GIT_EEXISTS, "Reference name `%s` conflicts with existing reference", ref);
 }
 
-/*****************************************
- * External Library API
- *****************************************/
-
-/**
- * Constructors
- */
-int git_reference_lookup(git_reference **ref_out, git_repository *repo, const char *name)
+int reference_lookup(reference **ref_out, git_repository *repo, const char *name)
 {
 	int error;
 	char normalized_name[GIT_REFNAME_MAX];
@@ -990,10 +1013,30 @@ int git_reference_lookup(git_reference **ref_out, git_repository *repo, const ch
 	return git__throw(GIT_ENOTFOUND, "Failed to lookup reference. Reference doesn't exist");
 }
 
+int git_reference_lookup(git_reference **ref_out, git_repository *repo, const char *name)
+{
+	int error;
+	reference *ref;
+
+	assert(ref_out && repo && name);
+
+	*ref_out = NULL;
+
+	error = reference_lookup(&ref, repo, name);
+	if (error < GIT_SUCCESS)
+		return error;
+
+	*ref_out = reference_create_external(ref);
+	if (*ref_out == NULL)
+		return GIT_ENOMEM;
+
+	return GIT_SUCCESS;
+}
+
 /**
  * Getters
  */
-git_rtype git_reference_type(git_reference *ref)
+static git_rtype ref_type(reference *ref)
 {
 	assert(ref);
 
@@ -1006,6 +1049,43 @@ git_rtype git_reference_type(git_reference *ref)
 	return GIT_REF_INVALID;
 }
 
+git_rtype git_reference_type(git_reference *ref_in)
+{
+	reference *ref;
+
+	assert(ref_in);
+
+	ref = reference_get_internal(ref_in);
+	if (ref == NULL)
+		return GIT_REF_INVALID;
+
+	return ref_type(ref);
+}
+
+int git_reference_is_packed(git_reference *ref_in)
+{
+	reference *ref;
+
+	assert(ref_in);
+
+	ref = reference_get_internal(ref_in);
+	if (ref == NULL)
+		return GIT_REF_INVALID;
+
+	return !!(ref->type & GIT_REF_PACKED);
+}
+
+void git_reference_free(git_reference *ref)
+{
+	if (ref == NULL)
+		return;
+
+	if (ref->name)
+		git__free(ref->name);
+
+	git__free(ref);
+}
+
 const char *git_reference_name(git_reference *ref)
 {
 	assert(ref);
@@ -1018,7 +1098,7 @@ git_repository *git_reference_owner(git_reference *ref)
 	return ref->owner;
 }
 
-const git_oid *git_reference_oid(git_reference *ref)
+static const git_oid *ref_oid(reference *ref)
 {
 	assert(ref);
 
@@ -1031,7 +1111,20 @@ const git_oid *git_reference_oid(git_reference *ref)
 	return &((reference_oid *)ref)->oid;
 }
 
-const char *git_reference_target(git_reference *ref)
+const git_oid *git_reference_oid(git_reference *ref_in)
+{
+	reference *ref;
+
+	assert(ref_in);
+
+	ref = reference_get_internal(ref_in);
+	if (ref == NULL)
+		return NULL;
+
+	return ref_oid(ref);
+}
+
+static const char *ref_target(reference *ref)
 {
 	assert(ref);
 
@@ -1044,14 +1137,27 @@ const char *git_reference_target(git_reference *ref)
 	return ((reference_symbolic *)ref)->target;
 }
 
-int git_reference_create_symbolic(git_reference **ref_out, git_repository *repo, const char *name, const char *target, int force)
+const char *git_reference_target(git_reference *ref_in)
+{
+	reference *ref;
+
+	assert(ref_in);
+
+	ref = reference_get_internal(ref_in);
+	if (ref == NULL)
+		return NULL;
+
+	return ref_target(ref);
+}
+
+static int reference_create_symbolic(reference **ref_out, git_repository *repo, const char *name, const char *target, int force)
 {
 	char normalized[GIT_REFNAME_MAX];
 	int error = GIT_SUCCESS, updated = 0;
-	git_reference *ref = NULL;
+	reference *ref = NULL;
 	void *old_ref = NULL;
 
-	if (git_reference_lookup(&ref, repo, name) == GIT_SUCCESS && !force)
+	if (reference_lookup(&ref, repo, name) == GIT_SUCCESS && !force)
 		return git__throw(GIT_EEXISTS, "Failed to create symbolic reference. Reference already exists");
 
 	/*
@@ -1076,7 +1182,7 @@ int git_reference_create_symbolic(git_reference **ref_out, git_repository *repo,
 		goto cleanup;
 
 	/* set the target; this will write the reference on disk */
-	error = git_reference_set_target(ref, normalized);
+	error = reference_set_target(ref, normalized);
 	if (error < GIT_SUCCESS)
 		goto cleanup;
 
@@ -1090,7 +1196,7 @@ int git_reference_create_symbolic(git_reference **ref_out, git_repository *repo,
 			goto cleanup;
 
 		if (old_ref != NULL)
-			reference_free((git_reference *)old_ref);
+			reference_free((reference *)old_ref);
 	}
 
 	*ref_out = ref;
@@ -1102,13 +1208,29 @@ cleanup:
 	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to create symbolic reference");
 }
 
-int git_reference_create_oid(git_reference **ref_out, git_repository *repo, const char *name, const git_oid *id, int force)
+int git_reference_create_symbolic(git_reference **ref_out, git_repository *repo, const char *name, const char *target, int force)
+{
+	int error;
+	reference *ref;
+
+	error = reference_create_symbolic(&ref, repo, name, target, force);
+	if (error < GIT_SUCCESS)
+		return error;
+
+	*ref_out = reference_create_external(ref);
+	if (*ref_out == NULL)
+		return GIT_ENOMEM;
+
+	return GIT_SUCCESS;
+}
+
+static int reference_create_oid(reference **ref_out, git_repository *repo, const char *name, const git_oid *id, int force)
 {
 	int error = GIT_SUCCESS, updated = 0;
-	git_reference *ref = NULL;
+	reference *ref = NULL;
 	void *old_ref = NULL;
 
-	if(git_reference_lookup(&ref, repo, name) == GIT_SUCCESS && !force)
+	if(reference_lookup(&ref, repo, name) == GIT_SUCCESS && !force)
 		return git__throw(GIT_EEXISTS, "Failed to create reference OID. Reference already exists");
 
 	if ((error = reference_available(repo, name, NULL)) < GIT_SUCCESS)
@@ -1131,7 +1253,7 @@ int git_reference_create_oid(git_reference **ref_out, git_repository *repo, cons
 	}
 
 	/* set the oid; this will write the reference on disk */
-	error = git_reference_set_oid(ref, id);
+	error = reference_set_oid(ref, id);
 	if (error < GIT_SUCCESS)
 		goto cleanup;
 
@@ -1141,7 +1263,7 @@ int git_reference_create_oid(git_reference **ref_out, git_repository *repo, cons
 			goto cleanup;
 
 		if (old_ref != NULL)
-			reference_free((git_reference *)old_ref);
+			reference_free((reference *)old_ref);
 	}
 
 	*ref_out = ref;
@@ -1153,9 +1275,21 @@ cleanup:
 	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to create reference OID");
 }
 
-/**
- * Setters
- */
+int git_reference_create_oid(git_reference **ref_out, git_repository *repo, const char *name, const git_oid *id, int force)
+{
+	int error;
+	reference *ref;
+
+	error = reference_create_oid(&ref, repo, name, id, force);
+	if (error < GIT_SUCCESS)
+		return error;
+
+	*ref_out = reference_create_external(ref);
+	if (*ref_out == NULL)
+		return GIT_ENOMEM;
+
+	return GIT_SUCCESS;
+}
 
 /*
  * Change the OID target of a reference.
@@ -1177,7 +1311,7 @@ cleanup:
  * 4. Write the original to the loose cache
  * 5. Replace the original with the copy (old reference) in the packfile cache
  */
-int git_reference_set_oid(git_reference *ref, const git_oid *id)
+int reference_set_oid(reference *ref, const git_oid *id)
 {
 	reference_oid *ref_oid;
 	reference_oid *ref_old = NULL;
@@ -1233,10 +1367,21 @@ int git_reference_set_oid(git_reference *ref, const git_oid *id)
 	return GIT_SUCCESS;
 
 cleanup:
-	reference_free((git_reference *)ref_old);
+	reference_free((reference *)ref_old);
 	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to set OID target of reference");
 }
 
+int git_reference_set_oid(git_reference *ref_in, const git_oid *target)
+{
+	reference *ref;
+
+	ref = reference_get_internal(ref_in);
+	if (ref == NULL)
+		return git__throw(GIT_ENOTFOUND, "Failed to rename reference. Reference `%s` doesn't exist", ref->name);
+
+	return reference_set_oid(ref, target);
+}
+
 /*
  * Change the target of a symbolic reference.
  *
@@ -1244,7 +1389,7 @@ cleanup:
  * a pack. We just change the target in memory
  * and overwrite the file on disk.
  */
-int git_reference_set_target(git_reference *ref, const char *target)
+int reference_set_target(reference *ref, const char *target)
 {
 	reference_symbolic *ref_sym;
 
@@ -1261,11 +1406,18 @@ int git_reference_set_target(git_reference *ref, const char *target)
 	return loose_write(ref);
 }
 
-/**
- * Other
- */
+int git_reference_set_target(git_reference *ref_in, const char *target)
+{
+	reference *ref;
+
+	ref = reference_get_internal(ref_in);
+	if (ref == NULL)
+		return git__throw(GIT_ENOTFOUND, "Failed to rename reference. Reference `%s` doesn't exist", ref->name);
 
-int git_reference_rename(git_reference *ref, const char *new_name, int force)
+	return reference_set_target(ref, target);
+}
+
+int git_reference_rename(git_reference *ref_in, const char *new_name, int force)
 {
 	int error;
 	char *old_name = NULL;
@@ -1276,9 +1428,13 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
 	const char *target_ref = NULL;
 	const char *head_target = NULL;
 	const git_oid *target_oid = NULL;
-	git_reference *new_ref = NULL, *head = NULL;
+	reference *ref = NULL, *new_ref = NULL, *head = NULL;
 
-	assert(ref);
+	assert(ref_in);
+
+	ref = reference_get_internal(ref_in);
+	if (ref == NULL)
+		return git__throw(GIT_ENOTFOUND, "Failed to rename reference. Reference `%s` doesn't exist", ref->name);
 
 	error = normalize_name(normalized, sizeof(normalized), new_name, ref->type & GIT_REF_OID);
 	if (error < GIT_SUCCESS)
@@ -1286,12 +1442,12 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
 
 	new_name = normalized;
 
-	error = git_reference_lookup(&new_ref, ref->owner, new_name);
+	error = reference_lookup(&new_ref, ref->owner, new_name);
 	if (error == GIT_SUCCESS) {
 		if (!force)
 			return git__throw(GIT_EEXISTS, "Failed to rename reference. Reference already exists");
 
-		error = git_reference_delete(new_ref);
+		error = reference_delete(new_ref);
 	}
 
 	if (error < GIT_SUCCESS) {
@@ -1314,10 +1470,10 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
 	old_name = git__strdup(ref->name);
 
 	if (ref->type & GIT_REF_SYMBOLIC) {
-		if ((target_ref = git_reference_target(ref)) == NULL)
+		if ((target_ref = ref_target(ref)) == NULL)
 			goto cleanup;
 	} else {
-		if ((target_oid = git_reference_oid(ref)) == NULL)
+		if ((target_oid = ref_oid(ref)) == NULL)
 			goto cleanup;
 	}
 
@@ -1375,22 +1531,18 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
 	 * Finally we can create the new reference.
 	 */
 	if (ref->type & GIT_REF_SYMBOLIC) {
-		if ((error = git_reference_create_symbolic(&new_ref, ref->owner, new_name, target_ref, 0)) < GIT_SUCCESS)
+		if ((error = reference_create_symbolic(&new_ref, ref->owner, new_name, target_ref, 0)) < GIT_SUCCESS)
 			goto rollback;
 	} else {
-		if ((error = git_reference_create_oid(&new_ref, ref->owner, new_name, target_oid, 0)) < GIT_SUCCESS)
+		if ((error = reference_create_oid(&new_ref, ref->owner, new_name, target_oid, 0)) < GIT_SUCCESS)
 			goto rollback;
 	}
 
-	git__free(ref->name);
-	ref->name = new_ref->name;
-
 	/*
-	 * No need in new_ref anymore. We created it to fix the change on disk.
-	 * TODO: Refactoring required.
+	 * Change the name of the reference given by the user.
 	 */
-	new_ref->name = NULL;
-	reference_free(new_ref);
+	git__free(ref_in->name);
+	ref_in->name = git__strdup(new_ref->name);
 
 	if ((error = git_hashtable_insert(ref->owner->references.loose_cache, ref->name, ref)) < GIT_SUCCESS)
 		goto rollback;
@@ -1399,13 +1551,13 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
 	 * Check if we have to update HEAD.
 	 */
 
-	if ((error = git_reference_lookup(&head, ref->owner, GIT_HEAD_FILE)) < GIT_SUCCESS)
+	if ((error = reference_lookup(&head, new_ref->owner, GIT_HEAD_FILE)) < GIT_SUCCESS)
 		goto cleanup;
 
-	head_target = git_reference_target(head);
+	head_target = ref_target(head);
 
 	if (head_target && !strcmp(head_target, old_name))
-		if ((error = git_reference_create_symbolic(&head, ref->owner, "HEAD", ref->name, 1)) < GIT_SUCCESS)
+		if ((error = reference_create_symbolic(&head, new_ref->owner, "HEAD", new_ref->name, 1)) < GIT_SUCCESS)
 			goto rollback;
 
 cleanup:
@@ -1417,11 +1569,11 @@ rollback:
 	 * Try to create the old reference again.
 	 */
 	if (ref->type & GIT_REF_SYMBOLIC)
-		error = git_reference_create_symbolic(&new_ref, ref->owner, old_name, target_ref, 0);
+		error = reference_create_symbolic(&new_ref, ref->owner, old_name, target_ref, 0);
 	else
-		error = git_reference_create_oid(&new_ref, ref->owner, old_name, target_oid, 0);
+		error = reference_create_oid(&new_ref, ref->owner, old_name, target_oid, 0);
 
-	ref->name = old_name;
+	ref_in->name = old_name;
 
 	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to rename reference. Failed to rollback");
 }
@@ -1441,10 +1593,10 @@ rollback:
  *
  * This obviously invalidates the `ref` pointer.
  */
-int git_reference_delete(git_reference *ref)
+int reference_delete(reference *ref)
 {
 	int error;
-	git_reference *reference;
+	reference *reference;
 
 	assert(ref);
 
@@ -1468,9 +1620,9 @@ int git_reference_delete(git_reference *ref)
 		/* When deleting a loose reference, we have to ensure that an older
 		 * packed version of it doesn't exist
 		 */
-		if (!git_reference_lookup(&reference, ref->owner, ref->name)) {
+		if (!reference_lookup(&reference, ref->owner, ref->name)) {
 			assert((reference->type & GIT_REF_PACKED) != 0);
-			error = git_reference_delete(reference);
+			error = reference_delete(reference);
 		}
 	}
 
@@ -1479,7 +1631,20 @@ cleanup:
 	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to delete reference");
 }
 
-int git_reference_resolve(git_reference **resolved_ref, git_reference *ref)
+int git_reference_delete(git_reference *ref_in)
+{
+	reference *ref;
+
+	ref = reference_get_internal(ref_in);
+	if (ref == NULL)
+		return git__throw(GIT_ENOTFOUND, "Failed to delete reference. Reference `%s` doesn't exist", ref_in->name);
+
+	git_reference_free(ref_in);
+
+	return reference_delete(ref);
+}
+
+static int reference_resolve(reference **resolved_ref, reference *ref)
 {
 	git_repository *repo;
 	int error, i;
@@ -1501,13 +1666,35 @@ int git_reference_resolve(git_reference **resolved_ref, git_reference *ref)
 			return GIT_SUCCESS;
 
 		ref_sym = (reference_symbolic *)ref;
-		if ((error = git_reference_lookup(&ref, repo, ref_sym->target)) < GIT_SUCCESS)
+		if ((error = reference_lookup(&ref, repo, ref_sym->target)) < GIT_SUCCESS)
 			return error;
 	}
 
 	return git__throw(GIT_ENOMEM, "Failed to resolve reference. Reference is too nested");
 }
 
+int git_reference_resolve(git_reference **resolved_ref, git_reference *ref_in)
+{
+	int error;
+	reference *ref = NULL, *out = NULL;
+
+	*resolved_ref = NULL;
+
+	ref = reference_get_internal(ref_in);
+	if (ref == NULL)
+		return git__throw(GIT_ENOTFOUND, "Failed to resolve reference. Reference `%s` doesn't exist", ref_in->name);
+
+	error = reference_resolve(&out, ref);
+	if (error < GIT_SUCCESS)
+		return error;
+
+	*resolved_ref = reference_create_external(out);
+	if (*resolved_ref == NULL)
+		return GIT_ENOMEM;
+
+	return GIT_SUCCESS;
+}
+
 int git_reference_packall(git_repository *repo)
 {
 	int error;
@@ -1588,12 +1775,6 @@ int git_reference_listall(git_strarray *array, git_repository *repo, unsigned in
 	return GIT_SUCCESS;
 }
 
-
-
-
-/*****************************************
- * Init/free (repository API)
- *****************************************/
 int git_repository__refcache_init(git_refcache *refs)
 {
 	assert(refs);
@@ -1612,7 +1793,7 @@ int git_repository__refcache_init(git_refcache *refs)
 
 void git_repository__refcache_free(git_refcache *refs)
 {
-	git_reference *reference;
+	reference *reference;
 	const void *GIT_UNUSED(_unused);
 
 	assert(refs);
@@ -1632,11 +1813,6 @@ void git_repository__refcache_free(git_refcache *refs)
 	}
 }
 
-
-
-/*****************************************
- * Name normalization
- *****************************************/
 static int check_valid_ref_char(char ch)
 {
 	if ((unsigned) ch <= ' ')
diff --git a/src/refs.h b/src/refs.h
index 33c1e69..db0f5c4 100644
--- a/src/refs.h
+++ b/src/refs.h
@@ -35,8 +35,6 @@
 struct git_reference {
 	git_repository *owner;
 	char *name;
-	unsigned int type;
-	time_t mtime;
 };
 
 typedef struct {
diff --git a/tests/t10-refs.c b/tests/t10-refs.c
index 4cb31d5..cfedff0 100644
--- a/tests/t10-refs.c
+++ b/tests/t10-refs.c
@@ -42,8 +42,8 @@ BEGIN_TEST(readtag0, "lookup a loose tag reference")
 	must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
 
 	must_pass(git_reference_lookup(&reference, repo, loose_tag_ref_name));
-	must_be_true(reference->type & GIT_REF_OID);
-	must_be_true((reference->type & GIT_REF_PACKED) == 0);
+	must_be_true(git_reference_type(reference) & GIT_REF_OID);
+	must_be_true(git_reference_is_packed(reference) == 0);
 	must_be_true(strcmp(reference->name, loose_tag_ref_name) == 0);
 
 	must_pass(git_object_lookup(&object, repo, git_reference_oid(reference), GIT_OBJ_ANY));
@@ -81,12 +81,12 @@ BEGIN_TEST(readsym0, "lookup a symbolic reference")
 	must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
 
 	must_pass(git_reference_lookup(&reference, repo, GIT_HEAD_FILE));
-	must_be_true(reference->type & GIT_REF_SYMBOLIC);
-	must_be_true((reference->type & GIT_REF_PACKED) == 0);
+	must_be_true(git_reference_type(reference) & GIT_REF_SYMBOLIC);
+	must_be_true(git_reference_is_packed(reference) == 0);
 	must_be_true(strcmp(reference->name, GIT_HEAD_FILE) == 0);
 
 	must_pass(git_reference_resolve(&resolved_ref, reference));
-	must_be_true(resolved_ref->type == GIT_REF_OID);
+	must_be_true(git_reference_type(resolved_ref) == GIT_REF_OID);
 
 	must_pass(git_object_lookup(&object, repo, git_reference_oid(resolved_ref), GIT_OBJ_ANY));
 	must_be_true(object != NULL);
@@ -108,12 +108,12 @@ BEGIN_TEST(readsym1, "lookup a nested symbolic reference")
 	must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
 
 	must_pass(git_reference_lookup(&reference, repo, head_tracker_sym_ref_name));
-	must_be_true(reference->type & GIT_REF_SYMBOLIC);
-	must_be_true((reference->type & GIT_REF_PACKED) == 0);
+	must_be_true(git_reference_type(reference) & GIT_REF_SYMBOLIC);
+	must_be_true(git_reference_is_packed(reference) == 0);
 	must_be_true(strcmp(reference->name, head_tracker_sym_ref_name) == 0);
 
 	must_pass(git_reference_resolve(&resolved_ref, reference));
-	must_be_true(resolved_ref->type == GIT_REF_OID);
+	must_be_true(git_reference_type(resolved_ref) == GIT_REF_OID);
 
 	must_pass(git_object_lookup(&object, repo, git_reference_oid(resolved_ref), GIT_OBJ_ANY));
 	must_be_true(object != NULL);
@@ -173,8 +173,8 @@ BEGIN_TEST(readpacked0, "lookup a packed reference")
 	must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
 
 	must_pass(git_reference_lookup(&reference, repo, packed_head_name));
-	must_be_true(reference->type & GIT_REF_OID);
-	must_be_true((reference->type & GIT_REF_PACKED) != 0);
+	must_be_true(git_reference_type(reference) & GIT_REF_OID);
+	must_be_true(git_reference_is_packed(reference));
 	must_be_true(strcmp(reference->name, packed_head_name) == 0);
 
 	must_pass(git_object_lookup(&object, repo, git_reference_oid(reference), GIT_OBJ_ANY));
@@ -192,8 +192,8 @@ BEGIN_TEST(readpacked1, "assure that a loose reference is looked up before a pac
 	must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
 	must_pass(git_reference_lookup(&reference, repo, packed_head_name));
 	must_pass(git_reference_lookup(&reference, repo, packed_test_head_name));
-	must_be_true(reference->type & GIT_REF_OID);
-	must_be_true((reference->type & GIT_REF_PACKED) == 0);
+	must_be_true(git_reference_type(reference) & GIT_REF_OID);
+	must_be_true(git_reference_is_packed(reference) == 0);
 	must_be_true(strcmp(reference->name, packed_test_head_name) == 0);
 
 	git_repository_free(repo);
@@ -219,13 +219,13 @@ BEGIN_TEST(create0, "create a new symbolic reference")
 
 	/* Ensure the reference can be looked-up... */
 	must_pass(git_reference_lookup(&looked_up_ref, repo, new_head_tracker));
-	must_be_true(looked_up_ref->type & GIT_REF_SYMBOLIC);
-	must_be_true((looked_up_ref->type & GIT_REF_PACKED) == 0);
+	must_be_true(git_reference_type(looked_up_ref) & GIT_REF_SYMBOLIC);
+	must_be_true(git_reference_is_packed(looked_up_ref) == 0);
 	must_be_true(strcmp(looked_up_ref->name, new_head_tracker) == 0);
 
 	/* ...peeled.. */
 	must_pass(git_reference_resolve(&resolved_ref, looked_up_ref));
-	must_be_true(resolved_ref->type == GIT_REF_OID);
+	must_be_true(git_reference_type(resolved_ref) == GIT_REF_OID);
 
 	/* ...and that it points to the current master tip */
 	must_be_true(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0);
@@ -283,8 +283,8 @@ BEGIN_TEST(create2, "create a new OID reference")
 
 	/* Ensure the reference can be looked-up... */
 	must_pass(git_reference_lookup(&looked_up_ref, repo, new_head));
-	must_be_true(looked_up_ref->type & GIT_REF_OID);
-	must_be_true((looked_up_ref->type & GIT_REF_PACKED) == 0);
+	must_be_true(git_reference_type(looked_up_ref) & GIT_REF_OID);
+	must_be_true(git_reference_is_packed(looked_up_ref) == 0);
 	must_be_true(strcmp(looked_up_ref->name, new_head) == 0);
 
 	/* ...and that it points to the current master tip */
@@ -359,14 +359,14 @@ BEGIN_TEST(overwrite1, "Overwrite an existing object id reference")
 	must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
 
 	must_pass(git_reference_lookup(&ref, repo, ref_master_name));
-	must_be_true(ref->type & GIT_REF_OID);
+	must_be_true(git_reference_type(ref) & GIT_REF_OID);
 	git_oid_cpy(&id, git_reference_oid(ref));
 
 	/* Create it */
 	must_pass(git_reference_create_oid(&ref, repo, ref_name, &id, 0));
 
 	must_pass(git_reference_lookup(&ref, repo, ref_test_name));
-	must_be_true(ref->type & GIT_REF_OID);
+	must_be_true(git_reference_type(ref) & GIT_REF_OID);
 	git_oid_cpy(&id, git_reference_oid(ref));
 
 	/* Ensure we can't overwrite unless we force it */
@@ -388,7 +388,7 @@ BEGIN_TEST(overwrite2, "Overwrite an existing object id reference with a symboli
 	must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
 
 	must_pass(git_reference_lookup(&ref, repo, ref_master_name));
-	must_be_true(ref->type & GIT_REF_OID);
+	must_be_true(git_reference_type(ref) & GIT_REF_OID);
 	git_oid_cpy(&id, git_reference_oid(ref));
 
 	must_pass(git_reference_create_oid(&ref, repo, ref_name, &id, 0));
@@ -411,7 +411,7 @@ BEGIN_TEST(overwrite3, "Overwrite an existing symbolic reference with an object 
 	must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
 
 	must_pass(git_reference_lookup(&ref, repo, ref_master_name));
-	must_be_true(ref->type & GIT_REF_OID);
+	must_be_true(git_reference_type(ref) & GIT_REF_OID);
 	git_oid_cpy(&id, git_reference_oid(ref));
 
 	/* Create the symbolic ref */
@@ -451,7 +451,7 @@ BEGIN_TEST(pack1, "create a packfile from all the loose rn a repo")
 
 	/* Ensure a known loose ref can be looked up */
 	must_pass(git_reference_lookup(&reference, repo, loose_tag_ref_name));
-	must_be_true((reference->type & GIT_REF_PACKED) == 0);
+	must_be_true(git_reference_is_packed(reference) == 0);
 	must_be_true(strcmp(reference->name, loose_tag_ref_name) == 0);
 
 	/*
@@ -467,7 +467,7 @@ BEGIN_TEST(pack1, "create a packfile from all the loose rn a repo")
 
 	/* Ensure the known ref can still be looked up but is now packed */
 	must_pass(git_reference_lookup(&reference, repo, loose_tag_ref_name));
-	must_be_true((reference->type & GIT_REF_PACKED) != 0);
+	must_be_true(git_reference_is_packed(reference));
 	must_be_true(strcmp(reference->name, loose_tag_ref_name) == 0);
 
 	/* Ensure the known ref has been removed from the loose folder structure */
@@ -493,7 +493,7 @@ BEGIN_TEST(rename0, "rename a loose reference")
 	must_pass(git_reference_lookup(&looked_up_ref, repo, loose_tag_ref_name));
 
 	/* ... which is indeed loose */
-	must_be_true((looked_up_ref->type & GIT_REF_PACKED) == 0);
+	must_be_true(git_reference_is_packed(looked_up_ref) == 0);
 
 	/* Now that the reference is renamed... */
 	must_pass(git_reference_rename(looked_up_ref, new_name, 0));
@@ -507,8 +507,8 @@ BEGIN_TEST(rename0, "rename a loose reference")
 	must_be_true(!strcmp(another_looked_up_ref->name, new_name));
 
 	/* .. the ref is still loose... */
-	must_be_true((another_looked_up_ref->type & GIT_REF_PACKED) == 0);
-	must_be_true((looked_up_ref->type & GIT_REF_PACKED) == 0);
+	must_be_true(git_reference_is_packed(another_looked_up_ref) == 0);
+	must_be_true(git_reference_is_packed(looked_up_ref) == 0);
 
 	/* ...and the ref can be found in the file system */
 	git_path_join(temp_path, repo->path_repository, new_name);
@@ -533,7 +533,7 @@ BEGIN_TEST(rename1, "rename a packed reference (should make it loose)")
 	must_pass(git_reference_lookup(&looked_up_ref, repo, packed_head_name));
 
 	/* .. and it's packed */
-	must_be_true((looked_up_ref->type & GIT_REF_PACKED) != 0);
+	must_be_true(git_reference_is_packed(looked_up_ref) != 0);
 
 	/* Now that the reference is renamed... */
 	must_pass(git_reference_rename(looked_up_ref, brand_new_name, 0));
@@ -547,8 +547,8 @@ BEGIN_TEST(rename1, "rename a packed reference (should make it loose)")
 	must_be_true(!strcmp(another_looked_up_ref->name, brand_new_name));
 
 	/* .. the ref is no longer packed... */
-	must_be_true((another_looked_up_ref->type & GIT_REF_PACKED) == 0);
-	must_be_true((looked_up_ref->type & GIT_REF_PACKED) == 0);
+	must_be_true(git_reference_is_packed(another_looked_up_ref) == 0);
+	must_be_true(git_reference_is_packed(looked_up_ref) == 0);
 
 	/* ...and the ref now happily lives in the file system */
 	git_path_join(temp_path, repo->path_repository, brand_new_name);
@@ -573,13 +573,13 @@ BEGIN_TEST(rename2, "renaming a packed reference does not pack another reference
 	must_pass(git_reference_lookup(&another_looked_up_ref, repo, packed_test_head_name));
 
 	/* Ensure it's loose */
-	must_be_true((another_looked_up_ref->type & GIT_REF_PACKED) == 0);
+	must_be_true(git_reference_is_packed(another_looked_up_ref) == 0);
 
 	/* Lookup the reference to rename */
 	must_pass(git_reference_lookup(&looked_up_ref, repo, packed_head_name));
 
 	/* Ensure it's packed */
-	must_be_true((looked_up_ref->type & GIT_REF_PACKED) != 0);
+	must_be_true(git_reference_is_packed(looked_up_ref) != 0);
 
 	/* Now that the reference is renamed... */
 	must_pass(git_reference_rename(looked_up_ref, brand_new_name, 0));
@@ -588,7 +588,7 @@ BEGIN_TEST(rename2, "renaming a packed reference does not pack another reference
 	must_pass(git_reference_lookup(&another_looked_up_ref, repo, packed_test_head_name));
 
 	/* Ensure it's loose */
-	must_be_true((another_looked_up_ref->type & GIT_REF_PACKED) == 0);
+	must_be_true(git_reference_is_packed(another_looked_up_ref) == 0);
 
 	/* Ensure the other ref still exists on the file system */
 	must_pass(git_futils_exists(temp_path));
@@ -699,7 +699,7 @@ BEGIN_TEST(rename7, "can not overwrite name of existing reference")
 	must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
 
 	must_pass(git_reference_lookup(&ref, repo, ref_master_name));
-	must_be_true(ref->type & GIT_REF_OID);
+	must_be_true(git_reference_type(ref) & GIT_REF_OID);
 
 	git_oid_cpy(&id, git_reference_oid(ref));
 
@@ -729,7 +729,7 @@ BEGIN_TEST(rename8, "can be renamed to a new name prefixed with the old name")
 	must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
 
 	must_pass(git_reference_lookup(&ref, repo, ref_master_name));
-	must_be_true(ref->type & GIT_REF_OID);
+	must_be_true(git_reference_type(ref) & GIT_REF_OID);
 
 	git_oid_cpy(&id, git_reference_oid(ref));
 
@@ -758,7 +758,7 @@ BEGIN_TEST(rename9, "can move a reference to a upper reference hierarchy")
     must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
 
     must_pass(git_reference_lookup(&ref, repo, ref_master_name));
-    must_be_true(ref->type & GIT_REF_OID);
+    must_be_true(git_reference_type(ref) & GIT_REF_OID);
 
     git_oid_cpy(&id, git_reference_oid(ref));
 
@@ -794,7 +794,7 @@ BEGIN_TEST(delete0, "deleting a ref which is both packed and loose should remove
 	must_pass(git_reference_lookup(&looked_up_ref, repo, packed_test_head_name));
 
 	/* Ensure it's the loose version that has been found */
-	must_be_true((looked_up_ref->type & GIT_REF_PACKED) == 0);
+	must_be_true(git_reference_is_packed(looked_up_ref) == 0);
 
 	/* Now that the reference is deleted... */
 	must_pass(git_reference_delete(looked_up_ref));