Commit 702efc89 by Russell Belfer

Make init_options fns use unsigned ints and macro

Use an unsigned int for the version and add a helper macro so the
code is simplified (and so the error message is a common string).
parent 9c8ed499
......@@ -83,17 +83,16 @@ 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.
*/
* Initializes a `git_blame_options` with default values. Equivalent to
* creating an instance with GIT_BLAME_OPTIONS_INIT.
*
* @param opts The `git_blame_options` struct to initialize
* @param version Version of struct; pass `GIT_BLAME_OPTIONS_VERSION`
* @return Zero on success; -1 on failure.
*/
GIT_EXTERN(int) git_blame_init_options(
git_blame_options* opts,
int version);
git_blame_options *opts,
unsigned int version);
/**
* Structure that represents a blame hunk.
......
......@@ -66,17 +66,16 @@ typedef struct git_clone_options {
#define GIT_CLONE_OPTIONS_INIT {GIT_CLONE_OPTIONS_VERSION, {GIT_CHECKOUT_OPTIONS_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.
*/
* Initializes a `git_clone_options` with default values. Equivalent to
* creating an instance with GIT_CLONE_OPTIONS_INIT.
*
* @param opts The `git_clone_options` struct to initialize
* @param version Version of struct; pass `GIT_CLONE_OPTIONS_VERSION`
* @return Zero on success; -1 on failure.
*/
GIT_EXTERN(int) git_clone_init_options(
git_clone_options* opts,
int version);
git_clone_options *opts,
unsigned int version);
/**
* Clone a remote repository.
......
......@@ -388,13 +388,13 @@ 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 Version of struct; pass `GIT_DIFF_OPTIONS_VERSION`
* @return Zero on success; -1 on failure.
*/
* Initializes a `git_diff_options` with default values. Equivalent to
* creating an instance with GIT_DIFF_OPTIONS_INIT.
*
* @param opts The `git_diff_options` struct to initialize
* @param version Version of struct; pass `GIT_DIFF_OPTIONS_VERSION`
* @return Zero on success; -1 on failure.
*/
GIT_EXTERN(int) git_diff_init_options(
git_diff_options *opts,
unsigned int version);
......@@ -628,13 +628,13 @@ typedef struct {
#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 Version of struct; pass `GIT_DIFF_FIND_OPTIONS_VERSION`
* @return Zero on success; -1 on failure.
*/
* 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` struct to initialize
* @param version Version of struct; pass `GIT_DIFF_FIND_OPTIONS_VERSION`
* @return Zero on success; -1 on failure.
*/
GIT_EXTERN(int) git_diff_find_init_options(
git_diff_find_options *opts,
unsigned int version);
......@@ -1221,14 +1221,14 @@ GIT_EXTERN(int) git_diff_commit_as_email(
const git_diff_options *diff_opts);
/**
* Initializes a `git_diff_format_email_options` with default values.
*
* Equivalent to creating an instance with GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT.
*
* @param opts Uhe `git_diff_format_email_options` instance to initialize.
* @param version Version of struct; pass `GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION`
* @return Zero on success; -1 on failure.
*/
* Initializes a `git_diff_format_email_options` with default values.
*
* Equivalent to creating an instance with GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT.
*
* @param opts The `git_diff_format_email_options` struct to initialize
* @param version Version of struct; pass `GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION`
* @return Zero on success; -1 on failure.
*/
GIT_EXTERN(int) git_diff_format_email_init_options(
git_diff_format_email_options *opts,
unsigned int version);
......
......@@ -57,7 +57,7 @@ typedef struct {
*/
GIT_EXTERN(int) git_merge_file_init_input(
git_merge_file_input *opts,
int version);
unsigned int version);
/**
* Flags for `git_merge_tree` options. A combination of these flags can be
......@@ -164,7 +164,7 @@ typedef struct {
*/
GIT_EXTERN(int) git_merge_file_init_options(
git_merge_file_options *opts,
int version);
unsigned int version);
typedef struct {
/**
......@@ -232,7 +232,7 @@ typedef struct {
*/
GIT_EXTERN(int) git_merge_init_options(
git_merge_options *opts,
int version);
unsigned int version);
/**
* The results of `git_merge_analysis` indicate the merge opportunities.
......
......@@ -49,8 +49,8 @@ typedef struct {
* @return Zero on success; -1 on failure.
*/
GIT_EXTERN(int) git_push_init_options(
git_push_options* opts,
int version);
git_push_options *opts,
unsigned int version);
/** Push network progress notification function */
typedef int (*git_push_transfer_progress)(
......
......@@ -271,14 +271,13 @@ typedef struct {
* 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.
* @param opts the `git_repository_init_options` struct to initialize
* @param version Version of struct; pass `GIT_REPOSITORY_INIT_OPTIONS_VERSION`
* @return Zero on success; -1 on failure.
*/
GIT_EXTERN(int) git_repository_init_init_options(
git_repository_init_options* opts,
int version);
git_repository_init_options *opts,
unsigned int version);
/**
* Create a new Git repository in the given folder with extended controls.
......
......@@ -480,14 +480,9 @@ int git_blame_buffer(
return 0;
}
int git_blame_init_options(git_blame_options* opts, int version)
int git_blame_init_options(git_blame_options *opts, unsigned 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;
}
GIT_INIT_STRUCTURE_FROM_TEMPLATE(
opts, version, git_blame_options, GIT_BLAME_OPTIONS_INIT);
return 0;
}
......@@ -445,14 +445,9 @@ int git_clone(
return error;
}
int git_clone_init_options(git_clone_options* opts, int version)
int git_clone_init_options(git_clone_options *opts, unsigned 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;
}
GIT_INIT_STRUCTURE_FROM_TEMPLATE(
opts, version, git_clone_options, GIT_CLONE_OPTIONS_INIT);
return 0;
}
......@@ -170,6 +170,11 @@ GIT_INLINE(void) git__init_structure(void *structure, size_t len, unsigned int v
}
#define GIT_INIT_STRUCTURE(S,V) git__init_structure(S, sizeof(*S), V)
#define GIT_INIT_STRUCTURE_FROM_TEMPLATE(PTR,VERSION,TYPE,TPL) do { \
TYPE _tmpl = TPL; \
GITERR_CHECK_VERSION(&(VERSION), _tmpl.version, #TYPE); \
memcpy((PTR), &_tmpl, sizeof(_tmpl)); } while (0)
/* NOTE: other giterr functions are in the public errors.h header file */
#include "util.h"
......
......@@ -1628,29 +1628,26 @@ int git_diff_commit_as_email(
return error;
}
int git_diff_init_options(git_diff_options* opts, unsigned int version)
int git_diff_init_options(git_diff_options *opts, unsigned int version)
{
git_diff_options o = GIT_DIFF_OPTIONS_INIT;
if (version != o.version)
return diff_options_bad_version(version, "git_diff_options");
memcpy(opts, &o, sizeof(o));
GIT_INIT_STRUCTURE_FROM_TEMPLATE(
opts, version, git_diff_options, GIT_DIFF_OPTIONS_INIT);
return 0;
}
int git_diff_find_init_options(git_diff_find_options* opts, unsigned int version)
int git_diff_find_init_options(
git_diff_find_options *opts, unsigned int version)
{
git_diff_find_options o = GIT_DIFF_FIND_OPTIONS_INIT;
if (version != o.version)
return diff_options_bad_version(version, "git_diff_find_options");
memcpy(opts, &o, sizeof(o));
GIT_INIT_STRUCTURE_FROM_TEMPLATE(
opts, version, git_diff_find_options, GIT_DIFF_FIND_OPTIONS_INIT);
return 0;
}
int git_diff_format_email_init_options(git_diff_format_email_options* opts, unsigned int version)
int git_diff_format_email_init_options(
git_diff_format_email_options *opts, unsigned int version)
{
git_diff_format_email_options o = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
if (version != o.version)
return diff_options_bad_version(version, "git_diff_format_email_options");
memcpy(opts, &o, sizeof(o));
GIT_INIT_STRUCTURE_FROM_TEMPLATE(
opts, version, git_diff_format_email_options,
GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT);
return 0;
}
......@@ -2803,38 +2803,24 @@ void git_merge_head_free(git_merge_head *head)
git__free(head);
}
int git_merge_init_options(git_merge_options *opts, int version)
int git_merge_init_options(git_merge_options *opts, unsigned int version)
{
if (version != GIT_MERGE_OPTIONS_VERSION) {
giterr_set(GITERR_INVALID, "Invalid version %d for git_merge_options", version);
return -1;
} else {
git_merge_options default_opts = GIT_MERGE_OPTIONS_INIT;
memcpy(opts, &default_opts, sizeof(git_merge_options));
return 0;
}
GIT_INIT_STRUCTURE_FROM_TEMPLATE(
opts, version, git_merge_options, GIT_MERGE_OPTIONS_INIT);
return 0;
}
int git_merge_file_init_input(git_merge_file_input *input, int version)
int git_merge_file_init_input(git_merge_file_input *input, unsigned int version)
{
if (version != GIT_MERGE_FILE_INPUT_VERSION) {
giterr_set(GITERR_INVALID, "Invalid version %d for git_merge_file_input", version);
return -1;
} else {
git_merge_file_input i = GIT_MERGE_FILE_INPUT_INIT;
memcpy(input, &i, sizeof(i));
return 0;
}
GIT_INIT_STRUCTURE_FROM_TEMPLATE(
input, version, git_merge_file_input, GIT_MERGE_FILE_INPUT_INIT);
return 0;
}
int git_merge_file_init_options(git_merge_file_options *opts, int version)
int git_merge_file_init_options(
git_merge_file_options *opts, unsigned int version)
{
if (version != GIT_MERGE_FILE_OPTIONS_VERSION) {
giterr_set(GITERR_INVALID, "Invalid version %d for git_merge_file_options", version);
return -1;
} else {
git_merge_file_options o = GIT_MERGE_FILE_OPTIONS_INIT;
memcpy(opts, &o, sizeof(o));
return 0;
}
GIT_INIT_STRUCTURE_FROM_TEMPLATE(
opts, version, git_merge_file_options, GIT_MERGE_FILE_OPTIONS_INIT);
return 0;
}
......@@ -716,14 +716,9 @@ void git_push_free(git_push *push)
git__free(push);
}
int git_push_init_options(git_push_options* opts, int version)
int git_push_init_options(git_push_options *opts, unsigned 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;
}
GIT_INIT_STRUCTURE_FROM_TEMPLATE(
opts, version, git_push_options, GIT_PUSH_OPTIONS_INIT);
return 0;
}
......@@ -2026,14 +2026,11 @@ int git_repository_is_shallow(git_repository *repo)
return st.st_size == 0 ? 0 : 1;
}
int git_repository_init_init_options(git_repository_init_options* opts, int version)
int git_repository_init_init_options(
git_repository_init_options *opts, unsigned 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;
}
GIT_INIT_STRUCTURE_FROM_TEMPLATE(
opts, version, git_repository_init_options,
GIT_REPOSITORY_INIT_OPTIONS_INIT);
return 0;
}
......@@ -518,14 +518,10 @@ int git_status_should_ignore(
return git_ignore_path_is_ignored(ignored, repo, path);
}
int git_status_init_options(git_status_options* opts, unsigned int version)
int git_status_init_options(git_status_options *opts, unsigned int version)
{
git_status_options o = GIT_STATUS_OPTIONS_INIT;
if (version != o.version) {
giterr_set(GITERR_INVALID, "Invalid version %d for git_status_options", version);
return -1;
}
memcpy(opts, &o, sizeof(o));
GIT_INIT_STRUCTURE_FROM_TEMPLATE(
opts, version, git_status_options, GIT_STATUS_OPTIONS_INIT);
return 0;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment