Commit 43b67d496b87a9c6ce38a500b7f3121bf136942e

Vicent Marti 2012-07-27T18:39:40

Merge remote-tracking branch 'nulltoken/topic/reflog-delete' into development

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
diff --git a/include/git2/reflog.h b/include/git2/reflog.h
index 8acba34..a314f94 100644
--- a/include/git2/reflog.h
+++ b/include/git2/reflog.h
@@ -23,6 +23,10 @@ GIT_BEGIN_DECL
 /**
  * Read the reflog for the given reference
  *
+ * If there is no reflog file for the given
+ * reference yet, an empty reflog object will
+ * be returned.
+ *
  * The reflog must be freed manually by using
  * git_reflog_free().
  *
@@ -33,22 +37,26 @@ GIT_BEGIN_DECL
 GIT_EXTERN(int) git_reflog_read(git_reflog **reflog, git_reference *ref);
 
 /**
- * Write a new reflog for the given reference
- *
- * If there is no reflog file for the given
- * reference yet, it will be created.
+ * Write an existing in-memory reflog object back to disk
+ * using an atomic file lock.
  *
- * `oid_old` may be NULL in case it's a new reference.
+ * @param reflog an existing reflog object
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_reflog_write(git_reflog *reflog);
+
+/**
+ * Add a new entry to the reflog.
  *
  * `msg` is optional and can be NULL.
  *
- * @param ref the changed reference
- * @param oid_old the OID the reference was pointing to
+ * @param reflog an existing reflog object
+ * @param new_oid 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_write(git_reference *ref, const git_oid *oid_old, const git_signature *committer, const char *msg);
+GIT_EXTERN(int) git_reflog_append(git_reflog *reflog, const git_oid *new_oid, const git_signature *committer, const char *msg);
 
 /**
  * Rename the reflog for the given reference
@@ -87,6 +95,26 @@ GIT_EXTERN(unsigned int) git_reflog_entrycount(git_reflog *reflog);
 GIT_EXTERN(const git_reflog_entry *) git_reflog_entry_byindex(git_reflog *reflog, unsigned int idx);
 
 /**
+ * Remove an entry from the reflog by its index
+ *
+ * To ensure there's no gap in the log history, set the `rewrite_previosu_entry` to 1.
+ * When deleting entry `n`, member old_oid of entry `n-1` (if any) will be updated with
+ * the value of memeber new_oid of entry `n+1`.
+ *
+ * @param reflog a previously loaded reflog.
+ *
+ * @param idx the position of the entry to remove.
+ *
+ * @param rewrite_previous_entry 1 to rewrite the history; 0 otherwise.
+ *
+ * @return 0 on success or an error code.
+ */
+GIT_EXTERN(int) git_reflog_entry_drop(
+	git_reflog *reflog,
+	unsigned int idx,
+	int rewrite_previous_entry);
+
+/**
  * Get the old oid
  *
  * @param entry a reflog entry
diff --git a/src/reflog.c b/src/reflog.c
index 004ba93..f841b21 100644
--- a/src/reflog.c
+++ b/src/reflog.c
@@ -28,66 +28,68 @@ static int reflog_init(git_reflog **reflog, git_reference *ref)
 		return -1;
 	}
 
+	log->owner = git_reference_owner(ref);
 	*reflog = log;
 
 	return 0;
 }
 
-static int reflog_write(const char *log_path, const char *oid_old,
-			const char *oid_new, const git_signature *committer,
-			const char *msg)
+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)
 {
-	int error;
-	git_buf log = GIT_BUF_INIT;
-	git_filebuf fbuf = GIT_FILEBUF_INIT;
-	bool trailing_newline = false;
+	char raw_old[GIT_OID_HEXSZ+1];
+	char raw_new[GIT_OID_HEXSZ+1];
 
-	assert(log_path && oid_old && oid_new && committer);
+	git_oid_tostr(raw_old, GIT_OID_HEXSZ+1, oid_old);
+	git_oid_tostr(raw_new, GIT_OID_HEXSZ+1, oid_new);
 
-	if (msg) {
-		const char *newline = strchr(msg, '\n');
-		if (newline) {
-			if (*(newline + 1) == '\0')
-				trailing_newline = true;
-			else {
-				giterr_set(GITERR_INVALID, "Reflog message cannot contain newline");
-				return -1;
-			}
-		}
-	}
+	git_buf_clear(buf);
 
-	git_buf_puts(&log, oid_old);
-	git_buf_putc(&log, ' ');
+	git_buf_puts(buf, raw_old);
+	git_buf_putc(buf, ' ');
+	git_buf_puts(buf, raw_new);
 
-	git_buf_puts(&log, oid_new);
+	git_signature__writebuf(buf, " ", committer);
 
-	git_signature__writebuf(&log, " ", committer);
-	git_buf_truncate(&log, log.size - 1); /* drop LF */
+	/* drop trailing LF */
+	git_buf_rtrim(buf);
 
 	if (msg) {
-		git_buf_putc(&log, '\t');
-		git_buf_puts(&log, msg);
+		git_buf_putc(buf, '\t');
+		git_buf_puts(buf, msg);
 	}
 
