Commit b6cc559a78a073f2aadd179fe40c09be7318c898

Edward Thomson 2013-05-11T02:42:49

Merge pull request #1385 from carlosmn/refs-iter Introduce a refs iterator

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
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
diff --git a/examples/general.c b/examples/general.c
index adc7ed8..d7a5847 100644
--- a/examples/general.c
+++ b/examples/general.c
@@ -453,7 +453,7 @@ int main (int argc, char** argv)
   // Here we will implement something like `git for-each-ref` simply listing
   // out all available references and the object SHA they resolve to.
   git_strarray ref_list;
-  git_reference_list(&ref_list, repo, GIT_REF_LISTALL);
+  git_reference_list(&ref_list, repo);
 
   const char *refname;
   git_reference *ref;
diff --git a/include/git2/refs.h b/include/git2/refs.h
index e788734..754bda7 100644
--- a/include/git2/refs.h
+++ b/include/git2/refs.h
@@ -297,12 +297,6 @@ GIT_EXTERN(int) git_reference_delete(git_reference *ref);
 /**
  * Fill a list with all the references that can be found in a repository.
  *
- * Using the `list_flags` parameter, the listed references may be filtered
- * by type (`GIT_REF_OID` or `GIT_REF_SYMBOLIC`) or using a bitwise OR of
- * `git_ref_t` values.  To include packed refs, include `GIT_REF_PACKED`.
- * For convenience, use the value `GIT_REF_LISTALL` to obtain all
- * references, including packed ones.
- *
  * The string array will be filled with the names of all references; these
  * values are owned by the user and should be free'd manually when no
  * longer needed, using `git_strarray_free()`.
@@ -310,36 +304,27 @@ GIT_EXTERN(int) git_reference_delete(git_reference *ref);
  * @param array Pointer to a git_strarray structure where
  *		the reference names will be stored
  * @param repo Repository where to find the refs
- * @param list_flags Filtering flags for the reference listing
  * @return 0 or an error code
  */
-GIT_EXTERN(int) git_reference_list(git_strarray *array, git_repository *repo, unsigned int list_flags);
+GIT_EXTERN(int) git_reference_list(git_strarray *array, git_repository *repo);
 
 typedef int (*git_reference_foreach_cb)(const char *refname, void *payload);
 
 /**
  * Perform a callback on each reference in the repository.
  *
- * Using the `list_flags` parameter, the references may be filtered by
- * type (`GIT_REF_OID` or `GIT_REF_SYMBOLIC`) or using a bitwise OR of
- * `git_ref_t` values.  To include packed refs, include `GIT_REF_PACKED`.
- * For convenience, use the value `GIT_REF_LISTALL` to obtain all
- * references, including packed ones.
- *
  * The `callback` function will be called for each reference in the
  * repository, receiving the name of the reference and the `payload` value
  * passed to this method.  Returning a non-zero value from the callback
  * will terminate the iteration.
  *
  * @param repo Repository where to find the refs
- * @param list_flags Filtering flags for the reference listing.
  * @param callback Function which will be called for every listed ref
  * @param payload Additional data to pass to the callback
  * @return 0 on success, GIT_EUSER on non-zero callback, or error code
  */
 GIT_EXTERN(int) git_reference_foreach(
 	git_repository *repo,
-	unsigned int list_flags,
 	git_reference_foreach_cb callback,
 	void *payload);
 
@@ -360,6 +345,31 @@ GIT_EXTERN(void) git_reference_free(git_reference *ref);
 GIT_EXTERN(int) git_reference_cmp(git_reference *ref1, git_reference *ref2);
 
 /**
+ * Create an iterator for the repo's references
+ *
+ * @param out pointer in which to store the iterator
+ * @param repo the repository
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_reference_iterator_new(git_reference_iterator **out, git_repository *repo);
+
+/**
+ * Get the next reference name
+ *
+ * @param out pointer in which to store the string
+ * @param iter the iterator
+ * @param 0, GIT_ITEROVER if there are no more; or an error code
+ */
+GIT_EXTERN(int) git_reference_next(const char **out, git_reference_iterator *iter);
+
+/**
+ * Free the iterator and its associated resources
+ *
+ * @param iter the iterator to free
+ */
+GIT_EXTERN(void) git_reference_iterator_free(git_reference_iterator *iter);
+
+/**
  * Perform a callback on each reference in the repository whose name
  * matches the given pattern.
  *
@@ -373,7 +383,6 @@ GIT_EXTERN(int) git_reference_cmp(git_reference *ref1, git_reference *ref2);
  *
  * @param repo Repository where to find the refs
  * @param glob Pattern to match (fnmatch-style) against reference name.
- * @param list_flags Filtering flags for the reference listing.
  * @param callback Function which will be called for every listed ref
  * @param payload Additional data to pass to the callback
  * @return 0 on success, GIT_EUSER on non-zero callback, or error code
@@ -381,7 +390,6 @@ GIT_EXTERN(int) git_reference_cmp(git_reference *ref1, git_reference *ref2);
 GIT_EXTERN(int) git_reference_foreach_glob(
 	git_repository *repo,
 	const char *glob,
-	unsigned int list_flags,
 	git_reference_foreach_cb callback,
 	void *payload);
 
diff --git a/include/git2/sys/refdb_backend.h b/include/git2/sys/refdb_backend.h
index d5f599f..8dbf38c 100644
--- a/include/git2/sys/refdb_backend.h
+++ b/include/git2/sys/refdb_backend.h
@@ -20,6 +20,23 @@
  */
 GIT_BEGIN_DECL
 
