From 4861eee95b374f7d97a1bdff59f28f6e0aef84dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=B6ffner?= Date: Sun, 7 Apr 2024 06:10:39 +0200 Subject: [PATCH 1/4] Add hide_url tests for git scm tool --- conans/test/functional/tools/scm/test_git.py | 62 ++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/conans/test/functional/tools/scm/test_git.py b/conans/test/functional/tools/scm/test_git.py index 90cd7025741..ae1df3cdeb3 100644 --- a/conans/test/functional/tools/scm/test_git.py +++ b/conans/test/functional/tools/scm/test_git.py @@ -289,6 +289,37 @@ def test_clone_checkout(self): assert c.load("source/src/myfile.h") == "myheader!" assert c.load("source/CMakeLists.txt") == "mycmake" + def test_clone_url_not_hidden(self): + 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.clone(url="{url}", target=".", hide_url=False) + """) + folder = os.path.join(temp_folder(), "myrepo") + url, _ = create_local_git_repo(files={"CMakeLists.txt": "mycmake"}, folder=folder) + + c = TestClient() + c.save({"conanfile.py": conanfile.format(url=url)}) + c.run("create . -v") + # Clone URL is explicitly printed + assert f'pkg/0.1: RUN: git clone "{url}" "."' in c.out + + # It also works in local flow + c.run("source .") + assert f'conanfile.py (pkg/0.1): RUN: git clone "{url}" "."' in c.out + def test_clone_target(self): # Clone to a different target folder # https://github.com/conan-io/conan/issues/14058 @@ -422,6 +453,37 @@ def test_clone_checkout(self): assert c.load("source/src/myfile.h") == "myheader!" assert c.load("source/CMakeLists.txt") == "mycmake" + def test_clone_url_not_hidden(self): + 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}", hide_url=False) + """) + folder = os.path.join(temp_folder(), "myrepo") + url, commit = create_local_git_repo(files={"CMakeLists.txt": "mycmake"}, folder=folder) + + c = TestClient() + c.save({"conanfile.py": conanfile.format(url=url, commit=commit)}) + c.run("create . -v") + # Clone URL is explicitly printed + assert f'pkg/0.1: RUN: git remote add origin "{url}"' in c.out + + # It also works in local flow + c.run("source .") + assert f'conanfile.py (pkg/0.1): RUN: git remote add origin "{url}"' in c.out + class TestGitCloneWithArgs: """ Git cloning passing additional arguments From a4f9be09b486eb653cd1b470de2ec0520a761061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=B6ffner?= Date: Sun, 7 Apr 2024 06:05:28 +0200 Subject: [PATCH 2/4] Add hide_url flag to clone and fetch_commit. Resolves #15684 --- conan/tools/scm/git.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/conan/tools/scm/git.py b/conan/tools/scm/git.py index bf2ea8ba064..1c8a12fc06d 100644 --- a/conan/tools/scm/git.py +++ b/conan/tools/scm/git.py @@ -197,13 +197,15 @@ def get_repo_root(self): folder = self.run("rev-parse --show-toplevel") return folder.replace("\\", "/") - def clone(self, url, target="", args=None): + def clone(self, url, target="", args=None, hide_url=True): """ Performs a ``git clone `` operation, where target is the target directory. :param url: URL of remote repository. :param target: Target folder. :param args: Extra arguments to pass to the git clone as a list. + :param hide_url: Hides the URL from the log output to prevent accidental + credential leaks. Can be disabled by passing ``False``. """ args = args or [] if os.path.exists(url): @@ -212,18 +214,24 @@ 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 # Avoid printing the clone command, it can contain tokens - self.run('clone "{}" {} {}'.format(url, " ".join(args), target_path), hidden_output=url) + self.run('clone "{}" {} {}'.format(url, " ".join(args), target_path), + hidden_output=url if hide_url else None) - def fetch_commit(self, url, commit): + def fetch_commit(self, url, commit, hide_url=True): """ - Experimental: does a 1 commit fetch and checkout, instead of a full clone, + Experimental: does a single commit fetch and checkout, instead of a full clone, should be faster. + + :param url: URL of remote repository. + :param commit: The commit ref to checkout. + :param hide_url: Hides the URL from the log output to prevent accidental + credential leaks. Can be disabled by passing ``False``. """ if os.path.exists(url): url = url.replace("\\", "/") # Windows local directory self._conanfile.output.info("Shallow fetch of git repo") self.run('init') - self.run(f'remote add origin "{url}"', hidden_output=url) + self.run(f'remote add origin "{url}"', hidden_output=url if hide_url else None) self.run(f'fetch --depth 1 origin {commit}') self.run('checkout FETCH_HEAD') From 4b8cdcf42d6125ecf6069bb08668bedc7fff8f56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Rinc=C3=B3n=20Blanco?= Date: Mon, 8 Apr 2024 17:55:34 +0200 Subject: [PATCH 3/4] Update conans/test/functional/tools/scm/test_git.py --- conans/test/functional/tools/scm/test_git.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conans/test/functional/tools/scm/test_git.py b/conans/test/functional/tools/scm/test_git.py index ae1df3cdeb3..7b8587ac528 100644 --- a/conans/test/functional/tools/scm/test_git.py +++ b/conans/test/functional/tools/scm/test_git.py @@ -310,7 +310,7 @@ def source(self): folder = os.path.join(temp_folder(), "myrepo") url, _ = create_local_git_repo(files={"CMakeLists.txt": "mycmake"}, folder=folder) - c = TestClient() + c = TestClient(light=True) c.save({"conanfile.py": conanfile.format(url=url)}) c.run("create . -v") # Clone URL is explicitly printed From f27808e318261a35908bee3cfdd500bd7b15d7a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Rinc=C3=B3n=20Blanco?= Date: Mon, 8 Apr 2024 17:55:38 +0200 Subject: [PATCH 4/4] Update conans/test/functional/tools/scm/test_git.py --- conans/test/functional/tools/scm/test_git.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conans/test/functional/tools/scm/test_git.py b/conans/test/functional/tools/scm/test_git.py index 7b8587ac528..bc7bf1b6cbe 100644 --- a/conans/test/functional/tools/scm/test_git.py +++ b/conans/test/functional/tools/scm/test_git.py @@ -474,7 +474,7 @@ def source(self): folder = os.path.join(temp_folder(), "myrepo") url, commit = create_local_git_repo(files={"CMakeLists.txt": "mycmake"}, folder=folder) - c = TestClient() + c = TestClient(light=True) c.save({"conanfile.py": conanfile.format(url=url, commit=commit)}) c.run("create . -v") # Clone URL is explicitly printed