Commit 8f4a8b096ba35a2af3f9c51dad7e0402790b58ac

Ben Straub 2013-10-28T06:20:28

Merge pull request #1802 from libgit2/cmn/reflog-backend Make reflog part of refdb

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
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
diff --git a/include/git2/reflog.h b/include/git2/reflog.h
index 4944530..2d1b6ee 100644
--- a/include/git2/reflog.h
+++ b/include/git2/reflog.h
@@ -31,10 +31,11 @@ GIT_BEGIN_DECL
  * git_reflog_free().
  *
  * @param out pointer to reflog
- * @param ref reference to read the reflog for
+ * @param repo the repostiory
+ * @param name reference to look up
  * @return 0 or an error code
  */
-GIT_EXTERN(int) git_reflog_read(git_reflog **out, const git_reference *ref);
+GIT_EXTERN(int) git_reflog_read(git_reflog **out, git_repository *repo,  const char *name);
 
 /**
  * Write an existing in-memory reflog object back to disk
@@ -59,26 +60,45 @@ GIT_EXTERN(int) git_reflog_write(git_reflog *reflog);
 GIT_EXTERN(int) git_reflog_append(git_reflog *reflog, const git_oid *id, const git_signature *committer, const char *msg);
 
 /**
- * Rename the reflog for the given reference
+ * Add a new entry to the named reflog.
+ *
+ * This utility function loads the named reflog, appends to it and
+ * writes it back out to the backend.
+ *
+ * `msg` is optional and can be NULL.
+ *
+ * @param repo the repository to act on
+ * @param name the reflog's name
+ * @param id the OID the reference is now pointing to
+ * @param committer the signature of the committer
+ * @param msg the reflog message
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_reflog_append_to(git_repository *repo, const char *name, const git_oid *id, const git_signature *committer, const char *msg);
+
+/**
+ * Rename a reflog
  *
  * The reflog to be renamed is expected to already exist
  *
  * The new name will be checked for validity.
  * See `git_reference_create_symbolic()` for rules about valid names.
  *
- * @param ref the reference
- * @param name the new name of the reference
+ * @param repo the repository
+ * @param old_name the old name of the reference
+ * @param new_name the new name of the reference
  * @return 0 on success, GIT_EINVALIDSPEC or an error code
  */
-GIT_EXTERN(int) git_reflog_rename(git_reference *ref, const char *name);
+GIT_EXTERN(int) git_reflog_rename(git_repository *repo, const char *old_name, const char *name);
 
 /**
  * Delete the reflog for the given reference
  *
- * @param ref the reference
+ * @param repo the repository
+ * @param name the reflog to delete
  * @return 0 or an error code
  */
-GIT_EXTERN(int) git_reflog_delete(git_reference *ref);
+GIT_EXTERN(int) git_reflog_delete(git_repository *repo, const char *name);
 
 /**
  * Get the number of log entries in a reflog
diff --git a/include/git2/sys/refdb_backend.h b/include/git2/sys/refdb_backend.h
index addaa86..9cf5073 100644
--- a/include/git2/sys/refdb_backend.h
+++ b/include/git2/sys/refdb_backend.h
@@ -119,6 +119,26 @@ struct git_refdb_backend {
 	 * provide this function; if it is not provided, nothing will be done.
 	 */
 	void (*free)(git_refdb_backend *backend);
+
+	/**
+	 * Read the reflog for the given reference name.
+	 */
+	int (*reflog_read)(git_reflog **out, git_refdb_backend *backend, const char *name);
+
+	/**
+	 * Write a reflog to disk.
+	 */
+	int (*reflog_write)(git_refdb_backend *backend, git_reflog *reflog);
+
+	/**
+	 * Rename a reflog
+	 */
+	int (*reflog_rename)(git_refdb_backend *_backend, const char *old_name, const char *new_name);
+
+	/**
+	 * Remove a reflog.
+	 */
+	int (*reflog_delete)(git_refdb_backend *backend, const char *name);
 };
 
 #define GIT_REFDB_BACKEND_VERSION 1
diff --git a/include/git2/sys/reflog.h b/include/git2/sys/reflog.h
new file mode 100644
index 0000000..c9d0041
--- /dev/null
+++ b/include/git2/sys/reflog.h
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_sys_git_reflog_h__
+#define INCLUDE_sys_git_reflog_h__
+
+#include "git2/common.h"
+#include "git2/types.h"
+#include "git2/oid.h"
+
+GIT_BEGIN_DECL
+
+GIT_EXTERN(git_reflog_entry *) git_reflog_entry__alloc(void);
+GIT_EXTERN(void) git_reflog_entry__free(git_reflog_entry *entry);
+
+GIT_END_DECL
+
+#endif
diff --git a/src/refdb.c b/src/refdb.c
index 7df61a5..adb5880 100644
--- a/src/refdb.c
+++ b/src/refdb.c
@@ -16,6 +16,7 @@
 #include "hash.h"
 #include "refdb.h"
 #include "refs.h"
+#include "reflog.h"
 
 int git_refdb_new(git_refdb **out, git_repository *repo)
 {
@@ -203,3 +204,18 @@ int git_refdb_delete(struct git_refdb *db, const char *ref_name)
 	assert(db && db->backend);
 	return db->backend->del(db->backend, ref_name);
 }
+
+int git_refdb_reflog_read(git_reflog **out, git_refdb *db,  const char *name)
+{
+	int error;
+
+	assert(db && db->backend);
+
+	if ((error = db->backend->reflog_read(out, db->backend, name)) < 0)
+		return error;
+
+	GIT_REFCOUNT_INC(db);
+	(*out)->db = db;
+
+	return 0;
+}
diff --git a/src/refdb.h b/src/refdb.h
index 3aea37b..0ee60d9 100644
--- a/src/refdb.h
+++ b/src/refdb.h
@@ -43,4 +43,8 @@ void git_refdb_iterator_free(git_reference_iterator *iter);
 int git_refdb_write(git_refdb *refdb, git_reference *ref, int force);
 int git_refdb_delete(git_refdb *refdb, const char *ref_name);
 
+int git_refdb_reflog_read(git_reflog **out, git_refdb *db,  const char *name);
+int git_refdb_reflog_write(git_reflog *reflog);
+
+
 #endif
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index afe1a2a..7ce09ba 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -16,12 +16,14 @@
 #include "refdb_fs.h"
 #include "iterator.h"
 #include "sortedcache.h"
