vendor/libgit2/src/errors.c in rugged-1.1.1 vs vendor/libgit2/src/errors.c in rugged-1.2.0

- old
+ new

@@ -5,37 +5,43 @@ * a Linking Exception. For full terms see the included COPYING file. */ #include "common.h" -#include "global.h" +#include "threadstate.h" #include "posix.h" #include "buffer.h" +#include "libgit2.h" /******************************************** * New error handling ********************************************/ static git_error g_git_oom_error = { "Out of memory", GIT_ERROR_NOMEMORY }; +static git_error g_git_uninitialized_error = { + "libgit2 has not been initialized; you must call git_libgit2_init", + GIT_ERROR_INVALID +}; + static void set_error_from_buffer(int error_class) { - git_error *error = &GIT_GLOBAL->error_t; - git_buf *buf = &GIT_GLOBAL->error_buf; + git_error *error = &GIT_THREADSTATE->error_t; + git_buf *buf = &GIT_THREADSTATE->error_buf; error->message = buf->ptr; error->klass = error_class; - GIT_GLOBAL->last_error = error; + GIT_THREADSTATE->last_error = error; } static void set_error(int error_class, char *string) { - git_buf *buf = &GIT_GLOBAL->error_buf; + git_buf *buf = &GIT_THREADSTATE->error_buf; git_buf_clear(buf); if (string) { git_buf_puts(buf, string); git__free(string); @@ -44,11 +50,11 @@ set_error_from_buffer(error_class); } void git_error_set_oom(void) { - GIT_GLOBAL->last_error = &g_git_oom_error; + GIT_THREADSTATE->last_error = &g_git_oom_error; } void git_error_set(int error_class, const char *fmt, ...) { va_list ap; @@ -62,11 +68,11 @@ { #ifdef GIT_WIN32 DWORD win32_error_code = (error_class == GIT_ERROR_OS) ? GetLastError() : 0; #endif int error_code = (error_class == GIT_ERROR_OS) ? errno : 0; - git_buf *buf = &GIT_GLOBAL->error_buf; + git_buf *buf = &GIT_THREADSTATE->error_buf; git_buf_clear(buf); if (fmt) { git_buf_vprintf(buf, fmt, ap); if (error_class == GIT_ERROR_OS) @@ -95,19 +101,14 @@ set_error_from_buffer(error_class); } int git_error_set_str(int error_class, const char *string) { - git_buf *buf = &GIT_GLOBAL->error_buf; + git_buf *buf = &GIT_THREADSTATE->error_buf; - assert(string); + GIT_ASSERT_ARG(string); - if (!string) { - git_error_set(GIT_ERROR_INVALID, "unspecified caller error"); - return -1; - } - git_buf_clear(buf); git_buf_puts(buf, string); if (git_buf_oom(buf)) return -1; @@ -116,29 +117,33 @@ return 0; } void git_error_clear(void) { - if (GIT_GLOBAL->last_error != NULL) { + if (GIT_THREADSTATE->last_error != NULL) { set_error(0, NULL); - GIT_GLOBAL->last_error = NULL; + GIT_THREADSTATE->last_error = NULL; } errno = 0; #ifdef GIT_WIN32 SetLastError(0); #endif } const git_error *git_error_last(void) { - return GIT_GLOBAL->last_error; + /* If the library is not initialized, return a static error. */ + if (!git_libgit2_init_count()) + return &g_git_uninitialized_error; + + return GIT_THREADSTATE->last_error; } int git_error_state_capture(git_error_state *state, int error_code) { - git_error *error = GIT_GLOBAL->last_error; - git_buf *error_buf = &GIT_GLOBAL->error_buf; + git_error *error = GIT_THREADSTATE->last_error; + git_buf *error_buf = &GIT_THREADSTATE->error_buf; memset(state, 0, sizeof(git_error_state)); if (!error_code) return 0;