-	if (!trailing_newline)
-		git_buf_putc(&log, '\n');
+	git_buf_putc(buf, '\n');
 
-	if (git_buf_oom(&log)) {
-		git_buf_free(&log);
-		return -1;
-	}
+	return git_buf_oom(buf);
+}
 
-	error = git_filebuf_open(&fbuf, log_path, GIT_FILEBUF_APPEND);
-	if (!error) {
-		if ((error = git_filebuf_write(&fbuf, log.ptr, log.size)) < 0)
-			git_filebuf_cleanup(&fbuf);
-		else
-			error = git_filebuf_commit(&fbuf, GIT_REFLOG_FILE_MODE);
-	}
+static int reflog_entry_new(git_reflog_entry **entry)
+{
+	git_reflog_entry *e;
 
-	git_buf_free(&log);
+	assert(entry);
 
-	return error;
+	e = git__malloc(sizeof(git_reflog_entry));
+	GITERR_CHECK_ALLOC(e);
+
+	memset(e, 0, sizeof(git_reflog_entry));
+
+	*entry = e;
+
+	return 0;
+}
+
+static void reflog_entry_free(git_reflog_entry *entry)
+{
+	git_signature_free(entry->committer);
+
+	git__free(entry->msg);
+	git__free(entry);
 }
 
 static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
@@ -105,8 +107,8 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
 	} while (0)
 
 	while (buf_size > GIT_REFLOG_SIZE_MIN) {
-		entry = git__malloc(sizeof(git_reflog_entry));
-		GITERR_CHECK_ALLOC(entry);
+		if (reflog_entry_new(&entry) < 0)
+			return -1;
 
 		entry->committer = git__malloc(sizeof(git_signature));
 		GITERR_CHECK_ALLOC(entry->committer);
@@ -153,10 +155,9 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
 #undef seek_forward
 
 fail:
-	if (entry) {
-		git__free(entry->committer);
-		git__free(entry);
-	}
+	if (entry)
+		reflog_entry_free(entry);
+
 	return -1;
 }
 
@@ -168,10 +169,7 @@ void git_reflog_free(git_reflog *reflog)
 	for (i=0; i < reflog->entries.length; i++) {
 		entry = git_vector_get(&reflog->entries, i);
 
-		git_signature_free(entry->committer);
-
-		git__free(entry->msg);
-		git__free(entry);
+		reflog_entry_free(entry);
 	}
 
 	git_vector_free(&reflog->entries);
@@ -179,6 +177,24 @@ void git_reflog_free(git_reflog *reflog)
 	git__free(reflog);
 }
 
+static int retrieve_reflog_path(git_buf *path, git_reference *ref)
+{
+	return git_buf_join_n(path, '/', 3,
+		git_reference_owner(ref)->path_repository, GIT_REFLOG_DIR, ref->name);
+}
+
+int create_new_reflog_file(const char *filepath)
+{
+	int fd;
+
+	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, git_reference *ref)
 {
 	int error;
@@ -188,83 +204,136 @@ int git_reflog_read(git_reflog **reflog, git_reference *ref)
 
 	*reflog = NULL;
 
+	assert(reflog && ref);
+
 	if (reflog_init(&log, ref) < 0)
 		return -1;
 
-	error = git_buf_join_n(&log_path, '/', 3,
-		ref->owner->path_repository, GIT_REFLOG_DIR, ref->name);
+	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)
-		error = git_futils_readbuffer(&log_file, log_path.ptr);
+	if ((error = reflog_parse(log,
+		git_buf_cstr(&log_file), git_buf_len(&log_file))) < 0)
+		goto cleanup;
 
