Commit bc91347b5894c98964a12c6637d5ca91d9723b42

Russell Belfer 2014-04-30T11:16:31

Fix remaining init_options inconsistencies There were a couple of "init_opts()" functions a few more cases of structure initialization that I somehow missed.

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
diff --git a/include/git2/checkout.h b/include/git2/checkout.h
index 69addb7..494f674 100644
--- a/include/git2/checkout.h
+++ b/include/git2/checkout.h
@@ -270,14 +270,13 @@ typedef struct git_checkout_options {
 * Initializes a `git_checkout_options` with default values. Equivalent to
 * creating an instance with GIT_CHECKOUT_OPTIONS_INIT.
 *
-* @param opts the `git_checkout_options` instance to initialize.
-* @param version the version of the struct; you should pass
-* `GIT_CHECKOUT_OPTIONS_VERSION` here.
+* @param opts the `git_checkout_options` struct to initialize.
+* @param version Version of struct; pass `GIT_CHECKOUT_OPTIONS_VERSION`
 * @return Zero on success; -1 on failure.
 */
-GIT_EXTERN(int) git_checkout_init_opts(
-	git_checkout_options* opts,
-	int version);
+GIT_EXTERN(int) git_checkout_init_options(
+	git_checkout_options *opts,
+	unsigned int version);
 
 /**
  * Updates files in the index and the working tree to match the content of
diff --git a/include/git2/cherrypick.h b/include/git2/cherrypick.h
index 7c48e66..e998d32 100644
--- a/include/git2/cherrypick.h
+++ b/include/git2/cherrypick.h
@@ -37,14 +37,13 @@ typedef struct {
  * Initializes a `git_cherry_pick_options` with default values. Equivalent to
  * creating an instance with GIT_CHERRY_PICK_OPTIONS_INIT.
  *
- * @param opts the `git_cherry_pick_options` instance to initialize.
- * @param version the version of the struct; you should pass
- *        `GIT_CHERRY_PICK_OPTIONS_VERSION` here.
+ * @param opts the `git_cherry_pick_options` struct to initialize
+ * @param version Version of struct; pass `GIT_CHERRY_PICK_OPTIONS_VERSION`
  * @return Zero on success; -1 on failure.
  */
-GIT_EXTERN(int) git_cherry_pick_init_opts(
-	git_cherry_pick_options* opts,
-	int version);
+GIT_EXTERN(int) git_cherry_pick_init_options(
+	git_cherry_pick_options *opts,
+	unsigned int version);
 
 /**
  * Cherry-picks the given commit against the given "our" commit, producing an
diff --git a/include/git2/remote.h b/include/git2/remote.h
index 11e1e26..3633501 100644
--- a/include/git2/remote.h
+++ b/include/git2/remote.h
@@ -501,14 +501,13 @@ struct git_remote_callbacks {
  * 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.
+ * @param opts the `git_remote_callbacks` struct to initialize
+ * @param version Version of struct; pass `GIT_REMOTE_CALLBACKS_VERSION`
  * @return Zero on success; -1 on failure.
  */
 GIT_EXTERN(int) git_remote_init_callbacks(
-	git_remote_callbacks* opts,
-	int version);
+	git_remote_callbacks *opts,
+	unsigned int version);
 
 /**
  * Set the callbacks for a remote
diff --git a/include/git2/revert.h b/include/git2/revert.h
index 3a6beb6..da37fbe 100644
--- a/include/git2/revert.h
+++ b/include/git2/revert.h
@@ -37,14 +37,13 @@ typedef struct {
  * Initializes a `git_revert_options` with default values. Equivalent to
  * creating an instance with GIT_REVERT_OPTIONS_INIT.
  *
- * @param opts the `git_revert_options` instance to initialize.
- * @param version the version of the struct; you should pass
- *        `GIT_REVERT_OPTIONS_VERSION` here.
+ * @param opts the `git_revert_options` struct to initialize
+ * @param version Version of struct; pass `GIT_REVERT_OPTIONS_VERSION`
  * @return Zero on success; -1 on failure.
  */
