Merge pull request #2166 from ethomson/giterr_restore clone: don't overwrite original error message
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
diff --git a/src/clone.c b/src/clone.c
index e19d02b..62f1035 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -430,10 +430,15 @@ int git_clone(
}
if (error != 0) {
+ git_error_state last_error = {0};
+ giterr_capture(&last_error, error);
+
git_repository_free(repo);
repo = NULL;
(void)git_futils_rmdir_r(local_path, NULL, rmdir_flags);
+
+ giterr_restore(&last_error);
}
*out = repo;
diff --git a/tests/core/errors.c b/tests/core/errors.c
index 512a413..366d8f1 100644
--- a/tests/core/errors.c
+++ b/tests/core/errors.c
@@ -85,3 +85,27 @@ void test_core_errors__new_school(void)
giterr_clear();
}
+
+void test_core_errors__restore(void)
+{
+ git_error_state err_state = {0};
+
+ giterr_clear();
+ cl_assert(giterr_last() == NULL);
+
+ cl_assert_equal_i(0, giterr_capture(&err_state, 0));
+
+ memset(&err_state, 0x0, sizeof(git_error_state));
+
+ giterr_set(42, "Foo: %s", "bar");
+ cl_assert_equal_i(-1, giterr_capture(&err_state, -1));
+
+ cl_assert(giterr_last() == NULL);
+
+ giterr_set(99, "Bar: %s", "foo");
+
+ giterr_restore(&err_state);
+
+ cl_assert_equal_i(42, giterr_last()->klass);
+ cl_assert_equal_s("Foo: bar", giterr_last()->message);
+}