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 giturlparser
There 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.git
https://git@127.0.0.1:12345/repo.git
ssh://username:password@host.xyz:port/path/to/the/repo.git
ssh://host.xyz:port/path/to/the/repo.git
file:///repo.git
file://user:passwd@host.xyz:port/path/to/the/repo.git
file://~/home/to/the/repo.git
[{user}[:{password}]@]{host}:[{org}/]*{repo}
For example:
git@host.xyz:repo.git
user: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.http
inhttp://github.com
, and its position.user
/user_pos
: User name, e.g.username
inssh://username@githost.com
, and its position.password
/password_pos
: Password, e.g.password
inssh://username:password@githost.com
, and its position.host
/host_pos
: Host name, e.g.githost.com
inssh://githost.com
, and its position.port
/port_pos
: Port, e.g.12345
inssh://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.git
inhttps://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.git
inhttps://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.linrongbin16
inhttps://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
org
component 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.GitUrlInfo
andnil
if success. - Returns
nil
and error messagestring
if failed.
To develop the project and make PR, please setup with:
To run unit tests, please install below dependencies:
Please open issue/PR for anything about giturlparser.lua.
Like giturlparser.lua? Consider