Commit 0e9ea5d0e30f3c4a06b1823c189fbc8c070eae7a

nulltoken 2012-11-17T18:42:40

Merge pull request #1077 from nulltoken/fix/api Itsy bitsy tiny winy minor api related fixes

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
diff --git a/examples/general.c b/examples/general.c
index d9467f5..9ccb4c5 100644
--- a/examples/general.c
+++ b/examples/general.c
@@ -239,7 +239,7 @@ int main (int argc, char** argv)
   // the tagger (a git_signature - name, email, timestamp), and the tag message.
   git_tag_target((git_object **)&commit, tag);
   tname = git_tag_name(tag);    // "test"
-  ttype = git_tag_type(tag);    // GIT_OBJ_COMMIT (otype enum)
+  ttype = git_tag_target_type(tag);    // GIT_OBJ_COMMIT (otype enum)
   tmessage = git_tag_message(tag); // "tag message\n"
   printf("Tag Message: %s\n", tmessage);
 
diff --git a/include/git2/config.h b/include/git2/config.h
index b427b85..8ec78e3 100644
--- a/include/git2/config.h
+++ b/include/git2/config.h
@@ -135,19 +135,6 @@ GIT_EXTERN(int) git_config_find_system(char *system_config_path, size_t length);
 GIT_EXTERN(int) git_config_open_default(git_config **out);
 
 /**
- * Create a configuration file backend for ondisk files
- *
- * These are the normal `.gitconfig` files that Core Git
- * processes. Note that you first have to add this file to a
- * configuration object before you can query it for configuration
- * variables.
- *
- * @param out the new backend
- * @param path where the config file is located
- */
-GIT_EXTERN(int) git_config_file__ondisk(struct git_config_file **out, const char *path);
-
-/**
  * Allocate a new configuration object
  *
  * This object is empty, so you have to add a file to it before you
@@ -202,7 +189,8 @@ GIT_EXTERN(int) git_config_add_file(
  * @param force if a config file already exists for the given
  *  priority level, replace it
  * @return 0 on success, GIT_EEXISTS when adding more than one file
- *  for a given priority level (and force_replace set to 0), or error code
+ *  for a given priority level (and force_replace set to 0),
+ *  GIT_ENOTFOUND when the file doesn't exist or error code
  */
 GIT_EXTERN(int) git_config_add_file_ondisk(
 	git_config *cfg,
@@ -210,7 +198,6 @@ GIT_EXTERN(int) git_config_add_file_ondisk(
 	unsigned int level,
 	int force);
 
-
 /**
  * Create a new config instance containing a single on-disk file
  *
@@ -219,11 +206,12 @@ GIT_EXTERN(int) git_config_add_file_ondisk(
  *	- git_config_new
  *	- git_config_add_file_ondisk
  *
- * @param cfg The configuration instance to create
+ * @param out The configuration instance to create
  * @param path Path to the on-disk file to open
- * @return 0 or an error code
+ * @return 0 on success, GIT_ENOTFOUND when the file doesn't exist
+ * or an error code
  */
-GIT_EXTERN(int) git_config_open_ondisk(git_config **cfg, const char *path);
+GIT_EXTERN(int) git_config_open_ondisk(git_config **out, const char *path);
 
 /**
  * Build a single-level focused config object from a multi-level one.
diff --git a/include/git2/reflog.h b/include/git2/reflog.h
index d48a897..72e1f17 100644
--- a/include/git2/reflog.h
+++ b/include/git2/reflog.h
@@ -88,8 +88,12 @@ GIT_EXTERN(unsigned int) git_reflog_entrycount(git_reflog *reflog);
 /**
  * Lookup an entry by its index
  *
+ * Requesting the reflog entry with an index of 0 (zero) will
+ * return the most recently created entry.
+ *
  * @param reflog a previously loaded reflog
- * @param idx the position to lookup
+ * @param idx the position of the entry to lookup. Should be greater than or
+ * equal to 0 (zero) and less than `git_reflog_entrycount()`.
  * @return the entry; NULL if not found
  */
 GIT_EXTERN(const git_reflog_entry *) git_reflog_entry_byindex(git_reflog *reflog, size_t idx);
@@ -103,15 +107,17 @@ GIT_EXTERN(const git_reflog_entry *) git_reflog_entry_byindex(git_reflog *reflog
  *
  * @param reflog a previously loaded reflog.
  *
- * @param idx the position of the entry to remove.
+ * @param idx the position of the entry to remove. Should be greater than or
+ * equal to 0 (zero) and less than `git_reflog_entrycount()`.
  *
  * @param rewrite_previous_entry 1 to rewrite the history; 0 otherwise.
  *
- * @return 0 on success or an error code.
+ * @return 0 on success, GIT_ENOTFOUND if the entry doesn't exist
+ * or an error code.
  */
 GIT_EXTERN(int) git_reflog_drop(
 	git_reflog *reflog,
-	unsigned int idx,
+	size_t idx,
 	int rewrite_previous_entry);
 
 /**
diff --git a/include/git2/repository.h b/include/git2/repository.h
index d606cfa..f891e91 100644
--- a/include/git2/repository.h
+++ b/include/git2/repository.h
@@ -305,7 +305,7 @@ GIT_EXTERN(int) git_repository_head_orphan(git_repository *repo);
  * Check if a repository is empty
  *
  * An empty repository has just been initialized and contains
- * no commits.
+ * no references.
  *
  * @param repo Repo to test
  * @return 1 if the repository is empty, 0 if it isn't, error code
diff --git a/include/git2/tag.h b/include/git2/tag.h
index 5602914..a1b685f 100644
--- a/include/git2/tag.h
+++ b/include/git2/tag.h
@@ -104,7 +104,7 @@ GIT_EXTERN(const git_oid *) git_tag_target_oid(git_tag *tag);
  * @param tag a previously loaded tag.
  * @return type of the tagged object
  */
-GIT_EXTERN(git_otype) git_tag_type(git_tag *tag);
+GIT_EXTERN(git_otype) git_tag_target_type(git_tag *tag);
 
 /**
  * Get the name of a tag
diff --git a/src/config.c b/src/config.c
index 01a54ec..412965b 100644
--- a/src/config.c
+++ b/src/config.c
@@ -90,6 +90,13 @@ int git_config_add_file_ondisk(
 	git_config_file *file = NULL;
 	int res;
 
+	assert(cfg && path);
+
+	if (!git_path_isfile(path)) {
+		giterr_set(GITERR_CONFIG, "File '%s' doesn't exists.", path);
+		return GIT_ENOTFOUND;
+	}
+
 	if (git_config_file__ondisk(&file, path) < 0)
 		return -1;
 
@@ -105,17 +112,22 @@ int git_config_add_file_ondisk(
 	return 0;
 }
 
-int git_config_open_ondisk(git_config **cfg, const char *path)
+int git_config_open_ondisk(git_config **out, const char *path)
 {
-	if (git_config_new(cfg) < 0)
-		return -1;
+	int error;
+	git_config *config;
 
-	if (git_config_add_file_ondisk(*cfg, path, GIT_CONFIG_LEVEL_LOCAL, 0) < 0) {
-		git_config_free(*cfg);
+	*out = NULL;
+
+	if (git_config_new(&config) < 0)
 		return -1;
-	}
 
-	return 0;
+	if ((error = git_config_add_file_ondisk(config, path, GIT_CONFIG_LEVEL_LOCAL, 0)) < 0)
+		git_config_free(config);
+	else
+		*out = config;
+
+	return error;
 }
 
 static int find_internal_file_by_level(
diff --git a/src/config.h b/src/config.h
index a0569ec..c7595ee 100644
--- a/src/config.h
+++ b/src/config.h
@@ -32,4 +32,17 @@ extern int git_config_rename_section(
 	const char *old_section_name,	/* eg "branch.dummy" */
 	const char *new_section_name);	/* NULL to drop the old section */
 
+/**
+ * Create a configuration file backend for ondisk files
+ *
+ * These are the normal `.gitconfig` files that Core Git
+ * processes. Note that you first have to add this file to a
+ * configuration object before you can query it for configuration
+ * variables.
+ *
+ * @param out the new backend
+ * @param path where the config file is located
+ */
+extern int git_config_file__ondisk(struct git_config_file **out, const char *path);
+
 #endif
diff --git a/src/pack-objects.h b/src/pack-objects.h
index 8c01f71..e34cc27 100644
--- a/src/pack-objects.h
+++ b/src/pack-objects.h
@@ -71,11 +71,11 @@ struct git_packbuilder {
 	git_cond progress_cond;
 
 	/* configs */
-	unsigned long delta_cache_size;
-	unsigned long max_delta_cache_size;
-	unsigned long cache_max_small_delta_size;
-	unsigned long big_file_threshold;
-	unsigned long window_memory_limit;
+	uint64_t delta_cache_size;
+	uint64_t max_delta_cache_size;
+	uint64_t cache_max_small_delta_size;
+	uint64_t big_file_threshold;
+	uint64_t window_memory_limit;
 
 	int nr_threads; /* nr of threads to use */
 
diff --git a/src/reflog.c b/src/reflog.c
index 0e333aa..7b07c6a 100644
--- a/src/reflog.c
+++ b/src/reflog.c
@@ -291,8 +291,8 @@ success:
 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 git_reflog_entry *previous;
 	const char *newline;
 
 	assert(reflog && new_oid && committer);
@@ -319,16 +319,12 @@ int git_reflog_append(git_reflog *reflog, const git_oid *new_oid,
 		}
 	}
 
-	count = git_reflog_entrycount(reflog);
+	previous = git_reflog_entry_byindex(reflog, 0);
 
-	if (count == 0)
+	if (previous == NULL)
 		git_oid_fromstr(&entry->oid_old, GIT_OID_HEX_ZERO);
-	else {
-		const git_reflog_entry *previous;
-
-		previous = git_reflog_entry_byindex(reflog, count -1);
+	else
 		git_oid_cpy(&entry->oid_old, &previous->oid_cur);
-	}
 
 	git_oid_cpy(&entry->oid_cur, new_oid);
 
@@ -417,8 +413,16 @@ unsigned int git_reflog_entrycount(git_reflog *reflog)
 
 const git_reflog_entry * git_reflog_entry_byindex(git_reflog *reflog, size_t idx)
 {
+	int pos;
+
 	assert(reflog);
-	return git_vector_get(&reflog->entries, idx);
+
+	pos = git_reflog_entrycount(reflog) - (idx + 1);
+
+	if (pos < 0)
+		return NULL;
+
+	return git_vector_get(&reflog->entries, pos);
 }
 
 const git_oid * git_reflog_entry_oidold(const git_reflog_entry *entry)
@@ -447,7 +451,7 @@ char * git_reflog_entry_msg(const git_reflog_entry *entry)
 
 int git_reflog_drop(
 	git_reflog *reflog,
-	unsigned int idx,
+	size_t idx,
 	int rewrite_previous_entry)
 {
 	unsigned int entrycount;
@@ -457,30 +461,31 @@ int git_reflog_drop(
 
 	entrycount = git_reflog_entrycount(reflog);
 
-	if (idx >= entrycount)
+	entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx);
+
+	if (entry == NULL)
 		return GIT_ENOTFOUND;
 
-	entry = git_vector_get(&reflog->entries, idx);
 	reflog_entry_free(entry);
 
-	if (git_vector_remove(&reflog->entries, idx) < 0)
+	if (git_vector_remove(&reflog->entries, entrycount - (idx + 1)) < 0)
 		return -1;
 
 	if (!rewrite_previous_entry)
 		return 0;
 
 	/* No need to rewrite anything when removing the most recent entry */
-	if (idx == entrycount - 1)
+	if (idx == 0)
 		return 0;
 
-	/* There are no more entries in the log */
+	/* Have the latest entry just been dropped? */
 	if (entrycount == 1)
 		return 0;
 
-	entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx);
+	entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx - 1);
 
 	/* If the oldest entry has just been removed... */
