Skip to content

Commit

Permalink
Allow to unhide git url (#16038)
Browse files Browse the repository at this point in the history
* Add hide_url tests for git scm tool

* Add hide_url flag to clone and fetch_commit.

Resolves #15684

* Update conans/test/functional/tools/scm/test_git.py

* Update conans/test/functional/tools/scm/test_git.py

---------

Co-authored-by: Rubén Rincón Blanco <git@rinconblanco.es>
  • Loading branch information
shoeffner and AbrilRBS authored Apr 8, 2024
1 parent 0f16d0b commit 38188da
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
18 changes: 13 additions & 5 deletions conan/tools/scm/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <url> <args> <target>`` 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):
Expand All @@ -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')

Expand Down
62 changes: 62 additions & 0 deletions conans/test/functional/tools/scm/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(light=True)
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
Expand Down Expand Up @@ -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(light=True)
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
Expand Down

0 comments on commit 38188da

Please sign in to comment.