+#include "signature.h"
 
 #include <git2/tag.h>
 #include <git2/object.h>
 #include <git2/refdb.h>
 #include <git2/sys/refdb_backend.h>
 #include <git2/sys/refs.h>
+#include <git2/sys/reflog.h>
 
 GIT__USE_STRMAP;
 
@@ -1071,6 +1073,345 @@ static int setup_namespace(git_buf *path, git_repository *repo)
 	return 0;
 }
 
+static int reflog_alloc(git_reflog **reflog, const char *name)
+{
+	git_reflog *log;
+
+	*reflog = NULL;
+
+	log = git__calloc(1, sizeof(git_reflog));
+	GITERR_CHECK_ALLOC(log);
+
+	log->ref_name = git__strdup(name);
+	GITERR_CHECK_ALLOC(log->ref_name);
+
+	if (git_vector_init(&log->entries, 0, NULL) < 0) {
+		git__free(log->ref_name);
+		git__free(log);
+		return -1;
+	}
+
+	*reflog = log;
+
+	return 0;
+}
+
+static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
+{
+	const char *ptr;
+	git_reflog_entry *entry;
+
+#define seek_forward(_increase) do { \
+	if (_increase >= buf_size) { \
+		giterr_set(GITERR_INVALID, "Ran out of data while parsing reflog"); \
+		goto fail; \
+	} \
+	buf += _increase; \
+	buf_size -= _increase; \
+	} while (0)
+
+	while (buf_size > GIT_REFLOG_SIZE_MIN) {
+		entry = git__calloc(1, sizeof(git_reflog_entry));
+		GITERR_CHECK_ALLOC(entry);
+
+		entry->committer = git__malloc(sizeof(git_signature));
+		GITERR_CHECK_ALLOC(entry->committer);
+
+		if (git_oid_fromstrn(&entry->oid_old, buf, GIT_OID_HEXSZ) < 0)
+			goto fail;
+		seek_forward(GIT_OID_HEXSZ + 1);
+
+		if (git_oid_fromstrn(&entry->oid_cur, buf, GIT_OID_HEXSZ) < 0)
+			goto fail;
+		seek_forward(GIT_OID_HEXSZ + 1);
+
+		ptr = buf;
+
+		/* Seek forward to the end of the signature. */
+		while (*buf && *buf != '\t' && *buf != '\n')
+			seek_forward(1);
+
+		if (git_signature__parse(entry->committer, &ptr, buf + 1, NULL, *buf) < 0)
+			goto fail;
+
+		if (*buf == '\t') {
+			/* We got a message. Read everything till we reach LF. */
+			seek_forward(1);
+			ptr = buf;
+
+			while (*buf && *buf != '\n')
+				seek_forward(1);
+
+			entry->msg = git__strndup(ptr, buf - ptr);
+			GITERR_CHECK_ALLOC(entry->msg);
+		} else
+			entry->msg = NULL;
+
+		while (*buf && *buf == '\n' && buf_size > 1)
+			seek_forward(1);
+
+		if (git_vector_insert(&log->entries, entry) < 0)
+			goto fail;
+	}
+
+	return 0;
+
+#undef seek_forward
+
+fail:
+	if (entry)
+		git_reflog_entry__free(entry);
+
+	return -1;
+}
+
+static int create_new_reflog_file(const char *filepath)
+{
+	int fd, error;
+
+	if ((error = git_futils_mkpath2file(filepath, GIT_REFLOG_DIR_MODE)) < 0)
+		return error;
+
+	if ((fd = p_open(filepath,
+			O_WRONLY | O_CREAT | O_TRUNC,
+			GIT_REFLOG_FILE_MODE)) < 0)
+		return -1;
+
+	return p_close(fd);
+}
+
+GIT_INLINE(int) retrieve_reflog_path(git_buf *path, git_repository *repo, const char *name)
+{
+	return git_buf_join_n(path, '/', 3, repo->path_repository, GIT_REFLOG_DIR, name);
+}
+
+static int refdb_reflog_fs__read(git_reflog **out, git_refdb_backend *_backend, const char *name)
+{
+	int error = -1;
+	git_buf log_path = GIT_BUF_INIT;
+	git_buf log_file = GIT_BUF_INIT;
+	git_reflog *log = NULL;
+	git_repository *repo;
+	refdb_fs_backend *backend;
+
+	assert(out && _backend && name);
+
+	backend = (refdb_fs_backend *) _backend;
+	repo = backend->repo;
+
+	if (reflog_alloc(&log, name) < 0)
+		return -1;
+
+	if (retrieve_reflog_path(&log_path, repo, name) < 0)
+		goto cleanup;
+
+	error = git_futils_readbuffer(&log_file, git_buf_cstr(&log_path));
+	if (error < 0 && error != GIT_ENOTFOUND)
+		goto cleanup;
+
+	if ((error == GIT_ENOTFOUND) &&
+		((error = create_new_reflog_file(git_buf_cstr(&log_path))) < 0))
+		goto cleanup;
+ 
+	if ((error = reflog_parse(log,
+		git_buf_cstr(&log_file), git_buf_len(&log_file))) < 0)
+		goto cleanup;
+
+	*out = log;
+	goto success;
+
+cleanup:
+	git_reflog_free(log);
+
+success:
+	git_buf_free(&log_file);
+	git_buf_free(&log_path);
+
+	return error;
+}
+
+static int serialize_reflog_entry(
+	git_buf *buf,
+	const git_oid *oid_old,
+	const git_oid *oid_new,
+	const git_signature *committer,
+	const char *msg)
+{
+	char raw_old[GIT_OID_HEXSZ+1];
+	char raw_new[GIT_OID_HEXSZ+1];
+
+	git_oid_tostr(raw_old, GIT_OID_HEXSZ+1, oid_old);
+	git_oid_tostr(raw_new, GIT_OID_HEXSZ+1, oid_new);
+
+	git_buf_clear(buf);
+
+	git_buf_puts(buf, raw_old);
+	git_buf_putc(buf, ' ');
+	git_buf_puts(buf, raw_new);
+
+	git_signature__writebuf(buf, " ", committer);
+
+	/* drop trailing LF */
+	git_buf_rtrim(buf);
+
+	if (msg) {
+		git_buf_putc(buf, '\t');
+		git_buf_puts(buf, msg);
+	}
+
+	git_buf_putc(buf, '\n');
+
+	return git_buf_oom(buf);
+}
+
+static int refdb_reflog_fs__write(git_refdb_backend *_backend, git_reflog *reflog)
+{
+	int error = -1;
+	unsigned int i;
+	git_reflog_entry *entry;
+	git_repository *repo;
+	refdb_fs_backend *backend;
+	git_buf log_path = GIT_BUF_INIT;
+	git_buf log = GIT_BUF_INIT;
+	git_filebuf fbuf = GIT_FILEBUF_INIT;
+
+	assert(_backend && reflog);
+
+	backend = (refdb_fs_backend *) _backend;
+	repo = backend->repo;
+
+	if (retrieve_reflog_path(&log_path, repo, reflog->ref_name) < 0)
+		return -1;
+
+	if (!git_path_isfile(git_buf_cstr(&log_path))) {
+		giterr_set(GITERR_INVALID,
+			"Log file for reference '%s' doesn't exist.", reflog->ref_name);
+		goto cleanup;
+	}
+
+	if ((error = git_filebuf_open(&fbuf, git_buf_cstr(&log_path), 0)) < 0)
+		goto cleanup;
+
+	git_vector_foreach(&reflog->entries, i, entry) {
+		if (serialize_reflog_entry(&log, &(entry->oid_old), &(entry->oid_cur), entry->committer, entry->msg) < 0)
+			goto cleanup;
+
+		if ((error = git_filebuf_write(&fbuf, log.ptr, log.size)) < 0)
+			goto cleanup;
+	}
+
+	error = git_filebuf_commit(&fbuf, GIT_REFLOG_FILE_MODE);
+	goto success;
+
+cleanup:
+	git_filebuf_cleanup(&fbuf);
+
+success:
+	git_buf_free(&log);
+	git_buf_free(&log_path);
+	return error;
+}
+
+static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_name, const char *new_name)
+{
+	int error = 0, fd;
+	git_buf old_path = GIT_BUF_INIT;
+	git_buf new_path = GIT_BUF_INIT;
+	git_buf temp_path = GIT_BUF_INIT;
+	git_buf normalized = GIT_BUF_INIT;
+	git_repository *repo;
+	refdb_fs_backend *backend;
+
+	assert(_backend && old_name && new_name);
+
+	backend = (refdb_fs_backend *) _backend;
+	repo = backend->repo;
+
+	if ((error = git_reference__normalize_name(
+		&normalized, new_name, GIT_REF_FORMAT_ALLOW_ONELEVEL)) < 0)
+			return error;
+
+	if (git_buf_joinpath(&temp_path, repo->path_repository, GIT_REFLOG_DIR) < 0)
+		return -1;
+
+	if (git_buf_joinpath(&old_path, git_buf_cstr(&temp_path), old_name) < 0)
+		return -1;
+
+	if (git_buf_joinpath(&new_path, git_buf_cstr(&temp_path), git_buf_cstr(&normalized)) < 0)
+		return -1;
+
+	/*
+	 * Move the reflog to a temporary place. This two-phase renaming is required
+	 * in order to cope with funny renaming use cases when one tries to move a reference
+	 * to a partially colliding namespace:
+	 *  - a/b -> a/b/c
+	 *  - a/b/c/d -> a/b/c
+	 */
+	if (git_buf_joinpath(&temp_path, git_buf_cstr(&temp_path), "temp_reflog") < 0)
+		return -1;
+
+	if ((fd = git_futils_mktmp(&temp_path, git_buf_cstr(&temp_path))) < 0) {
+		error = -1;
+		goto cleanup;
+	}
+
+	p_close(fd);
+
+	if (p_rename(git_buf_cstr(&old_path), git_buf_cstr(&temp_path)) < 0) {
+		giterr_set(GITERR_OS, "Failed to rename reflog for %s", new_name);
+		error = -1;
+		goto cleanup;
+	}
+
+	if (git_path_isdir(git_buf_cstr(&new_path)) && 
+		(git_futils_rmdir_r(git_buf_cstr(&new_path), NULL, GIT_RMDIR_SKIP_NONEMPTY) < 0)) {
+		error = -1;
+		goto cleanup;
+	}
+
+	if (git_futils_mkpath2file(git_buf_cstr(&new_path), GIT_REFLOG_DIR_MODE) < 0) {
+		error = -1;
+		goto cleanup;
+	}
+
+	if (p_rename(git_buf_cstr(&temp_path), git_buf_cstr(&new_path)) < 0) {
+		giterr_set(GITERR_OS, "Failed to rename reflog for %s", new_name);
+		error = -1;
+	}
+
+cleanup:
+	git_buf_free(&temp_path);
+	git_buf_free(&old_path);
+	git_buf_free(&new_path);
+	git_buf_free(&normalized);
+
+	return error;
+}
+
+static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name)
+{
+	int error;
+	git_buf path = GIT_BUF_INIT;
+
+	git_repository *repo;
+	refdb_fs_backend *backend;
+
+	assert(_backend && name);
+
+	backend = (refdb_fs_backend *) _backend;
+	repo = backend->repo;
+
+	error = retrieve_reflog_path(&path, repo, name);
+
+	if (!error && git_path_exists(path.ptr))
+		error = p_unlink(path.ptr);
+
+	git_buf_free(&path);
+
+	return error;
+
+}
+
 int git_refdb_backend_fs(
 	git_refdb_backend **backend_out,
 	git_repository *repository)
