summaryrefslogtreecommitdiff
path: root/src/common.h
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2014-06-20 14:42:16 +0200
committerVicent Marti <tanoku@gmail.com>2014-06-20 14:42:16 +0200
commit28f087c8642ff9c8dd6964e101e6d8539db6281a (patch)
tree3518d1bf420e92c964bed03074575d8a1db88654 /src/common.h
parent4b0a36e881506a02b43a4ae3c19c93c919b36eeb (diff)
parent1589aa0c4d48fb130d8a5db28c45cd3d173cde6d (diff)
downloadlibgit2-28f087c8642ff9c8dd6964e101e6d8539db6281a.tar.gz
libgit2 v0.21.0v0.21.0
Diffstat (limited to 'src/common.h')
-rw-r--r--src/common.h77
1 files changed, 63 insertions, 14 deletions
diff --git a/src/common.h b/src/common.h
index 159d31b2e..807e5fa39 100644
--- a/src/common.h
+++ b/src/common.h
@@ -10,6 +10,13 @@
#include "git2/common.h"
#include "cc-compat.h"
+/** Declare a function as always inlined. */
+#if defined(_MSC_VER)
+# define GIT_INLINE(type) static __inline type
+#else
+# define GIT_INLINE(type) static inline type
+#endif
+
#include <assert.h>
#include <errno.h>
#include <limits.h>
@@ -37,11 +44,15 @@
#else
# include <unistd.h>
+# include <strings.h>
# ifdef GIT_THREADS
# include <pthread.h>
+# include <sched.h>
# endif
#define GIT_STDLIB_CALL
+# include <arpa/inet.h>
+
#endif
#include "git2/types.h"
@@ -60,7 +71,7 @@
* Check a return value and propogate result if non-zero.
*/
#define GITERR_CHECK_ERROR(code) \
- do { int _err = (code); if (_err < 0) return _err; } while (0)
+ do { int _err = (code); if (_err) return _err; } while (0)
/**
* Set the error message for this thread, formatting as needed.
@@ -74,28 +85,61 @@ void giterr_set(int error_class, const char *string, ...);
int giterr_set_regex(const regex_t *regex, int error_code);
/**
- * Gets the system error code for this thread.
+ * Set error message for user callback if needed.
+ *
+ * If the error code in non-zero and no error message is set, this
+ * sets a generic error message.
+ *
+ * @return This always returns the `error_code` parameter.
*/
-GIT_INLINE(int) giterr_system_last(void)
+GIT_INLINE(int) giterr_set_after_callback_function(
+ int error_code, const char *action)
{
+ if (error_code) {
+ const git_error *e = giterr_last();
+ if (!e || !e->message)
+ giterr_set(e ? e->klass : GITERR_CALLBACK,
+ "%s callback returned %d", action, error_code);
+ }
+ return error_code;
+}
+
#ifdef GIT_WIN32
- return GetLastError();
+#define giterr_set_after_callback(code) \
+ giterr_set_after_callback_function((code), __FUNCTION__)
#else
- return errno;
+#define giterr_set_after_callback(code) \
+ giterr_set_after_callback_function((code), __func__)
#endif
-}
+
+/**
+ * Gets the system error code for this thread.
+ */
+int giterr_system_last(void);
/**
* Sets the system error code for this thread.
*/
-GIT_INLINE(void) giterr_system_set(int code)
-{
-#ifdef GIT_WIN32
- SetLastError(code);
-#else
- errno = code;
-#endif
-}
+void giterr_system_set(int code);
+
+/**
+ * Structure to preserve libgit2 error state
+ */
+typedef struct {
+ int error_code;
+ git_error error_msg;
+} git_error_state;
+
+/**
+ * Capture current error state to restore later, returning error code.
+ * If `error_code` is zero, this does nothing and returns zero.
+ */
+int giterr_capture(git_error_state *state, int error_code);
+
+/**
+ * Restore error state to a previous value, returning saved error code.
+ */
+int giterr_restore(git_error_state *state);
/**
* Check a versioned structure for validity
@@ -126,6 +170,11 @@ GIT_INLINE(void) git__init_structure(void *structure, size_t len, unsigned int v
}
#define GIT_INIT_STRUCTURE(S,V) git__init_structure(S, sizeof(*S), V)
+#define GIT_INIT_STRUCTURE_FROM_TEMPLATE(PTR,VERSION,TYPE,TPL) do { \
+ TYPE _tmpl = TPL; \
+ GITERR_CHECK_VERSION(&(VERSION), _tmpl.version, #TYPE); \
+ memcpy((PTR), &_tmpl, sizeof(_tmpl)); } while (0)
+
/* NOTE: other giterr functions are in the public errors.h header file */
#include "util.h"