From c7231c45fecf6c0ae91815a82db7e98c94689497 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 30 Nov 2012 16:31:42 -0800 Subject: Deploy GITERR_CHECK_VERSION --- src/checkout.c | 20 ++------------------ src/commit.c | 5 ++--- src/common.h | 17 +++++++++++++++++ src/config.c | 15 +-------------- src/diff.c | 5 ++--- src/diff.h | 13 ------------- src/diff_output.c | 3 +-- src/diff_tform.c | 15 +-------------- src/notes.c | 10 ++++------ src/odb.c | 15 +-------------- src/reflog.c | 3 +-- src/remote.c | 30 ++---------------------------- src/repository.c | 15 +-------------- src/signature.h | 12 ------------ src/stash.c | 3 +-- src/status.c | 15 +-------------- src/tag.c | 3 +-- 17 files changed, 38 insertions(+), 161 deletions(-) diff --git a/src/checkout.c b/src/checkout.c index 06407bb30..640b04857 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -603,19 +603,6 @@ static int checkout_create_submodules( return 0; } -static bool opts_is_valid_version(git_checkout_opts *opts) -{ - if (!opts) - return true; - - if (opts->version > 0 && opts->version <= GIT_CHECKOUT_OPTS_VERSION) - return true; - - giterr_set(GITERR_INVALID, "Invalid version %d on git_checkout_opts structure", - opts->version); - return false; -} - int git_checkout_index( git_repository *repo, git_index *index, @@ -632,6 +619,8 @@ int git_checkout_index( assert(repo); + GITERR_CHECK_VERSION(opts, GIT_CHECKOUT_OPTS_VERSION, "git_checkout_opts"); + if ((error = git_repository__ensure_not_bare(repo, "checkout")) < 0) return error; @@ -639,11 +628,6 @@ int git_checkout_index( GIT_DIFF_INCLUDE_UNMODIFIED | GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_INCLUDE_TYPECHANGE | GIT_DIFF_SKIP_BINARY_CHECK; - if (!opts_is_valid_version(opts)) { - error = -1; - goto cleanup; - } - if (opts && opts->paths.count > 0) diff_opts.pathspec = opts->paths; diff --git a/src/commit.c b/src/commit.c index 5197fdb6d..f9606dd72 100644 --- a/src/commit.c +++ b/src/commit.c @@ -93,9 +93,8 @@ int git_commit_create( git_odb *odb; assert(git_object_owner((const git_object *)tree) == repo); - if (!git_signature__has_valid_version(author) || - !git_signature__has_valid_version(committer)) - return -1; + GITERR_CHECK_VERSION(author, GIT_SIGNATURE_VERSION, "git_signature"); + GITERR_CHECK_VERSION(committer, GIT_SIGNATURE_VERSION, "git_signature"); git_oid__writebuf(&commit, "tree ", git_object_id((const git_object *)tree)); diff --git a/src/common.h b/src/common.h index a35239e3d..b779d2800 100644 --- a/src/common.h +++ b/src/common.h @@ -65,6 +65,23 @@ void giterr_set(int error_class, const char *string, ...); */ void giterr_set_regex(const regex_t *regex, int error_code); +/** + * Check a versioned structure for validity + */ +GIT_INLINE(bool) giterr__check_version(const void *structure, unsigned int expected_max, const char *name) +{ + if (!structure) + return true; + + unsigned int actual = *(unsigned int*)structure; + if (actual > 0 && actual <= expected_max) + return true; + + giterr_set(GITERR_INVALID, "Invalid version %d on %s", actual, name); + return false; +} +#define GITERR_CHECK_VERSION(S,V,N) if (!giterr__check_version(S,V,N)) return -1 + /* NOTE: other giterr functions are in the public errors.h header file */ #include "util.h" diff --git a/src/config.c b/src/config.c index 913108abb..d422447cf 100644 --- a/src/config.c +++ b/src/config.c @@ -248,18 +248,6 @@ int git_config_open_level( return 0; } -static bool config_backend_has_valid_version(git_config_backend *backend) -{ - if (!backend) - return true; - - if (backend->version > 0 && backend->version <= GIT_CONFIG_BACKEND_VERSION) - return true; - - giterr_set(GITERR_INVALID, "Invalid version %d for git_config_backend", backend->version); - return false; -} - int git_config_add_backend( git_config *cfg, git_config_backend *file, @@ -271,8 +259,7 @@ int git_config_add_backend( assert(cfg && file); - if (!config_backend_has_valid_version(file)) - return -1; + GITERR_CHECK_VERSION(file, GIT_CONFIG_BACKEND_VERSION, "git_config_backend"); if ((result = file->open(file, level)) < 0) return result; diff --git a/src/diff.c b/src/diff.c index 722acbead..46d96bb96 100644 --- a/src/diff.c +++ b/src/diff.c @@ -758,9 +758,8 @@ fail: #define DIFF_FROM_ITERATORS(MAKE_FIRST, MAKE_SECOND) do { \ git_iterator *a = NULL, *b = NULL; \ char *pfx = opts ? git_pathspec_prefix(&opts->pathspec) : NULL; \ - if (!git_diff__opts_has_valid_version(opts)) \ - error = -1; \ - else if (!(error = MAKE_FIRST) && !(error = MAKE_SECOND)) \ + GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); \ + if (!(error = MAKE_FIRST) && !(error = MAKE_SECOND)) \ error = diff_from_iterators(diff, repo, a, b, opts); \ git__free(pfx); git_iterator_free(a); git_iterator_free(b); \ } while (0) diff --git a/src/diff.h b/src/diff.h index 1f45af1cd..f93bab18d 100644 --- a/src/diff.h +++ b/src/diff.h @@ -62,18 +62,5 @@ extern int git_diff__oid_for_file( git_repository *, const char *, uint16_t, git_off_t, git_oid *); -GIT_INLINE(bool) git_diff__opts_has_valid_version(const git_diff_options *opts) -{ - if (!opts) - return true; - - if (opts->version > 0 && opts->version <= GIT_DIFF_OPTIONS_VERSION) - return true; - - giterr_set(GITERR_INVALID, "Invalid version %d in git_diff_options", opts->version); - return false; -} - - #endif diff --git a/src/diff_output.c b/src/diff_output.c index 6316bfa1e..b18255d58 100644 --- a/src/diff_output.c +++ b/src/diff_output.c @@ -1266,8 +1266,7 @@ int git_diff_blobs( git_diff_delta delta; git_diff_patch patch; - if (!git_diff__opts_has_valid_version(options)) - return -1; + GITERR_CHECK_VERSION(options, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); if (options && (options->flags & GIT_DIFF_REVERSE)) { git_blob *swap = old_blob; diff --git a/src/diff_tform.c b/src/diff_tform.c index 1df041044..0c588594a 100644 --- a/src/diff_tform.c +++ b/src/diff_tform.c @@ -168,18 +168,6 @@ int git_diff_merge( return error; } -static bool find_opts_has_valid_version(const git_diff_find_options *opts) -{ - if (!opts) - return true; - - if (opts->version > 0 && opts->version <= GIT_DIFF_FIND_OPTIONS_VERSION) - return true; - - giterr_set(GITERR_INVALID, "Invalid version %d on git_diff_find_options", opts->version); - return false; -} - #define DEFAULT_THRESHOLD 50 #define DEFAULT_BREAK_REWRITE_THRESHOLD 60 #define DEFAULT_TARGET_LIMIT 200 @@ -211,8 +199,7 @@ static int normalize_find_opts( opts->flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES; } - if (!find_opts_has_valid_version(opts)) - return -1; + GITERR_CHECK_VERSION(opts, GIT_DIFF_FIND_OPTIONS_VERSION, "git_diff_find_options"); /* some flags imply others */ diff --git a/src/notes.c b/src/notes.c index 87ee1e742..71a9e33ad 100644 --- a/src/notes.c +++ b/src/notes.c @@ -456,9 +456,8 @@ int git_note_create( git_commit *commit = NULL; git_tree *tree = NULL; - if (!git_signature__has_valid_version(author) || - !git_signature__has_valid_version(committer)) - return -1; + GITERR_CHECK_VERSION(author, GIT_SIGNATURE_VERSION, "git_signature"); + GITERR_CHECK_VERSION(committer, GIT_SIGNATURE_VERSION, "git_signature"); target = git_oid_allocfmt(oid); GITERR_CHECK_ALLOC(target); @@ -487,9 +486,8 @@ int git_note_remove(git_repository *repo, const char *notes_ref, git_commit *commit = NULL; git_tree *tree = NULL; - if (!git_signature__has_valid_version(author) || - !git_signature__has_valid_version(committer)) - return -1; + GITERR_CHECK_VERSION(author, GIT_SIGNATURE_VERSION, "git_signature"); + GITERR_CHECK_VERSION(committer, GIT_SIGNATURE_VERSION, "git_signature"); target = git_oid_allocfmt(oid); GITERR_CHECK_ALLOC(target); diff --git a/src/odb.c b/src/odb.c index e35e4caca..b6d1f798d 100644 --- a/src/odb.c +++ b/src/odb.c @@ -363,26 +363,13 @@ int git_odb_new(git_odb **out) return 0; } -static bool backend_has_valid_version(git_odb_backend *backend) -{ - if (!backend) - return true; - - if (backend->version > 0 && backend->version <= GIT_ODB_BACKEND_VERSION) - return true; - - giterr_set(GITERR_INVALID, "Invalid version %d on git_odb_backend", backend->version); - return false; -} - static int add_backend_internal(git_odb *odb, git_odb_backend *backend, int priority, int is_alternate) { backend_internal *internal; assert(odb && backend); - if (!backend_has_valid_version(backend)) - return -1; + GITERR_CHECK_VERSION(backend, GIT_ODB_BACKEND_VERSION, "git_odb_backend"); /* Check if the backend is already owned by another ODB */ assert(!backend->odb || backend->odb == odb); diff --git a/src/reflog.c b/src/reflog.c index 4d119bae7..c0af60f49 100644 --- a/src/reflog.c +++ b/src/reflog.c @@ -298,8 +298,7 @@ int git_reflog_append(git_reflog *reflog, const git_oid *new_oid, assert(reflog && new_oid && committer); - if (!git_signature__has_valid_version(committer)) - return -1; + GITERR_CHECK_VERSION(committer, GIT_SIGNATURE_VERSION, "git_signature"); if (reflog_entry_new(&entry) < 0) return -1; diff --git a/src/remote.c b/src/remote.c index 5fc6b4f35..4f5aa9dee 100644 --- a/src/remote.c +++ b/src/remote.c @@ -985,24 +985,11 @@ void git_remote_check_cert(git_remote *remote, int check) remote->check_cert = check; } -static bool callbacks_have_valid_version(git_remote_callbacks *callbacks) -{ - if (!callbacks) - return true; - - if (callbacks->version > 0 && callbacks->version <= GIT_REMOTE_CALLBACKS_VERSION) - return true; - - giterr_set(GITERR_INVALID, "Invalid version %d for git_remote_callbacks", callbacks->version); - return false; -} - int git_remote_set_callbacks(git_remote *remote, git_remote_callbacks *callbacks) { assert(remote && callbacks); - if (!callbacks_have_valid_version(callbacks)) - return -1; + GITERR_CHECK_VERSION(callbacks, GIT_REMOTE_CALLBACKS_VERSION, "git_remote_callbacks"); memcpy(&remote->callbacks, callbacks, sizeof(git_remote_callbacks)); @@ -1024,24 +1011,11 @@ void git_remote_set_cred_acquire_cb( remote->cred_acquire_cb = cred_acquire_cb; } -static bool transport_has_valid_version(const git_transport *transport) -{ - if (!transport) - return true; - - if (transport->version > 0 && transport->version <= GIT_TRANSPORT_VERSION) - return true; - - giterr_set(GITERR_INVALID, "Invalid version %d on git_transport", transport->version); - return false; -} - int git_remote_set_transport(git_remote *remote, git_transport *transport) { assert(remote && transport); - if (!transport_has_valid_version(transport)) - return -1; + GITERR_CHECK_VERSION(transport, GIT_TRANSPORT_VERSION, "git_transport"); if (remote->transport) { giterr_set(GITERR_NET, "A transport is already bound to this remote"); diff --git a/src/repository.c b/src/repository.c index 073efa484..10ed12b64 100644 --- a/src/repository.c +++ b/src/repository.c @@ -1160,18 +1160,6 @@ int git_repository_init( return git_repository_init_ext(repo_out, path, &opts); } -static bool options_have_valid_version(git_repository_init_options *opts) -{ - if (!opts) - return true; - - if (opts->version > 0 && opts->version <= GIT_REMOTE_CALLBACKS_VERSION) - return true; - - giterr_set(GITERR_INVALID, "Invalid version %d for git_repository_init_options", opts->version); - return false; -} - int git_repository_init_ext( git_repository **out, const char *given_repo, @@ -1182,8 +1170,7 @@ int git_repository_init_ext( assert(out && given_repo && opts); - if (!options_have_valid_version(opts)) - return -1; + GITERR_CHECK_VERSION(opts, GIT_REPOSITORY_INIT_OPTIONS_VERSION, "git_repository_init_options"); error = repo_init_directories(&repo_path, &wd_path, given_repo, opts); if (error < 0) diff --git a/src/signature.h b/src/signature.h index 599e19901..97b3a055e 100644 --- a/src/signature.h +++ b/src/signature.h @@ -15,16 +15,4 @@ int git_signature__parse(git_signature *sig, const char **buffer_out, const char *buffer_end, const char *header, char ender); void git_signature__writebuf(git_buf *buf, const char *header, const git_signature *sig); -GIT_INLINE(bool) git_signature__has_valid_version(const git_signature *sig) -{ - if (!sig) - return true; - - if (sig->version > 0 && sig->version <= GIT_SIGNATURE_VERSION) - return true; - - giterr_set(GITERR_INVALID, "Invalid version %d on git_signature", sig->version); - return false; -} - #endif diff --git a/src/stash.c b/src/stash.c index 67fa49838..14b48a595 100644 --- a/src/stash.c +++ b/src/stash.c @@ -523,8 +523,7 @@ int git_stash_save( assert(out && repo && stasher); - if (!git_signature__has_valid_version(stasher)) - return -1; + GITERR_CHECK_VERSION(stasher, GIT_SIGNATURE_VERSION, "git_signature"); if ((error = ensure_non_bare_repository(repo)) < 0) return error; diff --git a/src/status.c b/src/status.c index f23f40e6d..1ad835adb 100644 --- a/src/status.c +++ b/src/status.c @@ -101,18 +101,6 @@ static int status_invoke_cb( return usercb->cb(path, status, usercb->payload); } -static bool options_have_valid_version(const git_status_options *opts) -{ - if (!opts) - return true; - - if (opts->version > 0 && opts->version <= GIT_REMOTE_CALLBACKS_VERSION) - return true; - - giterr_set(GITERR_INVALID, "Invalid version %d for git_repository_init_options", opts->version); - return false; -} - int git_status_foreach_ext( git_repository *repo, const git_status_options *opts, @@ -129,8 +117,7 @@ int git_status_foreach_ext( assert(show <= GIT_STATUS_SHOW_INDEX_THEN_WORKDIR); - if (!options_have_valid_version(opts)) - return -1; + GITERR_CHECK_VERSION(opts, GIT_STATUS_OPTIONS_VERSION, "git_status_options"); if (show != GIT_STATUS_SHOW_INDEX_ONLY && (err = git_repository__ensure_not_bare(repo, "status")) < 0) diff --git a/src/tag.c b/src/tag.c index fb70837a2..c3b3319fb 100644 --- a/src/tag.c +++ b/src/tag.c @@ -244,8 +244,7 @@ static int git_tag_create__internal( assert(repo && tag_name && target); assert(!create_tag_annotation || (tagger && message)); - if (!git_signature__has_valid_version(tagger)) - return -1; + GITERR_CHECK_VERSION(tagger, GIT_SIGNATURE_VERSION, "git_signature"); if (git_object_owner(target) != repo) { giterr_set(GITERR_INVALID, "The given target does not belong to this repository"); -- cgit v1.2.1