@@ -1114,6 +1455,10 @@ int git_refdb_backend_fs(
 	backend->parent.rename = &refdb_fs_backend__rename;
 	backend->parent.compress = &refdb_fs_backend__compress;
 	backend->parent.free = &refdb_fs_backend__free;
+	backend->parent.reflog_read = &refdb_reflog_fs__read;
+	backend->parent.reflog_write = &refdb_reflog_fs__write;
+	backend->parent.reflog_rename = &refdb_reflog_fs__rename;
+	backend->parent.reflog_delete = &refdb_reflog_fs__delete;
 
 	*backend_out = (git_refdb_backend *)backend;
 	return 0;
diff --git a/src/reflog.c b/src/reflog.c
index a6752f6..cebb87d 100644
--- a/src/reflog.c
+++ b/src/reflog.c
@@ -9,82 +9,16 @@
 #include "repository.h"
 #include "filebuf.h"
 #include "signature.h"
+#include "refdb.h"
 
-static int reflog_init(git_reflog **reflog, const git_reference *ref)
-{
-	git_reflog *log;
-
-	*reflog = NULL;
-
-	log = git__calloc(1, sizeof(git_reflog));
-	GITERR_CHECK_ALLOC(log);
-
-	log->ref_name = git__strdup(ref->name);
-	GITERR_CHECK_ALLOC(log->ref_name);
-
-	if (git_vector_init(&log->entries, 0, NULL) < 0) {
-		git__free(log->ref_name);
-		git__free(log);
-		return -1;
-	}
-
-	log->owner = git_reference_owner(ref);
-	*reflog = log;
+#include <git2/sys/refdb_backend.h>
 
-	return 0;
-}
-
-static int serialize_reflog_entry(
-	git_buf *buf,
-	const git_oid *oid_old,
-	const git_oid *oid_new,
-	const git_signature *committer,
-	const char *msg)
+git_reflog_entry *git_reflog_entry__alloc(void)
 {
-	char raw_old[GIT_OID_HEXSZ+1];
-	char raw_new[GIT_OID_HEXSZ+1];
-
-	git_oid_tostr(raw_old, GIT_OID_HEXSZ+1, oid_old);
-	git_oid_tostr(raw_new, GIT_OID_HEXSZ+1, oid_new);
-
-	git_buf_clear(buf);
-
-	git_buf_puts(buf, raw_old);
-	git_buf_putc(buf, ' ');
-	git_buf_puts(buf, raw_new);
-
-	git_signature__writebuf(buf, " ", committer);
-
-	/* drop trailing LF */
-	git_buf_rtrim(buf);
-
-	if (msg) {
-		git_buf_putc(buf, '\t');
-		git_buf_puts(buf, msg);
-	}
-
-	git_buf_putc(buf, '\n');
-
-	return git_buf_oom(buf);
-}
-
-static int reflog_entry_new(git_reflog_entry **entry)
-{
-	git_reflog_entry *e;
-
-	assert(entry);
-
-	e = git__malloc(sizeof(git_reflog_entry));
-	GITERR_CHECK_ALLOC(e);
-
-	memset(e, 0, sizeof(git_reflog_entry));
-
-	*entry = e;
-
-	return 0;
+	return git__calloc(1, sizeof(git_reflog_entry));
 }
 
-static void reflog_entry_free(git_reflog_entry *entry)
+void git_reflog_entry__free(git_reflog_entry *entry)
 {
 	git_signature_free(entry->committer);
 
@@ -92,75 +26,6 @@ static void reflog_entry_free(git_reflog_entry *entry)
 	git__free(entry);
 }
 
-static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
-{
-	const char *ptr;
-	git_reflog_entry *entry;
-
-#define seek_forward(_increase) do { \
-	if (_increase >= buf_size) { \
-		giterr_set(GITERR_INVALID, "Ran out of data while parsing reflog"); \
-		goto fail; \
-	} \
-	buf += _increase; \
-	buf_size -= _increase; \
-	} while (0)
-
-	while (buf_size > GIT_REFLOG_SIZE_MIN) {
-		if (reflog_entry_new(&entry) < 0)
-			return -1;
-
-		entry->committer = git__malloc(sizeof(git_signature));
-		GITERR_CHECK_ALLOC(entry->committer);
-
-		if (git_oid_fromstrn(&entry->oid_old, buf, GIT_OID_HEXSZ) < 0)
-			goto fail;
-		seek_forward(GIT_OID_HEXSZ + 1);
-
-		if (git_oid_fromstrn(&entry->oid_cur, buf, GIT_OID_HEXSZ) < 0)
-			goto fail;
-		seek_forward(GIT_OID_HEXSZ + 1);
-
-		ptr = buf;
-
-		/* Seek forward to the end of the signature. */
-		while (*buf && *buf != '\t' && *buf != '\n')
-			seek_forward(1);
-
-		if (git_signature__parse(entry->committer, &ptr, buf + 1, NULL, *buf) < 0)
-			goto fail;
-
-		if (*buf == '\t') {
-			/* We got a message. Read everything till we reach LF. */
-			seek_forward(1);
-			ptr = buf;
-
-			while (*buf && *buf != '\n')
-				seek_forward(1);
-
-			entry->msg = git__strndup(ptr, buf - ptr);
-			GITERR_CHECK_ALLOC(entry->msg);
-		} else
-			entry->msg = NULL;
-
-		while (*buf && *buf == '\n' && buf_size > 1)
-			seek_forward(1);
-
-		if (git_vector_insert(&log->entries, entry) < 0)
-			goto fail;
-	}
-
-	return 0;
-
-#undef seek_forward
-
-fail:
-	if (entry)
-		reflog_entry_free(entry);
-
-	return -1;
-}
-
 void git_reflog_free(git_reflog *reflog)
 {
 	size_t i;
@@ -169,10 +34,13 @@ void git_reflog_free(git_reflog *reflog)
 	if (reflog == NULL)
 		return;
 
+	if (reflog->db)
+		GIT_REFCOUNT_DEC(reflog->db, git_refdb__free);
+
 	for (i=0; i < reflog->entries.length; i++) {
 		entry = git_vector_get(&reflog->entries, i);
 
-		reflog_entry_free(entry);
+		git_reflog_entry__free(entry);
 	}
 
 	git_vector_free(&reflog->entries);
@@ -180,115 +48,30 @@ void git_reflog_free(git_reflog *reflog)
 	git__free(reflog);
 }
 
