Unbreak git_remote_ls on a local transport after disconnecting.
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/src/transports/local.c b/src/transports/local.c
index 5500609..2a85e95 100644
--- a/src/transports/local.c
+++ b/src/transports/local.c
@@ -119,15 +119,24 @@ on_error:
static int store_refs(transport_local *t)
{
- unsigned int i;
+ size_t i;
+ git_remote_head *head;
git_strarray ref_names = {0};
assert(t);
- if (git_reference_list(&ref_names, t->repo) < 0 ||
- git_vector_init(&t->refs, ref_names.count, NULL) < 0)
+ if (git_reference_list(&ref_names, t->repo) < 0)
goto on_error;
+ /* Clear all heads we might have fetched in a previous connect */
+ git_vector_foreach(&t->refs, i, head) {
+ git__free(head->name);
+ git__free(head);
+ }
+
+ /* Clear the vector so we can reuse it */
+ git_vector_clear(&t->refs);
+
/* Sort the references first */
git__tsort((void **)ref_names.strings, ref_names.count, &git__strcmp_cb);
@@ -571,8 +580,6 @@ static void local_cancel(git_transport *transport)
static int local_close(git_transport *transport)
{
transport_local *t = (transport_local *)transport;
- size_t i;
- git_remote_head *head;
t->connected = 0;
@@ -586,13 +593,6 @@ static int local_close(git_transport *transport)
t->url = NULL;
}
- git_vector_foreach(&t->refs, i, head) {
- git__free(head->name);
- git__free(head);
- }
-
- git_vector_free(&t->refs);
-
return 0;
}
@@ -600,6 +600,8 @@ static void local_free(git_transport *transport)
{
transport_local *t = (transport_local *)transport;
+ git_vector_free(&t->refs);
+
/* Close the transport, if it's still open. */
local_close(transport);
@@ -632,6 +634,7 @@ int git_transport_local(git_transport **out, git_remote *owner, void *param)
t->parent.read_flags = local_read_flags;
t->parent.cancel = local_cancel;
+ git_vector_init(&t->refs, 0, NULL);
t->owner = owner;
*out = (git_transport *) t;
diff --git a/tests-clar/network/remote/local.c b/tests-clar/network/remote/local.c
index 3cb8a25..d5d75fd 100644
--- a/tests-clar/network/remote/local.c
+++ b/tests-clar/network/remote/local.c
@@ -75,6 +75,18 @@ void test_network_remote_local__retrieve_advertised_references(void)
cl_assert_equal_i(how_many_refs, 28);
}
+void test_network_remote_local__retrieve_advertised_references_after_disconnect(void)
+{
+ int how_many_refs = 0;
+
+ connect_to_local_repository(cl_fixture("testrepo.git"));
+ git_remote_disconnect(remote);
+
+ cl_git_pass(git_remote_ls(remote, &count_ref__cb, &how_many_refs));
+
+ cl_assert_equal_i(how_many_refs, 28);
+}
+
void test_network_remote_local__retrieve_advertised_references_from_spaced_repository(void)
{
int how_many_refs = 0;