-GIT_EXTERN(int) git_revert_init_opts(
-	git_revert_options* opts,
-	int version);
+GIT_EXTERN(int) git_revert_init_options(
+	git_revert_options *opts,
+	unsigned int version);
 
 /**
  * Reverts the given commit against the given "our" commit, producing an
diff --git a/include/git2/sys/config.h b/include/git2/sys/config.h
index 3df2ba3..46bb652 100644
--- a/include/git2/sys/config.h
+++ b/include/git2/sys/config.h
@@ -73,14 +73,13 @@ struct git_config_backend {
  * 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.
+ * @param opts the `git_config_backend` struct to initialize.
+ * @param version Version of struct; pass `GIT_CONFIG_BACKEND_VERSION`
  * @return Zero on success; -1 on failure.
  */
 GIT_EXTERN(int) git_config_init_backend(
-	git_config_backend* backend,
-	int version);
+	git_config_backend *backend,
+	unsigned int version);
 
 /**
  * Add a generic config file instance to an existing config
diff --git a/include/git2/sys/odb_backend.h b/include/git2/sys/odb_backend.h
index 77fe0dd..1fc3c31 100644
--- a/include/git2/sys/odb_backend.h
+++ b/include/git2/sys/odb_backend.h
@@ -93,14 +93,13 @@ struct git_odb_backend {
  * 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.
+ * @param opts the `git_odb_backend` struct to initialize.
+ * @param version Version the struct; pass `GIT_ODB_BACKEND_VERSION`
  * @return Zero on success; -1 on failure.
  */
 GIT_EXTERN(int) git_odb_init_backend(
-	git_odb_backend* backend,
-	int version);
+	git_odb_backend *backend,
+	unsigned int version);
 
 GIT_EXTERN(void *) git_odb_backend_malloc(git_odb_backend *backend, size_t len);
 
