Skip to content

New Validator: Git URL #211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
163 changes: 163 additions & 0 deletions tests/test_git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# -*- coding: utf-8 -*-
import pytest

from validators import url, ValidationFailure


@pytest.mark.parametrize('address', [
u'[email protected]:username/reponame.git',
u'https://[email protected]/otherusername/reponame.git',
u'https://github.com/username/reponame.git',
u'[email protected]:username/reponame.git',
u'[email protected]:groupname/reponame.git',
u'https://gitlab.com/groupname/reponame.git',
u'[email protected]:workspace/reponame.git',
u'https://[email protected]/workspace/reponame.git',
u'https://bitbucket.org/workspace/reponame.git',
u'[email protected]:username/reponame.git',
u'https://gitlab.com/username/reponame.git',
u'git://github.com/somthing/somthing.git#ff786f9f',
u'git://github.com/somthing/somthing.git#gh-pages',
u'git://github.com/somthing/somthing.git#master',
u'git://github.com/somthing/somthing.git#Quick-Fix',
u'git://github.com/somthing/somthing.git#quick_fix',
u'git://github.com/somthing/somthing.git#v0.1.0',
u'git://host.xz/path/to/repo.git/',
u'git://host.xz/~user/path/to/repo.git/',
u'[email protected]:user/project.git',
u'[email protected]:user/project.git',
u'[email protected]:user/some-project.git',
u'[email protected]:user/some-project.git',
u'[email protected]:user/some_project.git',
u'[email protected]:user/some_project.git',
u'http://github.com/user/project.git',
u'http://host.xz/path/to/repo.git/',
u'https://github.com/user/project.git',
u'https://host.xz/path/to/repo.git/',
u'https://username::;*%$:@github.com/username/repository.git',
u'https://username:$fooABC@:@github.com/username/repository.git',
u'https://username:[email protected]/username/repository.git',
u'ssh://host.xz/path/to/repo.git/',
u'ssh://host.xz/path/to/repo.git/',
u'ssh://host.xz/~/path/to/repo.git',
u'ssh://host.xz/~user/path/to/repo.git/',
u'ssh://[email protected]/path/to/repo.git/',
u'ssh://[email protected]/path/to/repo.git/',
u'ssh://[email protected]/~/path/to/repo.git',
u'ssh://[email protected]/~user/path/to/repo.git/',
u'ssh://[email protected]:port/path/to/repo.git/',
u'https://[email protected]:8080/otherusername/reponame.git',
u'ssh://host.xz:port/path/to/repo.git/',
u'ssh://host.xz:1234/path/to/repo.git/',
u'https://192.168.1.127/user/project.git',
u'http://192.168.1.127/user/project.git',
u'http://192.168.1.127:port/user/project.git',
])
def test_returns_true_on_valid_url(address):
assert url(address)


@pytest.mark.parametrize('address', [
u'[email protected]:username/reponame',
u'https://[email protected]/otherusername/reponame',
u'https://github.com/username/reponame',
u'[email protected]:username/reponame',
u'[email protected]:groupname/reponame',
u'https://gitlab.com/groupname/reponame',
u'[email protected]:workspace/reponame',
u'https://[email protected]/workspace/reponame',
u'https://bitbucket.org/workspace/reponame',
u'[email protected]:username/reponame',
u'https://gitlab.com/username/reponame',
u'git://host.xz/path/to/repo',
u'git://host.xz/~user/path/to/repo.',
u'[email protected]:user/project',
u'[email protected]:user/project',
u'[email protected]:user/some-project',
u'[email protected]:user/some-project',
u'[email protected]:user/some_project',
u'[email protected]:user/some_project',
u'http://github.com/user/project',
u'http://host.xz/path/to/repo.',
u'https://github.com/user/project',
u'https://host.xz/path/to/repo',
u'https://username::;*%$:@github.com/username/repository',
u'https://username:$fooABC@:@github.com/username/repository',
u'https://username:[email protected]/username/repository',
u'ssh://host.xz/path/to/repo',
u'ssh://host.xz/path/to/repo',
u'ssh://host.xz/~/path/to/rep',
u'ssh://host.xz/~user/path/to/repo',
u'ssh://[email protected]/path/to/repo',
u'ssh://[email protected]/path/to/repo',
u'ssh://[email protected]/~/path/to/repo',
u'ssh://[email protected]/~user/path/to/repo',
u'ssh://[email protected]:port/path/to/repo',
u'https://[email protected]:8080/otherusername/reponame',
u'ssh://host.xz:port/path/to/repo',
u'ssh://host.xz:1234/path/to/repo',
u'https://192.168.1.127/user/project',
u'http://192.168.1.127/user/project',
u'http://192.168.1.127:port/user/project',
])
def test_returns_true_on_valid_loose_url(address):
assert url(address, strict=False)


