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

Log Git tool commands when running in verbose mode #15514

Merged
merged 5 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 7 additions & 3 deletions conan/tools/scm/git.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fnmatch
import os

from conan.api.output import Color
from conan.tools.files import chdir, update_conandata
from conan.errors import ConanException
from conans.model.conf import ConfDefinition
Expand Down Expand Up @@ -31,12 +32,14 @@ def __init__(self, conanfile, folder=".", excluded=None):
else:
self._excluded = conf_excluded

def run(self, cmd):
def run(self, cmd, hidden_output=None):
"""
Executes ``git <cmd>``

:return: The console output of the command.
"""
print_cmd = cmd if hidden_output is None else cmd.replace(hidden_output, "<hidden>")
self._conanfile.output.verbose(f"RUN: git {print_cmd}", fg=Color.BRIGHT_BLUE)
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
with chdir(self._conanfile, self.folder):
# We tried to use self.conanfile.run(), but it didn't work:
# - when using win_bash, crashing because access to .settings (forbidden in source())
Expand Down Expand Up @@ -203,7 +206,8 @@ def clone(self, url, target="", args=None):
mkdir(self.folder)
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))
# Avoid printing the clone command, it can contain tokens
self.run('clone "{}" {} {}'.format(url, " ".join(args), target_path), hidden_output=url)

def fetch_commit(self, url, commit):
"""
Expand All @@ -214,7 +218,7 @@ def fetch_commit(self, url, commit):
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}"')
self.run(f'remote add origin "{url}"', hidden_output=url)
self.run(f'fetch --depth 1 origin {commit}')
self.run('checkout FETCH_HEAD')

Expand Down
8 changes: 6 additions & 2 deletions conans/test/functional/tools/scm/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,10 @@ def test_clone_checkout(self):

c = TestClient()
c.save({"conanfile.py": self.conanfile.format(url=url, commit=commit)})
c.run("create .")
c.run("create . -v")
# Clone is not printed, it might contain tokens
assert 'pkg/0.1: RUN: git clone "<hidden>" "."' in c.out
assert "pkg/0.1: RUN: git checkout" in c.out
assert "pkg/0.1: MYCMAKE: mycmake" in c.out
assert "pkg/0.1: MYFILE: myheader!" in c.out

Expand Down Expand Up @@ -407,7 +410,8 @@ def test_clone_checkout(self):

c = TestClient()
c.save({"conanfile.py": self.conanfile.format(url=url, commit=commit)})
c.run("create .")
c.run("create . -v")
assert 'pkg/0.1: RUN: git remote add origin "<hidden>"' in c.out
assert "pkg/0.1: MYCMAKE: mycmake" in c.out
assert "pkg/0.1: MYFILE: myheader!" in c.out

Expand Down