Skip to content

Commit

Permalink
[Functions] Fix Git Branches with Slashes not Supported (#2263)
Browse files Browse the repository at this point in the history
* fix git clone

* fmt

* fix test

* add copyright
  • Loading branch information
quaark committed Aug 25, 2022
1 parent 8ef6b90 commit d8cb483
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
11 changes: 9 additions & 2 deletions mlrun/utils/clones.py
Expand Up @@ -125,14 +125,21 @@ def get_secret(key):
clone_path = f"https://{host}{url_obj.path}"

branch = None
tag = None
if url_obj.fragment:
refs = url_obj.fragment
if refs.startswith("refs/"):
branch = refs[refs.rfind("/") + 1 :]
if refs.startswith("refs/heads/"):
branch = refs.replace("refs/heads/", "")
elif refs.startswith("refs/tags/"):
tag = refs.replace("refs/tags/", "")
else:
url = url.replace("#" + refs, f"#refs/heads/{refs}")
branch = refs

repo = Repo.clone_from(clone_path, context, single_branch=True, b=branch)
if tag:
repo.git.checkout(tag)

return url, repo


Expand Down
4 changes: 2 additions & 2 deletions tests/system/runtimes/test_archives.py
Expand Up @@ -219,7 +219,7 @@ def test_job_project(self):
project.set_function(
name="myjob",
handler="rootfn.job_handler",
image="mlrun/mlrun",
image=base_image,
kind="job",
with_repo=True,
)
Expand All @@ -237,7 +237,7 @@ def test_nuclio_project(self):
project.set_function(
name="mynuclio",
handler="rootfn:nuclio_handler",
image="mlrun/mlrun",
image=base_image,
kind="nuclio",
with_repo=True,
)
Expand Down
44 changes: 44 additions & 0 deletions tests/utils/test_clones.py
@@ -0,0 +1,44 @@
# Copyright 2018 Iguazio
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest.mock

import pytest

import mlrun.utils.clones


@pytest.mark.parametrize(
"ref,ref_type",
[
("without-slash", "branch"),
("with/slash", "branch"),
("without-slash", "tag"),
("without/slash", "tag"),
],
)
def test_clone_git_refs(ref, ref_type):
repo = "github.com/some-git-project/some-git-repo.git"
url = f"git://{repo}#refs/{'heads' if ref_type == 'branch' else 'tags'}/{ref}"
context = "non-existent-dir"
branch = ref if ref_type == "branch" else None
tag = ref if ref_type == "tag" else None

with unittest.mock.patch("git.Repo.clone_from") as clone_from:
_, repo_obj = mlrun.utils.clones.clone_git(url, context)
clone_from.assert_called_once_with(
f"https://{repo}", context, single_branch=True, b=branch
)
if tag:
repo_obj.git.checkout.assert_called_once_with(tag)

0 comments on commit d8cb483

Please sign in to comment.