Skip to content

Commit f753168

Browse files
committed
dist-git-client: better ssh clone url parsing
1 parent 31cce6d commit f753168

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

dist-git-client/dist_git_client.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import argparse
6+
from collections import namedtuple
67
import configparser
78
import errno
89
import glob
@@ -169,6 +170,28 @@ def _detect_clone_url():
169170
return git_conf_reader['remote "origin"']["url"]
170171

171172

173+
def parse_clone_url(url):
174+
"""
175+
Given Git clone url, return a named tuple with "hostname" and "path"
176+
parameters.
177+
"""
178+
Parsed = namedtuple('_ParsedUrl', ['hostname', 'path'])
179+
180+
autoparsed = urlparse(url)
181+
if autoparsed.scheme:
182+
hostname = autoparsed.hostname or "localhost"
183+
return Parsed(hostname, autoparsed.path)
184+
185+
if ':' in url and '@' in url:
186+
# user@hostname:/path format
187+
no_user = url.split("@", 1)[1]
188+
host, path = no_user.split(":", 1)
189+
return Parsed(host, path)
190+
191+
# local pathname, like /home/tester/test.git
192+
return Parsed("localhost", url)
193+
194+
172195
def get_distgit_config(config, forked_from=None):
173196
"""
174197
Given the '.git/config' file from current directory, return the
@@ -178,7 +201,7 @@ def get_distgit_config(config, forked_from=None):
178201
url = forked_from
179202
if not url:
180203
url = _detect_clone_url()
181-
parsed_url = urlparse(url)
204+
parsed_url = parse_clone_url(url)
182205
if parsed_url.hostname is None:
183206
hostname = "localhost"
184207
else:
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
""" test clone url parser """
2+
3+
from dist_git_client import parse_clone_url
4+
5+
6+
def _checker(url, hostname, path):
7+
parsed = parse_clone_url(url)
8+
assert parsed.hostname == hostname
9+
assert parsed.path == path
10+
11+
12+
def test_parse_clone_urls():
13+
""" Basic clone url formats """
14+
_checker("git@github.com:example/example-project.git",
15+
"github.com", "example/example-project.git")
16+
_checker("https://github.com/example/example-project.git",
17+
"github.com", "/example/example-project.git")
18+
_checker("https://github.com/example/example-project",
19+
"github.com", "/example/example-project")
20+
_checker("ssh://jdoe@pkgs.fedoraproject.org/rpms/example.git",
21+
"pkgs.fedoraproject.org", "/rpms/example.git")
22+
_checker("https://copr-dist-git.fedorainfracloud.org/git"
23+
"/@abrt/retrace-server-devel/retrace-server.git",
24+
"copr-dist-git.fedorainfracloud.org",
25+
"/git/@abrt/retrace-server-devel/retrace-server.git")
26+
_checker("file:///home/foo.git",
27+
"localhost", "/home/foo.git")
28+
_checker("/home/foo.git",
29+
"localhost", "/home/foo.git")

0 commit comments

Comments
 (0)