@pytest.mark.parametrize('address', 'strict', [
u'https://www.github.com',
u'[email protected]:username/repo name',
u'[email protected]:username/repo name.git',
u'https://gitlab.com/username/reponame.git -b branch',
u'https://gitlab.com/username/reponame.git --flag',
u'https://gitlab.com/username/reponame.git \\-f',
u'/path/to/repo.git/',
u'file:///path/to/repo.git/',
u'file://~/path/to/repo.git/',
u'[email protected]:user/some_project.git/foo',
u'[email protected]:user/some_project.gitfoo',
u'host.xz:/path/to/repo.git/',
u'host.xz:path/to/repo.git',
u'host.xz:path/to/repo',
u'host.xz:~user/path/to/repo.git/',
u'path/to/repo.git/',
u'rsync://host.xz/path/to/repo.git/',
u'[email protected]:/path/to/repo.git/',
u'[email protected]:path/to/repo.git',
u'[email protected]:~user/path/to/repo.git/',
u'~/path/to/repo.git',
u'[email protected]:username/reponame.gi',
])
def test_returns_failed_loose_validation_on_invalid_url(address):
assert isinstance(url(address), ValidationFailure)


@pytest.mark.parametrize('address', [
u'https://www.github.com',
u'[email protected]:username/"repo name".git',
u'https://gitlab.com/username/"reponame".git',
u'https://gitlab.com/username/\'reponame\'.git',
u'[email protected]:username/repo name',
u'[email protected]:username/repo name.git',
u'https://gitlab.com/username/reponame.git -b branch',
u'https://gitlab.com/username/reponame.git --flag',
u'https://gitlab.com/username/reponame.git \\-f',
u'/path/to/repo.git/',
u'file:///path/to/repo.git/',
u'file://~/path/to/repo.git/',
u'[email protected]:user/some_project.git/foo',
u'[email protected]:user/some_project.gitfoo',
u'host.xz:/path/to/repo.git/',
u'host.xz:path/to/repo.git',
u'host.xz:path/to/repo',
u'host.xz:~user/path/to/repo.git/',
u'path/to/repo.git/',
u'rsync://host.xz/path/to/repo.git/',
u'[email protected]:/path/to/repo.git/',
u'[email protected]:path/to/repo.git',
u'[email protected]:~user/path/to/repo.git/',
u'~/path/to/repo.git',
u'[email protected]:username/reponame.gi',
])
def test_returns_failed_strict_validation_on_invalid_url(address):
assert isinstance(url(address, strict=True), ValidationFailure)
70 changes: 70 additions & 0 deletions validators/git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import re

from .utils import validator

"""
sources:
https://github.com/git/git/blob/master/url.c
https://git-scm.com/docs/git-clone
https://stackoverflow.com/questions/23976019/how-to-verify-valid-format-of-url-as-a-git-repo/60000569#comment111902087_60000569
https://github.com/jonschlinkert/is-git-url
"""
url_regex = re.compile(
# protocol
# either prot:// (protocol captured as group 1) or git@serv (server captured as group 2)
r"(?:(git|ssh|https?)|git@([A-Za-z0-9][A-Za-z0-9\+\.\-_]+)):(?:\/\/)?"
# username and maybe password, if either provided (both are captured as group 3)
r"(?:(\S*)@)?"
# the actual targeted URL as per STD66 (RFC3986) [A-Za-z][A-Za-z0-9+.-]*
# with some added leniency for IPs and allowed URI special chars (_~/:) in order to capture full path+ports
# (entire URI captured as group 4)
r"([A-Za-z0-9][A-Za-z0-9\+\.\-_\/\~\:]*?)"
# .git matched if provided
r"(?:\.git)"
# ending '/' OR branch name/commit id (captured as group 5)
r"(\/?|\#[-\d\w._]+?)",
re.UNICODE | re.IGNORECASE
)


@validator
def git(value: str, strict: bool = False):
"""
Return whether a given value is a valid git URL.

If the value is valid git URL this function returns ``True``, otherwise
:class:`~validators.utils.ValidationFailure`.

Examples::

>>> git('[email protected]:username/reponame.git')
True

>>> git('[email protected]:username/reponame')
True

>>> git('http://192.168.1.127/user/project.git')
True

>>> git('git://bitbucket.org/org/repo.git#ff786f9f')
True

>>> git('https://www.github.com')
ValidationFailure(func=git, ...)

>>> git('[email protected]:username/reponame', strict=True)
ValidationFailure(func=git, ...)

:param value: URL address string to validate
:param strict: (default=False) enfocre URL end with .git (if false, upon validation failure '.git; is added
to the end if the procided value and validation retried)
"""
result = url_regex.match(value)

if result or strict:
return result
else: # not strict and failed to match
# try adding ".git" at end of value and retry
value = value[:-1] if value.endswith('/') else value
result = url_regex.match(value + ".git")
return result