Allow git_remote_ls after disconnecting from the remote Keep the data around until free, as expected by our own fetch example
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
diff --git a/src/remote.c b/src/remote.c
index 896361e..54f0a8a 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -586,11 +586,6 @@ int git_remote_ls(git_remote *remote, git_headlist_cb list_cb, void *payload)
{
assert(remote);
- if (!git_remote_connected(remote)) {
- giterr_set(GITERR_NET, "The remote is not connected");
- return -1;
- }
-
return remote->transport->ls(remote->transport, list_cb, payload);
}
diff --git a/src/transports/local.c b/src/transports/local.c
index ce89bb2..8af970e 100644
--- a/src/transports/local.c
+++ b/src/transports/local.c
@@ -36,7 +36,8 @@ typedef struct {
git_atomic cancelled;
git_repository *repo;
git_vector refs;
- unsigned connected : 1;
+ unsigned connected : 1,
+ have_refs : 1;
} transport_local;
static int add_ref(transport_local *t, const char *name)
@@ -139,6 +140,7 @@ static int store_refs(transport_local *t)
goto on_error;
}
+ t->have_refs = 1;
git_strarray_free(&ref_names);
return 0;
@@ -208,8 +210,8 @@ static int local_ls(git_transport *transport, git_headlist_cb list_cb, void *pay
unsigned int i;
git_remote_head *head = NULL;
- if (!t->connected) {
- giterr_set(GITERR_NET, "The transport is not connected");
+ if (!t->have_refs) {
+ giterr_set(GITERR_NET, "The transport has not yet loaded the refs");
return -1;
}
@@ -569,8 +571,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;
@@ -579,13 +579,6 @@ static int local_close(git_transport *transport)
t->repo = NULL;
}
- git_vector_foreach(&t->refs, i, head) {
- git__free(head->name);
- git__free(head);
- }
-
- git_vector_free(&t->refs);
-
if (t->url) {
git__free(t->url);
t->url = NULL;
@@ -597,10 +590,19 @@ static int local_close(git_transport *transport)
static void local_free(git_transport *transport)
{
transport_local *t = (transport_local *)transport;
+ size_t i;
+ git_remote_head *head;
/* Close the transport, if it's still open. */
local_close(transport);
+ git_vector_foreach(&t->refs, i, head) {
+ git__free(head->name);
+ git__free(head);
+ }
+
+ git_vector_free(&t->refs);
+
/* Free the transport */
git__free(t);
}
diff --git a/src/transports/smart.c b/src/transports/smart.c
index bfcce0c..416eb22 100644
--- a/src/transports/smart.c
+++ b/src/transports/smart.c
@@ -253,7 +253,6 @@ static int git_smart__read_flags(git_transport *transport, int *flags)
static int git_smart__close(git_transport *transport)
{
transport_smart *t = (transport_smart *)transport;
- git_vector *refs = &t->refs;
git_vector *common = &t->common;
unsigned int i;
git_pkt *p;
@@ -261,11 +260,6 @@ static int git_smart__close(git_transport *transport)
ret = git_smart__reset_stream(t, true);
- git_vector_foreach(refs, i, p)
- git_pkt_free(p);
-
- git_vector_free(refs);
-
git_vector_foreach(common, i, p)
git_pkt_free(p);
@@ -284,6 +278,9 @@ static int git_smart__close(git_transport *transport)
static void git_smart__free(git_transport *transport)
{
transport_smart *t = (transport_smart *)transport;
+ git_vector *refs = &t->refs;
+ unsigned int i;
+ git_pkt *p;
/* Make sure that the current stream is closed, if we have one. */
git_smart__close(transport);
@@ -291,6 +288,11 @@ static void git_smart__free(git_transport *transport)
/* Free the subtransport */
t->wrapped->free(t->wrapped);
+ git_vector_foreach(refs, i, p)
+ git_pkt_free(p);
+
+ git_vector_free(refs);
+
git__free(t);
}
diff --git a/tests-clar/online/fetch.c b/tests-clar/online/fetch.c
index a0ee7aa..bfa1eb9 100644
--- a/tests-clar/online/fetch.c
+++ b/tests-clar/online/fetch.c
@@ -134,3 +134,30 @@ void test_online_fetch__can_cancel(void)
git_remote_disconnect(remote);
git_remote_free(remote);
}
+
+int ls_cb(git_remote_head *rhead, void *payload)
+{
+ int *nr = payload;
+ GIT_UNUSED(rhead);
+
+ (*nr)++;
+
+ return 0;
+}
+
+void test_online_fetch__ls_disconnected(void)
+{
+ git_remote *remote;
+ int nr_before = 0, nr_after = 0;
+
+ cl_git_pass(git_remote_create(&remote, _repo, "test",
+ "http://github.com/libgit2/TestGitRepository.git"));
+ cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
+ cl_git_pass(git_remote_ls(remote, ls_cb, &nr_before));
+ git_remote_disconnect(remote);
+ cl_git_pass(git_remote_ls(remote, ls_cb, &nr_after));
+
+ cl_assert_equal_i(nr_before, nr_after);
+
+ git_remote_free(remote);
+}