-static int retrieve_reflog_path(git_buf *path, const git_reference *ref)
+int git_reflog_read(git_reflog **reflog, git_repository *repo,  const char *name)
 {
-	return git_buf_join_n(path, '/', 3,
-		git_reference_owner(ref)->path_repository, GIT_REFLOG_DIR, ref->name);
-}
+	git_refdb *refdb;
+	int error;
 
-static int create_new_reflog_file(const char *filepath)
-{
-	int fd, error;
+	assert(reflog && repo && name);
 
-	if ((error = git_futils_mkpath2file(filepath, GIT_REFLOG_DIR_MODE)) < 0)
+	if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
 		return error;
 
-	if ((fd = p_open(filepath,
-			O_WRONLY | O_CREAT | O_TRUNC,
-			GIT_REFLOG_FILE_MODE)) < 0)
-		return -1;
-
-	return p_close(fd);
-}
-
-int git_reflog_read(git_reflog **reflog, const git_reference *ref)
-{
-	int error = -1;
-	git_buf log_path = GIT_BUF_INIT;
-	git_buf log_file = GIT_BUF_INIT;
-	git_reflog *log = NULL;
-
-	assert(reflog && ref);
-
-	*reflog = NULL;
-
-	if (reflog_init(&log, ref) < 0)
-		return -1;
-
-	if (retrieve_reflog_path(&log_path, ref) < 0)
-		goto cleanup;
-
-	error = git_futils_readbuffer(&log_file, git_buf_cstr(&log_path));
-	if (error < 0 && error != GIT_ENOTFOUND)
-		goto cleanup;
-
-	if ((error == GIT_ENOTFOUND) &&
-		((error = create_new_reflog_file(git_buf_cstr(&log_path))) < 0))
-		goto cleanup;
-
-	if ((error = reflog_parse(log,
-		git_buf_cstr(&log_file), git_buf_len(&log_file))) < 0)
-		goto cleanup;
-
-	*reflog = log;
-	goto success;
-
-cleanup:
-	git_reflog_free(log);
-
-success:
-	git_buf_free(&log_file);
-	git_buf_free(&log_path);
-
-	return error;
+	return git_refdb_reflog_read(reflog, refdb, name);
 }
 
 int git_reflog_write(git_reflog *reflog)
 {
-	int error = -1;
-	unsigned int i;
-	git_reflog_entry *entry;
-	git_buf log_path = GIT_BUF_INIT;
-	git_buf log = GIT_BUF_INIT;
-	git_filebuf fbuf = GIT_FILEBUF_INIT;
+	git_refdb *db;
 
-	assert(reflog);
+	assert(reflog && reflog->db);
 
-	if (git_buf_join_n(&log_path, '/', 3,
-		git_repository_path(reflog->owner), GIT_REFLOG_DIR, reflog->ref_name) < 0)
-		return -1;
-
-	if (!git_path_isfile(git_buf_cstr(&log_path))) {
-		giterr_set(GITERR_INVALID,
-			"Log file for reference '%s' doesn't exist.", reflog->ref_name);
-		goto cleanup;
-	}
-
-	if ((error = git_filebuf_open(&fbuf, git_buf_cstr(&log_path), 0)) < 0)
-		goto cleanup;
-
-	git_vector_foreach(&reflog->entries, i, entry) {
-		if (serialize_reflog_entry(&log, &(entry->oid_old), &(entry->oid_cur), entry->committer, entry->msg) < 0)
-			goto cleanup;
-
-		if ((error = git_filebuf_write(&fbuf, log.ptr, log.size)) < 0)
-			goto cleanup;
-	}
-
-	error = git_filebuf_commit(&fbuf, GIT_REFLOG_FILE_MODE);
-	goto success;
-
-cleanup:
-	git_filebuf_cleanup(&fbuf);
-
-success:
-	git_buf_free(&log);
-	git_buf_free(&log_path);
-	return error;
+	db = reflog->db;
+	return db->backend->reflog_write(db->backend, reflog);
 }
 