-	if (!error)
-		error = reflog_parse(log, log_file.ptr, log_file.size);
+	*reflog = log;
+	goto success;
 
-	if (!error)
-		*reflog = log;
-	else
-		git_reflog_free(log);
+cleanup:
+	git_reflog_free(log);
 
+success:
 	git_buf_free(&log_file);
 	git_buf_free(&log_path);
 
 	return error;
 }
 
-int git_reflog_write(git_reference *ref, const git_oid *oid_old,
-				const git_signature *committer, const char *msg)
+int git_reflog_write(git_reflog *reflog)
 {
-	int error;
-	char old[GIT_OID_HEXSZ+1];
-	char new[GIT_OID_HEXSZ+1];
+	int error = -1;
+	unsigned int i;
+	git_reflog_entry *entry;
 	git_buf log_path = GIT_BUF_INIT;
-	git_reference *r;
-	const git_oid *oid;
+	git_buf log = GIT_BUF_INIT;
+	git_filebuf fbuf = GIT_FILEBUF_INIT;
+
+	assert(reflog);
 
-	if ((error = git_reference_resolve(&r, ref)) < 0)
-		return error;
 
-	oid = git_reference_oid(r);
-	if (oid == NULL) {
-		giterr_set(GITERR_REFERENCE,
-			"Failed to write reflog. Cannot resolve reference `%s`", r->name);
-		git_reference_free(r);
+	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;
+}
 
-	git_oid_tostr(new, GIT_OID_HEXSZ+1, oid);
+int git_reflog_append(git_reflog *reflog, const git_oid *new_oid,
+				const git_signature *committer, const char *msg)
+{
+	int count;
+	git_reflog_entry *entry;
+	const char *newline;
 
-	git_reference_free(r);
+	assert(reflog && new_oid && committer);
 
-	error = git_buf_join_n(&log_path, '/', 3,
-		ref->owner->path_repository, GIT_REFLOG_DIR, ref->name);
-	if (error < 0)
+	if (reflog_entry_new(&entry) < 0)
+		return -1;
+
+	if ((entry->committer = git_signature_dup(committer)) == NULL)
 		goto cleanup;
 
-	if (git_path_exists(log_path.ptr) == false) {
-		error = git_futils_mkpath2file(log_path.ptr, GIT_REFLOG_DIR_MODE);
-	} else if (git_path_isfile(log_path.ptr) == false) {
-		giterr_set(GITERR_REFERENCE,
-			"Failed to write reflog. `%s` is directory", log_path.ptr);
-		error = -1;
-	} else if (oid_old == NULL) {
-		giterr_set(GITERR_REFERENCE,
-			"Failed to write reflog. Old OID cannot be NULL for existing reference");
-		error = -1;
+	if (msg != NULL) {
+		if ((entry->msg = git__strdup(msg)) == NULL)
+			goto cleanup;
+
+		newline = strchr(msg, '\n');
+
+		if (newline) {
+			if (newline[1] != '\0') {
+				giterr_set(GITERR_INVALID, "Reflog message cannot contain newline");
+				goto cleanup;
+			}
+
+			entry->msg[newline - msg] = '\0';
+		}
 	}
-	if (error < 0)
-		goto cleanup;
 
-	if (oid_old)
-		git_oid_tostr(old, sizeof(old), oid_old);
-	else
-		p_snprintf(old, sizeof(old), "%0*d", GIT_OID_HEXSZ, 0);
+	count = git_reflog_entrycount(reflog);
+
+	if (count == 0)
+		git_oid_fromstr(&entry->oid_old, GIT_OID_HEX_ZERO);
+	else {
+		const git_reflog_entry *previous;
 
-	error = reflog_write(log_path.ptr, old, new, committer, msg);
+		previous = git_reflog_entry_byindex(reflog, count -1);
+		git_oid_cpy(&entry->oid_old, &previous->oid_cur);
+	}
+
+	git_oid_cpy(&entry->oid_cur, new_oid);
+
+	if (git_vector_insert(&reflog->entries, entry) < 0)
+		goto cleanup;
+
+	return 0;
 
 cleanup:
-	git_buf_free(&log_path);
-	return error;
+	reflog_entry_free(entry);
+	return -1;
 }
 
 int git_reflog_rename(git_reference *ref, const char *new_name)
@@ -276,7 +345,7 @@ int git_reflog_rename(git_reference *ref, const char *new_name)
 
 	assert(ref && new_name);
 
-	if (git_buf_joinpath(&temp_path, ref->owner->path_repository, GIT_REFLOG_DIR) < 0)
+	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)
@@ -324,8 +393,7 @@ int git_reflog_delete(git_reference *ref)
 	int error;
 	git_buf path = GIT_BUF_INIT;
 
-	error = git_buf_join_n(
-		&path, '/', 3, ref->owner->path_repository, GIT_REFLOG_DIR, ref->name);
+	error = retrieve_reflog_path(&path, ref);
 
 	if (!error && git_path_exists(path.ptr))
 		error = p_unlink(path.ptr);
@@ -370,3 +438,52 @@ char * git_reflog_entry_msg(const git_reflog_entry *entry)
 	assert(entry);
 	return entry->msg;
 }
