Helpers for local-filesystem remote URLs
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
diff --git a/tests-clar/clar_helpers.c b/tests-clar/clar_helpers.c
index 250c922..178ae68 100644
--- a/tests-clar/clar_helpers.c
+++ b/tests-clar/clar_helpers.c
@@ -1,5 +1,6 @@
#include "clar_libgit2.h"
#include "posix.h"
+#include "path.h"
void clar_on_init(void)
{
@@ -222,3 +223,51 @@ bool cl_is_chmod_supported(void)
return _is_supported;
}
+const char* cl_git_fixture_url(const char *fixturename)
+{
+ return cl_git_path_url(cl_fixture(fixturename));
+}
+
+const char* cl_git_path_url(const char *path)
+{
+ static char url[4096];
+
+ const char *in_buf;
+ git_buf path_buf = GIT_BUF_INIT;
+ git_buf url_buf = GIT_BUF_INIT;
+
+ cl_git_pass(git_path_prettify_dir(&path_buf, path, NULL));
+ cl_git_pass(git_buf_puts(&url_buf, "file://"));
+
+#ifdef _MSC_VER
+ /*
+ * A FILE uri matches the following format: file://[host]/path
+ * where "host" can be empty and "path" is an absolute path to the resource.
+ *
+ * In this test, no hostname is used, but we have to ensure the leading triple slashes:
+ *
+ * *nix: file:///usr/home/...
+ * Windows: file:///C:/Users/...
+ */
+ cl_git_pass(git_buf_putc(url_buf, '/'));
+#endif
+
+ in_buf = git_buf_cstr(&path_buf);
+
+ /*
+ * A very hacky Url encoding that only takes care of escaping the spaces
+ */
+ while (*in_buf) {
+ if (*in_buf == ' ')
+ cl_git_pass(git_buf_puts(&url_buf, "%20"));
+ else
+ cl_git_pass(git_buf_putc(&url_buf, *in_buf));
+
+ in_buf++;
+ }
+
+ strncpy(url, git_buf_cstr(&url_buf), 4096);
+ git_buf_free(&url_buf);
+ git_buf_free(&path_buf);
+ return url;
+}
diff --git a/tests-clar/clar_libgit2.h b/tests-clar/clar_libgit2.h
index ce3688e..fd20c12 100644
--- a/tests-clar/clar_libgit2.h
+++ b/tests-clar/clar_libgit2.h
@@ -57,4 +57,8 @@ int cl_rename(const char *source, const char *dest);
git_repository *cl_git_sandbox_init(const char *sandbox);
void cl_git_sandbox_cleanup(void);
+/* Local-repo url helpers */
+const char* cl_git_fixture_url(const char *fixturename);
+const char* cl_git_path_url(const char *path);
+
#endif
diff --git a/tests-clar/clone/nonetwork.c b/tests-clar/clone/nonetwork.c
index 36bf636..59f4336 100644
--- a/tests-clar/clone/nonetwork.c
+++ b/tests-clar/clone/nonetwork.c
@@ -19,46 +19,6 @@ static void cleanup_repository(void *path)
cl_fixture_cleanup((const char *)path);
}
-// TODO: This is copy/pasted from network/remotelocal.c.
-static void build_local_file_url(git_buf *out, const char *fixture)
-{
- const char *in_buf;
-
- git_buf path_buf = GIT_BUF_INIT;
-
- cl_git_pass(git_path_prettify_dir(&path_buf, fixture, NULL));
- cl_git_pass(git_buf_puts(out, "file://"));
-
-#ifdef GIT_WIN32
- /*
- * A FILE uri matches the following format: file://[host]/path
- * where "host" can be empty and "path" is an absolute path to the resource.
- *
- * In this test, no hostname is used, but we have to ensure the leading triple slashes:
- *
- * *nix: file:///usr/home/...
- * Windows: file:///C:/Users/...
- */
- cl_git_pass(git_buf_putc(out, '/'));
-#endif
-
- in_buf = git_buf_cstr(&path_buf);
-
- /*
- * A very hacky Url encoding that only takes care of escaping the spaces
- */
- while (*in_buf) {
- if (*in_buf == ' ')
- cl_git_pass(git_buf_puts(out, "%20"));
- else
- cl_git_pass(git_buf_putc(out, *in_buf));
-
- in_buf++;
- }
-
- git_buf_free(&path_buf);
-}
-
void test_clone_nonetwork__bad_url(void)
{
/* Clone should clean up the mess if the URL isn't a git repository */
@@ -70,24 +30,18 @@ void test_clone_nonetwork__bad_url(void)
void test_clone_nonetwork__local(void)
{
- git_buf src = GIT_BUF_INIT;
- build_local_file_url(&src, cl_fixture("testrepo.git"));
+ const char *src = cl_git_fixture_url("testrepo.git");
cl_set_cleanup(&cleanup_repository, "./local");
- cl_git_pass(git_clone(&g_repo, git_buf_cstr(&src), "./local", NULL, NULL, NULL));
-
- git_buf_free(&src);
+ cl_git_pass(git_clone(&g_repo, src, "./local", NULL, NULL, NULL));
}
void test_clone_nonetwork__local_bare(void)
{
- git_buf src = GIT_BUF_INIT;
- build_local_file_url(&src, cl_fixture("testrepo.git"));
+ const char *src = cl_git_fixture_url("testrepo.git");
cl_set_cleanup(&cleanup_repository, "./local.git");
- cl_git_pass(git_clone_bare(&g_repo, git_buf_cstr(&src), "./local.git", NULL, NULL));
-
- git_buf_free(&src);
+ cl_git_pass(git_clone_bare(&g_repo, src, "./local.git", NULL, NULL));
}
void test_clone_nonetwork__fail_when_the_target_is_a_file(void)
diff --git a/tests-clar/network/fetchlocal.c b/tests-clar/network/fetchlocal.c
index 85c2575..b5bb176 100644
--- a/tests-clar/network/fetchlocal.c
+++ b/tests-clar/network/fetchlocal.c
@@ -4,45 +4,6 @@
#include "path.h"
#include "remote.h"
-static void build_local_file_url(git_buf *out, const char *fixture)
-{
- const char *in_buf;
-
- git_buf path_buf = GIT_BUF_INIT;
-
- cl_git_pass(git_path_prettify_dir(&path_buf, fixture, NULL));
- cl_git_pass(git_buf_puts(out, "file://"));
-
-#ifdef _MSC_VER
- /*
- * A FILE uri matches the following format: file://[host]/path
- * where "host" can be empty and "path" is an absolute path to the resource.
- *
- * In this test, no hostname is used, but we have to ensure the leading triple slashes:
- *
- * *nix: file:///usr/home/...
- * Windows: file:///C:/Users/...
- */
- cl_git_pass(git_buf_putc(out, '/'));
-#endif
-
- in_buf = git_buf_cstr(&path_buf);
-
- /*
- * A very hacky Url encoding that only takes care of escaping the spaces
- */
- while (*in_buf) {
- if (*in_buf == ' ')
- cl_git_pass(git_buf_puts(out, "%20"));
- else
- cl_git_pass(git_buf_putc(out, *in_buf));
-
- in_buf++;
- }
-
- git_buf_free(&path_buf);
-}
-
static void transfer_cb(const git_transfer_progress *stats, void *payload)
{
int *callcount = (int*)payload;
@@ -52,16 +13,15 @@ static void transfer_cb(const git_transfer_progress *stats, void *payload)
void test_network_fetchlocal__complete(void)
{
- git_buf url = GIT_BUF_INIT;
git_repository *repo;
git_remote *origin;
int callcount = 0;
git_strarray refnames = {0};
- build_local_file_url(&url, cl_fixture("testrepo.git"));
+ const char *url = cl_git_fixture_url("testrepo.git");
cl_git_pass(git_repository_init(&repo, "foo", true));
- cl_git_pass(git_remote_add(&origin, repo, GIT_REMOTE_ORIGIN, git_buf_cstr(&url)));
+ cl_git_pass(git_remote_add(&origin, repo, GIT_REMOTE_ORIGIN, url));
cl_git_pass(git_remote_connect(origin, GIT_DIR_FETCH));
cl_git_pass(git_remote_download(origin, transfer_cb, &callcount));
cl_git_pass(git_remote_update_tips(origin));
@@ -78,16 +38,16 @@ void test_network_fetchlocal__complete(void)
void test_network_fetchlocal__partial(void)
{
git_repository *repo = cl_git_sandbox_init("partial-testrepo");
- git_buf url = GIT_BUF_INIT;
git_remote *origin;
int callcount = 0;
git_strarray refnames = {0};
+ const char *url;
cl_git_pass(git_reference_list(&refnames, repo, GIT_REF_LISTALL));
cl_assert_equal_i(1, refnames.count);
- build_local_file_url(&url, cl_fixture("testrepo.git"));
- cl_git_pass(git_remote_add(&origin, repo, GIT_REMOTE_ORIGIN, git_buf_cstr(&url)));
+ url = cl_git_fixture_url("testrepo.git");
+ cl_git_pass(git_remote_add(&origin, repo, GIT_REMOTE_ORIGIN, url));
cl_git_pass(git_remote_connect(origin, GIT_DIR_FETCH));
cl_git_pass(git_remote_download(origin, transfer_cb, &callcount));
cl_git_pass(git_remote_update_tips(origin));
diff --git a/tests-clar/network/remotelocal.c b/tests-clar/network/remotelocal.c
index d4bb1df..f7ae834 100644
--- a/tests-clar/network/remotelocal.c
+++ b/tests-clar/network/remotelocal.c
@@ -7,45 +7,6 @@ static git_repository *repo;
static git_buf file_path_buf = GIT_BUF_INIT;
static git_remote *remote;
-static void build_local_file_url(git_buf *out, const char *fixture)
-{
- const char *in_buf;
-
- git_buf path_buf = GIT_BUF_INIT;
-
- cl_git_pass(git_path_prettify_dir(&path_buf, fixture, NULL));
- cl_git_pass(git_buf_puts(out, "file://"));
-
-#ifdef _MSC_VER
- /*
- * A FILE uri matches the following format: file://[host]/path
- * where "host" can be empty and "path" is an absolute path to the resource.
- *
- * In this test, no hostname is used, but we have to ensure the leading triple slashes:
- *
- * *nix: file:///usr/home/...
- * Windows: file:///C:/Users/...
- */
- cl_git_pass(git_buf_putc(out, '/'));
-#endif
-
- in_buf = git_buf_cstr(&path_buf);
-
- /*
- * A very hacky Url encoding that only takes care of escaping the spaces
- */
- while (*in_buf) {
- if (*in_buf == ' ')
- cl_git_pass(git_buf_puts(out, "%20"));
- else
- cl_git_pass(git_buf_putc(out, *in_buf));
-
- in_buf++;
- }
-
- git_buf_free(&path_buf);
-}
-
void test_network_remotelocal__initialize(void)
{
cl_git_pass(git_repository_init(&repo, "remotelocal/", 0));
@@ -82,7 +43,7 @@ static int ensure_peeled__cb(git_remote_head *head, void *payload)
static void connect_to_local_repository(const char *local_repository)
{
- build_local_file_url(&file_path_buf, local_repository);
+ git_buf_sets(&file_path_buf, cl_git_path_url(local_repository));
cl_git_pass(git_remote_new(&remote, repo, NULL, git_buf_cstr(&file_path_buf), NULL));
cl_git_pass(git_remote_connect(remote, GIT_DIR_FETCH));