error: Simplify giterr_detach
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
diff --git a/include/git2/errors.h b/include/git2/errors.h
index 5f5d0ab..be7a31d 100644
--- a/include/git2/errors.h
+++ b/include/git2/errors.h
@@ -89,17 +89,14 @@ GIT_EXTERN(void) giterr_clear(void);
/**
* Get the last error data and clear it.
*
- * This copies the last error message into the given `git_buf` and returns
- * the associated `git_error_t`, leaving the error cleared as if
- * `giterr_clear` had been called. You must call `git_buf_free` on the
- * message to release the memory.
+ * This copies the last error into the given `git_error` struct
+ * and returns 0 if the copy was successful, leaving the error
+ * cleared as if `giterr_clear` had been called.
*
- * Note: it is possible that this will return `GITERR_NONE` and set the
- * buffer to NULL, so be prepared for that condition. Also, if the last
- * error was an out-of-memory error, this will return `GITERR_NOMEMORY`
- * but also leave the buffer set to NULL (to avoid allocation).
+ * If there was no existing error in the library, -1 will be returned
+ * and the contents of `cpy` will be left unmodified.
*/
-GIT_EXTERN(git_error_t) giterr_detach(git_buf *message);
+GIT_EXTERN(int) giterr_detach(git_error *cpy);
/**
* Set the error message string for this thread.
diff --git a/src/errors.c b/src/errors.c
index 70b5f26..d04da4c 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -112,27 +112,22 @@ void giterr_clear(void)
#endif
}
-git_error_t giterr_detach(git_buf *message)
+int giterr_detach(git_error *cpy)
{
- git_error_t rval;
git_error *error = GIT_GLOBAL->last_error;
- assert(message);
-
- git_buf_free(message);
+ assert(cpy);
if (!error)
- return GITERR_NONE;
-
- rval = error->klass;
+ return -1;
- if (error != &g_git_oom_error)
- git_buf_attach(message, error->message, 0);
+ cpy->message = error->message;
+ cpy->klass = error->klass;
error->message = NULL;
giterr_clear();
- return rval;
+ return 0;
}
const git_error *giterr_last(void)
diff --git a/src/iterator.c b/src/iterator.c
index 369a079..8646399 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -991,8 +991,9 @@ static int fs_iterator__expand_dir(fs_iterator *fi)
fi->base.start, fi->base.end, &ff->entries);
if (error < 0) {
- git_buf msg = GIT_BUF_INIT;
- git_error_t errt = giterr_detach(&msg);
+ git_error last_error = {0};
+
+ giterr_detach(&last_error);
/* these callbacks may clear the error message */
fs_iterator__free_frame(ff);
@@ -1000,9 +1001,9 @@ static int fs_iterator__expand_dir(fs_iterator *fi)
/* next time return value we skipped to */
fi->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS;
- if (msg.ptr) {
- giterr_set_str(errt, msg.ptr);
- git_buf_free(&msg);
+ if (last_error.message) {
+ giterr_set_str(last_error.klass, last_error.message);
+ free(last_error.message);
}
return error;