Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions dist-git-client/dist_git_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import argparse
from collections import namedtuple
import configparser
import errno
import glob
Expand Down Expand Up @@ -169,6 +170,28 @@ def _detect_clone_url():
return git_conf_reader['remote "origin"']["url"]


def parse_clone_url(url):
"""
Given Git clone url, return a named tuple with "hostname" and "path"
parameters.
"""
Parsed = namedtuple('_ParsedUrl', ['hostname', 'path'])

autoparsed = urlparse(url)
if autoparsed.scheme:
hostname = autoparsed.hostname or "localhost"
return Parsed(hostname, autoparsed.path)

if ':' in url and '@' in url:
# user@hostname:/path format
no_user = url.split("@", 1)[1]
host, path = no_user.split(":", 1)
return Parsed(host, path)

# local pathname, like /home/tester/test.git
return Parsed("localhost", url)


def get_distgit_config(config, forked_from=None):
"""
Given the '.git/config' file from current directory, return the
Expand All @@ -178,13 +201,18 @@ def get_distgit_config(config, forked_from=None):
url = forked_from
if not url:
url = _detect_clone_url()
parsed_url = urlparse(url)
parsed_url = parse_clone_url(url)
if parsed_url.hostname is None:
hostname = "localhost"
else:
hostname = parsed_url.hostname

prefixes = config["clone_host_map"][hostname]
try:
prefixes = config["clone_host_map"][hostname]
except KeyError as err:
raise RuntimeError(f"{hostname} (detected from clone URL) not "
"found in dist-git-client configuration") from err

prefix_found = None
for prefix in prefixes.keys():
if not parsed_url.path.startswith(prefix):
Expand Down
29 changes: 29 additions & 0 deletions dist-git-client/tests/test_url_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
""" test clone url parser """

from dist_git_client import parse_clone_url


def _checker(url, hostname, path):
parsed = parse_clone_url(url)
assert parsed.hostname == hostname
assert parsed.path == path


def test_parse_clone_urls():
""" Basic clone url formats """
_checker("git@github.com:example/example-project.git",
"github.com", "example/example-project.git")
_checker("https://github.com/example/example-project.git",
"github.com", "/example/example-project.git")
_checker("https://github.com/example/example-project",
"github.com", "/example/example-project")
_checker("ssh://jdoe@pkgs.fedoraproject.org/rpms/example.git",
"pkgs.fedoraproject.org", "/rpms/example.git")
_checker("https://copr-dist-git.fedorainfracloud.org/git"
"/@abrt/retrace-server-devel/retrace-server.git",
"copr-dist-git.fedorainfracloud.org",
"/git/@abrt/retrace-server-devel/retrace-server.git")
_checker("file:///home/foo.git",
"localhost", "/home/foo.git")
_checker("/home/foo.git",
"localhost", "/home/foo.git")