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.
13 /********************************************
15 ********************************************/
17 static git_error g_git_oom_error
= {
22 static void set_error(int error_class
, char *string
)
24 git_error
*error
= &GIT_GLOBAL
->error_t
;
26 if (error
->message
!= string
)
27 git__free(error
->message
);
29 error
->message
= string
;
30 error
->klass
= error_class
;
32 GIT_GLOBAL
->last_error
= error
;
35 void giterr_set_oom(void)
37 GIT_GLOBAL
->last_error
= &g_git_oom_error
;
40 void giterr_set(int error_class
, const char *string
, ...)
42 git_buf buf
= GIT_BUF_INIT
;
45 DWORD win32_error_code
= (error_class
== GITERR_OS
) ? GetLastError() : 0;
47 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
) {
55 char * win32_error
= git_win32_get_error_message(win32_error_code
);
57 git_buf_PUTS(&buf
, ": ");
58 git_buf_puts(&buf
, win32_error
);
59 git__free(win32_error
);
66 git_buf_PUTS(&buf
, ": ");
67 git_buf_puts(&buf
, strerror(error_code
));
74 if (!git_buf_oom(&buf
))
75 set_error(error_class
, git_buf_detach(&buf
));
78 void giterr_set_str(int error_class
, const char *string
)
84 message
= git__strdup(string
);
87 set_error(error_class
, message
);
90 int giterr_set_regex(const regex_t
*regex
, int error_code
)
96 regerror(error_code
, regex
, error_buf
, sizeof(error_buf
));
97 giterr_set_str(GITERR_REGEX
, error_buf
);
99 if (error_code
== REG_NOMATCH
)
100 return GIT_ENOTFOUND
;
102 return GIT_EINVALIDSPEC
;
105 void giterr_clear(void)
107 if (GIT_GLOBAL
->last_error
!= NULL
) {
109 GIT_GLOBAL
->last_error
= NULL
;
118 int giterr_detach(git_error
*cpy
)
120 git_error
*error
= GIT_GLOBAL
->last_error
;
127 cpy
->message
= error
->message
;
128 cpy
->klass
= error
->klass
;
130 error
->message
= NULL
;
136 const git_error
*giterr_last(void)
138 return GIT_GLOBAL
->last_error
;
141 int giterr_capture(git_error_state
*state
, int error_code
)
143 state
->error_code
= error_code
;
145 giterr_detach(&state
->error_msg
);
149 int giterr_restore(git_error_state
*state
)
151 if (state
&& state
->error_code
&& state
->error_msg
.message
)
152 set_error(state
->error_msg
.klass
, state
->error_msg
.message
);
156 return state
? state
->error_code
: 0;
159 int giterr_system_last(void)
162 return GetLastError();
168 void giterr_system_set(int code
)