diff options
author | Russell Belfer <rb@github.com> | 2014-04-30 10:57:42 -0700 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2014-05-02 09:21:33 -0700 |
commit | 702efc891f2a620f10998062ba0c00b34100f632 (patch) | |
tree | e80132037df687837a40b5c3d8f1d6a079d35e43 /src | |
parent | 9c8ed4999740e921ecc2966bbcd0dbcfc725f59a (diff) | |
download | libgit2-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).
Diffstat (limited to 'src')
-rw-r--r-- | src/blame.c | 13 | ||||
-rw-r--r-- | src/clone.c | 13 | ||||
-rw-r--r-- | src/common.h | 5 | ||||
-rw-r--r-- | src/diff.c | 27 | ||||
-rw-r--r-- | src/merge.c | 40 | ||||
-rw-r--r-- | src/push.c | 13 | ||||
-rw-r--r-- | src/repository.c | 15 | ||||
-rw-r--r-- | src/status.c | 10 |
8 files changed, 51 insertions, 85 deletions
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; } |