-	if (idx == 0) {
+	if (idx == entrycount - 1) {
 		/* ...clear the oid_old member of the "new" oldest entry */
 		if (git_oid_fromstr(&entry->oid_old, GIT_OID_HEX_ZERO) < 0)
 			return -1;
@@ -488,7 +493,7 @@ int git_reflog_drop(
 		return 0;
 	}
 
-	previous = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx - 1);
+	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/repository.c b/src/repository.c
index 101497c..c1756b1 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -750,6 +750,23 @@ static bool are_symlinks_supported(const char *wd_path)
 	return _symlinks_supported;
 }
 
+static int create_empty_file(const char *path, mode_t mode)
+{
+	int fd;
+
+	if ((fd = p_creat(path, mode)) < 0) {
+		giterr_set(GITERR_OS, "Error while creating '%s'", path);
+		return -1;
+	}
+
+	if (p_close(fd) < 0) {
+		giterr_set(GITERR_OS, "Error while closing '%s'", path);
+		return -1;
+	}
+
+	return 0;
+}
+
 static int repo_init_config(
 	const char *repo_dir,
 	const char *work_dir,
@@ -766,6 +783,12 @@ static int repo_init_config(
 	if (git_buf_joinpath(&cfg_path, repo_dir, GIT_CONFIG_FILENAME_INREPO) < 0)
 		return -1;
 
+	if (!git_path_isfile(git_buf_cstr(&cfg_path)) &&
+		create_empty_file(git_buf_cstr(&cfg_path), GIT_CONFIG_FILE_MODE) < 0) {
+			git_buf_free(&cfg_path);
+			return -1;
+	}
+
 	if (git_config_open_ondisk(&config, git_buf_cstr(&cfg_path)) < 0) {
 		git_buf_free(&cfg_path);
 		return -1;
@@ -1241,36 +1264,47 @@ int git_repository_head_orphan(git_repository *repo)
 	return 0;
 }
 
-int git_repository_is_empty(git_repository *repo)
+int at_least_one_cb(const char *refname, void *payload)
 {
-	git_reference *head = NULL, *branch = NULL;
-	int error;
+	GIT_UNUSED(refname);
+	GIT_UNUSED(payload);
 
-	if (git_reference_lookup(&head, repo, GIT_HEAD_FILE) < 0)
-		return -1;
+	return GIT_EUSER;
+}
 
-	if (git_reference_type(head) != GIT_REF_SYMBOLIC) {
-		git_reference_free(head);
-		return 0;
-	}
+static int repo_contains_no_reference(git_repository *repo)
+{
+	int error;
+	
+	error = git_reference_foreach(repo, GIT_REF_LISTALL, at_least_one_cb, NULL);
 
-	if (strcmp(git_reference_target(head), GIT_REFS_HEADS_DIR "master") != 0) {
-		git_reference_free(head);
+	if (error == GIT_EUSER)
 		return 0;
-	}
-
-	error = git_reference_resolve(&branch, head);
 
-	git_reference_free(head);
-	git_reference_free(branch);
+	return error == 0 ? 1 : error;
+}
 
-	if (error == GIT_ENOTFOUND)
-		return 1;
+int git_repository_is_empty(git_repository *repo)
+{
+	git_reference *head = NULL;
+	int error, ref_count = 0;
 
-	if (error < 0)
+	if (git_reference_lookup(&head, repo, GIT_HEAD_FILE) < 0)
 		return -1;
 
-	return 0;
+	if (!(error = git_reference_type(head) == GIT_REF_SYMBOLIC))
+		goto cleanup;
+
+	if (!(error = strcmp(
+		git_reference_target(head),
+		GIT_REFS_HEADS_DIR "master") == 0))
+			goto cleanup;
+
+	error = repo_contains_no_reference(repo);
+
+cleanup:
+	git_reference_free(head);
+	return error < 0 ? -1 : error;
 }
 
 const char *git_repository_path(git_repository *repo)
diff --git a/src/revparse.c b/src/revparse.c
index 83eea7d..6a7587d 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -201,7 +201,7 @@ static int retrieve_previously_checked_out_branch_or_revision(git_object **out, 
 
 	numentries  = git_reflog_entrycount(reflog);
 
-	for (i = numentries - 1; i >= 0; i--) {
+	for (i = 0; i < numentries; i++) {
 		entry = git_reflog_entry_byindex(reflog, i);
 		msg = git_reflog_entry_msg(entry);
 		
@@ -263,7 +263,7 @@ static int retrieve_oid_from_reflog(git_oid *oid, git_reference *ref, unsigned i
 		}
 
 		entry = git_reflog_entry_byindex(reflog, identifier);
-		git_oid_cpy(oid, git_reflog_entry_oidold(entry));
+		git_oid_cpy(oid, git_reflog_entry_oidnew(entry));
 		error = 0;
 		goto cleanup;
 
@@ -271,7 +271,7 @@ static int retrieve_oid_from_reflog(git_oid *oid, git_reference *ref, unsigned i
 		int i;
 		git_time commit_time;
 
-		for (i = numentries - 1; i >= 0; i--) {
+		for (i = 0; i < numentries; i++) {
 			entry = git_reflog_entry_byindex(reflog, i);
 			commit_time = git_reflog_entry_committer(entry)->when;
 					
diff --git a/src/stash.c b/src/stash.c
index 627d271..b74429a 100644
--- a/src/stash.c
+++ b/src/stash.c
@@ -600,7 +600,7 @@ int git_stash_foreach(
 
 	max = git_reflog_entrycount(reflog);
 	for (i = 0; i < max; i++) {
-		entry = git_reflog_entry_byindex(reflog, max - i - 1);
+		entry = git_reflog_entry_byindex(reflog, i);
 		
 		if (callback(i,
 			git_reflog_entry_msg(entry),
@@ -642,7 +642,7 @@ int git_stash_drop(
 		goto cleanup;
 	}
 
-	if ((error = git_reflog_drop(reflog, max - index - 1, true)) < 0)
+	if ((error = git_reflog_drop(reflog, index, true)) < 0)
 		goto cleanup;
 
 	if ((error = git_reflog_write(reflog)) < 0)
diff --git a/src/tag.c b/src/tag.c
index 4c3d811..13369d9 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -39,7 +39,7 @@ const git_oid *git_tag_target_oid(git_tag *t)
 	return &t->target;
 }
 
-git_otype git_tag_type(git_tag *t)
+git_otype git_tag_target_type(git_tag *t)
 {
 	assert(t);
 	return t->type;
diff --git a/tests-clar/clar_helpers.c b/tests-clar/clar_helpers.c
index 3d09bd4..b718d43 100644
--- a/tests-clar/clar_helpers.c
+++ b/tests-clar/clar_helpers.c
@@ -271,3 +271,52 @@ const char* cl_git_path_url(const char *path)
 	git_buf_free(&path_buf);
 	return url;
 }
+
+typedef struct {
+	const char *filename;
+	size_t filename_len;
+} remove_data;
+
+static int remove_placeholders_recurs(void *_data, git_buf *path)
+{
+	remove_data *data = (remove_data *)_data;
+	size_t pathlen;
+
+	if (git_path_isdir(path->ptr) == true)
+		return git_path_direach(path, remove_placeholders_recurs, data);
+
+	pathlen = path->size;
+
+	if (pathlen < data->filename_len)
+		return 0;
+
+	/* if path ends in '/'+filename (or equals filename) */
+	if (!strcmp(data->filename, path->ptr + pathlen - data->filename_len) &&
+		(pathlen == data->filename_len ||
+		 path->ptr[pathlen - data->filename_len - 1] == '/'))
+		return p_unlink(path->ptr);
+
+	return 0;
+}
+
+int cl_git_remove_placeholders(const char *directory_path, const char *filename)
+{
+	int error;
+	remove_data data;
+	git_buf buffer = GIT_BUF_INIT;
+
+	if (git_path_isdir(directory_path) == false)
+		return -1;
+
+	if (git_buf_sets(&buffer, directory_path) < 0)
+		return -1;
+
+	data.filename = filename;
+	data.filename_len = strlen(filename);
+
+	error = remove_placeholders_recurs(&data, &buffer);
+
+	git_buf_free(&buffer);
+
+	return error;
+}
diff --git a/tests-clar/clar_libgit2.h b/tests-clar/clar_libgit2.h
index fd20c12..91a5426 100644
--- a/tests-clar/clar_libgit2.h
+++ b/tests-clar/clar_libgit2.h
@@ -61,4 +61,7 @@ void cl_git_sandbox_cleanup(void);
 const char* cl_git_fixture_url(const char *fixturename);
 const char* cl_git_path_url(const char *path);
 
+/* Test repository cleaner */
+int cl_git_remove_placeholders(const char *directory_path, const char *filename);
+
 #endif
diff --git a/tests-clar/config/new.c b/tests-clar/config/new.c
index 6bd719f..dd6dbca 100644
--- a/tests-clar/config/new.c
+++ b/tests-clar/config/new.c
@@ -11,6 +11,7 @@ void test_config_new__write_new_config(void)
 	const char *out;
 	git_config *config;
 
+	cl_git_mkfile(TEST_CONFIG, "");
 	cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG));
 
 	cl_git_pass(git_config_set_string(config, "color.ui", "auto"));
diff --git a/tests-clar/config/read.c b/tests-clar/config/read.c
index 7b30b6e..d63ef4c 100644
--- a/tests-clar/config/read.c
+++ b/tests-clar/config/read.c
@@ -430,3 +430,23 @@ void test_config_read__simple_read_from_specific_level(void)
 	git_config_free(cfg_specific);
 	git_config_free(cfg);
 }
+
+void test_config_read__can_load_and_parse_an_empty_config_file(void)
+{
+	git_config *cfg;
+	int i;
+
+	cl_git_mkfile("./empty", "");
+	cl_git_pass(git_config_open_ondisk(&cfg, "./empty"));
+	cl_assert_equal_i(GIT_ENOTFOUND, git_config_get_int32(&i, cfg, "nope.neither"));
+
+	git_config_free(cfg);
+}
+
+void test_config_read__cannot_load_a_non_existing_config_file(void)
+{
+	git_config *cfg;
+	int i;
+
+	cl_assert_equal_i(GIT_ENOTFOUND, git_config_open_ondisk(&cfg, "./no.config"));
+}
diff --git a/tests-clar/core/env.c b/tests-clar/core/env.c
index 288222d..d849f76 100644
--- a/tests-clar/core/env.c
+++ b/tests-clar/core/env.c
@@ -84,13 +84,15 @@ void test_core_env__0(void)
 		cl_git_mkfile(path.ptr, "find me");
 		git_buf_rtruncate_at_char(&path, '/');
 
-		cl_git_fail(git_futils_find_global_file(&found, testfile));
+		cl_assert_equal_i(
+			GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
 
 		setenv_and_check("HOME", path.ptr);
 		cl_git_pass(git_futils_find_global_file(&found, testfile));
 
 		cl_setenv("HOME", env_save[0]);
-		cl_git_fail(git_futils_find_global_file(&found, testfile));
+		cl_assert_equal_i(
+			GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
 
 #ifdef GIT_WIN32
 		setenv_and_check("HOMEDRIVE", NULL);
@@ -106,7 +108,8 @@ void test_core_env__0(void)
 			if (root >= 0) {
 				setenv_and_check("USERPROFILE", NULL);
 
-				cl_git_fail(git_futils_find_global_file(&found, testfile));
+				cl_assert_equal_i(
+					GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
 
 				old = path.ptr[root];
 				path.ptr[root] = '\0';
@@ -128,7 +131,8 @@ void test_core_env__1(void)
 {
 	git_buf path = GIT_BUF_INIT;
 
-	cl_must_fail(git_futils_find_global_file(&path, "nonexistentfile"));
+	cl_assert_equal_i(
+		GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile"));
 
 	cl_git_pass(cl_setenv("HOME", "doesnotexist"));
 #ifdef GIT_WIN32
@@ -136,7 +140,8 @@ void test_core_env__1(void)
 	cl_git_pass(cl_setenv("USERPROFILE", "doesnotexist"));
 #endif
 
-	cl_must_fail(git_futils_find_global_file(&path, "nonexistentfile"));
+	cl_assert_equal_i(
+		GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile"));
 
 	cl_git_pass(cl_setenv("HOME", NULL));
 #ifdef GIT_WIN32
@@ -144,13 +149,16 @@ void test_core_env__1(void)
 	cl_git_pass(cl_setenv("USERPROFILE", NULL));
 #endif
 
-	cl_must_fail(git_futils_find_global_file(&path, "nonexistentfile"));
+	cl_assert_equal_i(
+		GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile"));
 
-	cl_must_fail(git_futils_find_system_file(&path, "nonexistentfile"));
+	cl_assert_equal_i(
+		GIT_ENOTFOUND, git_futils_find_system_file(&path, "nonexistentfile"));
 
 #ifdef GIT_WIN32
 	cl_git_pass(cl_setenv("PROGRAMFILES", NULL));
-	cl_must_fail(git_futils_find_system_file(&path, "nonexistentfile"));
+	cl_assert_equal_i(
+		GIT_ENOTFOUND, git_futils_find_system_file(&path, "nonexistentfile"));
 #endif
 
 	git_buf_free(&path);
diff --git a/tests-clar/object/raw/short.c b/tests-clar/object/raw/short.c
index 14b1ae2..93c79b6 100644
--- a/tests-clar/object/raw/short.c
+++ b/tests-clar/object/raw/short.c
@@ -43,7 +43,7 @@ void test_object_raw_short__oid_shortener_stresstest_git_oid_shorten(void)
 	for (i = 0; i < MAX_OIDS; ++i) {
 		char *oid_text;
 
-		sprintf(number_buffer, "%u", (unsigned int)i);
+		p_snprintf(number_buffer, 16, "%u", (unsigned int)i);
 		git_hash_buf(&oid, number_buffer, strlen(number_buffer));
 
 		oid_text = git__malloc(GIT_OID_HEXSZ + 1);
diff --git a/tests-clar/object/tag/read.c b/tests-clar/object/tag/read.c
index 62dd4ca..53272e9 100644
--- a/tests-clar/object/tag/read.c
+++ b/tests-clar/object/tag/read.c
@@ -38,7 +38,7 @@ void test_object_tag_read__parse(void)
    cl_git_pass(git_tag_lookup(&tag1, g_repo, &id1));
 
    cl_assert_equal_s(git_tag_name(tag1), "test");
-   cl_assert(git_tag_type(tag1) == GIT_OBJ_TAG);
+   cl_assert(git_tag_target_type(tag1) == GIT_OBJ_TAG);
 
    cl_git_pass(git_tag_target((git_object **)&tag2, tag1));
    cl_assert(tag2 != NULL);
diff --git a/tests-clar/refs/reflog/drop.c b/tests-clar/refs/reflog/drop.c
index 4be857b..512e8ba 100644
--- a/tests-clar/refs/reflog/drop.c
+++ b/tests-clar/refs/reflog/drop.c
@@ -49,14 +49,16 @@ void test_refs_reflog_drop__can_drop_an_entry_and_rewrite_the_log_history(void)
 
 	cl_assert(entrycount > 4);
 
-	before_current = git_reflog_entry_byindex(g_reflog, 2);
+	before_current = git_reflog_entry_byindex(g_reflog, 1);
+
 	git_oid_cpy(&before_current_old_oid, &before_current->oid_old);
 	git_oid_cpy(&before_current_cur_oid, &before_current->oid_cur);
 
-	cl_git_pass(git_reflog_drop(g_reflog, 2, 1));
+	cl_git_pass(git_reflog_drop(g_reflog, 1, 1));
+
 	cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog));
 
-	after_current = git_reflog_entry_byindex(g_reflog, 2);
+	after_current = git_reflog_entry_byindex(g_reflog, 0);
 
 	cl_assert_equal_i(0, git_oid_cmp(&before_current_old_oid, &after_current->oid_old));
 	cl_assert(0 != git_oid_cmp(&before_current_cur_oid, &after_current->oid_cur));
@@ -68,10 +70,10 @@ void test_refs_reflog_drop__can_drop_the_oldest_entry(void)
 
 	cl_assert(entrycount > 2);
 
-	cl_git_pass(git_reflog_drop(g_reflog, 0, 0));
+	cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 0));
 	cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog));
 
-	entry = git_reflog_entry_byindex(g_reflog, 0);
+	entry = git_reflog_entry_byindex(g_reflog, entrycount - 2);
 	cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) != 0);
 }
 
@@ -81,10 +83,10 @@ void test_refs_reflog_drop__can_drop_the_oldest_entry_and_rewrite_the_log_histor
 
 	cl_assert(entrycount > 2);
 
-	cl_git_pass(git_reflog_drop(g_reflog, 0, 1));
+	cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 1));
 	cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog));
 
-	entry = git_reflog_entry_byindex(g_reflog, 0);
+	entry = git_reflog_entry_byindex(g_reflog, entrycount - 2);
 	cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) == 0);
 }
 
@@ -93,8 +95,8 @@ void test_refs_reflog_drop__can_drop_all_the_entries(void)
 	cl_assert(--entrycount > 0);
 
 	do 	{
-		cl_git_pass(git_reflog_drop(g_reflog, --entrycount, 1));
-	} while (entrycount > 0);
+		cl_git_pass(git_reflog_drop(g_reflog, 0, 1));
+	} while (--entrycount > 0);
 
 	cl_git_pass(git_reflog_drop(g_reflog, 0, 1));
 
