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 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;
48 va_start(arglist
, string
);
49 git_buf_vprintf(&buf
, string
, arglist
);
52 if (error_class
== GITERR_OS
) {
54 if (win32_error_code
) {
58 FORMAT_MESSAGE_ALLOCATE_BUFFER
|
59 FORMAT_MESSAGE_FROM_SYSTEM
|
60 FORMAT_MESSAGE_IGNORE_INSERTS
,
61 NULL
, win32_error_code
, 0, (LPSTR
)&lpMsgBuf
, 0, NULL
)) {
62 git_buf_PUTS(&buf
, ": ");
63 git_buf_puts(&buf
, lpMsgBuf
);
72 git_buf_PUTS(&buf
, ": ");
73 git_buf_puts(&buf
, strerror(error_code
));
80 if (!git_buf_oom(&buf
))
81 set_error(error_class
, git_buf_detach(&buf
));
84 void giterr_set_str(int error_class
, const char *string
)
90 message
= git__strdup(string
);
93 set_error(error_class
, message
);
96 int giterr_set_regex(const regex_t
*regex
, int error_code
)
99 regerror(error_code
, regex
, error_buf
, sizeof(error_buf
));
100 giterr_set_str(GITERR_REGEX
, error_buf
);
102 if (error_code
== REG_NOMATCH
)
103 return GIT_ENOTFOUND
;
104 else if (error_code
> REG_BADPAT
)
105 return GIT_EINVALIDSPEC
;
110 void giterr_clear(void)
112 GIT_GLOBAL
->last_error
= NULL
;
120 const git_error
*giterr_last(void)
122 return GIT_GLOBAL
->last_error
;