Skip to content

Commit

Permalink
Proper handling of credential requests in Git client
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Koefferlein committed Nov 5, 2023
1 parent 9d589b3 commit 08a790d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/tl/tl/tlGit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@ checkout_branch (git_repository *repo, git_remote *remote, const git_checkout_op
check (git_checkout_head (repo, co_opts));
}

int
credentials_cb (git_credential ** /*out*/, const char * /*url*/, const char * /*username*/, unsigned int /*allowed_types*/, void *)
{
// no credentials aquired
git_error_set_str (GIT_ERROR_NONE, "anonymous access is supported only, but server requests credentials");
return GIT_EUSER;
}

void
GitObject::read (const std::string &org_url, const std::string &org_filter, const std::string &subfolder, const std::string &branch, double timeout, tl::InputHttpStreamCallback *callback)
{
Expand Down Expand Up @@ -294,6 +302,7 @@ GitObject::read (const std::string &org_url, const std::string &org_filter, cons
fetch_opts.depth = 1; // shallow (single commit)
#endif
fetch_opts.callbacks.transfer_progress = &fetch_progress;
fetch_opts.callbacks.credentials = &credentials_cb;
fetch_opts.callbacks.payload = (void *) &progress;

// build refspecs in case they are needed
Expand Down
13 changes: 13 additions & 0 deletions src/tl/unit_tests/tlGitTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "tlFileUtils.h"

static std::string test_url ("https://github.com/klayout/klayout_git_test.git");
static std::string test_url_invalid ("https://github.com/klayout/doesnotexist.git");

TEST(1_plain)
{
Expand Down Expand Up @@ -201,5 +202,17 @@ TEST(10_invalid_branch)
}
}

TEST(11_invalid_url)
{
std::string path = tl::TestBase::tmp_file ("repo");
tl::GitObject repo (path);
try {
repo.read (test_url_invalid, std::string (), std::string (), std::string ("brxxx"));
EXPECT_EQ (true, false);
} catch (tl::Exception &ex) {
EXPECT_EQ (ex.msg (), "Error cloning Git repo: anonymous access is supported only, but server requests credentials");
}
}

#endif

0 comments on commit 08a790d

Please sign in to comment.