Commit efc822ef0d8bb632f8ea5853321084193f5e95b9

Vicent Marti 2014-03-06T13:14:15

Merge pull request #2014 from mgbowen/cpp-options-init Function-based options initializers

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
diff --git a/include/git2/blame.h b/include/git2/blame.h
index 4ad51ee..b7fa9ae 100644
--- a/include/git2/blame.h
+++ b/include/git2/blame.h
@@ -83,6 +83,19 @@ typedef struct git_blame_options {
 #define GIT_BLAME_OPTIONS_INIT {GIT_BLAME_OPTIONS_VERSION}
 
 /**
+* Initializes a `git_blame_options` with default values. Equivalent to
+* creating an instance with GIT_BLAME_OPTIONS_INIT.
+*
+* @param opts the `git_blame_options` instance to initialize.
+* @param version the version of the struct; you should pass
+* `GIT_BLAME_OPTIONS_VERSION` here.
+* @return Zero on success; -1 on failure.
+*/
+GIT_EXTERN(int) git_blame_init_options(
+	git_blame_options* opts,
+	int version);
+
+/**
  * Structure that represents a blame hunk.
  *
  * - `lines_in_hunk` is the number of lines in this hunk
diff --git a/include/git2/checkout.h b/include/git2/checkout.h
index 0faf4ab..702e088 100644
--- a/include/git2/checkout.h
+++ b/include/git2/checkout.h
@@ -267,6 +267,19 @@ typedef struct git_checkout_opts {
 #define GIT_CHECKOUT_OPTS_INIT {GIT_CHECKOUT_OPTS_VERSION}
 
 /**
+* Initializes a `git_checkout_opts` with default values. Equivalent to
+* creating an instance with GIT_CHECKOUT_OPTS_INIT.
+*
+* @param opts the `git_checkout_opts` instance to initialize.
+* @param version the version of the struct; you should pass
+* `GIT_CHECKOUT_OPTS_VERSION` here.
+* @return Zero on success; -1 on failure.
+*/
+GIT_EXTERN(int) git_checkout_init_opts(
+	git_checkout_opts* opts,
+	int version);
+
+/**
  * Updates files in the index and the working tree to match the content of
  * the commit pointed at by HEAD.
  *
diff --git a/include/git2/clone.h b/include/git2/clone.h
index 3e885d1..98c6fb7 100644
--- a/include/git2/clone.h
+++ b/include/git2/clone.h
@@ -66,6 +66,19 @@ typedef struct git_clone_options {
 #define GIT_CLONE_OPTIONS_INIT {GIT_CLONE_OPTIONS_VERSION, {GIT_CHECKOUT_OPTS_VERSION, GIT_CHECKOUT_SAFE_CREATE}, GIT_REMOTE_CALLBACKS_INIT}
 
 /**
+* Initializes a `git_clone_options` with default values. Equivalent to
+* creating an instance with GIT_CLONE_OPTIONS_INIT.
+*
+* @param opts the `git_clone_options` instance to initialize.
+* @param version the version of the struct; you should pass
+* `GIT_CLONE_OPTIONS_VERSION` here.
+* @return Zero on success; -1 on failure.
+*/
+GIT_EXTERN(int) git_clone_init_options(
+	git_clone_options* opts,
+	int version);
+
+/**
  * Clone a remote repository.
  *
  * This version handles the simple case. If you'd like to create the
diff --git a/include/git2/diff.h b/include/git2/diff.h
index 943e2ec..f855f52 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -377,6 +377,19 @@ typedef struct {
 	{GIT_DIFF_OPTIONS_VERSION, 0, GIT_SUBMODULE_IGNORE_DEFAULT, {NULL,0}, NULL, NULL, 3}
 
 /**
+* Initializes a `git_diff_options` with default values. Equivalent to
+* creating an instance with GIT_DIFF_OPTIONS_INIT.
+*
+* @param opts the `git_diff_options` instance to initialize.
+* @param version the version of the struct; you should pass
+* `GIT_DIFF_OPTIONS_VERSION` here.
+* @return Zero on success; -1 on failure.
+*/
+GIT_EXTERN(int) git_diff_init_options(
+	git_diff_options* opts,
+	int version);
+
+/**
  * When iterating over a diff, callback that will be made per file.
  *
  * @param delta A pointer to the delta data for the file
@@ -604,6 +617,19 @@ typedef struct {
 #define GIT_DIFF_FIND_OPTIONS_VERSION 1
 #define GIT_DIFF_FIND_OPTIONS_INIT {GIT_DIFF_FIND_OPTIONS_VERSION}
 
+/**
+* Initializes a `git_diff_find_options` with default values. Equivalent to
+* creating an instance with GIT_DIFF_FIND_OPTIONS_INIT.
+*
+* @param opts the `git_diff_find_options` instance to initialize.
+* @param version the version of the struct; you should pass
+* `GIT_DIFF_FIND_OPTIONS_VERSION` here.
+* @return Zero on success; -1 on failure.
+*/
+GIT_EXTERN(int) git_diff_find_init_options(
+	git_diff_find_options* opts,
+	int version);
+
 /** @name Diff Generator Functions
  *
  * These are the functions you would use to create (or destroy) a
diff --git a/include/git2/merge.h b/include/git2/merge.h
index b45d0fd..dc89a04 100644
--- a/include/git2/merge.h
+++ b/include/git2/merge.h
@@ -104,6 +104,18 @@ typedef struct {
 #define GIT_MERGE_TREE_OPTS_VERSION 1
 #define GIT_MERGE_TREE_OPTS_INIT {GIT_MERGE_TREE_OPTS_VERSION}
 
+/**
+ * Initializes a `git_merge_tree_opts` with default values. Equivalent to
+ * creating an instance with GIT_MERGE_TREE_OPTS_INIT.
+ *
+ * @param opts the `git_merge_tree_opts` instance to initialize.
+ * @param version the version of the struct; you should pass
+ *        `GIT_MERGE_TREE_OPTS_VERSION` here.
+ * @return Zero on success; -1 on failure.
+ */
+GIT_EXTERN(int) git_merge_tree_init_opts(
+	git_merge_tree_opts* opts,
+	int version);
 
 /**
  * Option flags for `git_merge`.
@@ -144,6 +156,18 @@ typedef struct {
 #define GIT_MERGE_OPTS_VERSION 1
 #define GIT_MERGE_OPTS_INIT {GIT_MERGE_OPTS_VERSION, 0, GIT_MERGE_TREE_OPTS_INIT, GIT_CHECKOUT_OPTS_INIT}
 
+/**
+ * Initializes a `git_merge_opts` with default values. Equivalent to creating
+ * an instance with GIT_MERGE_OPTS_INIT.
+ *
+ * @param opts the `git_merge_opts` instance to initialize.
+ * @param version the version of the struct; you should pass
+ *        `GIT_MERGE_OPTS_VERSION` here.
+ * @return Zero on success; -1 on failure.
+ */
+GIT_EXTERN(int) git_merge_init_opts(
+	git_merge_opts* opts,
+	int version);
 
 /**
  * Find a merge base between two commits
diff --git a/include/git2/push.h b/include/git2/push.h
index 67702ac..899d21e 100644
--- a/include/git2/push.h
+++ b/include/git2/push.h
@@ -39,6 +39,19 @@ typedef struct {
 #define GIT_PUSH_OPTIONS_VERSION 1
 #define GIT_PUSH_OPTIONS_INIT { GIT_PUSH_OPTIONS_VERSION }
 
+/**
+ * Initializes a `git_push_options` with default values. Equivalent to
+ * creating an instance with GIT_PUSH_OPTIONS_INIT.
+ *
+ * @param opts the `git_push_options` instance to initialize.
+ * @param version the version of the struct; you should pass
+ *        `GIT_PUSH_OPTIONS_VERSION` here.
+ * @return Zero on success; -1 on failure.
+ */
+GIT_EXTERN(int) git_push_init_options(
+	git_push_options* opts,
+	int version);
+
 /** Push network progress notification function */
 typedef int (*git_push_transfer_progress)(
 	unsigned int current,
diff --git a/include/git2/remote.h b/include/git2/remote.h
index 238b6fd..82a46ac 100644
--- a/include/git2/remote.h
+++ b/include/git2/remote.h
@@ -495,6 +495,19 @@ struct git_remote_callbacks {
 #define GIT_REMOTE_CALLBACKS_INIT {GIT_REMOTE_CALLBACKS_VERSION}
 
 /**
+ * Initializes a `git_remote_callbacks` with default values. Equivalent to
+ * creating an instance with GIT_REMOTE_CALLBACKS_INIT.
+ *
+ * @param opts the `git_remote_callbacks` instance to initialize.
+ * @param version the version of the struct; you should pass
+ *        `GIT_REMOTE_CALLBACKS_VERSION` here.
+ * @return Zero on success; -1 on failure.
+ */
+GIT_EXTERN(int) git_remote_init_callbacks(
+	git_remote_callbacks* opts,
+	int version);
+
+/**
  * Set the callbacks for a remote
  *
  * Note that the remote keeps its own copy of the data and you need to
diff --git a/include/git2/repository.h b/include/git2/repository.h
index bf12c7a..4433e71 100644
--- a/include/git2/repository.h
+++ b/include/git2/repository.h
@@ -268,6 +268,19 @@ typedef struct {
 #define GIT_REPOSITORY_INIT_OPTIONS_INIT {GIT_REPOSITORY_INIT_OPTIONS_VERSION}
 
 /**
+ * Initializes a `git_repository_init_options` with default values. Equivalent
+ * to creating an instance with GIT_REPOSITORY_INIT_OPTIONS_INIT.
+ *
+ * @param opts the `git_repository_init_options` instance to initialize.
+ * @param version the version of the struct; you should pass
+ *        `GIT_REPOSITORY_INIT_OPTIONS_VERSION` here.
+ * @return Zero on success; -1 on failure.
+ */
+GIT_EXTERN(int) git_repository_init_init_options(
+	git_repository_init_options* opts,
+	int version);
+
+/**
  * Create a new Git repository in the given folder with extended controls.
  *
  * This will initialize a new git repository (creating the repo_path
diff --git a/include/git2/revert.h b/include/git2/revert.h
index 86a6e26..088bda9 100644
--- a/include/git2/revert.h
+++ b/include/git2/revert.h
@@ -34,6 +34,19 @@ typedef struct {
 #define GIT_REVERT_OPTS_INIT {GIT_REVERT_OPTS_VERSION, 0, GIT_MERGE_TREE_OPTS_INIT, GIT_CHECKOUT_OPTS_INIT}
 
 /**
+ * Initializes a `git_revert_opts` with default values. Equivalent to
+ * creating an instance with GIT_REVERT_OPTS_INIT.
+ *
+ * @param opts the `git_revert_opts` instance to initialize.
+ * @param version the version of the struct; you should pass
+ *        `GIT_REVERT_OPTS_VERSION` here.
+ * @return Zero on success; -1 on failure.
+ */
+GIT_EXTERN(int) git_revert_init_opts(
+	git_revert_opts* opts,
+	int version);
+
+/**
  * Reverts the given commit against the given "our" commit, producing an
  * index that reflects the result of the revert.
  *
diff --git a/include/git2/status.h b/include/git2/status.h
index aa68c0d..8491845 100644
--- a/include/git2/status.h
+++ b/include/git2/status.h
@@ -175,6 +175,19 @@ typedef struct {
 #define GIT_STATUS_OPTIONS_INIT {GIT_STATUS_OPTIONS_VERSION}
 
 /**
+ * Initializes a `git_status_options` with default values. Equivalent to
+ * creating an instance with GIT_STATUS_OPTIONS_INIT.
+ *
+ * @param opts the `git_status_options` instance to initialize.
+ * @param version the version of the struct; you should pass
+ *        `GIT_STATUS_OPTIONS_VERSION` here.
+ * @return Zero on success; -1 on failure.
+ */
+GIT_EXTERN(int) git_status_init_options(
+	git_status_options* opts,
+	int version);
+
+/**
  * A status entry, providing the differences between the file as it exists
  * in HEAD and the index, and providing the differences between the index
  * and the working directory.
diff --git a/include/git2/sys/config.h b/include/git2/sys/config.h
index 419ad7e..3df2ba3 100644
--- a/include/git2/sys/config.h
+++ b/include/git2/sys/config.h
@@ -70,6 +70,19 @@ struct git_config_backend {
 #define GIT_CONFIG_BACKEND_INIT {GIT_CONFIG_BACKEND_VERSION}
 
 /**
+ * Initializes a `git_config_backend` with default values. Equivalent to
+ * creating an instance with GIT_CONFIG_BACKEND_INIT.
+ *
+ * @param opts the `git_config_backend` instance to initialize.
+ * @param version the version of the struct; you should pass
+ *        `GIT_CONFIG_BACKEND_VERSION` here.
+ * @return Zero on success; -1 on failure.
+ */
+GIT_EXTERN(int) git_config_init_backend(
+	git_config_backend* backend,
+	int version);
+
+/**
  * Add a generic config file instance to an existing config
  *
  * Note that the configuration object will free the file
diff --git a/include/git2/sys/odb_backend.h b/include/git2/sys/odb_backend.h
index 4917ba0..81bb082 100644
--- a/include/git2/sys/odb_backend.h
+++ b/include/git2/sys/odb_backend.h
@@ -89,6 +89,19 @@ struct git_odb_backend {
 #define GIT_ODB_BACKEND_VERSION 1
 #define GIT_ODB_BACKEND_INIT {GIT_ODB_BACKEND_VERSION}
 
+/**
+ * Initializes a `git_odb_backend` with default values. Equivalent to
+ * creating an instance with GIT_ODB_BACKEND_INIT.
+ *
+ * @param opts the `git_odb_backend` instance to initialize.
+ * @param version the version of the struct; you should pass
+ *        `GIT_ODB_BACKEND_VERSION` here.
+ * @return Zero on success; -1 on failure.
+ */
+GIT_EXTERN(int) git_odb_init_backend(
+	git_odb_backend* backend,
+	int version);
+
 GIT_EXTERN(void *) git_odb_backend_malloc(git_odb_backend *backend, size_t len);
 
 GIT_END_DECL
diff --git a/include/git2/sys/refdb_backend.h b/include/git2/sys/refdb_backend.h
index aa5ef9e..dce142c 100644
--- a/include/git2/sys/refdb_backend.h
+++ b/include/git2/sys/refdb_backend.h
@@ -159,6 +159,19 @@ struct git_refdb_backend {
 #define GIT_REFDB_BACKEND_INIT {GIT_REFDB_BACKEND_VERSION}
 
 /**
+ * Initializes a `git_refdb_backend` with default values. Equivalent to
+ * creating an instance with GIT_REFDB_BACKEND_INIT.
+ *
+ * @param opts the `git_refdb_backend` instance to initialize.
+ * @param version the version of the struct; you should pass
+ *        `GIT_REFDB_BACKEND_VERSION` here.
+ * @return Zero on success; -1 on failure.
+ */
+GIT_EXTERN(int) git_refdb_init_backend(
+	git_refdb_backend* backend,
+	int version);
+
+/**
  * Constructors for default filesystem-based refdb backend
  *
  * Under normal usage, this is called for you when the repository is
diff --git a/include/git2/transport.h b/include/git2/transport.h
index 0393210..f2b0c63 100644
--- a/include/git2/transport.h
+++ b/include/git2/transport.h
@@ -280,6 +280,19 @@ struct git_transport {
 #define GIT_TRANSPORT_INIT {GIT_TRANSPORT_VERSION}
 
 /**
+ * Initializes a `git_transport` with default values. Equivalent to
+ * creating an instance with GIT_TRANSPORT_INIT.
+ *
+ * @param opts the `git_transport` instance to initialize.
+ * @param version the version of the struct; you should pass
+ *        `GIT_TRANSPORT_VERSION` here.
+ * @return Zero on success; -1 on failure.
+ */
+GIT_EXTERN(int) git_transport_init(
+	git_transport* opts,
+	int version);
+
+/**
  * Function to use to create a transport from a URL. The transport database
  * is scanned to find a transport that implements the scheme of the URI (i.e.
  * git:// or http://) and a transport object is returned to the caller.
diff --git a/src/blame.c b/src/blame.c
index 71bc460..01f88b7 100644
--- a/src/blame.c
+++ b/src/blame.c
@@ -476,3 +476,15 @@ int git_blame_buffer(
 	*out = blame;
 	return 0;
 }
+
+int git_blame_init_options(git_blame_options* opts, int version)
+{
+	if (version != GIT_BLAME_OPTIONS_VERSION) {
+		giterr_set(GITERR_INVALID, "Invalid version %d for git_blame_options", version);
+		return -1;
+	} else {
+		git_blame_options o = GIT_BLAME_OPTIONS_INIT;
+		memcpy(opts, &o, sizeof(o));
+		return 0;
+	}
+}
diff --git a/src/checkout.c b/src/checkout.c
index 72fe536..4ef8da0 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -2194,3 +2194,15 @@ int git_checkout_head(
 	assert(repo);
 	return git_checkout_tree(repo, NULL, opts);
 }
+
+int git_checkout_init_opts(git_checkout_opts* opts, int version)
+{
+	if (version != GIT_CHECKOUT_OPTS_VERSION) {
+		giterr_set(GITERR_INVALID, "Invalid version %d for git_checkout_opts", version);
+		return -1;
+	} else {
+		git_checkout_opts o = GIT_CHECKOUT_OPTS_INIT;
+		memcpy(opts, &o, sizeof(o));
+		return 0;
+	}
+}
diff --git a/src/clone.c b/src/clone.c
index bcc3867..b5333bd 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -439,3 +439,15 @@ int git_clone(
 	*out = repo;
 	return error;
 }
+
+int git_clone_init_options(git_clone_options* opts, int version)
+{
+	if (version != GIT_CLONE_OPTIONS_VERSION) {
+		giterr_set(GITERR_INVALID, "Invalid version %d for git_clone_options", version);
+		return -1;
+	} else {
+		git_clone_options o = GIT_CLONE_OPTIONS_INIT;
+		memcpy(opts, &o, sizeof(o));
+		return 0;
+	}
+}
diff --git a/src/config.c b/src/config.c
index ae093ed..1a205fe 100644
--- a/src/config.c
+++ b/src/config.c
@@ -1245,3 +1245,15 @@ cleanup:
 
 	return error;
 }
+
+int git_config_init_backend(git_config_backend* backend, int version)
+{
+	if (version != GIT_CONFIG_BACKEND_VERSION) {
+		giterr_set(GITERR_INVALID, "Invalid version %d for git_config_backend", version);
+		return -1;
+	} else {
+		git_config_backend b = GIT_CONFIG_BACKEND_INIT;
+		memcpy(backend, &b, sizeof(b));
+		return 0;
+	}
+}
diff --git a/src/diff.c b/src/diff.c
index 151990e..dc7735f 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -1413,3 +1413,27 @@ int git_diff__paired_foreach(
 
 	return error;
 }
+
+int git_diff_init_options(git_diff_options* opts, int version)
+{
+	if (version != GIT_DIFF_OPTIONS_VERSION) {
+		giterr_set(GITERR_INVALID, "Invalid version %d for git_diff_options", version);
+		return -1;
+	} else {
+		git_diff_options o = GIT_DIFF_OPTIONS_INIT;
+		memcpy(opts, &o, sizeof(o));
+		return 0;
+	}
+}
+
+int git_diff_find_init_options(git_diff_find_options* opts, int version)
+{
+	if (version != GIT_DIFF_FIND_OPTIONS_VERSION) {
+		giterr_set(GITERR_INVALID, "Invalid version %d for git_diff_find_options", version);
+		return -1;
+	} else {
+		git_diff_find_options o = GIT_DIFF_FIND_OPTIONS_INIT;
+		memcpy(opts, &o, sizeof(o));
+		return 0;
+	}
+}
diff --git a/src/merge.c b/src/merge.c
index 12ff1c9..124e8c3 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -2744,3 +2744,27 @@ void git_merge_head_free(git_merge_head *head)
 
 	git__free(head);
 }
+
+int git_merge_init_opts(git_merge_opts* opts, int version)
+{
+	if (version != GIT_MERGE_OPTS_VERSION) {
+		giterr_set(GITERR_INVALID, "Invalid version %d for git_merge_opts", version);
+		return -1;
+	} else {
+		git_merge_opts o = GIT_MERGE_OPTS_INIT;
+		memcpy(opts, &o, sizeof(o));
+		return 0;
+	}
+}
+
+int git_merge_tree_init_opts(git_merge_tree_opts* opts, int version)
+{
+	if (version != GIT_MERGE_TREE_OPTS_VERSION) {
+		giterr_set(GITERR_INVALID, "Invalid version %d for git_merge_tree_opts", version);
+		return -1;
+	} else {
+		git_merge_tree_opts o = GIT_MERGE_TREE_OPTS_INIT;
+		memcpy(opts, &o, sizeof(o));
+		return 0;
+	}
+}
diff --git a/src/odb.c b/src/odb.c
index 30bba96..085eda5 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -1113,3 +1113,14 @@ int git_odb__error_ambiguous(const char *message)
 	return GIT_EAMBIGUOUS;
 }
 
+int git_odb_init_backend(git_odb_backend* backend, int version)
+{
+	if (version != GIT_ODB_BACKEND_VERSION) {
+		giterr_set(GITERR_INVALID, "Invalid version %d for git_odb_backend", version);
+		return -1;
+	} else {
+		git_odb_backend b = GIT_ODB_BACKEND_INIT;
+		memcpy(backend, &b, sizeof(b));
+		return 0;
+	}
+}
diff --git a/src/push.c b/src/push.c
index c294780..5213556 100644
--- a/src/push.c
+++ b/src/push.c
@@ -705,3 +705,15 @@ void git_push_free(git_push *push)
 
 	git__free(push);
 }
+
+int git_push_init_options(git_push_options* opts, int version)
+{
+	if (version != GIT_PUSH_OPTIONS_VERSION) {
+		giterr_set(GITERR_INVALID, "Invalid version %d for git_push_options", version);
+		return -1;
+	} else {
+		git_push_options o = GIT_PUSH_OPTIONS_INIT;
+		memcpy(opts, &o, sizeof(o));
+		return 0;
+	}
+}
diff --git a/src/refdb.c b/src/refdb.c
index 984c3c7..3e7a592 100644
--- a/src/refdb.c
+++ b/src/refdb.c
@@ -235,3 +235,15 @@ int git_refdb_ensure_log(git_refdb *db, const char *refname)
 
 	return db->backend->ensure_log(db->backend, refname);
 }
+
+int git_refdb_init_backend(git_refdb_backend* backend, int version)
+{
+	if (version != GIT_REFDB_BACKEND_VERSION) {
+		giterr_set(GITERR_INVALID, "Invalid version %d for git_refdb_backend", version);
+		return -1;
+	} else {
+		git_refdb_backend b = GIT_REFDB_BACKEND_INIT;
+		memcpy(backend, &b, sizeof(b));
+		return 0;
+	}
+}
diff --git a/src/remote.c b/src/remote.c
index 6f97d56..caefc68 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -1731,3 +1731,15 @@ const git_refspec *git_remote_get_refspec(const git_remote *remote, size_t n)
 {
 	return git_vector_get(&remote->refspecs, n);
 }
+
+int git_remote_init_callbacks(git_remote_callbacks* opts, int version)
+{
+	if (version != GIT_REMOTE_CALLBACKS_VERSION) {
+		giterr_set(GITERR_INVALID, "Invalid version %d for git_remote_callbacks", version);
+		return -1;
+	} else {
+		git_remote_callbacks o = GIT_REMOTE_CALLBACKS_INIT;
+		memcpy(opts, &o, sizeof(o));
+		return 0;
+	}
+}
diff --git a/src/repository.c b/src/repository.c
index b94973c..fccc16f 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -2010,3 +2010,15 @@ int git_repository_is_shallow(git_repository *repo)
 		return error;
 	return st.st_size == 0 ? 0 : 1;
 }
+
+int git_repository_init_init_options(git_repository_init_options* opts, int version)
+{
+	if (version != GIT_REPOSITORY_INIT_OPTIONS_VERSION) {
+		giterr_set(GITERR_INVALID, "Invalid version %d for git_repository_init_options", version);
+		return -1;
+	} else {
+		git_repository_init_options o = GIT_REPOSITORY_INIT_OPTIONS_INIT;
+		memcpy(opts, &o, sizeof(o));
+		return 0;
+	}
+}
diff --git a/src/revert.c b/src/revert.c
index 4ba3299..a397a8e 100644
--- a/src/revert.c
+++ b/src/revert.c
@@ -218,3 +218,15 @@ done:
 
 	return error;
 }
+
+int git_revert_init_opts(git_revert_opts* opts, int version)
+{
+	if (version != GIT_REVERT_OPTS_VERSION) {
+		giterr_set(GITERR_INVALID, "Invalid version %d for git_revert_opts", version);
+		return -1;
+	} else {
+		git_revert_opts o = GIT_REVERT_OPTS_INIT;
+		memcpy(opts, &o, sizeof(o));
+		return 0;
+	}
+}
diff --git a/src/status.c b/src/status.c
index 3c95b34..c4b990a 100644
--- a/src/status.c
+++ b/src/status.c
@@ -495,3 +495,14 @@ int git_status_should_ignore(
 	return git_ignore_path_is_ignored(ignored, repo, path);
 }
 
+int git_status_init_options(git_status_options* opts, int version)
+{
+	if (version != GIT_STATUS_OPTIONS_VERSION) {
+		giterr_set(GITERR_INVALID, "Invalid version %d for git_status_options", version);
+		return -1;
+	} else {
+		git_status_options o = GIT_STATUS_OPTIONS_INIT;
+		memcpy(opts, &o, sizeof(o));
+		return 0;
+	}
+}
diff --git a/src/transport.c b/src/transport.c
index 2b0c6a1..dc074a5 100644
--- a/src/transport.c
+++ b/src/transport.c
@@ -217,3 +217,15 @@ int git_remote_supported_url(const char* url)
 
 	return fn != &git_transport_dummy;
 }
+
+int git_transport_init(git_transport* opts, int version)
+{
+	if (version != GIT_TRANSPORT_VERSION) {
+		giterr_set(GITERR_INVALID, "Invalid version %d for git_transport", version);
+		return -1;
+	} else {
+		git_transport o = GIT_TRANSPORT_INIT;
+		memcpy(opts, &o, sizeof(o));
+		return 0;
+	}
+}
diff --git a/tests/structinit/structinit.c b/tests/structinit/structinit.c
new file mode 100644
index 0000000..db7ee07
--- /dev/null
+++ b/tests/structinit/structinit.c
@@ -0,0 +1,120 @@
+#include "clar_libgit2.h"
+#include <git2/sys/config.h>
+#include <git2/sys/odb_backend.h>
+#include <git2/sys/refdb_backend.h>
+
+#define STRINGIFY(s) #s
+
+/* Checks two conditions for the specified structure:
+ *     1. That the initializers for the latest version produces the same
+ *        in-memory representation.
+ *     2. That the function-based initializer supports all versions from 1...n,
+ *        where n is the latest version (often represented by GIT_*_VERSION).
+ *
+ * Parameters:
+ *     structname: The name of the structure to test, e.g. git_blame_options.
+ *     structver: The latest version of the specified structure.
+ *     macroinit: The macro that initializes the latest version of the structure.
+ *     funcinitname: The function that initializes the structure. Must have the
+ *                   signature "int (structname* instance, int version)".
+ */
+#define CHECK_MACRO_FUNC_INIT_EQUAL(structname, structver, macroinit, funcinitname) \
+	structname structname##_macro_latest = macroinit; \
+	structname structname##_func_latest; \
+	cl_git_pass(funcinitname(&structname##_func_latest, structver)); \
+	cl_check_( \
+		memcmp(&structname##_macro_latest, &structname##_func_latest, \
+			sizeof(structname)) == 0, \
+		"Macro-based and function-based initializer for " STRINGIFY(structname) \
+			" are not equivalent."); \
+	\
+	int structname##_curr_ver = structver - 1; \
+	while (structname##_curr_ver > 0) \
+	{ \
+		structname macro; \
+		cl_git_pass(funcinitname(&macro, structname##_curr_ver)); \
+		structname##_curr_ver--; \
+	}
+
+void test_structinit_structinit__compare(void)
+{
+	/* blame */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_blame_options, GIT_BLAME_OPTIONS_VERSION, \
+		GIT_BLAME_OPTIONS_INIT, git_blame_init_options);
+
+	/* checkout */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_checkout_opts, GIT_CHECKOUT_OPTS_VERSION, \
+		GIT_CHECKOUT_OPTS_INIT, git_checkout_init_opts);
+
+	/* clone */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_clone_options, GIT_CLONE_OPTIONS_VERSION, \
+		GIT_CLONE_OPTIONS_INIT, git_clone_init_options);
+
+	/* diff */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_diff_options, GIT_DIFF_OPTIONS_VERSION, \
+		GIT_DIFF_OPTIONS_INIT, git_diff_init_options);
+
+	/* diff_find */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_diff_find_options, GIT_DIFF_FIND_OPTIONS_VERSION, \
+		GIT_DIFF_FIND_OPTIONS_INIT, git_diff_find_init_options);
+
+	/* merge_tree */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_merge_tree_opts, GIT_MERGE_TREE_OPTS_VERSION, \
+		GIT_MERGE_TREE_OPTS_INIT, git_merge_tree_init_opts);
+
+	/* merge */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_merge_opts, GIT_MERGE_OPTS_VERSION, \
+		GIT_MERGE_OPTS_INIT, git_merge_init_opts);
+
+	/* push */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_push_options, GIT_PUSH_OPTIONS_VERSION, \
+		GIT_PUSH_OPTIONS_INIT, git_push_init_options);
+
+	/* remote */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_remote_callbacks, GIT_REMOTE_CALLBACKS_VERSION, \
+		GIT_REMOTE_CALLBACKS_INIT, git_remote_init_callbacks);
+
+	/* repository_init */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_repository_init_options, GIT_REPOSITORY_INIT_OPTIONS_VERSION, \
+		GIT_REPOSITORY_INIT_OPTIONS_INIT, git_repository_init_init_options);
+
+	/* revert */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_revert_opts, GIT_REVERT_OPTS_VERSION, \
+		GIT_REVERT_OPTS_INIT, git_revert_init_opts);
+
+	/* status */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_status_options, GIT_STATUS_OPTIONS_VERSION, \
+		GIT_STATUS_OPTIONS_INIT, git_status_init_options);
+
+	/* transport */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_transport, GIT_TRANSPORT_VERSION, \
+		GIT_TRANSPORT_INIT, git_transport_init);
+
+	/* config_backend */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_config_backend, GIT_CONFIG_BACKEND_VERSION, \
+		GIT_CONFIG_BACKEND_INIT, git_config_init_backend);
+
+	/* odb_backend */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_odb_backend, GIT_ODB_BACKEND_VERSION, \
+		GIT_ODB_BACKEND_INIT, git_odb_init_backend);
+
+	/* refdb_backend */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_refdb_backend, GIT_REFDB_BACKEND_VERSION, \
+		GIT_REFDB_BACKEND_INIT, git_refdb_init_backend);
+}