-int git_reflog_append(git_reflog *reflog, const git_oid *new_oid,
-				const git_signature *committer, const char *msg)
+int git_reflog_append(git_reflog *reflog, const git_oid *new_oid, const git_signature *committer, const char *msg)
 {
 	git_reflog_entry *entry;
 	const git_reflog_entry *previous;
@@ -296,8 +79,8 @@ int git_reflog_append(git_reflog *reflog, const git_oid *new_oid,
 
 	assert(reflog && new_oid && committer);
 
-	if (reflog_entry_new(&entry) < 0)
-		return -1;
+	entry = git__calloc(1, sizeof(git_reflog_entry));
+	GITERR_CHECK_ALLOC(entry);
 
 	if ((entry->committer = git_signature_dup(committer)) == NULL)
 		goto cleanup;
@@ -333,94 +116,30 @@ int git_reflog_append(git_reflog *reflog, const git_oid *new_oid,
 	return 0;
 
 cleanup:
-	reflog_entry_free(entry);
+	git_reflog_entry__free(entry);
 	return -1;
 }
 
-int git_reflog_rename(git_reference *ref, const char *new_name)
+int git_reflog_rename(git_repository *repo, const char *old_name, const char *new_name)
 {
-	int error = 0, fd;
-	git_buf old_path = GIT_BUF_INIT;
-	git_buf new_path = GIT_BUF_INIT;
-	git_buf temp_path = GIT_BUF_INIT;
-	git_buf normalized = GIT_BUF_INIT;
-
-	assert(ref && new_name);
-
-	if ((error = git_reference__normalize_name(
-		&normalized, new_name, GIT_REF_FORMAT_ALLOW_ONELEVEL)) < 0)
-			return error;
-
-	if (git_buf_joinpath(&temp_path, git_reference_owner(ref)->path_repository, GIT_REFLOG_DIR) < 0)
-		return -1;
-
-	if (git_buf_joinpath(&old_path, git_buf_cstr(&temp_path), ref->name) < 0)
-		return -1;
-
-	if (git_buf_joinpath(&new_path, git_buf_cstr(&temp_path), git_buf_cstr(&normalized)) < 0)
-		return -1;
+	git_refdb *refdb;
+	int error;
 
-	/*
-	 * Move the reflog to a temporary place. This two-phase renaming is required
-	 * in order to cope with funny renaming use cases when one tries to move a reference
-	 * to a partially colliding namespace:
-	 *  - a/b -> a/b/c
-	 *  - a/b/c/d -> a/b/c
-	 */
-	if (git_buf_joinpath(&temp_path, git_buf_cstr(&temp_path), "temp_reflog") < 0)
+	if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
 		return -1;
 
-	if ((fd = git_futils_mktmp(&temp_path, git_buf_cstr(&temp_path))) < 0) {
-		error = -1;
-		goto cleanup;
-	}
-
-	p_close(fd);
-
-	if (p_rename(git_buf_cstr(&old_path), git_buf_cstr(&temp_path)) < 0) {
-		giterr_set(GITERR_OS, "Failed to rename reflog for %s", new_name);
-		error = -1;
-		goto cleanup;
-	}
-
-	if (git_path_isdir(git_buf_cstr(&new_path)) && 
-		(git_futils_rmdir_r(git_buf_cstr(&new_path), NULL, GIT_RMDIR_SKIP_NONEMPTY) < 0)) {
-		error = -1;
-		goto cleanup;
-	}
-
-	if (git_futils_mkpath2file(git_buf_cstr(&new_path), GIT_REFLOG_DIR_MODE) < 0) {
-		error = -1;
-		goto cleanup;
-	}
-
-	if (p_rename(git_buf_cstr(&temp_path), git_buf_cstr(&new_path)) < 0) {
-		giterr_set(GITERR_OS, "Failed to rename reflog for %s", new_name);
-		error = -1;
-	}
-
-cleanup:
-	git_buf_free(&temp_path);
-	git_buf_free(&old_path);
-	git_buf_free(&new_path);
-	git_buf_free(&normalized);
-
-	return error;
+	return refdb->backend->reflog_rename(refdb->backend, old_name, new_name);
 }
 
-int git_reflog_delete(git_reference *ref)
+int git_reflog_delete(git_repository *repo, const char *name)
 {
+	git_refdb *refdb;
 	int error;
-	git_buf path = GIT_BUF_INIT;
-
-	error = retrieve_reflog_path(&path, ref);
 
-	if (!error && git_path_exists(path.ptr))
-		error = p_unlink(path.ptr);
-
-	git_buf_free(&path);
+	if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
+		return -1;
 
-	return error;
+	return refdb->backend->reflog_delete(refdb->backend, name);
 }
 
 size_t git_reflog_entrycount(git_reflog *reflog)
@@ -429,11 +148,6 @@ size_t git_reflog_entrycount(git_reflog *reflog)
 	return reflog->entries.length;
 }
 
-GIT_INLINE(size_t) reflog_inverse_index(size_t idx, size_t total)
-{
-	return (total - 1) - idx;
-}
-
 const git_reflog_entry * git_reflog_entry_byindex(git_reflog *reflog, size_t idx)
 {
 	assert(reflog);
@@ -469,26 +183,21 @@ const char * git_reflog_entry_message(const git_reflog_entry *entry)
 	return entry->msg;
 }
 
-int git_reflog_drop(
-	git_reflog *reflog,
-	size_t idx,
-	int rewrite_previous_entry)
+int git_reflog_drop(git_reflog *reflog, size_t idx, int rewrite_previous_entry)
 {
 	size_t entrycount;
 	git_reflog_entry *entry, *previous;
 
-	assert(reflog);
-
 	entrycount = git_reflog_entrycount(reflog);
 
 	entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx);
 
 	if (entry == NULL) {
-		giterr_set(GITERR_REFERENCE, "No reflog entry at index %"PRIuZ, idx);
+		giterr_set(GITERR_REFERENCE, "No reflog entry at index "PRIuZ, idx);
 		return GIT_ENOTFOUND;
 	}
 
-	reflog_entry_free(entry);
+	git_reflog_entry__free(entry);
 
 	if (git_vector_remove(
 			&reflog->entries, reflog_inverse_index(idx, entrycount)) < 0)
@@ -521,3 +230,22 @@ int git_reflog_drop(
 
 	return 0;
 }
+
+int git_reflog_append_to(git_repository *repo, const char *name, const git_oid *id,
+			 const git_signature *committer, const char *msg)
+{
+	int error;
+	git_reflog *reflog;
+
+	if ((error = git_reflog_read(&reflog, repo, name)) < 0)
+		return error;
+
+	if ((error = git_reflog_append(reflog, id, committer, msg)) < 0)
+		goto cleanup;
+
+	error = git_reflog_write(reflog);
+
+cleanup:
+	git_reflog_free(reflog);
+	return error;
+}
diff --git a/src/reflog.h b/src/reflog.h
index 9444ebd..2d31ae4 100644
--- a/src/reflog.h
+++ b/src/reflog.h
@@ -27,9 +27,14 @@ struct git_reflog_entry {
 };
 
 struct git_reflog {
+	git_refdb *db;
 	char *ref_name;
-	git_repository *owner;
 	git_vector entries;
 };
 
+GIT_INLINE(size_t) reflog_inverse_index(size_t idx, size_t total)
+{
+	return (total - 1) - idx;
+}
+
 #endif /* INCLUDE_reflog_h__ */
diff --git a/src/refs.c b/src/refs.c
index 7530aee..269c5f5 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -483,7 +483,7 @@ int git_reference_rename(
 	if (reference_has_log < 0)
 		return reference_has_log;
 
-	if (reference_has_log && (error = git_reflog_rename(ref, new_name)) < 0)
+	if (reference_has_log && (error = git_reflog_rename(git_reference_owner(ref), git_reference_name(ref), new_name)) < 0)
 		return error;
 
 	return 0;
diff --git a/src/revparse.c b/src/revparse.c
index e470a95..c120b46 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -160,7 +160,7 @@ static int retrieve_previously_checked_out_branch_or_revision(git_object **out, 
 	if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0)
 		goto cleanup;
 
-	if (git_reflog_read(&reflog, ref) < 0)
+	if (git_reflog_read(&reflog, repo, GIT_HEAD_FILE) < 0)
 		goto cleanup;
 
 	numentries  = git_reflog_entrycount(reflog);
@@ -208,7 +208,7 @@ static int retrieve_oid_from_reflog(git_oid *oid, git_reference *ref, size_t ide
 	const git_reflog_entry *entry;
 	bool search_by_pos = (identifier <= 100000000);
 
-	if (git_reflog_read(&reflog, ref) < 0)
+	if (git_reflog_read(&reflog, git_reference_owner(ref), git_reference_name(ref)) < 0)
 		return -1;
 
 	numentries = git_reflog_entrycount(reflog);
diff --git a/src/stash.c b/src/stash.c
index 6fa74ac..083c2a4 100644
--- a/src/stash.c
+++ b/src/stash.c
@@ -411,14 +411,16 @@ static int update_reflog(
 	const git_signature *stasher,
 	const char *message)
 {
-	git_reference *stash = NULL;
+	git_reference *stash;
 	git_reflog *reflog = NULL;
 	int error;
 
 	if ((error = git_reference_create(&stash, repo, GIT_REFS_STASH_FILE, w_commit_oid, 1)) < 0)
 		goto cleanup;
 
-	if ((error = git_reflog_read(&reflog, stash)) < 0)
+	git_reference_free(stash);
+
+	if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE) < 0))
 		goto cleanup;
 
 	if ((error = git_reflog_append(reflog, w_commit_oid, stasher, message)) < 0)
@@ -428,7 +430,6 @@ static int update_reflog(
 		goto cleanup;
 
 cleanup:
-	git_reference_free(stash);
 	git_reflog_free(reflog);
 	return error;
 }
@@ -574,7 +575,7 @@ int git_stash_foreach(
 	if (error < 0)
 		goto cleanup;
 
-	if ((error = git_reflog_read(&reflog, stash)) < 0)
+	if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0)
 		goto cleanup;
 
 	max = git_reflog_entrycount(reflog);
@@ -608,7 +609,7 @@ int git_stash_drop(
 	if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0)
 		return error;
 
-	if ((error = git_reflog_read(&reflog, stash)) < 0)
+	if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0)
 		goto cleanup;
 
 	max = git_reflog_entrycount(reflog);
