Skip to content

Commit 87765f7

Browse files
committed
Add support for git URLs
Signed-off-by: TG1999 <tushar.goel.dav@gmail.com>
1 parent 85f46ba commit 87765f7

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

fetchcode/vcs/git.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# fetchcode is a free software tool from nexB Inc. and others.
2+
# Visit https://github.com/nexB/fetchcode for support and download.
3+
#
4+
# Copyright (c) nexB Inc. and others. All rights reserved.
5+
# http://nexb.com and http://aboutcode.org
6+
#
7+
# This software is licensed under the Apache License version 2.0.
8+
#
9+
# You may not use this software except in compliance with the License.
10+
# You may obtain a copy of the License at:
11+
# http://apache.org/licenses/LICENSE-2.0
12+
# Unless required by applicable law or agreed to in writing, software distributed
13+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations under the License.
16+
17+
import os
18+
import tempfile
19+
from urllib.parse import urlparse
20+
21+
from fetchcode.vcs.pip._internal.vcs.git import Git
22+
from fetchcode.vcs.pip._internal.utils import misc
23+
from fetchcode.vcs.pip._internal.vcs import vcs
24+
from fetchcode.vcs import VCSResponse
25+
26+
27+
def fetch_via_git(url):
28+
parsed_url = urlparse(url)
29+
scheme = parsed_url.scheme
30+
domain = parsed_url.netloc
31+
temp = tempfile.mkdtemp()
32+
os.rmdir(temp)
33+
if scheme not in Git.schemes:
34+
raise Exception("Not a Git based scheme.")
35+
36+
backend = vcs.get_backend(name="git")
37+
backend.obtain(dest=temp, url=misc.hide_url(url))
38+
39+
return VCSResponse(dest_dir=temp, vcs_type="git", domain=domain)

tests/test_vcs_git.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# fetchcode is a free software tool from nexB Inc. and others.
2+
# Visit https://github.com/nexB/fetchcode for support and download.
3+
#
4+
# Copyright (c) nexB Inc. and others. All rights reserved.
5+
# http://nexb.com and http://aboutcode.org
6+
#
7+
# This software is licensed under the Apache License version 2.0.
8+
#
9+
# You may not use this software except in compliance with the License.
10+
# You may obtain a copy of the License at:
11+
# http://apache.org/licenses/LICENSE-2.0
12+
# Unless required by applicable law or agreed to in writing, software distributed
13+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations under the License.
16+
17+
from unittest import mock
18+
19+
import pytest
20+
21+
from fetchcode.vcs.git import fetch_via_git
22+
23+
24+
def obtain(dest, url):
25+
pass
26+
27+
28+
@mock.patch("fetchcode.vcs.git.vcs.get_backend")
29+
def test_fetch_with_git_http_url_returns_a_response(mock_backend):
30+
mock_backend.return_value.obtain = obtain
31+
url = "git+http://github.com/jamesor/mongoose-versioner"
32+
response = fetch_via_git(url=url)
33+
assert response.vcs_type == "git"
34+
assert response.domain == "github.com"
35+
36+
37+
@mock.patch("fetchcode.vcs.git.vcs.get_backend")
38+
def test_fetch_with_git_url_returns_a_response(mock_backend):
39+
mock_backend.return_value.obtain = obtain
40+
url = "git://github.com/jamesor/mongoose-versioner"
41+
response = fetch_via_git(url=url)
42+
assert response.vcs_type == "git"
43+
assert response.domain == "github.com"
44+
45+
46+
@mock.patch("fetchcode.vcs.git.vcs.get_backend")
47+
def test_fetch_with_git_https_url_returns_a_response(mock_backend):
48+
mock_backend.return_value.obtain = obtain
49+
url = "git+https://github.com/jamesor/mongoose-versioner"
50+
response = fetch_via_git(url=url)
51+
assert response.vcs_type == "git"
52+
assert response.domain == "github.com"
53+
54+
55+
@mock.patch("fetchcode.vcs.git.vcs.get_backend")
56+
def test_fetch_with_git_ssh_url_returns_a_response(mock_backend):
57+
mock_backend.return_value.obtain = obtain
58+
url = "git+ssh://github.com/jamesor/mongoose-versioner"
59+
response = fetch_via_git(url=url)
60+
assert response.vcs_type == "git"
61+
assert response.domain == "github.com"
62+
63+
64+
@mock.patch("fetchcode.vcs.git.vcs.get_backend")
65+
def test_fetch_with_git_file_url_returns_a_response(mock_backend):
66+
mock_backend.return_value.obtain = obtain
67+
url = "git+file://github.com/jamesor/mongoose-versioner"
68+
response = fetch_via_git(url=url)
69+
assert response.vcs_type == "git"
70+
assert response.domain == "github.com"
71+
72+
73+
@mock.patch("fetchcode.vcs.git.vcs.get_backend")
74+
def test_fetch_with_git_ssh_url_returns_a_response(mock_backend):
75+
mock_backend.return_value.obtain = obtain
76+
url = "git+git://github.com/jamesor/mongoose-versioner"
77+
response = fetch_via_git(url=url)
78+
assert response.vcs_type == "git"
79+
assert response.domain == "github.com"
80+
81+
82+
def test_fetch_with_git_invalid_scheme():
83+
invalid_urls = [
84+
"https://github.com/TG1999/fetchcode",
85+
"https://bitbucket.org/TG1999/fetchcode",
86+
"https://gitlab.com/TG1999/fetchcode",
87+
]
88+
with pytest.raises(Exception) as e_info:
89+
for url in invalid_urls:
90+
fetch_via_git(url)
91+
assert "Not a Git based scheme." == e_info

0 commit comments

Comments
 (0)