Skip to content

Commit

Permalink
fix regresion in Git.run() when win_bash (#14756)
Browse files Browse the repository at this point in the history
  • Loading branch information
memsharded authored Sep 18, 2023
1 parent 5353d85 commit 1c5c5dd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
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

0 comments on commit 1c5c5dd

Please sign in to comment.