diff --git a/tests-clar/refs/reflog/drop.c b/tests-clar/refs/reflog/drop.c
index 21cc847..916bd99 100644
--- a/tests-clar/refs/reflog/drop.c
+++ b/tests-clar/refs/reflog/drop.c
@@ -8,15 +8,10 @@ static size_t entrycount;
 
 void test_refs_reflog_drop__initialize(void)
 {
-	git_reference *ref;
-
 	g_repo = cl_git_sandbox_init("testrepo.git");
-	cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD"));
 
-	git_reflog_read(&g_reflog, ref);
+	git_reflog_read(&g_reflog, g_repo, "HEAD");
 	entrycount = git_reflog_entrycount(g_reflog);
-
-	git_reference_free(ref);
 }
 
 void test_refs_reflog_drop__cleanup(void)
@@ -106,19 +101,15 @@ void test_refs_reflog_drop__can_drop_all_the_entries(void)
 
 void test_refs_reflog_drop__can_persist_deletion_on_disk(void)
 {
-	git_reference *ref;
-
 	cl_assert(entrycount > 2);
 
-	cl_git_pass(git_reference_lookup(&ref, g_repo, g_reflog->ref_name));
 	cl_git_pass(git_reflog_drop(g_reflog, 0, 1));
 	cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
 	cl_git_pass(git_reflog_write(g_reflog));
 
 	git_reflog_free(g_reflog);
 
-	git_reflog_read(&g_reflog, ref);
-	git_reference_free(ref);
+	git_reflog_read(&g_reflog, g_repo, "HEAD");
 
 	cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
 }