+
+int git_reflog_entry_drop(
+	git_reflog *reflog,
+	unsigned int idx,
+	int rewrite_previous_entry)
+{
+	unsigned int entrycount;
+	git_reflog_entry *entry, *previous;
+
+	assert(reflog);
+
+	entrycount = git_reflog_entrycount(reflog);
+
+	if (idx >= entrycount)
+		return GIT_ENOTFOUND;
+
+	entry = git_vector_get(&reflog->entries, idx);
+	reflog_entry_free(entry);
+
+	if (git_vector_remove(&reflog->entries, idx) < 0)
+		return -1;
+
+	if (!rewrite_previous_entry)
+		return 0;
+
+	/* No need to rewrite anything when removing the first entry */
+	if (idx == 0)
+		return 0;
+
+	/* There are no more entries in the log */
+	if (entrycount == 1)
+		return 0;
+
+	entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx - 1);
+
+	/* If the last entry has just been removed... */
+	if (idx == entrycount - 1) {
+		/* ...clear the oid_old member of the "new" last entry */
+		if (git_oid_fromstr(&entry->oid_old, GIT_OID_HEX_ZERO) < 0)
+			return -1;
+		
+		return 0;
+	}
+
+	previous = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx);
+	git_oid_cpy(&entry->oid_old, &previous->oid_cur);
+
+	return 0;
+}
diff --git a/src/reflog.h b/src/reflog.h
index 33cf077..3bbdf6e 100644
--- a/src/reflog.h
+++ b/src/reflog.h
@@ -17,6 +17,8 @@
 
 #define GIT_REFLOG_SIZE_MIN (2*GIT_OID_HEXSZ+2+17)
 