diff --git a/include/git2/sys/refdb_backend.h b/include/git2/sys/refdb_backend.h
index dce142c..3b216a2 100644
--- a/include/git2/sys/refdb_backend.h
+++ b/include/git2/sys/refdb_backend.h
@@ -162,14 +162,13 @@ struct git_refdb_backend {
  * 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.
+ * @param opts the `git_refdb_backend` struct to initialize
+ * @param version Version of struct; pass `GIT_REFDB_BACKEND_VERSION`
  * @return Zero on success; -1 on failure.
  */
 GIT_EXTERN(int) git_refdb_init_backend(
-	git_refdb_backend* backend,
-	int version);
+	git_refdb_backend *backend,
+	unsigned int version);
 
 /**
  * Constructors for default filesystem-based refdb backend
diff --git a/include/git2/transport.h b/include/git2/transport.h
index a33146c..af7812b 100644
--- a/include/git2/transport.h
+++ b/include/git2/transport.h
@@ -314,14 +314,13 @@ struct git_transport {
  * 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.
+ * @param opts the `git_transport` struct to initialize
+ * @param version Version of struct; pass `GIT_TRANSPORT_VERSION`
  * @return Zero on success; -1 on failure.
  */
 GIT_EXTERN(int) git_transport_init(
-	git_transport* opts,
-	int version);
+	git_transport *opts,
+	unsigned int version);
 
 /**
  * Function to use to create a transport from a URL. The transport database
diff --git a/src/checkout.c b/src/checkout.c
index 7279116..b869efe 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -2240,14 +2240,9 @@ int git_checkout_head(
 	return git_checkout_tree(repo, NULL, opts);
 }
 
-int git_checkout_init_opts(git_checkout_options* opts, int version)
+int git_checkout_init_options(git_checkout_options *opts, unsigned int version)
 {
-	if (version != GIT_CHECKOUT_OPTIONS_VERSION) {
-		giterr_set(GITERR_INVALID, "Invalid version %d for git_checkout_options", version);
-		return -1;
-	} else {
-		git_checkout_options o = GIT_CHECKOUT_OPTIONS_INIT;
-		memcpy(opts, &o, sizeof(o));
-		return 0;
-	}
+	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
+		opts, version, git_checkout_options, GIT_CHECKOUT_OPTIONS_INIT);
+	return 0;
 }
diff --git a/src/cherrypick.c b/src/cherrypick.c
index 6a5ca83..e02348a 100644
--- a/src/cherrypick.c
+++ b/src/cherrypick.c
@@ -217,14 +217,10 @@ done:
 	return error;
 }
 
-int git_cherry_pick_init_opts(git_cherry_pick_options* opts, int version)
+int git_cherry_pick_init_options(
+	git_cherry_pick_options *opts, unsigned int version)
 {
-	if (version != GIT_CHERRY_PICK_OPTIONS_VERSION) {
-		giterr_set(GITERR_INVALID, "Invalid version %d for git_cherry_pick_options", version);
-		return -1;
-	} else {
-		git_cherry_pick_options o = GIT_CHERRY_PICK_OPTIONS_INIT;
-		memcpy(opts, &o, sizeof(o));
-		return 0;
-	}
+	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
+		opts, version, git_cherry_pick_options, GIT_CHERRY_PICK_OPTIONS_INIT);
+	return 0;
 }
diff --git a/src/config.c b/src/config.c
index 16854c0..ee92398 100644
--- a/src/config.c
+++ b/src/config.c
@@ -1276,14 +1276,9 @@ cleanup:
 	return error;
 }
 
-int git_config_init_backend(git_config_backend* backend, int version)
+int git_config_init_backend(git_config_backend *backend, unsigned 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;
-	}
+	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
+		backend, version, git_config_backend, GIT_CONFIG_BACKEND_INIT);
+	return 0;
 }
diff --git a/src/diff.c b/src/diff.c
index 56f333f..781f23e 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -1310,17 +1310,10 @@ int git_diff_is_sorted_icase(const git_diff *diff)
 	return (diff->opts.flags & GIT_DIFF_IGNORE_CASE) != 0;
 }
 
-static int diff_options_bad_version(int version, const char *thing)
-{
-	giterr_set(GITERR_INVALID, "Invalid version %d for %s", version, thing);
-	return -1;
-}
-
 int git_diff_get_perfdata(git_diff_perfdata *out, const git_diff *diff)
 {
-	if (!out || out->version != GIT_DIFF_PERFDATA_VERSION)
-		return diff_options_bad_version(
-			out ? out->version : 0, "git_diff_perfdata");
+	assert(out);
+	GITERR_CHECK_VERSION(out, GIT_DIFF_PERFDATA_VERSION, "git_diff_perfdata");
 	out->stat_calls = diff->perf.stat_calls;
 	out->oid_calculations = diff->perf.oid_calculations;
 	return 0;
diff --git a/src/odb.c b/src/odb.c
index 00740d2..20a3f6c 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -1123,14 +1123,9 @@ int git_odb__error_ambiguous(const char *message)
 	return GIT_EAMBIGUOUS;
 }
 
-int git_odb_init_backend(git_odb_backend* backend, int version)
+int git_odb_init_backend(git_odb_backend *backend, unsigned 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;
-	}
+	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
+		backend, version, git_odb_backend, GIT_ODB_BACKEND_INIT);
+	return 0;
 }
diff --git a/src/refdb.c b/src/refdb.c
index 3e7a592..69bf747 100644
--- a/src/refdb.c
+++ b/src/refdb.c
@@ -236,14 +236,9 @@ 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)
+int git_refdb_init_backend(git_refdb_backend *backend, unsigned 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;
-	}
+	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
+		backend, version, git_refdb_backend, GIT_REFDB_BACKEND_INIT);
+	return 0;
 }
diff --git a/src/remote.c b/src/remote.c
index be7198a..8f302a5 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -1736,14 +1736,9 @@ 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)
+int git_remote_init_callbacks(git_remote_callbacks *opts, unsigned 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;
-	}
+	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
+		opts, version, git_remote_callbacks, GIT_REMOTE_CALLBACKS_INIT);
+	return 0;
 }
diff --git a/src/revert.c b/src/revert.c
index 29e124f..9c58772 100644
--- a/src/revert.c
+++ b/src/revert.c
@@ -220,14 +220,9 @@ done:
 	return error;
 }
 
-int git_revert_init_opts(git_revert_options* opts, int version)
+int git_revert_init_options(git_revert_options *opts, unsigned int version)
 {
-	if (version != GIT_REVERT_OPTIONS_VERSION) {
-		giterr_set(GITERR_INVALID, "Invalid version %d for git_revert_options", version);
-		return -1;
-	} else {
-		git_revert_options o = GIT_REVERT_OPTIONS_INIT;
-		memcpy(opts, &o, sizeof(o));
-		return 0;
-	}
+	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
+		opts, version, git_revert_options, GIT_REVERT_OPTIONS_INIT);
+	return 0;
 }
diff --git a/src/status.c b/src/status.c
index bcc2692..8d7612f 100644
--- a/src/status.c
+++ b/src/status.c
@@ -528,11 +528,8 @@ int git_status_init_options(git_status_options *opts, unsigned int version)
 int git_status_list_get_perfdata(
 	git_diff_perfdata *out, const git_status_list *status)
 {
-	if (!out || out->version != GIT_DIFF_PERFDATA_VERSION) {
-		giterr_set(GITERR_INVALID, "Invalid version %d for git_diff_perfdata",
-				   out ? out->version : 0);
-		return -1;
-	}
+	assert(out);
+	GITERR_CHECK_VERSION(out, GIT_DIFF_PERFDATA_VERSION, "git_diff_perfdata");
 
 	out->stat_calls = 0;
 	out->oid_calculations = 0;
diff --git a/src/transport.c b/src/transport.c
index dc074a5..2194b18 100644
--- a/src/transport.c
+++ b/src/transport.c
@@ -218,14 +218,9 @@ int git_remote_supported_url(const char* url)
 	return fn != &git_transport_dummy;
 }
 
-int git_transport_init(git_transport* opts, int version)
+int git_transport_init(git_transport *opts, unsigned 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;
-	}
+	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
+		opts, version, git_transport, GIT_TRANSPORT_INIT);
+	return 0;
 }
diff --git a/tests/structinit/structinit.c b/tests/structinit/structinit.c
index 2942099..38bedad 100644
--- a/tests/structinit/structinit.c
+++ b/tests/structinit/structinit.c
@@ -48,7 +48,7 @@ void test_structinit_structinit__compare(void)
 	/* checkout */
 	CHECK_MACRO_FUNC_INIT_EQUAL( \
 		git_checkout_options, GIT_CHECKOUT_OPTIONS_VERSION, \
-		GIT_CHECKOUT_OPTIONS_INIT, git_checkout_init_opts);
+		GIT_CHECKOUT_OPTIONS_INIT, git_checkout_init_options);
 
 	/* clone */
 	CHECK_MACRO_FUNC_INIT_EQUAL( \
@@ -98,7 +98,7 @@ void test_structinit_structinit__compare(void)
 	/* revert */
 	CHECK_MACRO_FUNC_INIT_EQUAL( \
 		git_revert_options, GIT_REVERT_OPTIONS_VERSION, \
-		GIT_REVERT_OPTIONS_INIT, git_revert_init_opts);
+		GIT_REVERT_OPTIONS_INIT, git_revert_init_options);
 
 	/* status */
 	CHECK_MACRO_FUNC_INIT_EQUAL( \