8000 clone: don't mix up "http://url" with "http:/url" when figuring out if we should do a local clone by boretrk · Pull Request #6361 · libgit2/libgit2 · GitHub
[go: up one dir, main page]

Skip to content

clone: don't mix up "http://url" with "http:/url" when figuring out if we should do a local clone #6361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions src/libgit2/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "fs_path.h"
#include "repository.h"
#include "odb.h"
#include "net.h"

static int clone_local_into(git_repository *repo, git_remote *remote, const git_fetch_options *fetch_opts, const git_checkout_options *co_opts, const char *branch, int link);

Expand Down Expand Up @@ -336,8 +337,9 @@ static int create_and_configure_origin(
git_remote_create_cb remote_create = options->remote_cb;
void *payload = options->remote_cb_payload;

/* If the path exists and is a dir, the url should be the absolute path */
if (git_fs_path_root(url) < 0 && git_fs_path_exists(url) && git_fs_path_isdir(url)) {
/* If the path is local and exists it should be the absolute path. */
if (!git_net_str_is_url(url) && git_fs_path_root(url) < 0 &&
git_fs_path_exists(url)) {
if (p_realpath(url, buf) == NULL)
return -1;

Expand Down Expand Up @@ -458,26 +460,25 @@ static int clone_into(
int git_clone__should_clone_local(const char *url_or_path, git_clone_local_t local)
{
git_str fromurl = GIT_STR_INIT;
const char *path = url_or_path;
bool is_url, is_local;
bool is_local;

if (local == GIT_CLONE_NO_LOCAL)
return 0;

if ((is_url = git_fs_path_is_local_file_url(url_or_path)) != 0) {
if (git_fs_path_fromurl(&fromurl, url_or_path) < 0) {
is_local = -1;
goto done;
}
if (git_net_str_is_url(url_or_path)) {
/* If GIT_CLONE_LOCAL_AUTO is specified, any url should be treated as remote */
if (local == GIT_CLONE_LOCAL_AUTO ||
!git_fs_path_is_local_file_url(url_or_path))
return 0;

path = fromurl.ptr;
if (git_fs_path_fromurl(&fromurl, url_or_path) == 0)
is_local = git_fs_path_isdir(git_str_cstr(&fromurl));
else
is_local = -1;
git_str_dispose(&fromurl);
} else {
is_local = git_fs_path_isdir(url_or_path);
}

is_local = (!is_url || local != GIT_CLONE_LOCAL_AUTO) &&
git_fs_path_isdir(path);

done:
git_str_dispose(&fromurl);
return is_local;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/libgit2/online/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "refs.h"

#define LIVE_REPO_URL "http://github.com/libgit2/TestGitRepository"
#define LIVE_REPO_AS_DIR "http:/github.com/libgit2/TestGitRepository"
#define LIVE_EMPTYREPO_URL "http://github.com/libgit2/TestEmptyRepository"
#define BB_REPO_URL "https://libgit2-test@bitbucket.org/libgit2-test/testgitrepository.git"
#define BB_REPO_URL_WITH_PASS "https://libgit2-test:YT77Ppm2nq8w4TYjGS8U@bitbucket.org/libgit2-test/testgitrepository.git"
Expand Down Expand Up @@ -115,6 +116,16 @@ void test_online_clone__initialize(void)

if (_remote_expectcontinue)
git_libgit2_opts(GIT_OPT_ENABLE_HTTP_EXPECT_CONTINUE, 1);

#if !defined(GIT_WIN32)
/*
* On system that allows ':' in filenames "http://path" can be misinterpreted
* as the local path "http:/path".
* Create a local non-repository path that looks like LIVE_REPO_URL to make
* sure we can handle cloning despite this directory being around.
*/
git_futils_mkdir_r(LIVE_REPO_AS_DIR, 0777);
#endif
}

void test_online_clone__cleanup(void)
Expand All @@ -127,6 +138,10 @@ void test_online_clone__cleanup(void)
cl_fixture_cleanup("./initial");
cl_fixture_cleanup("./subsequent");

#if !defined(GIT_WIN32)
cl_fixture_cleanup("http:");
#endif

git__free(_remote_url);
git__free(_remote_user);
git__free(_remote_pass);
Expand Down
0