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

fix regresion in Git.run() when win_bash #14756

Merged
merged 1 commit into from
Sep 18, 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
9 changes: 5 additions & 4 deletions conan/tools/scm/git.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
from io import StringIO

from conan.tools.files import chdir
from conan.errors import ConanException
from conans.util.files import mkdir
from conans.util.runners import check_output_runner


class Git:
Expand All @@ -25,9 +25,10 @@ def run(self, cmd):
:return: The console output of the command.
"""
with chdir(self._conanfile, self.folder):
output = StringIO()
self._conanfile.run(f"git {cmd}", stdout=output, quiet=True)
return output.getvalue().strip()
# We tried to use self.conanfile.run(), but it didn't work:
# - when using win_bash, crashing because access to .settings (forbidden in source())
# - the ``conan source`` command, not passing profiles, buildenv not injected
return check_output_runner("git {}".format(cmd)).strip()

def get_commit(self):
"""
Expand Down
42 changes: 42 additions & 0 deletions conans/test/functional/tools/scm/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,48 @@ def source(self):
assert "pkg/0.1: MYCMAKE: mycmake" in c.out
assert "pkg/0.1: MYFILE: myheader!" in c.out

@pytest.mark.tool("msys2")
def test_clone_msys2_win_bash(self):
# To avoid regression in https://github.com/conan-io/conan/issues/14754
folder = os.path.join(temp_folder(), "myrepo")
url, commit = create_local_git_repo(files={"src/myfile.h": "myheader!",
"CMakeLists.txt": "mycmake"}, folder=folder)

c = TestClient()
conanfile_win_bash = 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"
win_bash = True

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

def source(self):
git = Git(self)
git.clone(url="{url}", target=".")
git.checkout(commit="{commit}")
self.output.info("MYCMAKE: {{}}".format(load(self, "CMakeLists.txt")))
self.output.info("MYFILE: {{}}".format(load(self, "src/myfile.h")))
""")
c.save({"conanfile.py": conanfile_win_bash.format(url=url, commit=commit)})
conf = "-c tools.microsoft.bash:subsystem=msys2 -c tools.microsoft.bash:path=bash.exe"
c.run(f"create . {conf}")
assert "pkg/0.1: MYCMAKE: mycmake" in c.out
assert "pkg/0.1: MYFILE: myheader!" in c.out

# It also works in local flow, not running in msys2 at all
c.run(f"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"


@pytest.mark.tool("git")
class TestGitShallowClone:
Expand Down