Skip to content

Commit

Permalink
feat(git): add parse remote url method
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Cesarato committed Feb 11, 2021
1 parent 8182abc commit 81812c0
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Git/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,29 @@ protected static function parseShortcodes($content, $shortcodes)

return $result;
}

/**
* Parse remote url.
*
* @return array|false
*/
public static function parseRemoteUrl()
{
$url = self::getRemoteUrl();
$patterns = [
'#^(?P<protocol>https?|git|ssh|rsync)\://(?:(?P<user>.+)@)*(?P<host>[a-z0-9_.-]*)[:/]*(?P<port>[\d]+){0,1}(?P<pathname>\/((?P<owner>[\w\-]+)\/)?((?P<name>[\w\-\.]+?)(\.git|\/)?)?)$#smi',
'#(git\+)?((?P<protocol>\w+)://)((?P<user>\w+)@)?((?P<host>[\w\.\-]+))(:(?P<port>\d+))?(?P<pathname>(\/(?P<owner>\w+)/)?(\/?(?P<name>[\w\-]+)(\.git|\/)?)?)$#smi',
'#^(?:(?P<user>.+)@)*(?P<host>[a-z0-9_.-]*)[:]*(?P<port>[\d]+){0,1}(?P<pathname>\/?(?P<owner>.+)/(?P<name>.+).git)$#smi',
'#((?P<user>\w+)@)?((?P<host>[\w\.\-]+))[\:\/]{1,2}(?P<pathname>((?P<owner>\w+)/)?((?P<name>[\w\-]+)(\.git|\/)?)?)$#smi',
];
foreach ($patterns as $pattern) {
if (preg_match($pattern, $url, $match)) {
$match = array_filter($match, 'is_string', ARRAY_FILTER_USE_KEY);

return $match;
}
}

return false;
}
}

0 comments on commit 81812c0

Please sign in to comment.