Git url parsing library for lua, e.g. the output of git remote get-url origin.
- Lua >= 5.1, luajit >= 2.0.0.
- Single file & zero dependency.
- Full Git Protocols support (see Patterns).
luarocks install giturlparserThere are (mainly) three types of git url pattern:
Note
They are (just help to explain) written with a regex-like syntax:
- The
{}contains parsed components returned fromgiturlparser.GitUrlInfo. - The
[]contains optional (0 or 1) component. - The
[]*contains 0 or more component. - The
[]+contains 1 or more component. - The
|inside[]is or operator.
{protocol}://[[{user}[:{password}]@]{host}[:{port}]]/[{org}/]*{repo}
For example:
http://host.xyz/repo.githttps://git@127.0.0.1:12345/repo.gitssh://username:password@host.xyz:port/path/to/the/repo.gitssh://host.xyz:port/path/to/the/repo.gitfile:///repo.gitfile://user:passwd@host.xyz:port/path/to/the/repo.gitfile://~/home/to/the/repo.git
[{user}[:{password}]@]{host}:[{org}/]*{repo}
For example:
git@host.xyz:repo.gituser:passwd@host.xyz:path/to/the/repo.git
[[.|..|~]/][{org}/]*{repo}
For example:
repo.git./repo.git../path/to/the/repo.git~/home/to/the/repo.git/usr/home/to/the/repo.git
The string position of a component.
--- @alias giturlparser.GitUrlPos {start_pos:integer?,end_pos:integer?}It contains below fields:
start_pos: Start string index.end_pos: End string index.
Parsed information.
--- @alias giturlparser.GitUrlInfo {protocol:string?,protocol_pos:giturlparser.GitUrlPos?,user:string?,user_pos:giturlparser.GitUrlPos?,password:string?,password_pos:giturlparser.GitUrlPos?,host:string?,host_pos:giturlparser.GitUrlPos?,org:string?,org_pos:giturlparser.GitUrlPos?,repo:string,repo_pos:giturlparser.GitUrlPos,path:string,path_pos:giturlparser.GitUrlPos}It contains below fields:
protocol/protocol_pos: Protocol, e.g.httpinhttp://github.com, and its position.user/user_pos: User name, e.g.usernameinssh://username@githost.com, and its position.password/password_pos: Password, e.g.passwordinssh://username:password@githost.com, and its position.host/host_pos: Host name, e.g.githost.cominssh://githost.com, and its position.port/port_pos: Port, e.g.12345inssh://127.0.0.1:12345/org/repo.git, and its position.path/path_pos: All the left parts after host name (and optional port), e.g./linrongbin16/giturlparser.lua.gitinhttps://github.com/linrongbin16/giturlparser.lua.git, and its position.
There're 2 more sugar fields:
repo/repo_pos: Repository (the last part after the last slash/), e.g.giturlparser.lua.gitinhttps://github.com/linrongbin16/giturlparser.lua.git, and its position.org/org_pos: , Organization (the parts after host name (and optional port), before the last slash/), e.g.linrongbin16inhttps://github.com/linrongbin16/giturlparser.lua.git, and its position.
Note
- The
{path}component is (almost) equivalent to/{org}/{repo}. - The
{org}and{repo}component are trimmed from around slashes if there's any. - If there's only 1 slash, the
orgcomponent is omitted.
Parse url and returns the parsed info (lua table).
--- @param url string
--- @return giturlparser.GitUrlInfo?, string?
M.parse = function(url)Parameters:
url: Git url, e.g. the output ofgit remote get-url origin.
Returns:
- Returns
giturlparser.GitUrlInfoandnilif success. - Returns
niland error messagestringif failed.
To develop the project and make PR, please setup with:
To run unit tests, please install below dependencies: