* This file is part of libgit2, distributed under the GNU GPL v2 with
* 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",
- GITERR_NOMEMORY
+ 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) {
set_error_from_buffer(error_class);
}
-void giterr_set_oom(void)
+void git_error_set_oom(void)
{
- GIT_GLOBAL->last_error = &g_git_oom_error;
+ GIT_THREADSTATE->last_error = &g_git_oom_error;
}
-void giterr_set(int error_class, const char *string, ...)
+void git_error_set(int error_class, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ git_error_vset(error_class, fmt, ap);
+ va_end(ap);
+}
+
+void git_error_vset(int error_class, const char *fmt, va_list ap)
{
- va_list arglist;
#ifdef GIT_WIN32
- DWORD win32_error_code = (error_class == GITERR_OS) ? GetLastError() : 0;
+ DWORD win32_error_code = (error_class == GIT_ERROR_OS) ? GetLastError() : 0;
#endif
- int error_code = (error_class == GITERR_OS) ? errno : 0;
- git_buf *buf = &GIT_GLOBAL->error_buf;
+ int error_code = (error_class == GIT_ERROR_OS) ? errno : 0;
+ git_buf *buf = &GIT_THREADSTATE->error_buf;
git_buf_clear(buf);
- if (string) {
- va_start(arglist, string);
- git_buf_vprintf(buf, string, arglist);
- va_end(arglist);
-
- if (error_class == GITERR_OS)
+ if (fmt) {
+ git_buf_vprintf(buf, fmt, ap);
+ if (error_class == GIT_ERROR_OS)
git_buf_PUTS(buf, ": ");
}
- if (error_class == GITERR_OS) {
+ if (error_class == GIT_ERROR_OS) {
#ifdef GIT_WIN32
char * win32_error = git_win32_get_error_message(win32_error_code);
if (win32_error) {
set_error_from_buffer(error_class);
}
-void giterr_set_str(int error_class, const char *string)
+int git_error_set_str(int error_class, const char *string)
{
- git_buf *buf = &GIT_GLOBAL->error_buf;
-
- assert(string);
+ git_buf *buf = &GIT_THREADSTATE->error_buf;
- if (!string)
- return;
+ GIT_ASSERT_ARG(string);
git_buf_clear(buf);
git_buf_puts(buf, string);
- if (!git_buf_oom(buf))
- set_error_from_buffer(error_class);
-}
-int giterr_set_regex(const regex_t *regex, int error_code)
-{
- char error_buf[1024];
-
- assert(error_code);
-
- regerror(error_code, regex, error_buf, sizeof(error_buf));
- giterr_set_str(GITERR_REGEX, error_buf);
+ if (git_buf_oom(buf))
+ return -1;
- if (error_code == REG_NOMATCH)
- return GIT_ENOTFOUND;
-
- return GIT_EINVALIDSPEC;
+ set_error_from_buffer(error_class);
+ return 0;
}
-void giterr_clear(void)
+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;
#endif
}
-const git_error *giterr_last(void)
+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 giterr_state_capture(git_error_state *state, int error_code)
+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));
state->error_msg.message = git_buf_detach(error_buf);
}
- giterr_clear();
+ git_error_clear();
return error_code;
}
-int giterr_state_restore(git_error_state *state)
+int git_error_state_restore(git_error_state *state)
{
int ret = 0;
- giterr_clear();
+ git_error_clear();
if (state && state->error_msg.message) {
if (state->oom)
- giterr_set_oom();
+ git_error_set_oom();
else
set_error(state->error_msg.klass, state->error_msg.message);
return ret;
}
-void giterr_state_free(git_error_state *state)
+void git_error_state_free(git_error_state *state)
{
if (!state)
return;
memset(state, 0, sizeof(git_error_state));
}
-int giterr_system_last(void)
+int git_error_system_last(void)
{
#ifdef GIT_WIN32
return GetLastError();
#endif
}
-void giterr_system_set(int code)
+void git_error_system_set(int code)
{
#ifdef GIT_WIN32
SetLastError(code);
errno = code;
#endif
}
+
+/* Deprecated error values and functions */
+
+#ifndef GIT_DEPRECATE_HARD
+const git_error *giterr_last(void)
+{
+ return git_error_last();
+}
+
+void giterr_clear(void)
+{
+ git_error_clear();
+}
+
+void giterr_set_str(int error_class, const char *string)
+{
+ git_error_set_str(error_class, string);
+}
+
+void giterr_set_oom(void)
+{
+ git_error_set_oom();
+}
+#endif