+#define GIT_OID_HEX_ZERO "0000000000000000000000000000000000000000"
+
 struct git_reflog_entry {
 	git_oid oid_old;
 	git_oid oid_cur;
@@ -28,6 +30,7 @@ struct git_reflog_entry {
 
 struct git_reflog {
 	char *ref_name;
+	git_repository *owner;
 	git_vector entries;
 };
 
diff --git a/tests-clar/refs/reflog.c b/tests-clar/refs/reflog.c
deleted file mode 100644
index 05f3786..0000000
--- a/tests-clar/refs/reflog.c
+++ /dev/null
@@ -1,164 +0,0 @@
-#include "clar_libgit2.h"
-
-#include "repository.h"
-#include "git2/reflog.h"
-#include "reflog.h"
-
-
-static const char *new_ref = "refs/heads/test-reflog";
-static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
-static const char *commit_msg = "commit: bla bla";
-
-static git_repository *g_repo;
-
-
-// helpers
-static void assert_signature(git_signature *expected, git_signature *actual)
-{
-	cl_assert(actual);
-	cl_assert_equal_s(expected->name, actual->name);
-	cl_assert_equal_s(expected->email, actual->email);
-	cl_assert(expected->when.offset == actual->when.offset);
-	cl_assert(expected->when.time == actual->when.time);
-}
-
-
-// Fixture setup and teardown
-void test_refs_reflog__initialize(void)
-{
-   g_repo = cl_git_sandbox_init("testrepo.git");
-}
-
-void test_refs_reflog__cleanup(void)
-{
-   cl_git_sandbox_cleanup();
-}
-
-
-
-void test_refs_reflog__write_then_read(void)
-{
-   // 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_reflog *reflog;
-	git_reflog_entry *entry;
-	char oid_str[GIT_OID_HEXSZ+1];
-
-	/* Create a new branch pointing at the HEAD */
-	git_oid_fromstr(&oid, current_master_tip);
-	cl_git_pass(git_reference_create_oid(&ref, g_repo, new_ref, &oid, 0));
-	git_reference_free(ref);
-	cl_git_pass(git_reference_lookup(&ref, g_repo, new_ref));
-
-	cl_git_pass(git_signature_now(&committer, "foo", "foo@bar"));
-
-	cl_git_pass(git_reflog_write(ref, NULL, committer, NULL));
-	cl_git_fail(git_reflog_write(ref, NULL, committer, "no ancestor NULL for an existing reflog"));
-	cl_git_fail(git_reflog_write(ref, NULL, committer, "no\nnewline"));
-	cl_git_pass(git_reflog_write(ref, &oid, committer, commit_msg));
-
-	/* Reopen a new instance of the repository */
-	cl_git_pass(git_repository_open(&repo2, "testrepo.git"));
-
-	/* Lookup the preivously created branch */
-	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_assert(reflog->entries.length == 2);
-
-	entry = (git_reflog_entry *)git_vector_get(&reflog->entries, 0);
-	assert_signature(committer, entry->committer);
-	git_oid_tostr(oid_str, GIT_OID_HEXSZ+1, &entry->oid_old);
-	cl_assert_equal_s("0000000000000000000000000000000000000000", oid_str);
-	git_oid_tostr(oid_str, GIT_OID_HEXSZ+1, &entry->oid_cur);
-	cl_assert_equal_s(current_master_tip, oid_str);
-	cl_assert(entry->msg == NULL);
-
-	entry = (git_reflog_entry *)git_vector_get(&reflog->entries, 1);
-	assert_signature(committer, entry->committer);
-	git_oid_tostr(oid_str, GIT_OID_HEXSZ+1, &entry->oid_old);
-	cl_assert_equal_s(current_master_tip, oid_str);
-	git_oid_tostr(oid_str, GIT_OID_HEXSZ+1, &entry->oid_cur);
-	cl_assert_equal_s(current_master_tip, oid_str);
-	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__dont_write_bad(void)
-{
-   // avoid writing an obviously wrong reflog
-	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_oid(&ref, g_repo, new_ref, &oid, 0));
-	git_reference_free(ref);
-	cl_git_pass(git_reference_lookup(&ref, g_repo, new_ref));
-
-	cl_git_pass(git_signature_now(&committer, "foo", "foo@bar"));
-
-	/* Write the reflog for the new branch */
-	cl_git_pass(git_reflog_write(ref, NULL, committer, NULL));
-
-	/* Try to update the reflog with wrong information:
-	 * It's no new reference, so the ancestor OID cannot
-	 * be NULL. */
-	cl_git_fail(git_reflog_write(ref, NULL, committer, NULL));
-
-	git_signature_free(committer);
-
-	git_reference_free(ref);
-}
-
-void test_refs_reflog__renaming_the_reference_moves_the_reflog(void)
-{
-	git_reference *master;
-	git_buf master_log_path = GIT_BUF_INIT, moved_log_path = GIT_BUF_INIT;
-
-	git_buf_joinpath(&master_log_path, git_repository_path(g_repo), GIT_REFLOG_DIR);
-	git_buf_puts(&moved_log_path, git_buf_cstr(&master_log_path));
-	git_buf_joinpath(&master_log_path, git_buf_cstr(&master_log_path), "refs/heads/master");
-	git_buf_joinpath(&moved_log_path, git_buf_cstr(&moved_log_path), "refs/moved");
-
-	cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&master_log_path)));
-	cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&moved_log_path)));
-
-	cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master"));
-	cl_git_pass(git_reference_rename(master, "refs/moved", 0));
-
-	cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&master_log_path)));
-	cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&moved_log_path)));
-
-	git_reference_free(master);
-	git_buf_free(&moved_log_path);
-	git_buf_free(&master_log_path);
-}
-static void assert_has_reflog(bool expected_result, const char *name)
-{
-	git_reference *ref;
-
-	cl_git_pass(git_reference_lookup(&ref, g_repo, name));
-
-	cl_assert_equal_i(expected_result, git_reference_has_log(ref));
-
-	git_reference_free(ref);
-}
-
-void test_refs_reflog__reference_has_reflog(void)
-{
-	assert_has_reflog(true, "HEAD");
-	assert_has_reflog(true, "refs/heads/master");
-	assert_has_reflog(false, "refs/heads/subtrees");
-}
diff --git a/tests-clar/refs/reflog/drop.c b/tests-clar/refs/reflog/drop.c
new file mode 100644
index 0000000..3aa99fe
--- /dev/null
+++ b/tests-clar/refs/reflog/drop.c
@@ -0,0 +1,130 @@
+#include "clar_libgit2.h"
+
+#include "reflog.h"
+
+static git_repository *g_repo;
+static git_reflog *g_reflog;
+static unsigned int 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);
+	entrycount = git_reflog_entrycount(g_reflog);
+
+	git_reference_free(ref);
+}
+
+void test_refs_reflog_drop__cleanup(void)
+{
+	git_reflog_free(g_reflog);
+
+	cl_git_sandbox_cleanup();
+}
+
+void test_refs_reflog_drop__dropping_a_non_exisiting_entry_from_the_log_returns_ENOTFOUND(void)
+{
+	cl_assert_equal_i(GIT_ENOTFOUND, git_reflog_entry_drop(g_reflog, entrycount, 0));
+
+	cl_assert_equal_i(entrycount, git_reflog_entrycount(g_reflog));
+}
+
+void test_refs_reflog_drop__can_drop_an_entry(void)
+{
+	cl_assert(entrycount > 4);
+
+	cl_git_pass(git_reflog_entry_drop(g_reflog, 2, 0));
+	cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog));
+}
+
+void test_refs_reflog_drop__can_drop_an_entry_and_rewrite_the_log_history(void)
+{
+	const git_reflog_entry *before_previous, *before_next;
+	const git_reflog_entry *after_next;
+	git_oid before_next_old_oid;
+
+	cl_assert(entrycount > 4);
+
+	before_previous = git_reflog_entry_byindex(g_reflog, 3);
+	before_next = git_reflog_entry_byindex(g_reflog, 1);
+	git_oid_cpy(&before_next_old_oid, &before_next->oid_old);
+
+	cl_git_pass(git_reflog_entry_drop(g_reflog, 2, 1));
+	cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog));
+
+	after_next = git_reflog_entry_byindex(g_reflog, 1);
+
+	cl_assert_equal_i(0, git_oid_cmp(&before_next->oid_cur, &after_next->oid_cur));
+	cl_assert(git_oid_cmp(&before_next_old_oid, &after_next->oid_old) != 0);
+	cl_assert_equal_i(0, git_oid_cmp(&before_previous->oid_cur, &after_next->oid_old));
+}
+
+void test_refs_reflog_drop__can_drop_the_first_entry(void)
+{
+	cl_assert(entrycount > 2);
+
+	cl_git_pass(git_reflog_entry_drop(g_reflog, 0, 0));
+	cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog));
+}
+
+void test_refs_reflog_drop__can_drop_the_last_entry(void)
+{
+	const git_reflog_entry *entry;
+
+	cl_assert(entrycount > 2);
+
+	cl_git_pass(git_reflog_entry_drop(g_reflog, entrycount - 1, 0));
+	cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog));
+
+	entry = git_reflog_entry_byindex(g_reflog, entrycount - 2);
+	cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) != 0);
+}
+
+void test_refs_reflog_drop__can_drop_the_last_entry_and_rewrite_the_log_history(void)
+{
+	const git_reflog_entry *entry;
+
+	cl_assert(entrycount > 2);
+
+	cl_git_pass(git_reflog_entry_drop(g_reflog, entrycount - 1, 1));
+	cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog));
+
+	entry = git_reflog_entry_byindex(g_reflog, entrycount - 2);
+	cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) == 0);
+}
+
+void test_refs_reflog_drop__can_drop_all_the_entries(void)
+{
+	cl_assert(--entrycount > 0);
+
+	do 	{
+		cl_git_pass(git_reflog_entry_drop(g_reflog, --entrycount, 1));
+	} while (entrycount > 0);
+
+	cl_git_pass(git_reflog_entry_drop(g_reflog, 0, 1));
+
+	cl_assert_equal_i(0, git_reflog_entrycount(g_reflog));
+}
+
+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_entry_drop(g_reflog, entrycount - 1, 1));
+	cl_assert_equal_i(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);
+
+	cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog));
+}
diff --git a/tests-clar/refs/reflog/reflog.c b/tests-clar/refs/reflog/reflog.c
new file mode 100644
index 0000000..20f08f5
--- /dev/null
+++ b/tests-clar/refs/reflog/reflog.c
@@ -0,0 +1,172 @@
+#include "clar_libgit2.h"
+
+#include "repository.h"
+#include "git2/reflog.h"
+#include "reflog.h"
+
+
+static const char *new_ref = "refs/heads/test-reflog";
+static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
+#define commit_msg "commit: bla bla"
+
+static git_repository *g_repo;
+
+
+// helpers
+static void assert_signature(git_signature *expected, git_signature *actual)
+{
+	cl_assert(actual);
+	cl_assert_equal_s(expected->name, actual->name);
+	cl_assert_equal_s(expected->email, actual->email);
+	cl_assert(expected->when.offset == actual->when.offset);
+	cl_assert(expected->when.time == actual->when.time);
+}
+
+
+// Fixture setup and teardown
+void test_refs_reflog_reflog__initialize(void)
+{
+   g_repo = cl_git_sandbox_init("testrepo.git");
+}
+
+void test_refs_reflog_reflog__cleanup(void)
+{
+   cl_git_sandbox_cleanup();
+}
+
+void test_refs_reflog_reflog__append_then_read(void)
+{
+   // 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_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_oid(&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"));
+
+	/* Lookup the previously created branch */
+	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_assert_equal_i(2, git_reflog_entrycount(reflog));
+
+	entry = git_reflog_entry_byindex(reflog, 0);
+	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(entry->msg == NULL);
+
+	entry = git_reflog_entry_byindex(reflog, 1);
+	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_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__renaming_the_reference_moves_the_reflog(void)
+{
+	git_reference *master;
+	git_buf master_log_path = GIT_BUF_INIT, moved_log_path = GIT_BUF_INIT;
+
+	git_buf_joinpath(&master_log_path, git_repository_path(g_repo), GIT_REFLOG_DIR);
+	git_buf_puts(&moved_log_path, git_buf_cstr(&master_log_path));
+	git_buf_joinpath(&master_log_path, git_buf_cstr(&master_log_path), "refs/heads/master");
+	git_buf_joinpath(&moved_log_path, git_buf_cstr(&moved_log_path), "refs/moved");
+
+	cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&master_log_path)));
+	cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&moved_log_path)));
+
+	cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master"));
+	cl_git_pass(git_reference_rename(master, "refs/moved", 0));
+
+	cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&master_log_path)));
+	cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&moved_log_path)));
+
+	git_reference_free(master);
+	git_buf_free(&moved_log_path);
+	git_buf_free(&master_log_path);
+}
+
+static void assert_has_reflog(bool expected_result, const char *name)
+{
+	git_reference *ref;
+
+	cl_git_pass(git_reference_lookup(&ref, g_repo, name));
+
+	cl_assert_equal_i(expected_result, git_reference_has_log(ref));
+
+	git_reference_free(ref);
+}
+
+void test_refs_reflog_reflog__reference_has_reflog(void)
+{
+	assert_has_reflog(true, "HEAD");
+	assert_has_reflog(true, "refs/heads/master");
+	assert_has_reflog(false, "refs/heads/subtrees");
+}
+
+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;
+	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));
+	cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&subtrees_log_path)));
+
+	cl_git_pass(git_reflog_read(&reflog, subtrees));
+
+	cl_assert_equal_i(0, git_reflog_entrycount(reflog));
+
+	git_reflog_free(reflog);
+	git_reference_free(subtrees);
+	git_buf_free(&subtrees_log_path);
+}
+
+void test_refs_reflog_reflog__cannot_write_a_moved_reflog(void)
+{
+	git_reference *master;
+	git_buf master_log_path = GIT_BUF_INIT, moved_log_path = GIT_BUF_INIT;
+	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_write(reflog));
+	
+	cl_git_pass(git_reference_rename(master, "refs/moved", 0));
+
+	cl_git_fail(git_reflog_write(reflog));
+
+	git_reflog_free(reflog);
+	git_reference_free(master);
+	git_buf_free(&moved_log_path);
+	git_buf_free(&master_log_path);
+}