diff --git a/tests-clar/refs/reflog/reflog.c b/tests-clar/refs/reflog/reflog.c
index 095cabf..bcd2242 100644
--- a/tests-clar/refs/reflog/reflog.c
+++ b/tests-clar/refs/reflog/reflog.c
@@ -13,7 +13,7 @@ static git_repository *g_repo;
 
 
 // helpers
-static void assert_signature(git_signature *expected, git_signature *actual)
+static void assert_signature(const git_signature *expected, const git_signature *actual)
 {
 	cl_assert(actual);
 	cl_assert_equal_s(expected->name, actual->name);
@@ -34,30 +34,13 @@ void test_refs_reflog_reflog__cleanup(void)
    cl_git_sandbox_cleanup();
 }
 
-void test_refs_reflog_reflog__append_then_read(void)
+static void assert_appends(const git_signature *committer, const git_oid *oid)
 {
-   // write a reflog for a given reference and ensure it can be read back
 	git_repository *repo2;
-	git_reference *ref, *lookedup_ref;
-	git_oid oid;
-	git_signature *committer;
+	git_reference *lookedup_ref;
 	git_reflog *reflog;
 	const git_reflog_entry *entry;
 
-	/* Create a new branch pointing at the HEAD */
-	git_oid_fromstr(&oid, current_master_tip);
-	cl_git_pass(git_reference_create(&ref, g_repo, new_ref, &oid, 0));
-
-	cl_git_pass(git_signature_now(&committer, "foo", "foo@bar"));
-
-	cl_git_pass(git_reflog_read(&reflog, ref));
-
-	cl_git_fail(git_reflog_append(reflog, &oid, committer, "no inner\nnewline"));
-	cl_git_pass(git_reflog_append(reflog, &oid, committer, NULL));
-	cl_git_pass(git_reflog_append(reflog, &oid, committer, commit_msg "\n"));
-	cl_git_pass(git_reflog_write(reflog));
-	git_reflog_free(reflog);
-
 	/* Reopen a new instance of the repository */
 	cl_git_pass(git_repository_open(&repo2, "testrepo.git"));
 
@@ -65,29 +48,78 @@ void test_refs_reflog_reflog__append_then_read(void)
 	cl_git_pass(git_reference_lookup(&lookedup_ref, repo2, new_ref));
 
 	/* Read and parse the reflog for this branch */
-	cl_git_pass(git_reflog_read(&reflog, lookedup_ref));
+	cl_git_pass(git_reflog_read(&reflog, repo2, new_ref));
 	cl_assert_equal_i(2, (int)git_reflog_entrycount(reflog));
 
 	entry = git_reflog_entry_byindex(reflog, 1);
 	assert_signature(committer, entry->committer);
 	cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) == 0);
-	cl_assert(git_oid_cmp(&oid, &entry->oid_cur) == 0);
+	cl_assert(git_oid_cmp(oid, &entry->oid_cur) == 0);
 	cl_assert(entry->msg == NULL);
 
 	entry = git_reflog_entry_byindex(reflog, 0);
 	assert_signature(committer, entry->committer);
