summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2014-04-30 10:57:42 -0700
committerRussell Belfer <rb@github.com>2014-05-02 09:21:33 -0700
commit702efc891f2a620f10998062ba0c00b34100f632 (patch)
treee80132037df687837a40b5c3d8f1d6a079d35e43
parent9c8ed4999740e921ecc2966bbcd0dbcfc725f59a (diff)
downloadlibgit2-702efc891f2a620f10998062ba0c00b34100f632.tar.gz
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).
-rw-r--r--include/git2/blame.h19
-rw-r--r--include/git2/clone.h19
-rw-r--r--include/git2/diff.h44
-rw-r--r--include/git2/merge.h6
-rw-r--r--include/git2/push.h4
-rw-r--r--include/git2/repository.h9
-rw-r--r--src/blame.c13
-rw-r--r--src/clone.c13
-rw-r--r--src/common.h5
-rw-r--r--src/diff.c27
-rw-r--r--src/merge.c40
-rw-r--r--src/push.c13
-rw-r--r--src/repository.c15
-rw-r--r--src/status.c10
14 files changed, 100 insertions, 137 deletions
diff --git a/include/git2/blame.h b/include/git2/blame.h
index b7fa9aeda..7f0de1731 100644
--- a/include/git2/blame.h
+++ b/include/git2/blame.h
@@ -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.
diff --git a/include/git2/clone.h b/include/git2/clone.h
index 20be1a105..985c04bf6 100644
--- a/include/git2/clone.h
+++ b/include/git2/clone.h
@@ -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.
diff --git a/include/git2/diff.h b/include/git2/diff.h
index b849e3bb5..b40cc6135 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -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);
diff --git a/include/git2/merge.h b/include/git2/merge.h
index 6d97e81e6..ef2dc3804 100644
--- a/include/git2/merge.h
+++ b/include/git2/merge.h
@@ -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.
diff --git a/include/git2/push.h b/include/git2/push.h
index 7a8bec12c..cbf115661 100644
--- a/include/git2/push.h
+++ b/include/git2/push.h
@@ -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)(
diff --git a/include/git2/repository.h b/include/git2/repository.h
index 4433e71a2..e3f687a29 100644
--- a/include/git2/repository.h
+++ b/include/git2/repository.h
@@ -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.
diff --git a/src/blame.c b/src/blame.c
index e45c0ee1c..eb977c287 100644
--- a/src/blame.c
+++ b/src/blame.c
@@ -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;
}
diff --git a/src/clone.c b/src/clone.c
index 62f103561..c6be00f0e 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -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;
}
diff --git a/src/common.h b/src/common.h
index dd97a3099..807e5fa39 100644
--- a/src/common.h
+++ b/src/common.h
@@ -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"
diff --git a/src/diff.c b/src/diff.c
index 26e671dce..56f333f76 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -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;
}
diff --git a/src/merge.c b/src/merge.c
index 69c42bc0c..6a8e5874f 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -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;
}
diff --git a/src/push.c b/src/push.c
index 9943f215c..be5ec1c0e 100644
--- a/src/push.c
+++ b/src/push.c
@@ -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;
}
diff --git a/src/repository.c b/src/repository.c
index 8daa04d5d..ac7af7692 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -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;
}
diff --git a/src/status.c b/src/status.c
index aab838bcf..bcc2692d2 100644
--- a/src/status.c
+++ b/src/status.c
@@ -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;
}