Skip to content
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

Git.fetch_commit #13096

Merged
merged 4 commits into from
Feb 16, 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
13 changes: 13 additions & 0 deletions conan/tools/scm/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ def clone(self, url, target="", args=None):
self._conanfile.output.info("Cloning git repo")
self.run('clone "{}" {} {}'.format(url, " ".join(args), target))

def fetch_commit(self, url, commit):
"""
Experimental: does a 1 commit fetch and checkout, instead of a full clone,
should be faster.
"""
if os.path.exists(url):
url = url.replace("\\", "/") # Windows local directory
self._conanfile.output.info("Shallow fetch of git repo")
self.run('init')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For anyone worried about this init as I was, reading https://git-scm.com/docs/git-init#_description says that calling init multiple times is ok

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In any case, this keeps failing in Linux because of #13081 (comment), no idea why

self.run(f'remote add origin "{url}"')
self.run(f'fetch --depth 1 origin {commit}')
self.run(f'checkout FETCH_HEAD')

def checkout(self, commit):
self._conanfile.output.info("Checkout: {}".format(commit))
self.run('checkout {}'.format(commit))
Expand Down
47 changes: 47 additions & 0 deletions conans/test/functional/tools/scm/test_git.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import platform
import re
import textwrap

Expand Down Expand Up @@ -209,6 +210,52 @@ def test_clone_checkout(self):
assert c.load("source/CMakeLists.txt") == "mycmake"


class TestGitShallowClone:
""" base Git cloning operations
"""
conanfile = textwrap.dedent("""
import os
from conan import ConanFile
from conan.tools.scm import Git
from conan.tools.files import load

class Pkg(ConanFile):
name = "pkg"
version = "0.1"

def layout(self):
self.folders.source = "source"

def source(self):
git = Git(self)
git.fetch_commit(url="{url}", commit="{commit}")
self.output.info("MYCMAKE: {{}}".format(load(self, "CMakeLists.txt")))
self.output.info("MYFILE: {{}}".format(load(self, "src/myfile.h")))
""")

@pytest.mark.skipif(platform.system() == "Linux", reason="Git version in Linux not support it")
def test_clone_checkout(self):
folder = os.path.join(temp_folder(), "myrepo")
url, commit = create_local_git_repo(files={"src/myfile.h": "myheader!",
"CMakeLists.txt": "mycmake"}, folder=folder)
# This second commit will NOT be used, as I will use the above commit in the conanfile
save_files(path=folder, files={"src/myfile.h": "my2header2!"})
git_add_changes_commit(folder=folder)

c = TestClient()
c.save({"conanfile.py": self.conanfile.format(url=url, commit=commit)})
c.run("create .")
assert "pkg/0.1: MYCMAKE: mycmake" in c.out
assert "pkg/0.1: MYFILE: myheader!" in c.out

# It also works in local flow
c.run("source .")
assert "conanfile.py (pkg/0.1): MYCMAKE: mycmake" in c.out
assert "conanfile.py (pkg/0.1): MYFILE: myheader!" in c.out
assert c.load("source/src/myfile.h") == "myheader!"
assert c.load("source/CMakeLists.txt") == "mycmake"


class TestGitCloneWithArgs:
""" Git cloning passing additional arguments
"""
Expand Down