Commit 854eccbb2d86c2910f9d98dc52f9ebd0e37c262a

Russell Belfer 2012-02-29T12:04:59

Clean up GIT_UNUSED macros on all platforms It turns out that commit 31e9cfc4cbcaf1b38cdd3dbe3282a8f57e5366a5 did not fix the GIT_USUSED behavior on all platforms. This commit walks through and really cleans things up more thoroughly, getting rid of the unnecessary stuff. To remove the use of some GIT_UNUSED, I ended up adding a couple of new iterators for hashtables that allow you to iterator just over keys or just over values. In making this change, I found a bug in the clar tests (where we were doing *count++ but meant to do (*count)++ to increment the value). I fixed that but then found the test failing because it was not really using an empty repo. So, I took some of the code that I wrote for iterator testing and moved it to clar_helpers.c, then made use of that to make it easier to open fixtures on a per test basis even within a single test file.

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
diff --git a/src/attr.c b/src/attr.c
index a7c65f9..603498d 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -408,10 +408,9 @@ void git_attr_cache_flush(
 		return;
 
 	if (repo->attrcache.files) {
-		const void *GIT_UNUSED(name);
 		git_attr_file *file;
 
-		GIT_HASHTABLE_FOREACH(repo->attrcache.files, name, file,
+		GIT_HASHTABLE_FOREACH_VALUE(repo->attrcache.files, file,
 			git_attr_file__free(file));
 
 		git_hashtable_free(repo->attrcache.files);
@@ -419,10 +418,9 @@ void git_attr_cache_flush(
 	}
 
 	if (repo->attrcache.macros) {
-		const void *GIT_UNUSED(name);
 		git_attr_rule *rule;
 
-		GIT_HASHTABLE_FOREACH(repo->attrcache.macros, name, rule,
+		GIT_HASHTABLE_FOREACH_VALUE(repo->attrcache.macros, rule,
 			git_attr_rule__free(rule));
 
 		git_hashtable_free(repo->attrcache.macros);
diff --git a/src/cc-compat.h b/src/cc-compat.h
index bbccd1f..3df36b6 100644
--- a/src/cc-compat.h
+++ b/src/cc-compat.h
@@ -33,8 +33,7 @@
 #	define GIT_TYPEOF(x)
 #endif
 
-#define GIT_UNUSED(x) x
-#define GIT_UNUSED_ARG(x) ((void)(x))
+#define GIT_UNUSED(x) ((void)(x))
 
 /* Define the printf format specifer to use for size_t output */
 #if defined(_MSC_VER) || defined(__MINGW32__)
diff --git a/src/config_file.c b/src/config_file.c
index ce76493..3c7c593 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -125,13 +125,12 @@ static int normalize_name(const char *in, char **out)
 
 static void free_vars(git_hashtable *values)
 {
-	const char *GIT_UNUSED(_unused) = NULL;
 	cvar_t *var = NULL;
 
 	if (values == NULL)
 		return;
 
-	GIT_HASHTABLE_FOREACH(values, _unused, var,
+	GIT_HASHTABLE_FOREACH_VALUE(values, var,
 	      do {
 		      cvar_t *next = CVAR_LIST_NEXT(var);
 		      cvar_free(var);
diff --git a/src/diff.c b/src/diff.c
index 9e41055..dcc0aef 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -325,7 +325,7 @@ static int maybe_modified(
 	int error = GIT_SUCCESS;
 	git_oid noid, *use_noid = NULL;
 
-	GIT_UNUSED_ARG(old);
+	GIT_UNUSED(old);
 
 	/* support "assume unchanged" & "skip worktree" bits */
 	if ((oitem->flags_extended & GIT_IDXENTRY_INTENT_TO_ADD) != 0 ||
diff --git a/src/diff_output.c b/src/diff_output.c
index ac60e98..b800be9 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -161,7 +161,7 @@ static int file_is_binary_by_content(
 	git_map *old_data,
 	git_map *new_data)
 {
-	GIT_UNUSED_ARG(diff);
+	GIT_UNUSED(diff);
 
 	if ((delta->old.flags & BINARY_DIFF_FLAGS) == 0) {
 		size_t search_len = min(old_data->len, 4000);
@@ -448,7 +448,7 @@ static int print_compact(void *data, git_diff_delta *delta, float progress)
 	diff_print_info *pi = data;
 	char code, old_suffix, new_suffix;
 
-	GIT_UNUSED_ARG(progress);
+	GIT_UNUSED(progress);
 
 	switch (delta->status) {
 	case GIT_STATUS_ADDED: code = 'A'; break;
@@ -546,7 +546,7 @@ static int print_patch_file(void *data, git_diff_delta *delta, float progress)
 	const char *newpfx = pi->diff->opts.dst_prefix;
 	const char *newpath = delta->new.path;
 
-	GIT_UNUSED_ARG(progress);
+	GIT_UNUSED(progress);
 
 	git_buf_clear(pi->buf);
 	git_buf_printf(pi->buf, "diff --git %s%s %s%s\n", oldpfx, delta->old.path, newpfx, delta->new.path);
@@ -593,8 +593,8 @@ static int print_patch_hunk(
 {
 	diff_print_info *pi = data;
 
-	GIT_UNUSED_ARG(d);
-	GIT_UNUSED_ARG(r);
+	GIT_UNUSED(d);
+	GIT_UNUSED(r);
 
 	git_buf_clear(pi->buf);
 
@@ -613,7 +613,7 @@ static int print_patch_line(
 {
 	diff_print_info *pi = data;
 
-	GIT_UNUSED_ARG(delta);
+	GIT_UNUSED(delta);
 
 	git_buf_clear(pi->buf);
 
diff --git a/src/hashtable.h b/src/hashtable.h
index f6fbb85..e099659 100644
--- a/src/hashtable.h
+++ b/src/hashtable.h
@@ -65,20 +65,20 @@ GIT_INLINE(int) git_hashtable_insert(git_hashtable *h, const void *key, void *va
 
 #define git_hashtable_node_at(nodes, pos) ((git_hashtable_node *)(&nodes[pos]))
 
-#define GIT_HASHTABLE_FOREACH(self, pkey, pvalue, code) {\
-	git_hashtable *_self = (self);\
-	git_hashtable_node *_nodes = _self->nodes;\
-	unsigned int _i, _size = _self->size;\
-	for (_i = 0; _i < _size; _i ++) {\
-		git_hashtable_node *_node = git_hashtable_node_at(_nodes, _i);\
-		if (_node->key)\
-		{\
-			pkey = _node->key;\
-			pvalue = _node->value;\
-			code;\
-		}\
-	}\
-}
+#define GIT_HASHTABLE__FOREACH(self,block) { \
+	unsigned int _c; \
+	git_hashtable_node *_n = (self)->nodes; \
+	for (_c = (self)->size; _c > 0; _c--, _n++)	{ \
+		if (!_n->key) continue; block } }
+
+#define GIT_HASHTABLE_FOREACH(self, pkey, pvalue, code)\
+	GIT_HASHTABLE__FOREACH(self,{(pkey)=_n->key;(pvalue)=_n->value;code;})
+
+#define GIT_HASHTABLE_FOREACH_KEY(self, pkey, code)\
+	GIT_HASHTABLE__FOREACH(self,{(pkey)=_n->key;code;})
+
+#define GIT_HASHTABLE_FOREACH_VALUE(self, pvalue, code)\
+	GIT_HASHTABLE__FOREACH(self,{(pvalue)=_n->value;code;})
 
 #define GIT_HASHTABLE_FOREACH_DELETE() {\
 	_node->key = NULL; _node->value = NULL; _self->key_count--;\
diff --git a/src/odb_pack.c b/src/odb_pack.c
index 9e1004e..249144a 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -159,9 +159,9 @@ static int pack_entry_find_prefix(struct git_pack_entry *e,
  *
  ***********************************************************/
 
-GIT_INLINE(void) pack_window_free_all(struct pack_backend *GIT_UNUSED(backend), struct git_pack_file *p)
+GIT_INLINE(void) pack_window_free_all(struct pack_backend *backend, struct git_pack_file *p)
 {
-	GIT_UNUSED_ARG(backend);
+	GIT_UNUSED(backend);
 	git_mwindow_free_all(&p->mwf);
 }
 
diff --git a/src/pkt.c b/src/pkt.c
index df972e7..51da55d 100644
--- a/src/pkt.c
+++ b/src/pkt.c
@@ -41,11 +41,11 @@ static int flush_pkt(git_pkt **out)
 }
 
 /* the rest of the line will be useful for multi_ack */
-static int ack_pkt(git_pkt **out, const char *GIT_UNUSED(line), size_t GIT_UNUSED(len))
+static int ack_pkt(git_pkt **out, const char *line, size_t len)
 {
 	git_pkt *pkt;
-	GIT_UNUSED_ARG(line);
-	GIT_UNUSED_ARG(len);
+	GIT_UNUSED(line);
+	GIT_UNUSED(len);
 
 	pkt = git__malloc(sizeof(git_pkt));
 	if (pkt == NULL)
diff --git a/src/refs.c b/src/refs.c
index 2e1d92d..f3388bf 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -105,7 +105,7 @@ static int reference_alloc(
 
 	reference->name = git__strdup(name);
 	if (reference->name == NULL) {
-		free(reference);
+		git__free(reference);
 		return GIT_ENOMEM;
 	}
 
@@ -222,7 +222,7 @@ static int loose_lookup(git_reference *ref)
 		return GIT_SUCCESS;
 
 	if (ref->flags & GIT_REF_SYMBOLIC) {
-		free(ref->target.symbolic);
+		git__free(ref->target.symbolic);
 		ref->target.symbolic = NULL;
 	}
 
@@ -278,7 +278,8 @@ static int loose_lookup_to_packfile(
 
 cleanup:
 	git_buf_free(&ref_file);
-	free(ref);
+	git__free(ref);
+
 	return git__rethrow(error, "Failed to lookup loose reference");
 }
 
@@ -420,7 +421,7 @@ static int packed_parse_oid(
 	return GIT_SUCCESS;
 
 cleanup:
-	free(ref);
+	git__free(ref);
 	return git__rethrow(error, "Failed to parse OID of packed reference");
 }
 
@@ -495,7 +496,7 @@ static int packed_load(git_repository *repo)
 
 		error = git_hashtable_insert(ref_cache->packfile, ref->name, ref);
 		if (error < GIT_SUCCESS) {
-			free(ref);
+			git__free(ref);
 			goto cleanup;
 		}
 	}
@@ -560,12 +561,12 @@ static int _dirent_loose_load(void *data, git_buf *full_path)
 		if (git_hashtable_insert2(
 			repository->references.packfile,
 			ref->name, ref, &old_ref) < GIT_SUCCESS) {
-			free(ref);
+			git__free(ref);
 			return GIT_ENOMEM;
 		}
 
 		if (old_ref != NULL)
-			free(old_ref);
+			git__free(old_ref);
 	}
 
 	return error == GIT_SUCCESS ?
@@ -773,9 +774,8 @@ static int packed_write(git_repository *repo)
 	/* Load all the packfile into a vector */
 	{
 		struct packref *reference;
-		const void *GIT_UNUSED(_unused);
 
-		GIT_HASHTABLE_FOREACH(repo->references.packfile, _unused, reference,
+		GIT_HASHTABLE_FOREACH_VALUE(repo->references.packfile, reference,
 			/* cannot fail: vector already has the right size */
 			git_vector_insert(&packing_list, reference);
 		);
@@ -929,7 +929,7 @@ static int packed_lookup(git_reference *ref)
 		return GIT_SUCCESS;
 
 	if (ref->flags & GIT_REF_SYMBOLIC) {
-		free(ref->target.symbolic);
+		git__free(ref->target.symbolic);
 		ref->target.symbolic = NULL;
 	}
 
@@ -1513,12 +1513,11 @@ int git_reference_foreach(
 	/* list all the packed references first */
 	if (list_flags & GIT_REF_PACKED) {
 		const char *ref_name;
-		void *GIT_UNUSED(_unused);
 
 		if ((error = packed_load(repo)) < GIT_SUCCESS)
 			return git__rethrow(error, "Failed to list references");
 
-		GIT_HASHTABLE_FOREACH(repo->references.packfile, ref_name, _unused,
+		GIT_HASHTABLE_FOREACH_KEY(repo->references.packfile, ref_name,
 			if ((error = callback(ref_name, payload)) < GIT_SUCCESS)
 				return git__throw(error,
 					"Failed to list references. User callback failed");
@@ -1595,12 +1594,10 @@ void git_repository__refcache_free(git_refcache *refs)
 	assert(refs);
 
 	if (refs->packfile) {
-		const void *GIT_UNUSED(_unused);
 		struct packref *reference;
 
-		GIT_HASHTABLE_FOREACH(refs->packfile, _unused, reference,
-			free(reference);
-		);
+		GIT_HASHTABLE_FOREACH_VALUE(
+			refs->packfile, reference, git__free(reference));
 
 		git_hashtable_free(refs->packfile);
 	}
diff --git a/src/remote.c b/src/remote.c
index 5b442e9..52b6aac 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -431,13 +431,13 @@ struct cb_data {
 	regex_t *preg;
 };
 
-static int remote_list_cb(const char *name, const char *GIT_UNUSED(value), void *data_)
+static int remote_list_cb(const char *name, const char *value, void *data_)
 {
 	struct cb_data *data = (struct cb_data *)data_;
 	size_t nmatch = 2;
 	regmatch_t pmatch[2];
 	int error;
-	GIT_UNUSED_ARG(value);
+	GIT_UNUSED(value);
 
 	if (!regexec(data->preg, name, nmatch, pmatch, 0)) {
 		char *remote_name = git__strndup(&name[pmatch[1].rm_so], pmatch[1].rm_eo - pmatch[1].rm_so);
diff --git a/src/revwalk.c b/src/revwalk.c
index cd971b5..997771f 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -590,7 +590,6 @@ int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo)
 void git_revwalk_free(git_revwalk *walk)
 {
 	unsigned int i;
-	const void *GIT_UNUSED(_unused);
 	commit_object *commit;
 
 	if (walk == NULL)
@@ -602,7 +601,7 @@ void git_revwalk_free(git_revwalk *walk)
 	/* if the parent has more than PARENTS_PER_COMMIT parents,
 	 * we had to allocate a separate array for those parents.
 	 * make sure it's being free'd */
-	GIT_HASHTABLE_FOREACH(walk->commits, _unused, commit, {
+	GIT_HASHTABLE_FOREACH_VALUE(walk->commits, commit, {
 		if (commit->out_degree > PARENTS_PER_COMMIT)
 			git__free(commit->parents);
 	});
@@ -669,12 +668,11 @@ int git_revwalk_next(git_oid *oid, git_revwalk *walk)
 
 void git_revwalk_reset(git_revwalk *walk)
 {
-	const void *GIT_UNUSED(_unused);
 	commit_object *commit;
 
 	assert(walk);
 
-	GIT_HASHTABLE_FOREACH(walk->commits, _unused, commit,
+	GIT_HASHTABLE_FOREACH_VALUE(walk->commits, commit,
 		commit->seen = 0;
 		commit->in_degree = 0;
 		commit->topo_delay = 0;
diff --git a/src/transport.c b/src/transport.c
index 672eb6e..785ddc3 100644
--- a/src/transport.c
+++ b/src/transport.c
@@ -43,9 +43,9 @@ static git_transport_cb transport_find_fn(const char *url)
  * Public API *
  **************/
 
-int git_transport_dummy(git_transport **GIT_UNUSED(transport))
+int git_transport_dummy(git_transport **transport)
 {
-	GIT_UNUSED_ARG(transport);
+	GIT_UNUSED(transport);
 	return git__throw(GIT_ENOTIMPLEMENTED, "This protocol isn't implemented. Sorry");
 }
 
diff --git a/src/transports/local.c b/src/transports/local.c
index 1dfc8ed..eb24db0 100644
--- a/src/transports/local.c
+++ b/src/transports/local.c
@@ -154,7 +154,7 @@ static int local_ls(git_transport *transport, git_headlist_cb list_cb, void *pay
  * Try to open the url as a git directory. The direction doesn't
  * matter in this case because we're calulating the heads ourselves.
  */
-static int local_connect(git_transport *transport, int GIT_UNUSED(direction))
+static int local_connect(git_transport *transport, int direction)
 {
 	git_repository *repo;
 	int error;
@@ -162,7 +162,7 @@ static int local_connect(git_transport *transport, int GIT_UNUSED(direction))
 	const char *path;
 	git_buf buf = GIT_BUF_INIT;
 
-	GIT_UNUSED_ARG(direction);
+	GIT_UNUSED(direction);
 
 	/* The repo layer doesn't want the prefix */
 	if (!git__prefixcmp(transport->url, "file://")) {
@@ -194,7 +194,7 @@ static int local_connect(git_transport *transport, int GIT_UNUSED(direction))
 	return GIT_SUCCESS;
 }
 
-static int local_close(git_transport *GIT_UNUSED(transport))
+static int local_close(git_transport *transport)
 {
 	transport_local *t = (transport_local *)transport;
 
diff --git a/src/win32/posix.h b/src/win32/posix.h
index f4c1c12..60adc96 100644
--- a/src/win32/posix.h
+++ b/src/win32/posix.h
@@ -11,20 +11,20 @@
 #include "fnmatch.h"
 #include "utf-conv.h"
 
-GIT_INLINE(int) p_link(const char *GIT_UNUSED(old), const char *GIT_UNUSED(new))
+GIT_INLINE(int) p_link(const char *old, const char *new)
 {
-	GIT_UNUSED_ARG(old);
-	GIT_UNUSED_ARG(new);
+	GIT_UNUSED(old);
+	GIT_UNUSED(new);
 	errno = ENOSYS;
 	return -1;
 }
 
-GIT_INLINE(int) p_mkdir(const char *path, mode_t GIT_UNUSED(mode))
+GIT_INLINE(int) p_mkdir(const char *path, mode_t mode)
 {
 	wchar_t* buf = gitwin_to_utf16(path);
 	int ret = _wmkdir(buf);
 
-	GIT_UNUSED_ARG(mode);
+	GIT_UNUSED(mode);
 
 	git__free(buf);
 	return ret;
diff --git a/src/win32/pthread.c b/src/win32/pthread.c
index cbce639..3db5368 100644
--- a/src/win32/pthread.c
+++ b/src/win32/pthread.c
@@ -8,10 +8,10 @@
 #include "pthread.h"
 
 int pthread_create(pthread_t *GIT_RESTRICT thread,
-					const pthread_attr_t *GIT_RESTRICT GIT_UNUSED(attr),
+					const pthread_attr_t *GIT_RESTRICT attr,
 					void *(*start_routine)(void*), void *GIT_RESTRICT arg)
 {
-	GIT_UNUSED_ARG(attr);
+	GIT_UNUSED(attr);
 	*thread = (pthread_t) CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, arg, 0, NULL);
 	return *thread ? GIT_SUCCESS : git__throw(GIT_EOSERR, "Failed to create pthread");
 }
@@ -26,9 +26,9 @@ int pthread_join(pthread_t thread, void **value_ptr)
 }
 
 int pthread_mutex_init(pthread_mutex_t *GIT_RESTRICT mutex,
-						const pthread_mutexattr_t *GIT_RESTRICT GIT_UNUSED(mutexattr))
+						const pthread_mutexattr_t *GIT_RESTRICT mutexattr)
 {
-	GIT_UNUSED_ARG(mutexattr);
+	GIT_UNUSED(mutexattr);
 	InitializeCriticalSection(mutex);
 	return 0;
 }
diff --git a/tests-clar/attr/repo.c b/tests-clar/attr/repo.c
index 9f6a49b..2afea23 100644
--- a/tests-clar/attr/repo.c
+++ b/tests-clar/attr/repo.c
@@ -104,12 +104,12 @@ void test_attr_repo__get_many(void)
 }
 
 static int count_attrs(
-	const char *GIT_UNUSED(name),
-	const char *GIT_UNUSED(value),
+	const char *name,
+	const char *value,
 	void *payload)
 {
-	GIT_UNUSED_ARG(name);
-	GIT_UNUSED_ARG(value);
+	GIT_UNUSED(name);
+	GIT_UNUSED(value);
 
 	*((int *)payload) += 1;
 
diff --git a/tests-clar/clar_helpers.c b/tests-clar/clar_helpers.c
index 1ef5a9b..22db56f 100644
--- a/tests-clar/clar_helpers.c
+++ b/tests-clar/clar_helpers.c
@@ -39,3 +39,52 @@ void cl_git_append2file(const char *filename, const char *new_content)
 	cl_must_pass(p_chmod(filename, 0644));
 }
 
+static const char *_cl_sandbox = NULL;
+static git_repository *_cl_repo = NULL;
+
+git_repository *cl_git_sandbox_init(const char *sandbox)
+{
+	/* Copy the whole sandbox folder from our fixtures to our test sandbox
+	 * area.  After this it can be accessed with `./sandbox`
+	 */
+	cl_fixture_sandbox(sandbox);
+	_cl_sandbox = sandbox;
+
+	p_chdir(sandbox);
+
+	/* Rename `sandbox/.gitted` to `sandbox/.git` which must be done since
+	 * we cannot store a folder named `.git` inside the fixtures folder of
+	 * our libgit2 repo.
+	 */
+	cl_git_pass(p_rename(".gitted", ".git"));
+
+	/* If we have `gitattributes`, rename to `.gitattributes`.  This may
+	 * be necessary if we don't want the attributes to be applied in the
+	 * libgit2 repo, but just during testing.
+	 */
+	if (p_access("gitattributes", F_OK) == 0)
+		cl_git_pass(p_rename("gitattributes", ".gitattributes"));
+
+	/* As with `gitattributes`, we may need `gitignore` just for testing. */
+	if (p_access("gitignore", F_OK) == 0)
+		cl_git_pass(p_rename("gitignore", ".gitignore"));
+
+	p_chdir("..");
+
+	/* Now open the sandbox repository and make it available for tests */
+	cl_git_pass(git_repository_open(&_cl_repo, sandbox));
+
+	return _cl_repo;
+}
+
+void cl_git_sandbox_cleanup(void)
+{
+	if (_cl_repo) {
+		git_repository_free(_cl_repo);
+		_cl_repo = NULL;
+	}
+	if (_cl_sandbox) {
+		cl_fixture_cleanup(_cl_sandbox);
+		_cl_sandbox = NULL;
+	}
+}
diff --git a/tests-clar/clar_libgit2.h b/tests-clar/clar_libgit2.h
index 43dc4e8..5c034a3 100644
--- a/tests-clar/clar_libgit2.h
+++ b/tests-clar/clar_libgit2.h
@@ -58,4 +58,9 @@ GIT_INLINE(void) cl_assert_strequal_internal(
 void cl_git_mkfile(const char *filename, const char *content);
 void cl_git_append2file(const char *filename, const char *new_content);
 
+/* Git sandbox setup helpers */
+
+git_repository *cl_git_sandbox_init(const char *sandbox);
+void cl_git_sandbox_cleanup(void);
+
 #endif
diff --git a/tests-clar/config/multivar.c b/tests-clar/config/multivar.c
index bccdc12..a8451ac 100644
--- a/tests-clar/config/multivar.c
+++ b/tests-clar/config/multivar.c
@@ -12,10 +12,12 @@ void test_config_multivar__cleanup(void)
 	cl_fixture_cleanup("config");
 }
 
-static int mv_read_cb(const char *name, const char *GIT_UNUSED(value), void *data)
+static int mv_read_cb(const char *name, const char *value, void *data)
 {
 	int *n = (int *) data;
 
+	GIT_UNUSED(value);
+
 	if (!strcmp(name, _name))
 		(*n)++;
 
@@ -35,10 +37,12 @@ void test_config_multivar__foreach(void)
 	git_config_free(cfg);
 }
 
-static int cb(const char *GIT_UNUSED(val), void *data)
+static int cb(const char *val, void *data)
 {
 	int *n = (int *) data;
 
+	GIT_UNUSED(val);
+
 	(*n)++;
 
 	return GIT_SUCCESS;
diff --git a/tests-clar/core/dirent.c b/tests-clar/core/dirent.c
index 7823709..9c366bf 100644
--- a/tests-clar/core/dirent.c
+++ b/tests-clar/core/dirent.c
@@ -88,10 +88,10 @@ static int one_entry(void *state, git_buf *path)
 	return GIT_ERROR;
 }
 
-static int dont_call_me(void *GIT_UNUSED(state), git_buf *GIT_UNUSED(path))
+static int dont_call_me(void *state, git_buf *path)
 {
-	GIT_UNUSED_ARG(state);
-	GIT_UNUSED_ARG(path);
+	GIT_UNUSED(state);
+	GIT_UNUSED(path);
 	return GIT_ERROR;
 }
 
diff --git a/tests-clar/diff/blob.c b/tests-clar/diff/blob.c
index 7aa8ceb..bdfe8ba 100644
--- a/tests-clar/diff/blob.c
+++ b/tests-clar/diff/blob.c
@@ -5,17 +5,12 @@ static git_repository *g_repo = NULL;
 
 void test_diff_blob__initialize(void)
 {
-	cl_fixture_sandbox("attr");
-	cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
-	cl_git_pass(p_rename("attr/gitattributes", "attr/.gitattributes"));
-	cl_git_pass(git_repository_open(&g_repo, "attr/.git"));
+	g_repo = cl_git_sandbox_init("attr");
 }
 
 void test_diff_blob__cleanup(void)
 {
-	git_repository_free(g_repo);
-	g_repo = NULL;
-	cl_fixture_cleanup("attr");
+	cl_git_sandbox_cleanup();
 }
 
 void test_diff_blob__0(void)
diff --git a/tests-clar/diff/index.c b/tests-clar/diff/index.c
index 0941c7c..171815d 100644
--- a/tests-clar/diff/index.c
+++ b/tests-clar/diff/index.c
@@ -5,16 +5,12 @@ static git_repository *g_repo = NULL;
 
 void test_diff_index__initialize(void)
 {
-	cl_fixture_sandbox("status");
-	cl_git_pass(p_rename("status/.gitted", "status/.git"));
-	cl_git_pass(git_repository_open(&g_repo, "status/.git"));
+	g_repo = cl_git_sandbox_init("status");
 }
 
 void test_diff_index__cleanup(void)
 {
-	git_repository_free(g_repo);
-	g_repo = NULL;
-	cl_fixture_cleanup("status");
+	cl_git_sandbox_cleanup();
 }
 
 void test_diff_index__0(void)
diff --git a/tests-clar/diff/iterator.c b/tests-clar/diff/iterator.c
index 1ad126c..3953fd8 100644
--- a/tests-clar/diff/iterator.c
+++ b/tests-clar/diff/iterator.c
@@ -2,37 +2,6 @@
 #include "diff_helpers.h"
 #include "iterator.h"
 
-static git_repository *g_repo = NULL;
-static const char *g_sandbox = NULL;
-
-static void setup_sandbox(const char *sandbox)
-{
-	cl_fixture_sandbox(sandbox);
-	g_sandbox = sandbox;
-
-	p_chdir(sandbox);
-	cl_git_pass(p_rename(".gitted", ".git"));
-	if (p_access("gitattributes", F_OK) == 0)
-		cl_git_pass(p_rename("gitattributes", ".gitattributes"));
-	if (p_access("gitignore", F_OK) == 0)
-		cl_git_pass(p_rename("gitignore", ".gitignore"));
-	p_chdir("..");
-
-	cl_git_pass(git_repository_open(&g_repo, sandbox));
-}
-
-static void cleanup_sandbox(void)
-{
-	if (g_repo) {
-		git_repository_free(g_repo);
-		g_repo = NULL;
-	}
-	if (g_sandbox) {
-		cl_fixture_cleanup(g_sandbox);
-		g_sandbox = NULL;
-	}
-}
-
 void test_diff_iterator__initialize(void)
 {
 	/* since we are doing tests with different sandboxes, defer setup
@@ -44,7 +13,7 @@ void test_diff_iterator__initialize(void)
 
 void test_diff_iterator__cleanup(void)
 {
-	cleanup_sandbox();
+	cl_git_sandbox_cleanup();
 }
 
 
@@ -60,11 +29,10 @@ static void tree_iterator_test(
 	git_iterator *i;
 	const git_index_entry *entry;
 	int count = 0;
+	git_repository *repo = cl_git_sandbox_init(sandbox);
 
-	setup_sandbox(sandbox);
-
-	cl_assert(t = resolve_commit_oid_to_tree(g_repo, treeish));
-	cl_git_pass(git_iterator_for_tree(g_repo, t, &i));
+	cl_assert(t = resolve_commit_oid_to_tree(repo, treeish));
+	cl_git_pass(git_iterator_for_tree(repo, t, &i));
 	cl_git_pass(git_iterator_current(i, &entry));
 
 	while (entry != NULL) {
@@ -183,10 +151,9 @@ static void index_iterator_test(
 	git_iterator *i;
 	const git_index_entry *entry;
 	int count = 0;
+	git_repository *repo = cl_git_sandbox_init(sandbox);
 
-	setup_sandbox(sandbox);
-
-	cl_git_pass(git_iterator_for_index(g_repo, &i));
+	cl_git_pass(git_iterator_for_index(repo, &i));
 	cl_git_pass(git_iterator_current(i, &entry));
 
 	while (entry != NULL) {
@@ -303,10 +270,9 @@ static void workdir_iterator_test(
 	git_iterator *i;
 	const git_index_entry *entry;
 	int count = 0, count_all = 0;
+	git_repository *repo = cl_git_sandbox_init(sandbox);
 
-	setup_sandbox(sandbox);
-
-	cl_git_pass(git_iterator_for_workdir(g_repo, &i));
+	cl_git_pass(git_iterator_for_workdir(repo, &i));
 	cl_git_pass(git_iterator_current(i, &entry));
 
 	while (entry != NULL) {
diff --git a/tests-clar/diff/tree.c b/tests-clar/diff/tree.c
index f5fdfba..91e1343 100644
--- a/tests-clar/diff/tree.c
+++ b/tests-clar/diff/tree.c
@@ -5,17 +5,12 @@ static git_repository *g_repo = NULL;
 
 void test_diff_tree__initialize(void)
 {
-	cl_fixture_sandbox("attr");
-	cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
-	cl_git_pass(p_rename("attr/gitattributes", "attr/.gitattributes"));
-	cl_git_pass(git_repository_open(&g_repo, "attr/.git"));
+	g_repo = cl_git_sandbox_init("attr");
 }
 
 void test_diff_tree__cleanup(void)
 {
-	git_repository_free(g_repo);
-	g_repo = NULL;
-	cl_fixture_cleanup("attr");
+	cl_git_sandbox_cleanup();
 }
 
 void test_diff_tree__0(void)
diff --git a/tests-clar/diff/workdir.c b/tests-clar/diff/workdir.c
index 7312a72..28cfa23 100644
--- a/tests-clar/diff/workdir.c
+++ b/tests-clar/diff/workdir.c
@@ -5,16 +5,12 @@ static git_repository *g_repo = NULL;
 
 void test_diff_workdir__initialize(void)
 {
-	cl_fixture_sandbox("status");
-	cl_git_pass(p_rename("status/.gitted", "status/.git"));
-	cl_git_pass(git_repository_open(&g_repo, "status/.git"));
+	g_repo = cl_git_sandbox_init("status");
 }
 
 void test_diff_workdir__cleanup(void)
 {
-	git_repository_free(g_repo);
-	g_repo = NULL;
-	cl_fixture_cleanup("status");
+	cl_git_sandbox_cleanup();
 }
 
 void test_diff_workdir__to_index(void)
diff --git a/tests-clar/status/ignore.c b/tests-clar/status/ignore.c
index 67aecba..99cb9e8 100644
--- a/tests-clar/status/ignore.c
+++ b/tests-clar/status/ignore.c
@@ -7,21 +7,12 @@ static git_repository *g_repo = NULL;
 
 void test_status_ignore__initialize(void)
 {
-	/* Before each test, instantiate the attr repo from the fixtures and
-	 * rename the .gitted to .git so it is a repo with a working dir.  Also
-	 * rename gitignore to .gitignore.
-	 */
-	cl_fixture_sandbox("attr");
-	cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
-	cl_git_pass(p_rename("attr/gitignore", "attr/.gitignore"));
-	cl_git_pass(git_repository_open(&g_repo, "attr/.git"));
+	g_repo = cl_git_sandbox_init("attr");
 }
 
 void test_status_ignore__cleanup(void)
 {
-	git_repository_free(g_repo);
-	g_repo = NULL;
-	cl_fixture_cleanup("attr");
+	cl_git_sandbox_cleanup();
 }
 
 void test_status_ignore__0(void)
diff --git a/tests-clar/status/worktree.c b/tests-clar/status/worktree.c
index 7d730bb..f654b8a 100644
--- a/tests-clar/status/worktree.c
+++ b/tests-clar/status/worktree.c
@@ -5,12 +5,6 @@
 
 
 /**
- * Test fixtures
- */
-static git_repository *_repository = NULL;
-
-
-/**
  * Auxiliary methods
  */
 static int
@@ -37,48 +31,27 @@ exit:
 }
 
 static int
-cb_status__count(const char *GIT_UNUSED(p), unsigned int GIT_UNUSED(s), void *payload)
+cb_status__count(const char *p, unsigned int s, void *payload)
 {
 	volatile int *count = (int *)payload;
 
-	GIT_UNUSED_ARG(p);
-	GIT_UNUSED_ARG(s);
+	GIT_UNUSED(p);
+	GIT_UNUSED(s);
 
-	*count++;
+	(*count)++;
 
 	return GIT_SUCCESS;
 }
 
 
-
 /**
  * Initializer
  *
- * This method is called once before starting each
- * test, and will load the required fixtures
+ * Not all of the tests in this file use the same fixtures, so we allow each
+ * test to load their fixture at the top of the test function.
  */
 void test_status_worktree__initialize(void)
 {
-	/*
-	 * Sandbox the `status/` repository from our Fixtures.
-	 * This will copy the whole folder to our sandbox,
-	 * so now it can be accessed with `./status`
-	 */
-	cl_fixture_sandbox("status");
-
-	/*
-	 * Rename `status/.gitted` to `status/.git`
-	 * We do this because we cannot store a folder named `.git`
-	 * inside the fixtures folder in our libgit2 repo.
-	 */
-	cl_git_pass(
-		p_rename("status/.gitted", "status/.git")
-	);
-
-	/*
-	 * Open the sandboxed "status" repository
-	 */
-	cl_git_pass(git_repository_open(&_repository, "status/.git"));
 }
 
 /**
@@ -89,10 +62,7 @@ void test_status_worktree__initialize(void)
  */
 void test_status_worktree__cleanup(void)
 {
-	git_repository_free(_repository);
-	_repository = NULL;
-
-	cl_fixture_cleanup("status");
+	cl_git_sandbox_cleanup();
 }
 
 /**
@@ -101,6 +71,7 @@ void test_status_worktree__cleanup(void)
 void test_status_worktree__whole_repository(void)
 {
 	struct status_entry_counts counts;
+	git_repository *repo = cl_git_sandbox_init("status");
 
 	memset(&counts, 0x0, sizeof(struct status_entry_counts));
 	counts.expected_entry_count = entry_count0;
@@ -108,7 +79,7 @@ void test_status_worktree__whole_repository(void)
 	counts.expected_statuses = entry_statuses0;
 
 	cl_git_pass(
-		git_status_foreach(_repository, cb_status__normal, &counts)
+		git_status_foreach(repo, cb_status__normal, &counts)
 	);
 
 	cl_assert(counts.entry_count == counts.expected_entry_count);
@@ -119,8 +90,10 @@ void test_status_worktree__whole_repository(void)
 void test_status_worktree__empty_repository(void)
 {
 	int count = 0;
+	git_repository *repo = cl_git_sandbox_init("empty_standard_repo");
+
+	cl_git_pass(git_status_foreach(repo, cb_status__count, &count));
 
-	git_status_foreach(_repository, cb_status__count, &count);
 	cl_assert(count == 0);
 }
 
@@ -128,10 +101,11 @@ void test_status_worktree__single_file(void)
 {
 	int i;
 	unsigned int status_flags;
+	git_repository *repo = cl_git_sandbox_init("status");
 
 	for (i = 0; i < (int)entry_count0; i++) {
 		cl_git_pass(
-			git_status_file(&status_flags, _repository, entry_paths0[i])
+			git_status_file(&status_flags, repo, entry_paths0[i])
 		);
 		cl_assert(entry_statuses0[i] == status_flags);
 	}
@@ -140,15 +114,22 @@ void test_status_worktree__single_file(void)
 void test_status_worktree__ignores(void)
 {
 	int i, ignored;
+	git_repository *repo = cl_git_sandbox_init("status");
 
 	for (i = 0; i < (int)entry_count0; i++) {
-		cl_git_pass(git_status_should_ignore(_repository, entry_paths0[i], &ignored));
+		cl_git_pass(
+			git_status_should_ignore(repo, entry_paths0[i], &ignored)
+		);
 		cl_assert(ignored == (entry_statuses0[i] == GIT_STATUS_WT_IGNORED));
 	}
 
-	cl_git_pass(git_status_should_ignore(_repository, "nonexistent_file", &ignored));
+	cl_git_pass(
+		git_status_should_ignore(repo, "nonexistent_file", &ignored)
+	);
 	cl_assert(!ignored);
 
-	cl_git_pass(git_status_should_ignore(_repository, "ignored_nonexistent_file", &ignored));
+	cl_git_pass(
+		git_status_should_ignore(repo, "ignored_nonexistent_file", &ignored)
+	);
 	cl_assert(ignored);
 }
diff --git a/tests/t00-core.c b/tests/t00-core.c
index aff48b0..7f142ba 100644
--- a/tests/t00-core.c
+++ b/tests/t00-core.c
@@ -424,10 +424,10 @@ static walk_data empty = {
 	GIT_BUF_INIT
 };
 
-static int dont_call_me(void *GIT_UNUSED(state), git_buf *GIT_UNUSED(path))
+static int dont_call_me(void *state, git_buf *path)
 {
-	GIT_UNUSED_ARG(state);
-	GIT_UNUSED_ARG(path);
+	GIT_UNUSED(state);
+	GIT_UNUSED(path);
 	return GIT_ERROR;
 }
 
diff --git a/tests/t07-hashtable.c b/tests/t07-hashtable.c
index 41d52af..6beaeac 100644
--- a/tests/t07-hashtable.c
+++ b/tests/t07-hashtable.c
@@ -154,7 +154,6 @@ BEGIN_TEST(tableit0, "iterate through all the contents of the table")
 	const int objects_n = 32;
 	int i;
 	table_item *objects, *ob;
-	const void *GIT_UNUSED(_unused);
 
 	git_hashtable *table = NULL;
 
@@ -170,9 +169,7 @@ BEGIN_TEST(tableit0, "iterate through all the contents of the table")
 		must_pass(git_hashtable_insert(table, &(objects[i].id), &(objects[i])));
 	}
 
-	GIT_HASHTABLE_FOREACH(table, _unused, ob,
-		ob->visited = 1;
-	);
+	GIT_HASHTABLE_FOREACH_VALUE(table, ob, ob->visited = 1);
 
 	/* make sure all nodes have been visited */
 	for (i = 0; i < objects_n; ++i)
diff --git a/tests/t18-status.c b/tests/t18-status.c
index 8bccb71..aeadd5e 100644
--- a/tests/t18-status.c
+++ b/tests/t18-status.c
@@ -156,14 +156,14 @@ BEGIN_TEST(statuscb0, "test retrieving status for worktree of repository")
 	git_futils_rmdir_r(TEMP_REPO_FOLDER, 1);
 END_TEST
 
-static int status_cb1(const char *GIT_UNUSED(path), unsigned int GIT_UNUSED(status_flags), void *payload)
+static int status_cb1(const char *path, unsigned int status_flags, void *payload)
 {
 	int *count = (int *)payload;;
 
-	GIT_UNUSED_ARG(path);
-	GIT_UNUSED_ARG(status_flags);
+	GIT_UNUSED(path);
+	GIT_UNUSED(status_flags);
 
-	(void) *count++;
+	(*count)++;
 
 	return GIT_SUCCESS;
 }
diff --git a/tests/test_main.c b/tests/test_main.c
index 732d25a..50256e9 100644
--- a/tests/test_main.c
+++ b/tests/test_main.c
@@ -70,12 +70,12 @@ int __cdecl
 #else
 int
 #endif
-main(int GIT_UNUSED(argc), char *GIT_UNUSED(argv[]))
+main(int argc, char *argv[])
 {
 	unsigned int i, failures;
 
-	GIT_UNUSED_ARG(argc);
-	GIT_UNUSED_ARG(argv);
+	GIT_UNUSED(argc);
+	GIT_UNUSED(argv);
 
 	git_threads_init();