From 84244a959d1bf0f42d32eca28f6cc47809016bca Mon Sep 17 00:00:00 2001 From: memsharded Date: Fri, 9 Jun 2023 19:44:59 +0200 Subject: [PATCH 1/3] Git.clone change folder to target one --- conan/tools/scm/git.py | 5 ++- conans/test/functional/tools/scm/test_git.py | 39 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/conan/tools/scm/git.py b/conan/tools/scm/git.py index 5bf1b29702a..aac9b02cce4 100644 --- a/conan/tools/scm/git.py +++ b/conan/tools/scm/git.py @@ -162,7 +162,10 @@ def clone(self, url, target="", args=None): url = url.replace("\\", "/") # Windows local directory mkdir(self.folder) self._conanfile.output.info("Cloning git repo") - self.run('clone "{}" {} {}'.format(url, " ".join(args), target)) + target_path = f'"{target}"' if target else "" # quote in case there are spaces in path + self.run('clone "{}" {} {}'.format(url, " ".join(args), target_path)) + if target: + self.folder = target def fetch_commit(self, url, commit): """ diff --git a/conans/test/functional/tools/scm/test_git.py b/conans/test/functional/tools/scm/test_git.py index a8e5343742f..7ed255706cd 100644 --- a/conans/test/functional/tools/scm/test_git.py +++ b/conans/test/functional/tools/scm/test_git.py @@ -210,6 +210,45 @@ def test_clone_checkout(self): assert c.load("source/src/myfile.h") == "myheader!" assert c.load("source/CMakeLists.txt") == "mycmake" + def test_clone_target(self): + # Clone to a different target folder, then need to define a new Git, changing the + # folder + # https://github.com/conan-io/conan/issues/14058 + 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) + target = os.path.join(self.source_folder, "target") + git.clone(url="{url}", target=target) + git.checkout(commit="{commit}") + + self.output.info("MYCMAKE: {{}}".format(load(self, "target/CMakeLists.txt"))) + self.output.info("MYFILE: {{}}".format(load(self, "target/src/myfile.h"))) + """) + 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": 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 + @pytest.mark.tool("git") class TestGitShallowClone: From 396f479e5614c75f0870f0ca672eaf13d755b052 Mon Sep 17 00:00:00 2001 From: memsharded Date: Wed, 14 Jun 2023 13:55:00 +0200 Subject: [PATCH 2/3] not automatic cd folder --- conan/tools/scm/git.py | 2 -- conans/test/functional/tools/scm/test_git.py | 14 +++++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/conan/tools/scm/git.py b/conan/tools/scm/git.py index fd2a24a541c..a47caa0b905 100644 --- a/conan/tools/scm/git.py +++ b/conan/tools/scm/git.py @@ -164,8 +164,6 @@ def clone(self, url, target="", args=None): self._conanfile.output.info("Cloning git repo") target_path = f'"{target}"' if target else "" # quote in case there are spaces in path self.run('clone "{}" {} {}'.format(url, " ".join(args), target_path)) - if target: - self.folder = target def fetch_commit(self, url, commit): """ diff --git a/conans/test/functional/tools/scm/test_git.py b/conans/test/functional/tools/scm/test_git.py index 7ed255706cd..6ce16a32fe7 100644 --- a/conans/test/functional/tools/scm/test_git.py +++ b/conans/test/functional/tools/scm/test_git.py @@ -211,8 +211,7 @@ def test_clone_checkout(self): assert c.load("source/CMakeLists.txt") == "mycmake" def test_clone_target(self): - # Clone to a different target folder, then need to define a new Git, changing the - # folder + # Clone to a different target folder # https://github.com/conan-io/conan/issues/14058 conanfile = textwrap.dedent(""" import os @@ -228,10 +227,15 @@ def layout(self): self.folders.source = "source" def source(self): + # Alternative, first defining the folder + # git = Git(self, "target") + # git.clone(url="{url}", target=".") + # git.checkout(commit="{commit}") + git = Git(self) - target = os.path.join(self.source_folder, "target") - git.clone(url="{url}", target=target) - git.checkout(commit="{commit}") + git.clone(url="{url}", target="target") # git clone url target + git.folder = "target" # cd target + git.checkout(commit="{commit}") # git checkout commit self.output.info("MYCMAKE: {{}}".format(load(self, "target/CMakeLists.txt"))) self.output.info("MYFILE: {{}}".format(load(self, "target/src/myfile.h"))) From 2ba1ac713d8fa628e99f40bbf115b9b56e83e95b Mon Sep 17 00:00:00 2001 From: memsharded Date: Wed, 14 Jun 2023 14:18:16 +0200 Subject: [PATCH 3/3] improve test --- conans/test/functional/tools/scm/test_git.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/conans/test/functional/tools/scm/test_git.py b/conans/test/functional/tools/scm/test_git.py index 6ce16a32fe7..876b7a9c762 100644 --- a/conans/test/functional/tools/scm/test_git.py +++ b/conans/test/functional/tools/scm/test_git.py @@ -233,12 +233,12 @@ def source(self): # git.checkout(commit="{commit}") git = Git(self) - git.clone(url="{url}", target="target") # git clone url target - git.folder = "target" # cd target + git.clone(url="{url}", target="tar get") # git clone url target + git.folder = "tar get" # cd target git.checkout(commit="{commit}") # git checkout commit - self.output.info("MYCMAKE: {{}}".format(load(self, "target/CMakeLists.txt"))) - self.output.info("MYFILE: {{}}".format(load(self, "target/src/myfile.h"))) + self.output.info("MYCMAKE: {{}}".format(load(self, "tar get/CMakeLists.txt"))) + self.output.info("MYFILE: {{}}".format(load(self, "tar get/src/myfile.h"))) """) folder = os.path.join(temp_folder(), "myrepo") url, commit = create_local_git_repo(files={"src/myfile.h": "myheader!",