Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Repo.clone_from localhost #1493

Unanswered
HealthyPear asked this question in Q&A
Discussion options

Within a pytest suite I have created a Server(Thread) class and a server_tmp_path factory in order to simulate the download of a git repository in my project (actually I might need to download only a specific file, but I have to download the whole repo first, am I right?).

I create the test repo like this,

@pytest.fixture(scope="module")
def test_git_repo(server_tmp_path):
 """Initialize a dummy git repository"""
 import git
 repo_dir = server_tmp_path / "test_git_repo"
 file_name = "test_file.txt"
 file_name_2 = "test_file_2.txt"
 r = git.Repo.init(repo_dir)
 Path(repo_dir / file_name).touch()
 Path(repo_dir / file_name_2).touch()
 r.index.add([file_name])
 r.index.add([file_name_2])
 r.index.commit("initial commit")
 return repo_dir

and then I define the test function as test_download_git_repo(server, test_git_repo, tmp_path) so then from inside I can retrieve the URL of the test repo (e.g. http://localhost:51848/test_git_repo/) and see that the stuff is there,

image

When I cd the directory I can see that it corresponds to an initialized git repository with 1 main branch and the initial commit.

So I am a bit baffled that when I try to do the following,

path = tmp_path / test_git_repo.name
git.repo.Repo.clone_from(f"{server.url}/{test_git_repo.name}", path)

I get the following Traceback,

127.0.0.1 - - [13/Sep/2022 18:07:34] code 404, message File not found
127.0.0.1 - - [13/Sep/2022 18:07:34] "GET /test_git_repo/info/refs?service=git-upload-pack HTTP/1.1" 404 -
*** git.exc.GitCommandError: Cmd('git') failed due to: exit code(128)
 cmdline: git clone -v http://localhost:51848/test_git_repo /private/var/folders/2z/142033n17rbfy969s6h4hymw0000gn/T/pytest-of-michele/pytest-27/test_download_git_repo0/test_git_repo
 stderr: 'Cloning into '/private/var/folders/2z/142033n17rbfy969s6h4hymw0000gn/T/pytest-of-michele/pytest-27/test_download_git_repo0/test_git_repo'...
fatal: repository 'http://localhost:51848/test_git_repo/' not found

Any idea what could be the problem?

You must be logged in to vote

Replies: 2 comments

Comment options

This looks like the server should support the dumb http protocol which requires a little more than hosting a git repository via http. I assume the repository is available on the same machine so it's fine to specify a local filesystem path as url.

To see if it works, I suggest to use the git command-line rather than GitPython for less convoluted error reporting.

You must be logged in to vote
0 replies
Comment options

I assume the repository is available on the same machine so it's fine to specify a local filesystem path as url

This worked,

git.repo.Repo.clone_from("/private/var/folders/2z/142033n17rbfy969s6h4hymw0000gn/T/pytest-of-michele/pytest-26/server0/test_git_repo", path)

though it kind of fails to test what really will happens in the real case (the function to test works on a gitlab repository - using the ssh protocol)

I went to the git page to read about this dumb protocol, but I am not sure how to implement it...my server class and instance look like this,

class Server(Thread):
 def __init__(self, directory):
 super().__init__()
 self.event = Event()
 handler = partial(SimpleHTTPRequestHandler, directory=directory)
 self.httpd = HTTPServer(("", 0), handler)
 self.url = f"http://localhost:{self.httpd.server_port}"
 self.httpd.timeout = 0.1
 self.httpd.handle_timeout = lambda: None
 def run(self):
 with self.httpd:
 while not self.event.is_set():
 self.httpd.handle_request()
@pytest.fixture(scope="module")
def server(server_tmp_path):
 s = Server(server_tmp_path)
 s.start()
 try:
 yield s
 finally:
 s.event.set()
 s.join()
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #1492 on September 14, 2022 00:26.

AltStyle によって変換されたページ (->オリジナル) /