-	cl_assert(git_oid_cmp(&oid, &entry->oid_old) == 0);
-	cl_assert(git_oid_cmp(&oid, &entry->oid_cur) == 0);
+	cl_assert(git_oid_cmp(oid, &entry->oid_old) == 0);
+	cl_assert(git_oid_cmp(oid, &entry->oid_cur) == 0);
 	cl_assert_equal_s(commit_msg, entry->msg);
 
-	git_signature_free(committer);
 	git_reflog_free(reflog);
 	git_repository_free(repo2);
 
-	git_reference_free(ref);
 	git_reference_free(lookedup_ref);
 }
 
+void test_refs_reflog_reflog__append_then_read(void)
+{
+	/* write a reflog for a given reference and ensure it can be read back */
+	git_reference *ref;
+	git_oid oid;
+	git_signature *committer;
+	git_reflog *reflog;
+
+	/* Create a new branch pointing at the HEAD */
+	git_oid_fromstr(&oid, current_master_tip);
+	cl_git_pass(git_reference_create(&ref, g_repo, new_ref, &oid, 0));
+	git_reference_free(ref);
+
+	cl_git_pass(git_signature_now(&committer, "foo", "foo@bar"));
+
+	cl_git_pass(git_reflog_read(&reflog, g_repo, new_ref));
+
+	cl_git_fail(git_reflog_append(reflog, &oid, committer, "no inner\nnewline"));
+	cl_git_pass(git_reflog_append(reflog, &oid, committer, NULL));
+	cl_git_pass(git_reflog_append(reflog, &oid, committer, commit_msg "\n"));
+	cl_git_pass(git_reflog_write(reflog));
+	git_reflog_free(reflog);
+
+	assert_appends(committer, &oid);
+
+	git_signature_free(committer);
+}
+
+void test_refs_reflog_reflog__append_to_then_read(void)
+{
+	/* write a reflog for a given reference and ensure it can be read back */
+	git_reference *ref;
+	git_oid oid;
+	git_signature *committer;
+
+	/* Create a new branch pointing at the HEAD */
+	git_oid_fromstr(&oid, current_master_tip);
+	cl_git_pass(git_reference_create(&ref, g_repo, new_ref, &oid, 0));
+	git_reference_free(ref);
+
+	cl_git_pass(git_signature_now(&committer, "foo", "foo@bar"));
+
+	cl_git_fail(git_reflog_append_to(g_repo, new_ref, &oid, committer, "no inner\nnewline"));
+	cl_git_pass(git_reflog_append_to(g_repo, new_ref, &oid, committer, NULL));
+	cl_git_pass(git_reflog_append_to(g_repo, new_ref, &oid, committer, commit_msg "\n"));
+
+	assert_appends(committer, &oid);
+
+	git_signature_free(committer);
+}
+
 void test_refs_reflog_reflog__renaming_the_reference_moves_the_reflog(void)
 {
 	git_reference *master, *new_master;
@@ -133,21 +165,18 @@ void test_refs_reflog_reflog__reference_has_reflog(void)
 
 void test_refs_reflog_reflog__reading_the_reflog_from_a_reference_with_no_log_returns_an_empty_one(void)
 {
-	git_reference *subtrees;
 	git_reflog *reflog;
+	const char *refname = "refs/heads/subtrees";
 	git_buf subtrees_log_path = GIT_BUF_INIT;
 
-	cl_git_pass(git_reference_lookup(&subtrees, g_repo, "refs/heads/subtrees"));
-
-	git_buf_join_n(&subtrees_log_path, '/', 3, git_repository_path(g_repo), GIT_REFLOG_DIR, git_reference_name(subtrees));
+	git_buf_join_n(&subtrees_log_path, '/', 3, git_repository_path(g_repo), GIT_REFLOG_DIR, refname);
 	cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&subtrees_log_path)));
 
-	cl_git_pass(git_reflog_read(&reflog, subtrees));
+	cl_git_pass(git_reflog_read(&reflog, g_repo, refname));
 
 	cl_assert_equal_i(0, (int)git_reflog_entrycount(reflog));
 
 	git_reflog_free(reflog);
-	git_reference_free(subtrees);
 	git_buf_free(&subtrees_log_path);
 }
 
@@ -158,7 +187,7 @@ void test_refs_reflog_reflog__cannot_write_a_moved_reflog(void)
 	git_reflog *reflog;
 
 	cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master"));
-	cl_git_pass(git_reflog_read(&reflog, master));
+	cl_git_pass(git_reflog_read(&reflog, g_repo, "refs/heads/master"));
 
 	cl_git_pass(git_reflog_write(reflog));
 
@@ -175,12 +204,6 @@ void test_refs_reflog_reflog__cannot_write_a_moved_reflog(void)
 
 void test_refs_reflog_reflog__renaming_with_an_invalid_name_returns_EINVALIDSPEC(void)
 {
-	git_reference *master;
-
-	cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master"));
-
 	cl_assert_equal_i(GIT_EINVALIDSPEC,
-		git_reflog_rename(master, "refs/heads/Inv@{id"));
-
-	git_reference_free(master);
+			  git_reflog_rename(g_repo, "refs/heads/master", "refs/heads/Inv@{id"));
 }
diff --git a/tests-clar/stash/drop.c b/tests-clar/stash/drop.c
index 59413f0..63ff037 100644
--- a/tests-clar/stash/drop.c
+++ b/tests-clar/stash/drop.c
@@ -102,7 +102,7 @@ void test_stash_drop__dropping_an_entry_rewrites_reflog_history(void)
 
 	cl_git_pass(git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE));
 
-	cl_git_pass(git_reflog_read(&reflog, stash));
+	cl_git_pass(git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE));
 	entry = git_reflog_entry_byindex(reflog, 1);
 
 	git_oid_cpy(&oid, git_reflog_entry_id_old(entry));
@@ -112,7 +112,7 @@ void test_stash_drop__dropping_an_entry_rewrites_reflog_history(void)
 
 	cl_git_pass(git_stash_drop(repo, 1));
 
-	cl_git_pass(git_reflog_read(&reflog, stash));
+	cl_git_pass(git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE));
 	entry = git_reflog_entry_byindex(reflog, 0);
 
 	cl_assert_equal_i(0, git_oid_cmp(&oid, git_reflog_entry_id_old(entry)));