2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
12 /********************************************
14 ********************************************/
16 static git_error g_git_oom_error
= {
21 static void set_error(int error_class
, char *string
)
23 git_error
*error
= &GIT_GLOBAL
->error_t
;
25 if (error
->message
!= string
)
26 git__free(error
->message
);
28 error
->message
= string
;
29 error
->klass
= error_class
;
31 GIT_GLOBAL
->last_error
= error
;
34 void giterr_set_oom(void)
36 GIT_GLOBAL
->last_error
= &g_git_oom_error
;
39 void giterr_set(int error_class
, const char *string
, ...)
41 git_buf buf
= GIT_BUF_INIT
;
44 DWORD win32_error_code
= (error_class
== GITERR_OS
) ? GetLastError() : 0;
46 int error_code
= (error_class
== GITERR_OS
) ? errno
: 0;
49 va_start(arglist
, string
);
50 git_buf_vprintf(&buf
, string
, arglist
);
53 if (error_class
== GITERR_OS
)
54 git_buf_PUTS(&buf
, ": ");
57 if (error_class
== GITERR_OS
) {
59 char * win32_error
= git_win32_get_error_message(win32_error_code
);
61 git_buf_puts(&buf
, win32_error
);
62 git__free(win32_error
);
69 git_buf_puts(&buf
, strerror(error_code
));
75 if (!git_buf_oom(&buf
))
76 set_error(error_class
, git_buf_detach(&buf
));
79 void giterr_set_str(int error_class
, const char *string
)
85 message
= git__strdup(string
);
88 set_error(error_class
, message
);
91 int giterr_set_regex(const regex_t
*regex
, int error_code
)
97 regerror(error_code
, regex
, error_buf
, sizeof(error_buf
));
98 giterr_set_str(GITERR_REGEX
, error_buf
);
100 if (error_code
== REG_NOMATCH
)
101 return GIT_ENOTFOUND
;
103 return GIT_EINVALIDSPEC
;
106 void giterr_clear(void)
108 if (GIT_GLOBAL
->last_error
!= NULL
) {
110 GIT_GLOBAL
->last_error
= NULL
;
119 int giterr_detach(git_error
*cpy
)
121 git_error
*error
= GIT_GLOBAL
->last_error
;
128 cpy
->message
= error
->message
;
129 cpy
->klass
= error
->klass
;
131 error
->message
= NULL
;
137 const git_error
*giterr_last(void)
139 return GIT_GLOBAL
->last_error
;
142 int giterr_capture(git_error_state
*state
, int error_code
)
144 state
->error_code
= error_code
;
146 giterr_detach(&state
->error_msg
);
150 int giterr_restore(git_error_state
*state
)
152 if (state
&& state
->error_code
&& state
->error_msg
.message
)
153 set_error(state
->error_msg
.klass
, state
->error_msg
.message
);
157 return state
? state
->error_code
: 0;
160 int giterr_system_last(void)
163 return GetLastError();
169 void giterr_system_set(int code
)