@@ -108,7 +110,7 @@ void test_refs_reflog_drop__can_persist_deletion_on_disk(void)
 	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, entrycount - 1, 1));
+	cl_git_pass(git_reflog_drop(g_reflog, 0, 1));
 	cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog));
 	cl_git_pass(git_reflog_write(g_reflog));
 
diff --git a/tests-clar/refs/reflog/reflog.c b/tests-clar/refs/reflog/reflog.c
index 20f08f5..09b9356 100644
--- a/tests-clar/refs/reflog/reflog.c
+++ b/tests-clar/refs/reflog/reflog.c
@@ -68,13 +68,13 @@ void test_refs_reflog_reflog__append_then_read(void)
 	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);
+	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(entry->msg == NULL);
 
-	entry = git_reflog_entry_byindex(reflog, 1);
+	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);
diff --git a/tests-clar/repo/getters.c b/tests-clar/repo/getters.c
index ffcd171..b372f5b 100644
--- a/tests-clar/repo/getters.c
+++ b/tests-clar/repo/getters.c
@@ -1,26 +1,26 @@
 #include "clar_libgit2.h"
 
-void test_repo_getters__initialize(void)
+void test_repo_getters__is_empty_correctly_deals_with_pristine_looking_repos(void)
 {
-	cl_fixture_sandbox("testrepo.git");
-}
+	git_repository *repo;
 