+
+/**
+ * Every backend's iterator must have a pointer to itself as the first
+ * element, so the API can talk to it. You'd define your iterator as
+ *
+ *     struct my_iterator {
+ *             git_reference_iterator parent;
+ *             ...
+ *     }
+ *
+ * and assign `iter->parent.backend` to your `git_refdb_backend`.
+ */
+struct git_reference_iterator {
+	git_refdb_backend *backend;
+	char *glob;
+};
+
 /** An instance for a custom backend */
 struct git_refdb_backend {
     unsigned int version;
@@ -43,29 +60,42 @@ struct git_refdb_backend {
 		const char *ref_name);
 
 	/**
-	 * Enumerates each reference in the refdb.  A refdb implementation must
-	 * provide this function.
+	 * Allocate an iterator object for the backend.
+	 *
+	 * A refdb implementation must provide this function.
 	 */
-	int (*foreach)(
-		git_refdb_backend *backend,
-		unsigned int list_flags,
-		git_reference_foreach_cb callback,
-		void *payload);
+	int (*iterator)(
+		git_reference_iterator **iter,
+		struct git_refdb_backend *backend);
 
 	/**
-	 * 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.
+	 * Allocate a glob-filtering iterator object for the backend.
+	 *
+	 * A refdb implementation may provide this function. If it's
+	 * not available, the glob matching will be done by the frontend.
 	 */
-	int (*foreach_glob)(
-		git_refdb_backend *backend,
-		const char *glob,
-		unsigned int list_flags,
-		git_reference_foreach_cb callback,
-		void *payload);
+	int (*iterator_glob)(
+		git_reference_iterator **iter,
+		struct git_refdb_backend *backend,
+		const char *glob);
+
+	/**
+	 * Return the current value and advance the iterator.
+	 *
+	 * A refdb implementation must provide this function.
+	 */
+	int (*next)(
+		const char **name,
+		git_reference_iterator *iter);
 
 	/**
+	 * Free the iterator
+	 *
+	 * A refdb implementation must provide this function.
+	 */
+	void (*iterator_free)(
+		git_reference_iterator *iter);
+	/*
 	 * Writes the given reference to the refdb.  A refdb implementation
 	 * must provide this function.
 	 */
diff --git a/include/git2/types.h b/include/git2/types.h
index aca9ed9..43751d3 100644
--- a/include/git2/types.h
+++ b/include/git2/types.h
@@ -165,6 +165,10 @@ typedef struct git_signature {
 /** In-memory representation of a reference. */
 typedef struct git_reference git_reference;
 
+/** Iterator for references */
+typedef struct git_reference_iterator  git_reference_iterator;
+
+
 /** Basic type of any Git reference. */
 typedef enum {
 	GIT_REF_INVALID = 0, /** Invalid reference */
diff --git a/src/branch.c b/src/branch.c
index 8302949..dd6dc68 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -124,40 +124,43 @@ on_error:
 	return error;
 }
 
-typedef struct {
-	git_branch_foreach_cb branch_cb;
-	void *callback_payload;
-	unsigned int branch_type;
-} branch_foreach_filter;
-
-static int branch_foreach_cb(const char *branch_name, void *payload)
-{
-	branch_foreach_filter *filter = (branch_foreach_filter *)payload;
-
-	if (filter->branch_type & GIT_BRANCH_LOCAL &&
-		git__prefixcmp(branch_name, GIT_REFS_HEADS_DIR) == 0)
-		return filter->branch_cb(branch_name + strlen(GIT_REFS_HEADS_DIR), GIT_BRANCH_LOCAL, filter->callback_payload);
-
-	if (filter->branch_type & GIT_BRANCH_REMOTE &&
-		git__prefixcmp(branch_name, GIT_REFS_REMOTES_DIR) == 0)
-		return filter->branch_cb(branch_name + strlen(GIT_REFS_REMOTES_DIR), GIT_BRANCH_REMOTE, filter->callback_payload);
-
-	return 0;
-}
-
 int git_branch_foreach(
 	git_repository *repo,
 	unsigned int list_flags,
-	git_branch_foreach_cb branch_cb,
+	git_branch_foreach_cb callback,
 	void *payload)
 {
-	branch_foreach_filter filter;
+	git_reference_iterator *iter;
+	const char *name;
+	int error;
+
+	if (git_reference_iterator_new(&iter, repo) < 0)
+		return -1;
+
+	while ((error = git_reference_next(&name, iter)) == 0) {
+		if (list_flags & GIT_BRANCH_LOCAL &&
+		    git__prefixcmp(name, GIT_REFS_HEADS_DIR) == 0) {
+			if (callback(name + strlen(GIT_REFS_HEADS_DIR), GIT_BRANCH_LOCAL, payload)) {
+				error = GIT_EUSER;
+				break;
+			}
+		}
+
+		if (list_flags & GIT_BRANCH_REMOTE &&
+		    git__prefixcmp(name, GIT_REFS_REMOTES_DIR) == 0) {
+			if (callback(name + strlen(GIT_REFS_REMOTES_DIR), GIT_BRANCH_REMOTE, payload)) {
+				error = GIT_EUSER;
+				break;
+			}
+		}
+	}
 
-	filter.branch_cb = branch_cb;
-	filter.branch_type = list_flags;
-	filter.callback_payload = payload;
+	if (error == GIT_ITEROVER)
+		error = 0;
+
+	git_reference_iterator_free(iter);
+	return error;
 
-	return git_reference_foreach(repo, GIT_REF_LISTALL, &branch_foreach_cb, (void *)&filter);
 }
 
 int git_branch_move(
diff --git a/src/clone.c b/src/clone.c
index 2ff119e..499195d 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -243,7 +243,6 @@ static int update_head_to_remote(git_repository *repo, git_remote *remote)
 	/* Not master. Check all the other refs. */
 	if (git_reference_foreach(
 		repo,
-		GIT_REF_LISTALL,
 		reference_matches_remote_head,
 		&head_info) < 0)
 			goto cleanup;
diff --git a/src/refdb.c b/src/refdb.c
index 33a1934..9f9037c 100644
--- a/src/refdb.c
+++ b/src/refdb.c
@@ -124,60 +124,70 @@ int git_refdb_lookup(git_reference **out, git_refdb *db, const char *ref_name)
 	return error;
 }
 
-int git_refdb_foreach(
-	git_refdb *db,
-	unsigned int list_flags,
-	git_reference_foreach_cb callback,
-	void *payload)
+int git_refdb_iterator(git_reference_iterator **out, git_refdb *db)
 {
-	assert(db && db->backend);
+	if (!db->backend || !db->backend->iterator) {
+		giterr_set(GITERR_REFERENCE, "This backend doesn't support iterators");
+		return -1;
+	}
 
-	return db->backend->foreach(db->backend, list_flags, callback, payload);
-}
+	if (db->backend->iterator(out, db->backend) < 0)
+		return -1;
 
-struct glob_cb_data {
-	const char *glob;
-	git_reference_foreach_cb callback;
-	void *payload;
-};
+	return 0;
+}
 
-static int fromglob_cb(const char *reference_name, void *payload)
+int git_refdb_iterator_glob(git_reference_iterator **out, git_refdb *db, const char *glob)
 {
-	struct glob_cb_data *data = (struct glob_cb_data *)payload;
+	if (!db->backend) {
+		giterr_set(GITERR_REFERENCE, "There are no backends loaded");
+		return -1;
+	}
 
-	if (!p_fnmatch(data->glob, reference_name, 0))
-		return data->callback(reference_name, data->payload);
+	if (db->backend->iterator_glob)
+		return db->backend->iterator_glob(out, db->backend, glob);
+
+	/* If the backend doesn't support glob-filtering themselves, we have to do it */
+	if (db->backend->iterator(out, db->backend) < 0)
+		return -1;
+
+	(*out)->glob = git__strdup(glob);
+	if (!(*out)->glob) {
+		db->backend->iterator_free(*out);
+		return -1;
+	}
 
 	return 0;
 }
 
-int git_refdb_foreach_glob(
-	git_refdb *db,
-	const char *glob,
-	unsigned int list_flags,
-	git_reference_foreach_cb callback,
-	void *payload)
+int git_refdb_next(const char **out, git_reference_iterator *iter)
 {
 	int error;
-	struct glob_cb_data data;
 
-	assert(db && db->backend && glob && callback);
+	if (!iter->glob)
+		return iter->backend->next(out, iter);
 
-	if(db->backend->foreach_glob != NULL)
-		error = db->backend->foreach_glob(db->backend,
-			glob, list_flags, callback, payload);
-	else {
-		data.glob = glob;
-		data.callback = callback;
-		data.payload = payload;
-
-		error = db->backend->foreach(db->backend,
-			list_flags, fromglob_cb, &data);
+	/* If the iterator has a glob, we need to filter */
+	while ((error = iter->backend->next(out, iter)) == 0) {
+		if (!p_fnmatch(iter->glob, *out, 0))
+			break;
 	}
 
 	return error;
 }
 
+void git_refdb_iterator_free(git_reference_iterator *iter)
+{
+	git__free(iter->glob);
+	iter->backend->iterator_free(iter);
+}
+
+struct glob_cb_data {
+	const char *glob;
+	git_reference_foreach_cb callback;
+	void *payload;
+};
+
 int git_refdb_write(git_refdb *db, const git_reference *ref)
 {
 	assert(db && db->backend);
diff --git a/src/refdb.h b/src/refdb.h
index 047113a..2edd05d 100644
--- a/src/refdb.h
+++ b/src/refdb.h
@@ -26,18 +26,10 @@ int git_refdb_lookup(
 	git_refdb *refdb,
 	const char *ref_name);
 
-int git_refdb_foreach(
-	git_refdb *refdb,
-	unsigned int list_flags,
-	git_reference_foreach_cb callback,
-	void *payload);
-
-int git_refdb_foreach_glob(
-	git_refdb *refdb,
-	const char *glob,
-	unsigned int list_flags,
-	git_reference_foreach_cb callback,
-	void *payload);
+int git_refdb_iterator(git_reference_iterator **out, git_refdb *db);
+int git_refdb_iterator_glob(git_reference_iterator **out, git_refdb *db, const char *glob);
+int git_refdb_next(const char **out, git_reference_iterator *iter);
+void git_refdb_iterator_free(git_reference_iterator *iter);
 
 int git_refdb_write(git_refdb *refdb, const git_reference *ref);
 
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index bb2cd0a..9d0af57 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -13,6 +13,7 @@
 #include "reflog.h"
 #include "refdb.h"
 #include "refdb_fs.h"
+#include "iterator.h"
 
 #include <git2/tag.h>
 #include <git2/object.h>
@@ -180,6 +181,9 @@ static int packed_load(refdb_fs_backend *backend)
 		GITERR_CHECK_ALLOC(ref_cache->packfile);
 	}
 
+	if (backend->path == NULL)
+		return 0;
+
 	result = reference_read(&packfile, &ref_cache->packfile_time,
 		backend->path, GIT_PACKEDREFS_FILE, &updated);
 
@@ -558,98 +562,126 @@ struct dirent_list_data {
 	int callback_error;
 };
 
-static git_ref_t loose_guess_rtype(const git_buf *full_path)
+typedef struct {
+	git_reference_iterator parent;
+	unsigned int loose;
+	/* packed */
+	git_strmap *h;
+	khiter_t k;
+	/* loose */
+	git_iterator *fsiter;
+	git_buf buf;
+} refdb_fs_iter;
+
+static int refdb_fs_backend__iterator(git_reference_iterator **out, git_refdb_backend *_backend)
 {
-	git_buf ref_file = GIT_BUF_INIT;
-	git_ref_t type;
+	refdb_fs_iter *iter;
+	refdb_fs_backend *backend;
 
-	type = GIT_REF_INVALID;
+	assert(_backend);
+	backend = (refdb_fs_backend *)_backend;
 
-	if (git_futils_readbuffer(&ref_file, full_path->ptr) == 0) {
-		if (git__prefixcmp((const char *)(ref_file.ptr), GIT_SYMREF) == 0)
-			type = GIT_REF_SYMBOLIC;
-		else
-			type = GIT_REF_OID;
-	}
+	if (packed_load(backend) < 0)
+		return -1;
 
-	git_buf_free(&ref_file);
-	return type;
+	iter = git__calloc(1, sizeof(refdb_fs_iter));
+	GITERR_CHECK_ALLOC(iter);
+
+	iter->parent.backend = _backend;
+	iter->h = backend->refcache.packfile;
+	iter->k = kh_begin(backend->refcache.packfile);
+
+	*out = (git_reference_iterator *)iter;
+
+	return 0;
 }
 
-static int _dirent_loose_listall(void *_data, git_buf *full_path)
+static void refdb_fs_backend__iterator_free(git_reference_iterator *_iter)
 {
-	struct dirent_list_data *data = (struct dirent_list_data *)_data;
-	const char *file_path = full_path->ptr + data->repo_path_len;
-
-	if (git_path_isdir(full_path->ptr) == true)
-		return git_path_direach(full_path, _dirent_loose_listall, _data);
+	refdb_fs_iter *iter = (refdb_fs_iter *) _iter;
 
-	/* do not add twice a reference that exists already in the packfile */
-	if (git_strmap_exists(data->backend->refcache.packfile, file_path))
-		return 0;
+	git_buf_free(&iter->buf);
+	git_iterator_free(iter->fsiter);
+	git__free(iter);
+}
 
-	if (data->list_type != GIT_REF_LISTALL) {
-		if ((data->list_type & loose_guess_rtype(full_path)) == 0)
-			return 0; /* we are filtering out this reference */
+static int iter_packed(const char **out, refdb_fs_iter *iter)
+{
+	/* Move forward to the next entry */
+	while (!kh_exist(iter->h, iter->k)) {
+		iter->k++;
+		if (iter->k == kh_end(iter->h))
+			return GIT_ITEROVER;
 	}
 
-	/* Locked references aren't returned */
-	if (!git__suffixcmp(file_path, GIT_FILELOCK_EXTENSION))
-		return 0;
+	*out = kh_key(iter->h, iter->k);
+	iter->k++;
 
-	if (data->callback(file_path, data->callback_payload))
-		data->callback_error = GIT_EUSER;
-
-	return data->callback_error;
+	return 0;
 }
 
-static int refdb_fs_backend__foreach(
-	git_refdb_backend *_backend,
-	unsigned int list_type,
-	git_reference_foreach_cb callback,
-	void *payload)
+static int iter_loose(const char **out, refdb_fs_iter *iter)
 {
-	refdb_fs_backend *backend;
-	int result;
-	struct dirent_list_data data;
-	git_buf refs_path = GIT_BUF_INIT;
-	const char *ref_name;
-	void *ref = NULL;
+	const git_index_entry *entry;
+	int retry;
+	git_strmap *packfile_refs;
+	refdb_fs_backend *backend = (refdb_fs_backend *) iter->parent.backend;
 
-	GIT_UNUSED(ref);
+	packfile_refs = backend->refcache.packfile;
 
-	assert(_backend);
-	backend = (refdb_fs_backend *)_backend;
+	do {
+		khiter_t pos;
+		if (git_iterator_current(&entry, iter->fsiter) < 0)
+			return -1;
 
-	if (packed_load(backend) < 0)
-		return -1;
+		git_buf_clear(&iter->buf);
+		if (!entry)
+			return GIT_ITEROVER;
 
-	/* list all the packed references first */
-	if (list_type & GIT_REF_OID) {
-		git_strmap_foreach(backend->refcache.packfile, ref_name, ref, {
-			if (callback(ref_name, payload))
-				return GIT_EUSER;
-		});
-	}
+		if (git_buf_printf(&iter->buf, "refs/%s", entry->path) < 0)
+			return -1;
 
-	/* now list the loose references, trying not to
-	 * duplicate the ref names already in the packed-refs file */
+		git_iterator_advance(NULL, iter->fsiter);
 
-	data.repo_path_len = strlen(backend->path);
-	data.list_type = list_type;
-	data.backend = backend;
-	data.callback = callback;
-	data.callback_payload = payload;
-	data.callback_error = 0;
+		/* Skip this one if we already listed it in packed */
+		pos = git_strmap_lookup_index(packfile_refs, git_buf_cstr(&iter->buf));
+		retry = 0;
+		if (git_strmap_valid_index(packfile_refs, pos) ||
+		    !git_reference_is_valid_name(git_buf_cstr(&iter->buf)))
+			retry = 1;
 
-	if (git_buf_joinpath(&refs_path, backend->path, GIT_REFS_DIR) < 0)
+		*out = git_buf_cstr(&iter->buf);
+	} while (retry);
+
+	return 0;
+}
+
+static int iter_loose_setup(refdb_fs_iter *iter)
+{
+	refdb_fs_backend *backend = (refdb_fs_backend *) iter->parent.backend;
+
+	git_buf_clear(&iter->buf);
+	if (git_buf_printf(&iter->buf, "%s/refs", backend->path) < 0)
 		return -1;
 
-	result = git_path_direach(&refs_path, _dirent_loose_listall, &data);
+	return git_iterator_for_filesystem(&iter->fsiter, git_buf_cstr(&iter->buf), 0, NULL, NULL);
+}
 
-	git_buf_free(&refs_path);
+static int refdb_fs_backend__next(const char **out, git_reference_iterator *_iter)
+{
+	refdb_fs_iter *iter = (refdb_fs_iter *)_iter;
 
-	return data.callback_error ? GIT_EUSER : result;
+	/* First round of checks to make sure where we are */
+	if (!iter->loose && iter->k == kh_end(iter->h)) {
+		if (iter_loose_setup(iter) < 0)
+			return -1;
+		iter->loose = 1;
+	}
+
+	if (!iter->loose)
+		return iter_packed(out, iter);
+	else
+		return iter_loose(out, iter);
 }
 
 static int loose_write(refdb_fs_backend *backend, const git_reference *ref)
@@ -1027,6 +1059,10 @@ static int setup_namespace(git_buf *path, git_repository *repo)
 {
 	char *parts, *start, *end; 
 
+	/* Not all repositories have a path */
+	if (repo->path_repository == NULL)
+		return 0;
+
 	/* Load the path to the repo first */
 	git_buf_puts(path, repo->path_repository);
 
@@ -1081,7 +1117,9 @@ int git_refdb_backend_fs(
 
 	backend->parent.exists = &refdb_fs_backend__exists;
 	backend->parent.lookup = &refdb_fs_backend__lookup;
-	backend->parent.foreach = &refdb_fs_backend__foreach;
+	backend->parent.iterator = &refdb_fs_backend__iterator;
+	backend->parent.next = &refdb_fs_backend__next;
+	backend->parent.iterator_free = &refdb_fs_backend__iterator_free;
 	backend->parent.write = &refdb_fs_backend__write;
 	backend->parent.delete = &refdb_fs_backend__delete;
 	backend->parent.compress = &refdb_fs_backend__compress;
diff --git a/src/refs.c b/src/refs.c
index 684c54c..9c6c5c6 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -139,7 +139,7 @@ static int reference_path_available(
 	data.available = 1;
 
 	error = git_reference_foreach(
-		repo, GIT_REF_LISTALL, _reference_available_cb, (void *)&data);
+		repo, _reference_available_cb, (void *)&data);
 	if (error < 0)
 		return error;
 
@@ -619,14 +619,59 @@ int git_reference_resolve(git_reference **ref_out, const git_reference *ref)
 
 int git_reference_foreach(
 	git_repository *repo,
-	unsigned int list_flags,
 	git_reference_foreach_cb callback,
 	void *payload)
 {
+	git_reference_iterator *iter;
+	const char *name;
+	int error;
+
+	if (git_reference_iterator_new(&iter, repo) < 0)
+		return -1;
+
+	while ((error = git_reference_next(&name, iter)) == 0) {
+		if (callback(name, payload)) {
+			error = GIT_EUSER;
+			goto out;
+		}
+	}
+
+	if (error == GIT_ITEROVER)
+		error = 0;
+
+out:
+	git_reference_iterator_free(iter);
+	return error;
+}
+
+int git_reference_iterator_new(git_reference_iterator **out, git_repository *repo)
+{
+	git_refdb *refdb;
+
+	if (git_repository_refdb__weakptr(&refdb, repo) < 0)
+		return -1;
+
+	return git_refdb_iterator(out, refdb);
+}
+
+int git_reference_iterator_glob_new(git_reference_iterator **out, git_repository *repo, const char *glob)
+{
 	git_refdb *refdb;
-	git_repository_refdb__weakptr(&refdb, repo);
 
-	return git_refdb_foreach(refdb, list_flags, callback, payload);
+	if (git_repository_refdb__weakptr(&refdb, repo) < 0)
+		return -1;
+
+	return git_refdb_iterator_glob(out, refdb, glob);
+}
+
+int git_reference_next(const char **out, git_reference_iterator *iter)
+{
+	return git_refdb_next(out, iter);
+}
+
+void git_reference_iterator_free(git_reference_iterator *iter)
+{
+	git_refdb_iterator_free(iter);
 }
 
 static int cb__reflist_add(const char *ref, void *data)
@@ -636,8 +681,7 @@ static int cb__reflist_add(const char *ref, void *data)
 
 int git_reference_list(
 	git_strarray *array,
-	git_repository *repo,
-	unsigned int list_flags)
+	git_repository *repo)
 {
 	git_vector ref_list;
 
@@ -650,7 +694,7 @@ int git_reference_list(
 		return -1;
 
 	if (git_reference_foreach(
-			repo, list_flags, &cb__reflist_add, (void *)&ref_list) < 0) {
+			repo, &cb__reflist_add, (void *)&ref_list) < 0) {
 		git_vector_free(&ref_list);
 		return -1;
 	}
@@ -950,19 +994,29 @@ int git_reference__update_terminal(
 int git_reference_foreach_glob(
 	git_repository *repo,
 	const char *glob,
-	unsigned int list_flags,
-	int (*callback)(
-		const char *reference_name,
-		void *payload),
+	git_reference_foreach_cb callback,
 	void *payload)
 {
-	git_refdb *refdb;
+	git_reference_iterator *iter;
+	const char *name;
+	int error;
 
-	assert(repo && glob && callback);
+	if (git_reference_iterator_glob_new(&iter, repo, glob) < 0)
+		return -1;
 
-	git_repository_refdb__weakptr(&refdb, repo);
+	while ((error = git_reference_next(&name, iter)) == 0) {
+		if (callback(name, payload)) {
+			error = GIT_EUSER;
+			goto out;
+		}
+	}
 
-	return git_refdb_foreach_glob(refdb, glob, list_flags, callback, payload);
+	if (error == GIT_ITEROVER)
+		error = 0;
+
+out:
+	git_reference_iterator_free(iter);
+	return error;
 }
 
 int git_reference_has_log(
diff --git a/src/remote.c b/src/remote.c
index 8a2982c..e5a7df7 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -1251,14 +1251,6 @@ static int update_branch_remote_config_entry(
 		update_config_entries_cb, &data);
 }
 
-static int rename_cb(const char *ref, void *data)
-{
-	if (git__prefixcmp(ref, GIT_REFS_REMOTES_DIR))
-		return 0;
-	
-	return git_vector_insert((git_vector *)data, git__strdup(ref));
-}
-
 static int rename_one_remote_reference(
 	git_repository *repo,
 	const char *reference_name,
@@ -1298,16 +1290,29 @@ static int rename_remote_references(
 	int error = -1;
 	unsigned int i;
 	char *name;
+	const char *refname;
+	git_reference_iterator *iter;
 
 	if (git_vector_init(&refnames, 8, NULL) < 0)
+		return -1;
+
+	if (git_reference_iterator_new(&iter, repo) < 0)
 		goto cleanup;
 
-	if (git_reference_foreach(
-		repo,
-		GIT_REF_LISTALL,
-		rename_cb,
-		&refnames) < 0)
-			goto cleanup;
+	while ((error = git_reference_next(&refname, iter)) == 0) {
+		if (git__prefixcmp(refname, GIT_REFS_REMOTES_DIR))
+			continue;
+
+		if ((error = git_vector_insert(&refnames, git__strdup(refname))) < 0)
+			break;
+
+	}
+
+	git_reference_iterator_free(iter);
+	if (error == GIT_ITEROVER)
+		error = 0;
+	else
+		goto cleanup;
 
 	git_vector_foreach(&refnames, i, name) {
 		if ((error = rename_one_remote_reference(repo, name, old_name, new_name)) < 0)
diff --git a/src/repository.c b/src/repository.c
index 0b8fc39..4ab3921 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -1473,7 +1473,7 @@ static int at_least_one_cb(const char *refname, void *payload)
 
 static int repo_contains_no_reference(git_repository *repo)
 {
-	int error = git_reference_foreach(repo, GIT_REF_LISTALL, at_least_one_cb, NULL);
+	int error = git_reference_foreach(repo, at_least_one_cb, NULL);
 
 	if (error == GIT_EUSER)
 		return 0;
diff --git a/src/revwalk.c b/src/revwalk.c
index 16f0662..528d02b 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -186,7 +186,7 @@ static int push_glob(git_revwalk *walk, const char *glob, int hide)
 	data.hide = hide;
 
 	if (git_reference_foreach_glob(
-		walk->repo, git_buf_cstr(&buf), GIT_REF_LISTALL, push_glob_cb, &data) < 0)
+		walk->repo, git_buf_cstr(&buf), push_glob_cb, &data) < 0)
 		goto on_error;
 
 	regfree(&preg);
diff --git a/src/tag.c b/src/tag.c
index a4f2e25..f81956d 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -427,7 +427,7 @@ int git_tag_foreach(git_repository *repo, git_tag_foreach_cb cb, void *cb_data)
 	data.cb_data = cb_data;
 	data.repo = repo;
 
-	return git_reference_foreach(repo, GIT_REF_OID, &tags_cb, &data);
+	return git_reference_foreach(repo, &tags_cb, &data);
 }
 
 typedef struct {
diff --git a/src/transports/local.c b/src/transports/local.c
index 8b4d50c..bd3bf93 100644
--- a/src/transports/local.c
+++ b/src/transports/local.c
@@ -124,7 +124,7 @@ static int store_refs(transport_local *t)
 
 	assert(t);
 
-	if (git_reference_list(&ref_names, t->repo, GIT_REF_LISTALL) < 0 ||
+	if (git_reference_list(&ref_names, t->repo) < 0 ||
 		git_vector_init(&t->refs, ref_names.count, NULL) < 0)
 		goto on_error;
 
diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c
index 765b914..09bef1a 100644
--- a/src/transports/smart_protocol.c
+++ b/src/transports/smart_protocol.c
@@ -193,7 +193,7 @@ static int fetch_setup_walk(git_revwalk **out, git_repository *repo)
 	unsigned int i;
 	git_reference *ref;
 
-	if (git_reference_list(&refs, repo, GIT_REF_LISTALL) < 0)
+	if (git_reference_list(&refs, repo) < 0)
 		return -1;
 
 	if (git_revwalk_new(&walk, repo) < 0)
diff --git a/tests-clar/network/fetchlocal.c b/tests-clar/network/fetchlocal.c
index bcf298c..09335b3 100644
--- a/tests-clar/network/fetchlocal.c
+++ b/tests-clar/network/fetchlocal.c
@@ -34,7 +34,7 @@ void test_network_fetchlocal__complete(void)
 	cl_git_pass(git_remote_download(origin, transfer_cb, &callcount));
 	cl_git_pass(git_remote_update_tips(origin));
 
-	cl_git_pass(git_reference_list(&refnames, repo, GIT_REF_LISTALL));
+	cl_git_pass(git_reference_list(&refnames, repo));
 	cl_assert_equal_i(19, (int)refnames.count);
 	cl_assert(callcount > 0);
 
@@ -58,7 +58,7 @@ void test_network_fetchlocal__partial(void)
 	const char *url;
 
 	cl_set_cleanup(&cleanup_sandbox, NULL);
-	cl_git_pass(git_reference_list(&refnames, repo, GIT_REF_LISTALL));
+	cl_git_pass(git_reference_list(&refnames, repo));
 	cl_assert_equal_i(1, (int)refnames.count);
 
 	url = cl_git_fixture_url("testrepo.git");
@@ -69,7 +69,7 @@ void test_network_fetchlocal__partial(void)
 
 	git_strarray_free(&refnames);
 
-	cl_git_pass(git_reference_list(&refnames, repo, GIT_REF_LISTALL));
+	cl_git_pass(git_reference_list(&refnames, repo));
 	cl_assert_equal_i(20, (int)refnames.count); /* 18 remote + 1 local */
 	cl_assert(callcount > 0);
 
diff --git a/tests-clar/refdb/inmemory.c b/tests-clar/refdb/inmemory.c
index 243b5bb..d2594cd 100644
--- a/tests-clar/refdb/inmemory.c
+++ b/tests-clar/refdb/inmemory.c
@@ -163,7 +163,7 @@ void test_refdb_inmemory__foreach(void)
 	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_git_pass(git_reference_foreach(repo,foreach_test, &i));
 	cl_assert_equal_i(3, (int)i);
 
 	git_reference_free(write1);
@@ -210,7 +210,7 @@ void test_refdb_inmemory__delete(void)
 	git_reference_delete(write3);
 	git_reference_free(write3);
 
-	cl_git_pass(git_reference_foreach(repo, GIT_REF_LISTALL, delete_test, &i));
+	cl_git_pass(git_reference_foreach(repo, delete_test, &i));
 	cl_assert_equal_i(1, (int)i);
 
 	git_reference_free(write2);
diff --git a/tests-clar/refdb/testdb.c b/tests-clar/refdb/testdb.c
index 627254e..961e18d 100644
--- a/tests-clar/refdb/testdb.c
+++ b/tests-clar/refdb/testdb.c
@@ -112,33 +112,49 @@ static int refdb_test_backend__lookup(
 	return GIT_ENOTFOUND;
 }
 
-static int refdb_test_backend__foreach(
-	git_refdb_backend *_backend,
-	unsigned int list_flags,
-	git_reference_foreach_cb callback,
-	void *payload)
-{
-	refdb_test_backend *backend;
-	refdb_test_entry *entry;
+typedef struct {
+	git_reference_iterator parent;
 	size_t i;
+} refdb_test_iter;
 
-	assert(_backend);
-	backend = (refdb_test_backend *)_backend;
+static int refdb_test_backend__iterator(git_reference_iterator **out, git_refdb_backend *_backend)
+{
+	refdb_test_iter *iter;
 
-	git_vector_foreach(&backend->refs, i, entry) {
-		if (entry->type == GIT_REF_OID && (list_flags & GIT_REF_OID) == 0)
-			continue;
+	GIT_UNUSED(_backend);
 
-		if (entry->type == GIT_REF_SYMBOLIC && (list_flags & GIT_REF_SYMBOLIC) == 0)
-			continue;
+	iter = git__calloc(1, sizeof(refdb_test_iter));
+	GITERR_CHECK_ALLOC(iter);
 
-		if (callback(entry->name, payload) != 0)
-			return GIT_EUSER;
-	}
+	iter->parent.backend = _backend;
+	iter->i = 0;
+
+	*out = (git_reference_iterator *) iter;
 
 	return 0;
 }
 
+static int refdb_test_backend__next(const char **name, git_reference_iterator *_iter)
+{
+	refdb_test_entry *entry;
+	refdb_test_backend *backend = (refdb_test_backend *) _iter->backend;
+	refdb_test_iter *iter = (refdb_test_iter *) _iter;
+
+	entry = git_vector_get(&backend->refs, iter->i);
+	if (!entry)
+		return GIT_ITEROVER;
+
+	*name = entry->name;
+	iter->i++;
+
+	return 0;
+}
+
+static void refdb_test_backend__iterator_free(git_reference_iterator *iter)
+{
+	git__free(iter);
+}
+
 static void refdb_test_entry_free(refdb_test_entry *entry)
 {
 	if (entry->type == GIT_REF_SYMBOLIC)
@@ -200,7 +216,9 @@ int refdb_backend_test(
 
 	backend->parent.exists = &refdb_test_backend__exists;
 	backend->parent.lookup = &refdb_test_backend__lookup;
-	backend->parent.foreach = &refdb_test_backend__foreach;
+	backend->parent.iterator = &refdb_test_backend__iterator;
+	backend->parent.next = &refdb_test_backend__next;
+	backend->parent.iterator_free = &refdb_test_backend__iterator_free;
 	backend->parent.write = &refdb_test_backend__write;
 	backend->parent.delete = &refdb_test_backend__delete;
 	backend->parent.free = &refdb_test_backend__free;
diff --git a/tests-clar/refs/foreachglob.c b/tests-clar/refs/foreachglob.c
index 4da1a15..2c45808 100644
--- a/tests-clar/refs/foreachglob.c
+++ b/tests-clar/refs/foreachglob.c
@@ -37,11 +37,11 @@ static int count_cb(const char *reference_name, void *payload)
 	return 0;
 }
 
-static void assert_retrieval(const char *glob, unsigned int flags, int expected_count)
+static void assert_retrieval(const char *glob, int expected_count)
 {
 	int count = 0;
 
-	cl_git_pass(git_reference_foreach_glob(repo, glob, flags, count_cb, &count));
+	cl_git_pass(git_reference_foreach_glob(repo, glob, count_cb, &count));
 
 	cl_assert_equal_i(expected_count, count);
 }
@@ -49,17 +49,17 @@ static void assert_retrieval(const char *glob, unsigned int flags, int expected_
 void test_refs_foreachglob__retrieve_all_refs(void)
 {
 	/* 12 heads (including one packed head) + 1 note + 2 remotes + 7 tags */
-	assert_retrieval("*", GIT_REF_LISTALL, 22);
+	assert_retrieval("*", 22);
 }
 
 void test_refs_foreachglob__retrieve_remote_branches(void)
 {
-	assert_retrieval("refs/remotes/*", GIT_REF_LISTALL, 2);
+	assert_retrieval("refs/remotes/*", 2);
 }
 
 void test_refs_foreachglob__retrieve_local_branches(void)
 {
-	assert_retrieval("refs/heads/*", GIT_REF_LISTALL, 12);
+	assert_retrieval("refs/heads/*", 12);
 }
 
 void test_refs_foreachglob__retrieve_partially_named_references(void)
@@ -69,7 +69,7 @@ void test_refs_foreachglob__retrieve_partially_named_references(void)
 	 * refs/remotes/test/master, refs/tags/test
 	 */
 
-	assert_retrieval("*test*", GIT_REF_LISTALL, 4);
+	assert_retrieval("*test*", 4);
 }
 
 
@@ -89,7 +89,7 @@ void test_refs_foreachglob__can_cancel(void)
 	int count = 0;
 
 	cl_assert_equal_i(GIT_EUSER, git_reference_foreach_glob(
-		repo, "*", GIT_REF_LISTALL, interrupt_cb, &count) );
+		repo, "*", interrupt_cb, &count) );
 
 	cl_assert_equal_i(11, count);
 }
diff --git a/tests-clar/refs/iterator.c b/tests-clar/refs/iterator.c
new file mode 100644
index 0000000..d5555c6
--- /dev/null
+++ b/tests-clar/refs/iterator.c
@@ -0,0 +1,94 @@
+#include "clar_libgit2.h"
+#include "refs.h"
+#include "vector.h"
+
+static git_repository *repo;
+
+void test_refs_iterator__initialize(void)
+{
+	cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
+}
+
+void test_refs_iterator__cleanup(void)
+{
+	git_repository_free(repo);
+}
+
+static const char *refnames[] = {
+	"refs/heads/br2",
+	"refs/heads/cannot-fetch",
+	"refs/heads/chomped",
+	"refs/heads/haacked",
+	"refs/heads/master",
+	"refs/heads/not-good",
+	"refs/heads/packed",
+	"refs/heads/packed-test",
+	"refs/heads/subtrees",
+	"refs/heads/test",
+	"refs/heads/track-local",
+	"refs/heads/trailing",
+	"refs/notes/fanout",
+	"refs/remotes/test/master",
+	"refs/tags/annotated_tag_to_blob",
+	"refs/tags/e90810b",
+	"refs/tags/hard_tag",
+	"refs/tags/point_to_blob",
+	"refs/tags/taggerless",
+	"refs/tags/test",
+	"refs/tags/wrapped_tag",
+};
+
+void test_refs_iterator__list(void)
+{
+	git_reference_iterator *iter;
+	git_vector output;
+	char *refname;
+	int error;
+	size_t i;
+
+	cl_git_pass(git_vector_init(&output, 32, git__strcmp_cb));
+	cl_git_pass(git_reference_iterator_new(&iter, repo));
+
+	do {
+		const char *name;
+		error = git_reference_next(&name, iter);
+		cl_assert(error == 0 || error == GIT_ITEROVER);
+		if (error != GIT_ITEROVER) {
+			char *dup = git__strdup(name);
+			cl_assert(dup != NULL);
+			cl_git_pass(git_vector_insert(&output, dup));
+		}
+	} while (!error);
+
+	cl_assert_equal_i(output.length, ARRAY_SIZE(refnames));
+
+	git_vector_sort(&output);
+	git_vector_foreach(&output, i, refname) {
+		cl_assert_equal_s(refname, refnames[i]);
+	}
+
+	git_reference_iterator_free(iter);
+
+	git_vector_foreach(&output, i, refname) {
+		git__free(refname);
+	}
+	git_vector_free(&output);
+}
+
+void test_refs_iterator__empty(void)
+{
+	git_reference_iterator *iter;
+	git_odb *odb;
+	const char *name;
+	git_repository *empty;
+
+	cl_git_pass(git_odb_new(&odb));
+	cl_git_pass(git_repository_wrap_odb(&empty, odb));
+
+	cl_git_pass(git_reference_iterator_new(&iter, empty));
+	cl_assert_equal_i(GIT_ITEROVER, git_reference_next(&name, iter));
+
+	git_reference_iterator_free(iter);
+	git_odb_free(odb);
+	git_repository_free(empty);
+}
diff --git a/tests-clar/refs/list.c b/tests-clar/refs/list.c
index 3948b2b..c9c2af4 100644
--- a/tests-clar/refs/list.c
+++ b/tests-clar/refs/list.c
@@ -25,7 +25,7 @@ void test_refs_list__all(void)
    // try to list all the references in our test repo
 	git_strarray ref_list;
 
-	cl_git_pass(git_reference_list(&ref_list, g_repo, GIT_REF_LISTALL));
+	cl_git_pass(git_reference_list(&ref_list, g_repo));
 
 	/*{
 		unsigned short i;
@@ -41,17 +41,6 @@ void test_refs_list__all(void)
 	git_strarray_free(&ref_list);
 }
 
-void test_refs_list__symbolic_only(void)
-{
-   // try to list only the symbolic references
-	git_strarray ref_list;
-
-	cl_git_pass(git_reference_list(&ref_list, g_repo, GIT_REF_SYMBOLIC));
-	cl_assert(ref_list.count == 0); /* no symrefs in the test repo */
-
-	git_strarray_free(&ref_list);
-}
-
 void test_refs_list__do_not_retrieve_references_which_name_end_with_a_lock_extension(void)
 {
 	git_strarray ref_list;
@@ -61,7 +50,7 @@ void test_refs_list__do_not_retrieve_references_which_name_end_with_a_lock_exten
 		"./testrepo/.git/refs/heads/hanwen.lock",
 		"144344043ba4d4a405da03de3844aa829ae8be0e\n");
 
-	cl_git_pass(git_reference_list(&ref_list, g_repo, GIT_REF_LISTALL));
+	cl_git_pass(git_reference_list(&ref_list, g_repo));
 	cl_assert_equal_i((int)ref_list.count, 13);
 
 	git_strarray_free(&ref_list);
diff --git a/tests-clar/refs/listall.c b/tests-clar/refs/listall.c
index 8f4c374..c696fbb 100644
--- a/tests-clar/refs/listall.c
+++ b/tests-clar/refs/listall.c
@@ -9,7 +9,7 @@ static void ensure_no_refname_starts_with_a_forward_slash(const char *path)
 	size_t i;
 
 	cl_git_pass(git_repository_open(&repo, path));
-	cl_git_pass(git_reference_list(&ref_list, repo, GIT_REF_LISTALL));
+	cl_git_pass(git_reference_list(&ref_list, repo));
 
 	cl_assert(ref_list.count > 0);
 
@@ -38,7 +38,7 @@ void test_refs_listall__from_repository_opened_through_gitdir_path(void)
 void test_refs_listall__from_repository_with_no_trailing_newline(void)
 {
 	cl_git_pass(git_repository_open(&repo, cl_fixture("bad_tag.git")));
-	cl_git_pass(git_reference_list(&ref_list, repo, GIT_REF_LISTALL));
+	cl_git_pass(git_reference_list(&ref_list, repo));
 
 	cl_assert(ref_list.count > 0);