Skip to content

Commit 815bf4e

Browse files
uilianriesmemsharded
authored andcommitted
[fix] Add support for Git tree-less repository when getting the URL (conan-io#18444)
* Add test to validate Git treeless clone support Signed-off-by: Uilian Ries <[email protected]> * Remove git method from the remote url Signed-off-by: Uilian Ries <[email protected]> * rsplit FTW Signed-off-by: Uilian Ries <[email protected]> * Always return string Signed-off-by: Uilian Ries <[email protected]> * Filter out (fetch) and (push) Signed-off-by: Uilian Ries <[email protected]> --------- Signed-off-by: Uilian Ries <[email protected]>
1 parent 97e357e commit 815bf4e

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

conan/tools/scm/git.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ def get_remote_url(self, remote="origin"):
8888
name, url = r.split(maxsplit=1)
8989
if name == remote:
9090
url, _ = url.rsplit(None, 1)
91+
# if the url still has (fetch) or (push) at the end
92+
if url.endswith("(fetch)") or url.endswith("(push)"):
93+
url, _ = url.rsplit(None, 1)
9194
if os.path.exists(url): # Windows local directory
9295
url = url.replace("\\", "/")
9396
return url

test/functional/tools/scm/test_git.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,3 +1253,63 @@ def test_detect_commit_not_in_remote(self):
12531253
assert "pkg/0.1: URL: {}".format(url) in c.out
12541254
assert "pkg/0.1: COMMIT IN REMOTE: False" in c.out
12551255
assert "pkg/0.1: DIRTY: False" in c.out
1256+
1257+
1258+
@pytest.mark.tool("git")
1259+
class TestGitTreelessRemote:
1260+
1261+
conanfile = textwrap.dedent("""
1262+
from conan import ConanFile
1263+
from conan.tools.scm import Git
1264+
import os
1265+
1266+
class Pkg(ConanFile):
1267+
name = "pkg"
1268+
version = "0.1"
1269+
1270+
def layout(self):
1271+
self.folders.source = "source"
1272+
1273+
def export(self):
1274+
git = Git(self)
1275+
git.clone(url="{url}", args=["--filter=tree:0"], target="target")
1276+
git.folder = "target"
1277+
cloned_url = git.run("remote -v")
1278+
self.output.info("git remote: %s ===" % cloned_url)
1279+
cloned_url = git.get_remote_url()
1280+
self.output.info("get_remote_url(): %s ===" % cloned_url)
1281+
""")
1282+
1283+
def test_treeless_clone(self):
1284+
"""
1285+
When cloning a git repository with the `--filter=tree:0` option,
1286+
the Git.get_remote_url() should only the URL of the repository.
1287+
1288+
Validate the issue https://github.com/conan-io/conan/issues/18415
1289+
"""
1290+
repository = temp_folder(path_with_spaces=False)
1291+
url, commit = create_local_git_repo(files={"README": "Lumen naturale ratum est"},
1292+
folder=repository)
1293+
1294+
client = TestClient()
1295+
client.save({"conanfile.py": self.conanfile.format(url=url)})
1296+
client.run("export .")
1297+
# We expect [tree:0] for regular git remote command. Requires Git +2.43
1298+
assert f"git remote: origin\t{url} (fetch) [tree:0]" in client.out
1299+
# Then get_remote_url filters it to only the URL
1300+
assert f"get_remote_url(): {url} ===" in client.out
1301+
1302+
def test_treeless_clone_with_parenthesis(self):
1303+
"""
1304+
Windows can use C:\Program Files (x86) path, which contains parenthesis.
1305+
As the URL will have (fetch) or (push), get_remote_url() should be able to parse it.
1306+
"""
1307+
repository = os.path.join(temp_folder(), "Program Files (x86)", "myrepo")
1308+
os.makedirs(repository)
1309+
url, commit = create_local_git_repo(files={"README": "Pacem in maribus."},
1310+
folder=repository)
1311+
1312+
client = TestClient()
1313+
client.save({"conanfile.py": self.conanfile.format(url=url)})
1314+
client.run("export .")
1315+
assert f"get_remote_url(): {url} ===" in client.out

0 commit comments

Comments
 (0)