-void test_repo_getters__cleanup(void)
-{
-	cl_fixture_cleanup("testrepo.git");
+	repo = cl_git_sandbox_init("empty_bare.git");
+	cl_git_remove_placeholders(git_repository_path(repo), "dummy-marker.txt");
+
+	cl_assert_equal_i(true, git_repository_is_empty(repo));
+
+	cl_git_sandbox_cleanup();
 }
 
-void test_repo_getters__empty(void)
+void test_repo_getters__is_empty_can_detect_used_repositories(void)
 {
-	git_repository *repo_empty, *repo_normal;
+	git_repository *repo;
 
-	cl_git_pass(git_repository_open(&repo_normal, cl_fixture("testrepo.git")));
-	cl_assert(git_repository_is_empty(repo_normal) == 0);
-	git_repository_free(repo_normal);
+	cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
 
-	cl_git_pass(git_repository_open(&repo_empty, cl_fixture("empty_bare.git")));
-	cl_assert(git_repository_is_empty(repo_empty) == 1);
-	git_repository_free(repo_empty);
+	cl_assert_equal_i(false, git_repository_is_empty(repo));
+
+	git_repository_free(repo);
 }
 
 void test_repo_getters__retrieving_the_odb_honors_the_refcount(void)
@@ -28,7 +28,7 @@ void test_repo_getters__retrieving_the_odb_honors_the_refcount(void)
 	git_odb *odb;
 	git_repository *repo;
 
-	cl_git_pass(git_repository_open(&repo, "testrepo.git"));
+	cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
 
 	cl_git_pass(git_repository_odb(&odb, repo));
 	cl_assert(((git_refcount *)odb)->refcount == 2);
diff --git a/tests-clar/stash/drop.c b/tests-clar/stash/drop.c
index 39139cc..b4f73b9 100644
--- a/tests-clar/stash/drop.c
+++ b/tests-clar/stash/drop.c
@@ -99,7 +99,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));
-	entry = git_reflog_entry_byindex(reflog, 1);
+	entry = git_reflog_entry_byindex(reflog, 0);
 
 	cl_assert_equal_i(0, git_oid_cmp(&oid, git_reflog_entry_oidold(entry)));
 	cl_assert_equal_i(count - 1, git_reflog_entrycount(reflog));