From 2ff1a0d0f0eb7a214253fea3769c3fe64142446c Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 9 Nov 2012 16:59:46 -0800 Subject: [PATCH] Helpers for local-filesystem remote URLs --- tests-clar/clar_helpers.c | 49 +++++++++++++++++++++++++++++ tests-clar/clar_libgit2.h | 4 +++ tests-clar/clone/nonetwork.c | 54 +++----------------------------- tests-clar/network/fetchlocal.c | 50 +++-------------------------- tests-clar/network/remotelocal.c | 41 +----------------------- 5 files changed, 63 insertions(+), 135 deletions(-) diff --git a/tests-clar/clar_helpers.c b/tests-clar/clar_helpers.c index 250c9223b..178ae68b1 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 ce3688ec4..fd20c1259 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 36bf63670..59f43362f 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 85c2575bf..b5bb1761c 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 d4bb1dfef..f7ae83423 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));