From fd76ecfd7e61e7b5f4711510e60eb32dd894f500 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 09:01:23 +0800 Subject: [PATCH 01/61] chore(test): add test cases --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 5896132..f9f84fa 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,21 @@ Pure Lua implemented git URL parsing library, e.g. the output of git remote get-url origin.

+## Table of Contents + +- [Requirements](#requirements) +- [Features](#features) +- [Install](#install) +- [Documents](#documents) + - [Types](#types) + - [`giturlparser.GitUrlPos`](#giturlparsergiturlpos) + - [`giturlparser.GitUrlInfo`](#giturlparsergiturlinfo) + - [Functions](#functions) + - [`parse`](#parse) +- [References](#references) +- [Development](#development) +- [Contribute](#contribute) + ## Requirements - Lua >= 5.1, luajit >= 2.0.0. From 36a0b92c6e8acf2c8247d8960fc6fc10a23dcfc5 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 09:05:26 +0800 Subject: [PATCH 02/61] chore --- spec/giturlparser_spec.lua | 24 ++++++++++++++++++++++++ src/giturlparser.lua | 3 +-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 3d49659..c49ce31 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -169,4 +169,28 @@ describe("giturlparser", function() assert_eq(actual.repo_pos.end_pos, 49) end) end) + describe("[ssh]", function() + it("ssh://user@host.xz:org/repo.git", function() + local actual = giturlparser.parse("ssh://user@host.xz:org/repo.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "ssh") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 3) + assert_eq(actual.user, "user") + assert_eq(actual.user_pos.start_pos, 9) + assert_eq(actual.user_pos.end_pos, 16) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "host.xz") + assert_eq(actual.host_pos.start_pos, 27) + assert_eq(actual.host_pos.end_pos, 39) + assert_eq(actual.org, "org") + assert_eq(actual.org_pos.start_pos, 41) + assert_eq(actual.org_pos.end_pos, 53) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 41) + assert_eq(actual.repo_pos.end_pos, 49) + end) + it("ssh://git@github:linrongbin16/giturlparser.lua.git", function() end) + end) end) diff --git a/src/giturlparser.lua b/src/giturlparser.lua index 3d0e6e6..489f19d 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -107,8 +107,7 @@ end -- --- @alias giturlparser.GitUrlPos {start_pos:integer?,end_pos:integer?} --- @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} -local GitUrlInfo = {} - +-- --- @param url string --- @param start_pos integer --- @param end_pos integer From 8737de9a40abb434e1a9a74fe7420ad2ec2eff29 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 09:11:57 +0800 Subject: [PATCH 03/61] chore --- src/giturlparser.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/giturlparser.lua b/src/giturlparser.lua index 489f19d..d952b61 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -192,6 +192,14 @@ M.parse = function(url) -- missing org, org_pos end else + print( + string.format( + "second colon ':'(%s) is after at '@'(%s), url:%s\n", + tostring(second_colon_pos), + tostring(first_at_pos), + tostring(url) + ) + ) local first_slash_pos = M._find(url, "/", first_at_pos + 1) if type(first_slash_pos) == "number" From e70b99902d238a4ab6ab6b10440e97eaca63e0f8 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 09:30:19 +0800 Subject: [PATCH 04/61] chore --- README.md | 21 +++++++++++++++++++++ src/giturlparser.lua | 12 ++++++++++++ 2 files changed, 33 insertions(+) diff --git a/README.md b/README.md index f9f84fa..1e642c1 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,27 @@ Pure Lua implemented git URL parsing library, e.g. the output of git remot ## Documents +The git url syntax is: + +1. `{protocol}://host[:{port}]/{repo}` +2. `{protocol}://host[:{port}]/[{org}]+{repo}` +3. `{protocol}://{user}@host[:{port}]/[{org}]*{repo}` +4. `{protocol}://{user}:{password}@host[:{port}]/[{org}]*{repo}` +5. `{user}[:{password}]@host[:{port}]/[{org}]*{repo}` +6. `/[{org}/]*{repo}` +7. `~[/{org}]*/{repo}` + +> [!NOTE] +> +> 1. The `{}` contains parsed components returned from [`giturlparser.GitUrlInfo`](#giturlparsergiturlinfo). +> 2. The `[]` contains optional (0 or 1) component. +> 3. The `[]*` contains zero or more (≥ 0) component. +> 4. The `[]+` contains 1 or more (≥ 1) component. + +All of above can be written by: + +`[[{protocol}://][{user}[:{password}]@]host[:{port}]]/[{org}]*{repo}` + ### Types #### `giturlparser.GitUrlPos` diff --git a/src/giturlparser.lua b/src/giturlparser.lua index d952b61..3916b27 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -154,6 +154,18 @@ M.parse = function(url) then -- https, ssh, file, sftp, etc protocol, protocol_pos = M._make(url, 1, protocol_delimiter_pos - 1) + + -- first @, end pos of user and password + local first_at_pos = M._find(url, "@", protocol_delimiter_pos + 3) + if + type(first_at_pos) == "number" + and first_at_pos > protocol_delimiter_pos + 3 + then + -- user and password ends at @ + else + -- user and password not found + end + local first_colon_pos = M._find(url, ":", protocol_delimiter_pos + 3) if type(first_colon_pos) == "number" From ddee384e7193af25ca9959848982a638162afa2d Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 09:42:30 +0800 Subject: [PATCH 05/61] chore --- README.md | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 1e642c1..69f7b17 100644 --- a/README.md +++ b/README.md @@ -40,15 +40,27 @@ Pure Lua implemented git URL parsing library, e.g. the output of git remot ## Documents -The git url syntax is: - -1. `{protocol}://host[:{port}]/{repo}` -2. `{protocol}://host[:{port}]/[{org}]+{repo}` -3. `{protocol}://{user}@host[:{port}]/[{org}]*{repo}` -4. `{protocol}://{user}:{password}@host[:{port}]/[{org}]*{repo}` -5. `{user}[:{password}]@host[:{port}]/[{org}]*{repo}` -6. `/[{org}/]*{repo}` -7. `~[/{org}]*/{repo}` +The git url syntax contains many use cases: + +1. `{protocol}://host[:{port}]/[{org}/]*{repo}` + - `http://host.xyz/repo.git` + - `ssh://host.xyz:port/path/to/the/repo.git` +2. `{protocol}://[{user}[:{password}]@]host[:{port}]/[{org}/]*{repo}` + - `https://git@host.xyz/repo.git` + - `ssh://username:password@host.xyz:port/path/to/the/repo.git` +3. `{protocol}://[[{user}[:{password}]@]host[:{port}]]/[{org}/]*{repo}` + - `file:///repo.git` + - `file://user:passwd@host.xyz:port/repo.git` + - `file://~/home/to/the/repo.git` +4. `[{protocol}://][[{user}[:{password}]@]host[:{port}]]/[{org}/]*{repo}` + - `git@host.xyz/repo.git` + - `user:passwd@host.xyz:port/path/to/the/repo.git` +5. `[~][/{org}]*/{repo}` + - `repo.git` + - `./repo.git` + - `../path/to/the/repo.git` + - `~/home/to/the/repo.git` + - `/usr/home/to/the/repo.git` > [!NOTE] > @@ -56,10 +68,12 @@ The git url syntax is: > 2. The `[]` contains optional (0 or 1) component. > 3. The `[]*` contains zero or more (≥ 0) component. > 4. The `[]+` contains 1 or more (≥ 1) component. +> 5. The `|` inside `[]` is **_or_** operator. All of above can be written by: -`[[{protocol}://][{user}[:{password}]@]host[:{port}]]/[{org}]*{repo}` +1. `[{protocol}://][[{user}[:{password}]@]host[:{port}]]/[{org}/]*{repo}` +2. `[~][/{org}]*/{repo}` ### Types From ed565a7243f27a7795b8fb84b2e7702c741e26e7 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 09:51:17 +0800 Subject: [PATCH 06/61] chore --- .luacheckrc | 1 + src/giturlparser.lua | 31 ++++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index 56f2305..7e585f4 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -3,3 +3,4 @@ max_line_length = 500 unused = false unused_args = false exclude_files = {} +ignore = { 542 } diff --git a/src/giturlparser.lua b/src/giturlparser.lua index 3916b27..e34b2ac 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -106,7 +106,7 @@ end -- 'path' is all payload after 'host', e.g. 'org/repo'. -- --- @alias giturlparser.GitUrlPos {start_pos:integer?,end_pos:integer?} ---- @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} +--- @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?,port:string?,port_pos:giturlparser.GitUrlPos?,org:string?,org_pos:giturlparser.GitUrlPos?,repo:string,repo_pos:giturlparser.GitUrlPos,path:string,path_pos:giturlparser.GitUrlPos} -- --- @param url string --- @param start_pos integer @@ -141,6 +141,8 @@ M.parse = function(url) local password_pos = nil local host = nil local host_pos = nil + local port = nil + local port_pos = nil local org = nil local org_pos = nil local repo = nil @@ -148,22 +150,41 @@ M.parse = function(url) local path = nil local path_pos = nil + -- find first '://', the end position of protocol local protocol_delimiter_pos = M._find(url, "://") if type(protocol_delimiter_pos) == "number" and protocol_delimiter_pos > 1 then - -- https, ssh, file, sftp, etc + -- protocol end with '://' protocol, protocol_pos = M._make(url, 1, protocol_delimiter_pos - 1) - -- first @, end pos of user and password + -- find first '@', the end position of user and password local first_at_pos = M._find(url, "@", protocol_delimiter_pos + 3) if type(first_at_pos) == "number" and first_at_pos > protocol_delimiter_pos + 3 then - -- user and password ends at @ + -- user (and password) end with '@' + + -- find first ':', the end position of password + local first_colon_pos = M._find(url, ":", protocol_delimiter_pos + 3) + if + type(first_colon_pos) == "number" + and first_colon_pos > protocol_delimiter_pos + 3 + and first_colon_pos < first_at_pos + then + -- password end with ':' + user, user_pos = + M._make(url, protocol_delimiter_pos + 3, first_colon_pos - 1) + password, password_pos = + M._make(url, first_colon_pos + 1, first_at_pos - 1) + else + -- password not found, user end with '@' + user, user_pos = + M._make(url, protocol_delimiter_pos + 3, first_at_pos - 1) + end else - -- user and password not found + -- user (and password) not found end local first_colon_pos = M._find(url, ":", protocol_delimiter_pos + 3) From 230c4ac8b9be2c76ca69c708ca93782f796fbe5e Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 09:54:10 +0800 Subject: [PATCH 07/61] chore --- README.md | 4 ++-- src/giturlparser.lua | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 69f7b17..eb98fc3 100644 --- a/README.md +++ b/README.md @@ -31,8 +31,8 @@ Pure Lua implemented git URL parsing library, e.g. the output of git remot ## Features -- [x] Single file & zero dependency. -- [x] Full [Git Protocols](https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols) support. +- Single file & zero dependency. +- Full [Git Protocols](https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols) support. ## Install diff --git a/src/giturlparser.lua b/src/giturlparser.lua index e34b2ac..041cd63 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -183,8 +183,14 @@ M.parse = function(url) user, user_pos = M._make(url, protocol_delimiter_pos + 3, first_at_pos - 1) end + + -- find second ':' (after '@'), the end position of host, start position of port + local second_colon_pos = M._find(url, ":", first_at_pos + 1) else -- user (and password) not found + + -- find first ':', the end position of host, start position of port + local first_colon_pos = M._find(url, ":", first_at_pos + 1) end local first_colon_pos = M._find(url, ":", protocol_delimiter_pos + 3) From 4ab7d523f553392335b7e74cd6766d1e28945d91 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 10:15:02 +0800 Subject: [PATCH 08/61] chore --- src/giturlparser.lua | 90 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 83 insertions(+), 7 deletions(-) diff --git a/src/giturlparser.lua b/src/giturlparser.lua index 041cd63..c7d1db6 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -106,7 +106,7 @@ end -- 'path' is all payload after 'host', e.g. 'org/repo'. -- --- @alias giturlparser.GitUrlPos {start_pos:integer?,end_pos:integer?} ---- @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?,port:string?,port_pos:giturlparser.GitUrlPos?,org:string?,org_pos:giturlparser.GitUrlPos?,repo:string,repo_pos:giturlparser.GitUrlPos,path:string,path_pos:giturlparser.GitUrlPos} +--- @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?,port:string?,port_pos:giturlparser.GitUrlPos?,org:string?,org_pos:giturlparser.GitUrlPos?,repo:string?,repo_pos:giturlparser.GitUrlPos?,path:string?,path_pos:giturlparser.GitUrlPos?} -- --- @param url string --- @param start_pos integer @@ -122,6 +122,46 @@ M._make = function(url, start_pos, end_pos) return component, pos end +--- @alias giturlparser._GitUrlPath {org:string?,org_pos:giturlparser.GitUrlPos?,repo:string?,repo_pos:giturlparser.GitUrlPos?,path:string?,path_pos:giturlparser.GitUrlPos?} +-- +--- @param p string +--- @return giturlparser._GitUrlPath +M._make_path = function(p) + assert(not M._startswith(p, "/")) + assert(not M._endswith(p, "/")) + + local org = nil + local org_pos = nil + local repo = nil + local repo_pos = nil + local path = nil + local path_pos = nil + + local plen = string.len(p) + local last_slash_pos = M._rfind(p, "/") + if + type(last_slash_pos) == "number" + and last_slash_pos > 1 + and last_slash_pos < plen + then + org, org_pos = M._make(p, 1, last_slash_pos - 1) + repo, repo_pos = M._make(p, last_slash_pos + 1, plen) + else + -- no slash found, only 1 path component + repo, repo_pos = M._make(p, last_slash_pos + 1, plen) + end + path, path_pos = M._make(p, 1, plen) + + return { + org = org, + org_pos = org_pos, + repo = repo, + repo_pos = repo_pos, + path = path, + path_pos = path_pos, + } +end + --- @param url string --- @return giturlparser.GitUrlInfo?, string? M.parse = function(url) @@ -133,6 +173,8 @@ M.parse = function(url) url = string.sub(url, 1, #url - 1) end + local urllen = string.len(url) + local protocol = nil local protocol_pos = nil local user = nil @@ -143,12 +185,8 @@ M.parse = function(url) local host_pos = nil local port = nil local port_pos = nil - local org = nil - local org_pos = nil - local repo = nil - local repo_pos = nil - local path = nil - local path_pos = nil + --- @type giturlparser._GitUrlPath + local path_obj = {} -- find first '://', the end position of protocol local protocol_delimiter_pos = M._find(url, "://") @@ -186,6 +224,44 @@ M.parse = function(url) -- find second ':' (after '@'), the end position of host, start position of port local second_colon_pos = M._find(url, ":", first_at_pos + 1) + if + type(second_colon_pos) == "number" + and second_colon_pos > first_at_pos + 1 + then + -- host end with ':' + host, host_pos = M._make(url, first_at_pos + 1, second_colon_pos - 1) + + -- find first slash '/' (after second ':'), the end position of port, start position of path + local first_slash_pos = M._find(url, "/", second_colon_pos + 1) + if + type(first_slash_pos) == "number" + and first_slash_pos > second_colon_pos + 1 + then + -- port end with '/' + port, port_pos = + M._make(url, second_colon_pos + 1, first_slash_pos - 1) + path_obj = M._make_path(string.sub(url, first_slash_pos + 1)) + else + -- path not found, port end until url end + port, port_pos = M._make(url, second_colon_pos + 1, urllen) + end + else + -- port not found, host (highly possibly) end with '/' + + -- find first slash '/' (after '@'), the end position of host, start position of path + local first_slash_pos = M._find(url, "/", first_at_pos + 1) + if + type(first_slash_pos) == "number" + and first_slash_pos > first_at_pos + 1 + then + -- host end with '/' + host, host_pos = M._make(url, first_at_pos + 1, first_slash_pos - 1) + path_obj = M._make_path(string.sub(url, first_slash_pos + 1)) + else + -- first slash not found, host end until url end + host, host_pos = M._make(url, first_at_pos + 1, urllen) + end + end else -- user (and password) not found From 4ca43bd9a37e07f5a1bb267bb90f822017afd603 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 10:16:25 +0800 Subject: [PATCH 09/61] chore --- src/giturlparser.lua | 126 ------------------------------------------- 1 file changed, 126 deletions(-) diff --git a/src/giturlparser.lua b/src/giturlparser.lua index c7d1db6..f776714 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -268,132 +268,6 @@ M.parse = function(url) -- find first ':', the end position of host, start position of port local first_colon_pos = M._find(url, ":", first_at_pos + 1) end - - local first_colon_pos = M._find(url, ":", protocol_delimiter_pos + 3) - if - type(first_colon_pos) == "number" - and first_colon_pos > protocol_delimiter_pos + 3 - then - -- host end with ':', or user end with ':' - local first_at_pos = M._find(url, "@", first_colon_pos + 1) - if - type(first_at_pos) == "number" and first_at_pos > first_colon_pos + 1 - then - -- ssh password end pos - local second_colon_pos = M._find(url, ":", first_at_pos + 1) - if - type(second_colon_pos) == "number" - and second_colon_pos > first_at_pos + 1 - then - -- host end with ':' - host, host_pos = M._make(url, first_at_pos + 1, second_colon_pos - 1) - password, password_pos = - M._make(url, first_colon_pos + 1, first_at_pos - 1) - user, user_pos = - M._make(url, protocol_delimiter_pos + 3, first_colon_pos - 1) - - local last_slash_pos = M._rfind(url, "/") - if - type(last_slash_pos) == "number" - and last_slash_pos > second_colon_pos + 1 - then - repo, repo_pos = M._make(url, last_slash_pos + 1, string.len(url)) - org, org_pos = - M._make(url, second_colon_pos + 1, last_slash_pos - 1) - path, path_pos = M._make(url, second_colon_pos + 1, string.len(url)) - else - repo, repo_pos = M._make(url, second_colon_pos + 1, string.len(url)) - path, path_pos = M._make(url, second_colon_pos + 1, string.len(url)) - -- missing org, org_pos - end - else - print( - string.format( - "second colon ':'(%s) is after at '@'(%s), url:%s\n", - tostring(second_colon_pos), - tostring(first_at_pos), - tostring(url) - ) - ) - local first_slash_pos = M._find(url, "/", first_at_pos + 1) - if - type(first_slash_pos) == "number" - and first_slash_pos > first_at_pos + 1 - then - -- host end with '/' - host, host_pos = M._make(url, first_at_pos + 1, first_slash_pos - 1) - password, password_pos = - M._make(url, first_colon_pos + 1, first_at_pos - 1) - user, user_pos = - M._make(url, protocol_delimiter_pos + 3, first_colon_pos - 1) - - local last_slash_pos = M._rfind(url, "/") - if - type(last_slash_pos) == "number" - and last_slash_pos > first_slash_pos + 1 - then - repo, repo_pos = M._make(url, last_slash_pos + 1, string.len(url)) - org, org_pos = - M._make(url, first_slash_pos + 1, last_slash_pos - 1) - path, path_pos = - M._make(url, first_slash_pos + 1, string.len(url)) - else - repo, repo_pos = - M._make(url, first_slash_pos + 1, string.len(url)) - path, path_pos = - M._make(url, first_slash_pos + 1, string.len(url)) - -- missing org, org_pos - end - else - return nil, "invalid url" - end - end - else - -- host end with ':' - host, host_pos = - M._make(url, protocol_delimiter_pos + 3, first_colon_pos - 1) - -- missing user, password - - local last_slash_pos = M._rfind(url, "/") - if - type(last_slash_pos) == "number" - and last_slash_pos > first_colon_pos + 1 - then - repo, repo_pos = M._make(url, last_slash_pos + 1, string.len(url)) - org, org_pos = M._make(url, first_colon_pos + 1, last_slash_pos - 1) - path, path_pos = M._make(url, first_colon_pos + 1, string.len(url)) - else - repo, repo_pos = M._make(url, first_colon_pos + 1, string.len(url)) - path, path_pos = M._make(url, first_colon_pos + 1, string.len(url)) - -- missing org, org_pos - end - end - else - local first_slash_pos = M._find(url, "/", protocol_delimiter_pos + 3) - if - type(first_slash_pos) == "number" - and first_slash_pos > protocol_delimiter_pos + 3 - then - -- host end with '/' - host, host_pos = - M._make(url, protocol_delimiter_pos + 3, first_slash_pos - 1) - local last_slash_pos = M._rfind(url, "/") - if - type(last_slash_pos) == "number" - and last_slash_pos > first_slash_pos + 1 - then - repo, repo_pos = M._make(url, last_slash_pos + 1, string.len(url)) - org, org_pos = M._make(url, first_slash_pos + 1, last_slash_pos - 1) - path, path_pos = M._make(url, first_slash_pos + 1, string.len(url)) - else - repo, repo_pos = M._make(url, first_slash_pos + 1, string.len(url)) - path, path_pos = M._make(url, first_slash_pos + 1, string.len(url)) - -- missing org - end - else - return nil, "invalid url" - end - end else -- missing protocol, either ssh/local file path local first_at_pos = M._find(url, "@") From cc763755959c804d98188907895a433901b6a63a Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 10:19:47 +0800 Subject: [PATCH 10/61] chore --- src/giturlparser.lua | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/giturlparser.lua b/src/giturlparser.lua index f776714..dad15c6 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -266,7 +266,28 @@ M.parse = function(url) -- user (and password) not found -- find first ':', the end position of host, start position of port - local first_colon_pos = M._find(url, ":", first_at_pos + 1) + local first_colon_pos = M._find(url, ":", protocol_delimiter_pos + 3) + if + type(first_colon_pos) == "number" + and first_colon_pos > protocol_delimiter_pos + 3 + then + -- host end with ':', port start with ':' + host, host_pos = + M._make(url, protocol_delimiter_pos + 3, first_colon_pos - 1) + + -- find first '/', the end position of port, start position of path + local first_slash_pos = M._find(url, "/", first_colon_pos + 1) + if + type(first_slash_pos) == "number" + and first_slash_pos > first_colon_pos + 1 + then + -- port end with '/' + port, port_pos = + M._make(url, first_colon_pos + 1, first_slash_pos - 1) + else + end + else + end end else -- missing protocol, either ssh/local file path From ba99054bf687fac6c0547591dbe7b355047e81be Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 10:34:31 +0800 Subject: [PATCH 11/61] chore --- src/giturlparser.lua | 149 ++++++++++++++++++++++++------------------- 1 file changed, 83 insertions(+), 66 deletions(-) diff --git a/src/giturlparser.lua b/src/giturlparser.lua index dad15c6..81e7a1b 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -162,6 +162,66 @@ M._make_path = function(p) } end +--- @alias giturlparser._GitUrlHost {host:string?,host_pos:giturlparser.GitUrlPos?,port:string?,port_pos:giturlparser.GitUrlPos?,path_obj:giturlparser._GitUrlPath} +-- +--- @param p string +--- @return giturlparser._GitUrlHost +M._make_host = function(p) + assert(not M._startswith(p, "/")) + assert(not M._endswith(p, "/")) + + local host = nil + local host_pos = nil + local port = nil + local port_pos = nil + --- @type giturlparser._GitUrlPath + local path_obj = {} + + local plen = string.len(p) + + -- find ':', the end position of host, start position of port + local first_colon_pos = M._find(p, ":") + if type(first_colon_pos) == "number" and first_colon_pos > 1 then + -- host end with ':', port start with ':' + host, host_pos = M._make(p, 1, first_colon_pos - 1) + + -- find first slash '/' (after second ':'), the end position of port, start position of path + local first_slash_pos = M._find(p, "/", first_colon_pos + 1) + if + type(first_slash_pos) == "number" + and first_slash_pos > first_colon_pos + 1 + then + -- port end with '/' + port, port_pos = M._make(p, first_colon_pos + 1, first_slash_pos - 1) + path_obj = M._make_path(string.sub(p, first_slash_pos + 1)) + else + -- path not found, port end until url end + port, port_pos = M._make(p, first_colon_pos + 1, plen) + end + else + -- port not found, host (highly possibly) end with '/' + + -- find first slash '/', the end position of host, start position of path + local first_slash_pos = M._find(p, "/") + if type(first_slash_pos) == "number" and first_slash_pos > 1 then + -- host end with '/' + host, host_pos = M._make(p, 1, first_slash_pos - 1) + path_obj = M._make_path(string.sub(p, first_slash_pos + 1)) + else + -- first slash not found, host end until url end + host, host_pos = M._make(p, 1, plen) + end + end + + return { + host = host, + host_pos = host_pos, + port = port, + port_pos = port_pos, + path_obj = path_obj, + } +end + --- @param url string --- @return giturlparser.GitUrlInfo?, string? M.parse = function(url) @@ -185,8 +245,12 @@ M.parse = function(url) local host_pos = nil local port = nil local port_pos = nil - --- @type giturlparser._GitUrlPath - local path_obj = {} + local org = nil + local org_pos = nil + local repo = nil + local repo_pos = nil + local path = nil + local path_pos = nil -- find first '://', the end position of protocol local protocol_delimiter_pos = M._find(url, "://") @@ -196,6 +260,8 @@ M.parse = function(url) -- protocol end with '://' protocol, protocol_pos = M._make(url, 1, protocol_delimiter_pos - 1) + local host_start_pos = protocol_delimiter_pos + 3 + -- find first '@', the end position of user and password local first_at_pos = M._find(url, "@", protocol_delimiter_pos + 3) if @@ -222,73 +288,24 @@ M.parse = function(url) M._make(url, protocol_delimiter_pos + 3, first_at_pos - 1) end - -- find second ':' (after '@'), the end position of host, start position of port - local second_colon_pos = M._find(url, ":", first_at_pos + 1) - if - type(second_colon_pos) == "number" - and second_colon_pos > first_at_pos + 1 - then - -- host end with ':' - host, host_pos = M._make(url, first_at_pos + 1, second_colon_pos - 1) - - -- find first slash '/' (after second ':'), the end position of port, start position of path - local first_slash_pos = M._find(url, "/", second_colon_pos + 1) - if - type(first_slash_pos) == "number" - and first_slash_pos > second_colon_pos + 1 - then - -- port end with '/' - port, port_pos = - M._make(url, second_colon_pos + 1, first_slash_pos - 1) - path_obj = M._make_path(string.sub(url, first_slash_pos + 1)) - else - -- path not found, port end until url end - port, port_pos = M._make(url, second_colon_pos + 1, urllen) - end - else - -- port not found, host (highly possibly) end with '/' - - -- find first slash '/' (after '@'), the end position of host, start position of path - local first_slash_pos = M._find(url, "/", first_at_pos + 1) - if - type(first_slash_pos) == "number" - and first_slash_pos > first_at_pos + 1 - then - -- host end with '/' - host, host_pos = M._make(url, first_at_pos + 1, first_slash_pos - 1) - path_obj = M._make_path(string.sub(url, first_slash_pos + 1)) - else - -- first slash not found, host end until url end - host, host_pos = M._make(url, first_at_pos + 1, urllen) - end - end + -- host start from '@', user (and password) end position + host_start_pos = first_at_pos + 1 else -- user (and password) not found - - -- find first ':', the end position of host, start position of port - local first_colon_pos = M._find(url, ":", protocol_delimiter_pos + 3) - if - type(first_colon_pos) == "number" - and first_colon_pos > protocol_delimiter_pos + 3 - then - -- host end with ':', port start with ':' - host, host_pos = - M._make(url, protocol_delimiter_pos + 3, first_colon_pos - 1) - - -- find first '/', the end position of port, start position of path - local first_slash_pos = M._find(url, "/", first_colon_pos + 1) - if - type(first_slash_pos) == "number" - and first_slash_pos > first_colon_pos + 1 - then - -- port end with '/' - port, port_pos = - M._make(url, first_colon_pos + 1, first_slash_pos - 1) - else - end - else - end + -- host start from '://' end + + local host_obj = M._make_host(string.sub(url, host_start_pos)) + host = host_obj.host + host_pos = host_obj.host_pos + port = host_obj.port + port_pos = host_obj.port_pos + org = host_obj.path_obj.org + org_pos = host_obj.path_obj.org_pos + repo = host_obj.path_obj.repo + repo_pos = host_obj.path_obj.repo_pos + path = host_obj.path_obj.path + path_pos = host_obj.path_obj.path_pos else -- missing protocol, either ssh/local file path local first_at_pos = M._find(url, "@") From 755cc9e84054e8cf22a15c75225102d41d5c4ef6 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 10:52:21 +0800 Subject: [PATCH 12/61] chore --- src/giturlparser.lua | 309 ++++++++++++++++++++----------------------- 1 file changed, 143 insertions(+), 166 deletions(-) diff --git a/src/giturlparser.lua b/src/giturlparser.lua index 81e7a1b..2fd14e0 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -222,6 +222,63 @@ M._make_host = function(p) } end +--- @alias giturlparser._GitUrlUser {user:string?,user_pos:giturlparser.GitUrlPos?,password:string?,password_pos:giturlparser.GitUrlPos?,host_obj:giturlparser._GitUrlHost} +-- +--- @param p string +--- @return giturlparser._GitUrlUser +M._make_user = function(p) + assert(not M._startswith(p, "/")) + assert(not M._endswith(p, "/")) + + local user = nil + local user_pos = nil + local password = nil + local password_pos = nil + --- @type giturlparser._GitUrlHost + local host_obj = {} + + local plen = string.len(p) + + local host_start_pos = 1 + + -- find first '@', the end position of user and password + local first_at_pos = M._find(p, "@") + if type(first_at_pos) == "number" and first_at_pos > 1 then + -- user (and password) end with '@' + + -- find first ':' (before '@'), the end position of password + local first_colon_pos = M._find(p, ":") + if + type(first_colon_pos) == "number" + and first_colon_pos > 1 + and first_colon_pos < first_at_pos + then + -- password end with ':' + user, user_pos = M._make(p, 1, first_colon_pos - 1) + password, password_pos = M._make(p, first_colon_pos + 1, first_at_pos - 1) + else + -- password not found, user end with '@' + user, user_pos = M._make(p, 1, first_at_pos - 1) + end + + -- host start from '@', user (and password) end position + host_start_pos = first_at_pos + 1 + else + -- user (and password) not found + -- host start from beginning + end + + host_obj = M._make_host(string.sub(p, host_start_pos)) + + return { + user = user, + user_pos = user_pos, + password = password, + password_pos = password_pos, + host_obj = host_obj, + } +end + --- @param url string --- @return giturlparser.GitUrlInfo?, string? M.parse = function(url) @@ -260,178 +317,98 @@ M.parse = function(url) -- protocol end with '://' protocol, protocol_pos = M._make(url, 1, protocol_delimiter_pos - 1) - local host_start_pos = protocol_delimiter_pos + 3 - - -- find first '@', the end position of user and password - local first_at_pos = M._find(url, "@", protocol_delimiter_pos + 3) - if - type(first_at_pos) == "number" - and first_at_pos > protocol_delimiter_pos + 3 - then - -- user (and password) end with '@' - - -- find first ':', the end position of password - local first_colon_pos = M._find(url, ":", protocol_delimiter_pos + 3) - if - type(first_colon_pos) == "number" - and first_colon_pos > protocol_delimiter_pos + 3 - and first_colon_pos < first_at_pos - then - -- password end with ':' - user, user_pos = - M._make(url, protocol_delimiter_pos + 3, first_colon_pos - 1) - password, password_pos = - M._make(url, first_colon_pos + 1, first_at_pos - 1) - else - -- password not found, user end with '@' - user, user_pos = - M._make(url, protocol_delimiter_pos + 3, first_at_pos - 1) - end - - -- host start from '@', user (and password) end position - host_start_pos = first_at_pos + 1 - else - -- user (and password) not found - -- host start from '://' - end - - local host_obj = M._make_host(string.sub(url, host_start_pos)) - host = host_obj.host - host_pos = host_obj.host_pos - port = host_obj.port - port_pos = host_obj.port_pos - org = host_obj.path_obj.org - org_pos = host_obj.path_obj.org_pos - repo = host_obj.path_obj.repo - repo_pos = host_obj.path_obj.repo_pos - path = host_obj.path_obj.path - path_pos = host_obj.path_obj.path_pos + local user_obj = M._make_user(string.sub(url, protocol_delimiter_pos + 3)) + local host_obj = user_obj.host_obj + local path_obj = host_obj.path_obj + + return { + protocol = protocol, + protocol_pos = protocol_pos, + + -- user + user = user_obj.user, + user_pos = user_obj.user_pos, + password = user_obj.password, + password_pos = user_obj.password_pos, + + -- host + host = host_obj.host, + host_pos = host_obj.host_pos, + port = host_obj.port, + port_pos = host_obj.port_pos, + + -- path + org = path_obj.org, + org_pos = path_obj.org_pos, + repo = path_obj.repo, + repo_pos = path_obj.repo_pos, + path = path_obj.path, + path_pos = path_obj.path_pos, + } else - -- missing protocol, either ssh/local file path + -- protocol not found, either ssh/local file path + + -- find first '@', user (and password) end position local first_at_pos = M._find(url, "@") if type(first_at_pos) == "number" and first_at_pos > 1 then - local first_colon_pos = M._find(url, ":") - if - type(first_colon_pos) == "number" - and first_colon_pos > 1 - and first_colon_pos < first_at_pos - then - -- user end with ':', password end with '@' - user, user_pos = M._make(url, 1, first_colon_pos - 1) - password, password_pos = - M._make(url, first_colon_pos + 1, first_at_pos - 1) - - local second_colon_pos = M._find(url, ":", first_at_pos + 1) - if - type(second_colon_pos) == "number" - and second_colon_pos > first_at_pos + 1 - then - -- host end with ':' - host, host_pos = M._make(url, first_at_pos + 1, second_colon_pos - 1) + local user_obj = M._make_user(url) + local host_obj = user_obj.host_obj + local path_obj = host_obj.path_obj + + return { + -- no protocol + + -- user + user = user_obj.user, + user_pos = user_obj.user_pos, + password = user_obj.password, + password_pos = user_obj.password_pos, + + -- host + host = host_obj.host, + host_pos = host_obj.host_pos, + port = host_obj.port, + port_pos = host_obj.port_pos, + + -- path + org = path_obj.org, + org_pos = path_obj.org_pos, + repo = path_obj.repo, + repo_pos = path_obj.repo_pos, + path = path_obj.path, + path_pos = path_obj.path_pos, + } + else + -- user not found - local last_slash_pos = M._rfind(url, "/") - if - type(last_slash_pos) == "number" - and last_slash_pos > second_colon_pos + 1 - then - repo, repo_pos = M._make(url, last_slash_pos + 1, string.len(url)) - org, org_pos = - M._make(url, second_colon_pos + 1, last_slash_pos - 1) - path, path_pos = M._make(url, second_colon_pos + 1, string.len(url)) - else - repo, repo_pos = M._make(url, second_colon_pos + 1, string.len(url)) - path, path_pos = M._make(url, second_colon_pos + 1, string.len(url)) - -- missing org - end - else - local first_slash_pos = M._find(url, "/", first_at_pos + 1) - if - type(first_slash_pos) == "number" - and first_slash_pos > first_at_pos + 1 - then - -- host end with '/' - host, host_pos = M._make(url, first_at_pos + 1, first_slash_pos - 1) - - local last_slash_pos = M._rfind(url, "/") - if - type(last_slash_pos) == "number" - and last_slash_pos > first_slash_pos + 1 - then - repo, repo_pos = M._make(url, last_slash_pos + 1, string.len(url)) - org, org_pos = - M._make(url, first_slash_pos + 1, last_slash_pos - 1) - path, path_pos = - M._make(url, first_slash_pos + 1, string.len(url)) - else - repo, repo_pos = - M._make(url, first_slash_pos + 1, string.len(url)) - path, path_pos = - M._make(url, first_slash_pos + 1, string.len(url)) - -- missing org - end - else - return nil, "invalid url" - end - end + -- find first ':', host end position, port start position + local first_colon_pos = M._find(url, ":") + if type(first_colon_pos) == "number" and first_colon_pos > 1 then + -- host end with ':', port start with ':' + + local host_obj = M._make_host(url) + local path_obj = host_obj.path_obj + return { + -- no protocol + -- no user + + -- host + host = host_obj.host, + host_pos = host_obj.host_pos, + port = host_obj.port, + port_pos = host_obj.port_pos, + + -- path + org = path_obj.org, + org_pos = path_obj.org_pos, + repo = path_obj.repo, + repo_pos = path_obj.repo_pos, + path = path_obj.path, + path_pos = path_obj.path_pos, + } else - -- user end with '@' - user, user_pos = M._make(url, 1, first_at_pos - 1) - -- missing password - - local second_colon_pos = M._find(url, ":", first_at_pos + 1) - if - type(second_colon_pos) == "number" - and second_colon_pos > first_at_pos + 1 - then - -- host end with ':' - host, host_pos = M._make(url, first_at_pos + 1, second_colon_pos - 1) - - local last_slash_pos = M._rfind(url, "/") - if - type(last_slash_pos) == "number" - and last_slash_pos > second_colon_pos + 1 - then - repo, repo_pos = M._make(url, last_slash_pos + 1, string.len(url)) - org, org_pos = - M._make(url, second_colon_pos + 1, last_slash_pos - 1) - path, path_pos = M._make(url, second_colon_pos + 1, string.len(url)) - else - repo, repo_pos = M._make(url, second_colon_pos + 1, string.len(url)) - path, path_pos = M._make(url, second_colon_pos + 1, string.len(url)) - -- missing org - end - else - local first_slash_pos = M._find(url, "/", first_at_pos + 1) - if - type(first_slash_pos) == "number" - and first_slash_pos > first_at_pos + 1 - then - -- host end with '/' - host, host_pos = M._make(url, first_at_pos + 1, first_slash_pos - 1) - - local last_slash_pos = M._rfind(url, "/") - if - type(last_slash_pos) == "number" - and last_slash_pos > first_slash_pos + 1 - then - repo, repo_pos = M._make(url, last_slash_pos + 1, string.len(url)) - org, org_pos = - M._make(url, first_slash_pos + 1, last_slash_pos - 1) - path, path_pos = - M._make(url, first_slash_pos + 1, string.len(url)) - else - repo, repo_pos = - M._make(url, first_slash_pos + 1, string.len(url)) - path, path_pos = - M._make(url, first_slash_pos + 1, string.len(url)) - -- missing org - end - else - return nil, "invalid url" - end - end end - else + local first_colon_pos = M._find(url, ":") if type(first_colon_pos) == "number" and first_colon_pos > 1 then -- host end with ':' From 7ca912be08cc6cedff38050fa3eca3d176e289ab Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 10:56:08 +0800 Subject: [PATCH 13/61] chore --- src/giturlparser.lua | 62 +++++++++++++------------------------------- 1 file changed, 18 insertions(+), 44 deletions(-) diff --git a/src/giturlparser.lua b/src/giturlparser.lua index 2fd14e0..a536fda 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -127,7 +127,7 @@ end --- @param p string --- @return giturlparser._GitUrlPath M._make_path = function(p) - assert(not M._startswith(p, "/")) + assert(M._startswith(p, "/")) assert(not M._endswith(p, "/")) local org = nil @@ -193,7 +193,7 @@ M._make_host = function(p) then -- port end with '/' port, port_pos = M._make(p, first_colon_pos + 1, first_slash_pos - 1) - path_obj = M._make_path(string.sub(p, first_slash_pos + 1)) + path_obj = M._make_path(string.sub(p, first_slash_pos)) else -- path not found, port end until url end port, port_pos = M._make(p, first_colon_pos + 1, plen) @@ -206,7 +206,7 @@ M._make_host = function(p) if type(first_slash_pos) == "number" and first_slash_pos > 1 then -- host end with '/' host, host_pos = M._make(p, 1, first_slash_pos - 1) - path_obj = M._make_path(string.sub(p, first_slash_pos + 1)) + path_obj = M._make_path(string.sub(p, first_slash_pos)) else -- first slash not found, host end until url end host, host_pos = M._make(p, 1, plen) @@ -407,48 +407,22 @@ M.parse = function(url) path_pos = path_obj.path_pos, } else - end + -- port not found, treat as path, either absolute/relative - local first_colon_pos = M._find(url, ":") - if type(first_colon_pos) == "number" and first_colon_pos > 1 then - -- host end with ':' - host, host_pos = M._make(url, 1, first_colon_pos - 1) - - local last_slash_pos = M._rfind(url, "/") - if - type(last_slash_pos) == "number" - and last_slash_pos > first_colon_pos + 1 - then - repo, repo_pos = M._make(url, last_slash_pos + 1, string.len(url)) - org, org_pos = M._make(url, first_colon_pos + 1, last_slash_pos - 1) - path, path_pos = M._make(url, first_colon_pos + 1, string.len(url)) - else - repo, repo_pos = M._make(url, first_colon_pos + 1, string.len(url)) - path, path_pos = M._make(url, first_colon_pos + 1, string.len(url)) - -- missing org - end - else - local first_slash_pos = M._find(url, "/") - if type(first_slash_pos) == "number" and first_slash_pos > 1 then - -- host end with '/' - host, host_pos = M._make(url, 1, first_slash_pos - 1) - - local last_slash_pos = M._rfind(url, "/") - if - type(last_slash_pos) == "number" - and last_slash_pos > first_colon_pos + 1 - then - repo, repo_pos = M._make(url, last_slash_pos + 1, string.len(url)) - org, org_pos = M._make(url, first_colon_pos + 1, last_slash_pos - 1) - path, path_pos = M._make(url, first_colon_pos + 1, string.len(url)) - else - repo, repo_pos = M._make(url, first_colon_pos + 1, string.len(url)) - path, path_pos = M._make(url, first_colon_pos + 1, string.len(url)) - -- missing org - end - else - return nil, "invalid url" - end + local path_obj = M._make_path(url) + return { + -- no protocol + -- no user + -- no host + + -- path + org = path_obj.org, + org_pos = path_obj.org_pos, + repo = path_obj.repo, + repo_pos = path_obj.repo_pos, + path = path_obj.path, + path_pos = path_obj.path_pos, + } end end end From 3b188632e7bd1a64c60eddf6863b7f55ad225067 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 11:02:47 +0800 Subject: [PATCH 14/61] chore --- src/giturlparser.lua | 98 +++++++++++++++----------------------------- 1 file changed, 34 insertions(+), 64 deletions(-) diff --git a/src/giturlparser.lua b/src/giturlparser.lua index a536fda..b9f0366 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -125,8 +125,10 @@ end --- @alias giturlparser._GitUrlPath {org:string?,org_pos:giturlparser.GitUrlPos?,repo:string?,repo_pos:giturlparser.GitUrlPos?,path:string?,path_pos:giturlparser.GitUrlPos?} -- --- @param p string +--- @param start integer --- @return giturlparser._GitUrlPath -M._make_path = function(p) +M._make_path = function(p, start) + assert(type(start) == "number") assert(M._startswith(p, "/")) assert(not M._endswith(p, "/")) @@ -141,16 +143,16 @@ M._make_path = function(p) local last_slash_pos = M._rfind(p, "/") if type(last_slash_pos) == "number" - and last_slash_pos > 1 + and last_slash_pos > start and last_slash_pos < plen then - org, org_pos = M._make(p, 1, last_slash_pos - 1) - repo, repo_pos = M._make(p, last_slash_pos + 1, plen) + org, org_pos = M._make(p, start, last_slash_pos - 1) + repo, repo_pos = M._make(p, last_slash_pos, plen) else -- no slash found, only 1 path component - repo, repo_pos = M._make(p, last_slash_pos + 1, plen) + repo, repo_pos = M._make(p, start, plen) end - path, path_pos = M._make(p, 1, plen) + path, path_pos = M._make(p, start, plen) return { org = org, @@ -165,8 +167,10 @@ end --- @alias giturlparser._GitUrlHost {host:string?,host_pos:giturlparser.GitUrlPos?,port:string?,port_pos:giturlparser.GitUrlPos?,path_obj:giturlparser._GitUrlPath} -- --- @param p string +--- @param start integer --- @return giturlparser._GitUrlHost -M._make_host = function(p) +M._make_host = function(p, start) + assert(type(start) == "number") assert(not M._startswith(p, "/")) assert(not M._endswith(p, "/")) @@ -180,8 +184,8 @@ M._make_host = function(p) local plen = string.len(p) -- find ':', the end position of host, start position of port - local first_colon_pos = M._find(p, ":") - if type(first_colon_pos) == "number" and first_colon_pos > 1 then + local first_colon_pos = M._find(p, ":", start) + if type(first_colon_pos) == "number" and first_colon_pos > start then -- host end with ':', port start with ':' host, host_pos = M._make(p, 1, first_colon_pos - 1) @@ -193,7 +197,7 @@ M._make_host = function(p) then -- port end with '/' port, port_pos = M._make(p, first_colon_pos + 1, first_slash_pos - 1) - path_obj = M._make_path(string.sub(p, first_slash_pos)) + path_obj = M._make_path(p, first_slash_pos) else -- path not found, port end until url end port, port_pos = M._make(p, first_colon_pos + 1, plen) @@ -202,14 +206,14 @@ M._make_host = function(p) -- port not found, host (highly possibly) end with '/' -- find first slash '/', the end position of host, start position of path - local first_slash_pos = M._find(p, "/") - if type(first_slash_pos) == "number" and first_slash_pos > 1 then + local first_slash_pos = M._find(p, "/", start) + if type(first_slash_pos) == "number" and first_slash_pos > start then -- host end with '/' - host, host_pos = M._make(p, 1, first_slash_pos - 1) - path_obj = M._make_path(string.sub(p, first_slash_pos)) + host, host_pos = M._make(p, start, first_slash_pos - 1) + path_obj = M._make_path(p, first_slash_pos) else -- first slash not found, host end until url end - host, host_pos = M._make(p, 1, plen) + host, host_pos = M._make(p, start, plen) end end @@ -225,8 +229,10 @@ end --- @alias giturlparser._GitUrlUser {user:string?,user_pos:giturlparser.GitUrlPos?,password:string?,password_pos:giturlparser.GitUrlPos?,host_obj:giturlparser._GitUrlHost} -- --- @param p string +--- @param start integer --- @return giturlparser._GitUrlUser -M._make_user = function(p) +M._make_user = function(p, start) + assert(type(start) == "number") assert(not M._startswith(p, "/")) assert(not M._endswith(p, "/")) @@ -242,23 +248,23 @@ M._make_user = function(p) local host_start_pos = 1 -- find first '@', the end position of user and password - local first_at_pos = M._find(p, "@") - if type(first_at_pos) == "number" and first_at_pos > 1 then + local first_at_pos = M._find(p, "@", start) + if type(first_at_pos) == "number" and first_at_pos > start then -- user (and password) end with '@' -- find first ':' (before '@'), the end position of password - local first_colon_pos = M._find(p, ":") + local first_colon_pos = M._find(p, ":", start) if type(first_colon_pos) == "number" - and first_colon_pos > 1 + and first_colon_pos > start and first_colon_pos < first_at_pos then -- password end with ':' - user, user_pos = M._make(p, 1, first_colon_pos - 1) + user, user_pos = M._make(p, start, first_colon_pos - 1) password, password_pos = M._make(p, first_colon_pos + 1, first_at_pos - 1) else -- password not found, user end with '@' - user, user_pos = M._make(p, 1, first_at_pos - 1) + user, user_pos = M._make(p, start, first_at_pos - 1) end -- host start from '@', user (and password) end position @@ -268,7 +274,7 @@ M._make_user = function(p) -- host start from beginning end - host_obj = M._make_host(string.sub(p, host_start_pos)) + host_obj = M._make_host(p, host_start_pos) return { user = user, @@ -290,34 +296,15 @@ M.parse = function(url) url = string.sub(url, 1, #url - 1) end - local urllen = string.len(url) - - local protocol = nil - local protocol_pos = nil - local user = nil - local user_pos = nil - local password = nil - local password_pos = nil - local host = nil - local host_pos = nil - local port = nil - local port_pos = nil - local org = nil - local org_pos = nil - local repo = nil - local repo_pos = nil - local path = nil - local path_pos = nil - -- find first '://', the end position of protocol local protocol_delimiter_pos = M._find(url, "://") if type(protocol_delimiter_pos) == "number" and protocol_delimiter_pos > 1 then -- protocol end with '://' - protocol, protocol_pos = M._make(url, 1, protocol_delimiter_pos - 1) + local protocol, protocol_pos = M._make(url, 1, protocol_delimiter_pos - 1) - local user_obj = M._make_user(string.sub(url, protocol_delimiter_pos + 3)) + local user_obj = M._make_user(url, protocol_delimiter_pos + 3) local host_obj = user_obj.host_obj local path_obj = host_obj.path_obj @@ -351,7 +338,7 @@ M.parse = function(url) -- find first '@', user (and password) end position local first_at_pos = M._find(url, "@") if type(first_at_pos) == "number" and first_at_pos > 1 then - local user_obj = M._make_user(url) + local user_obj = M._make_user(url, 1) local host_obj = user_obj.host_obj local path_obj = host_obj.path_obj @@ -386,7 +373,7 @@ M.parse = function(url) if type(first_colon_pos) == "number" and first_colon_pos > 1 then -- host end with ':', port start with ':' - local host_obj = M._make_host(url) + local host_obj = M._make_host(url, 1) local path_obj = host_obj.path_obj return { -- no protocol @@ -409,7 +396,7 @@ M.parse = function(url) else -- port not found, treat as path, either absolute/relative - local path_obj = M._make_path(url) + local path_obj = M._make_path(url, 1) return { -- no protocol -- no user @@ -426,23 +413,6 @@ M.parse = function(url) end end end - - return { - protocol = protocol, - protocol_pos = protocol_pos, - user = user, - user_pos = user_pos, - password = password, - password_pos = password_pos, - host = host, - host_pos = host_pos, - org = org, - org_pos = org_pos, - repo = repo, - repo_pos = repo_pos, - path = path, - path_pos = path_pos, - } end return M From 0ecebac7e2101bd4714ea76f18a8483a8c78bf0b Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 11:08:00 +0800 Subject: [PATCH 15/61] chore --- spec/giturlparser_spec.lua | 10 ++++++++-- src/giturlparser.lua | 13 ++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index c49ce31..61ea611 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -6,7 +6,13 @@ describe("giturlparser", function() before_each(function() end) local giturlparser = require("giturlparser") - describe("[http(s)]", function() + describe("[_make_path]", function() + it("repo.git", function() + local actual = giturlparser._make_path("repo.git") + end) + end) + + describe("[parse http(s)]", function() it("http://host.xz/path/to/repo.git/", function() local actual = giturlparser.parse("http://host.xz/path/to/repo.git/") assert_eq(type(actual), "table") @@ -169,7 +175,7 @@ describe("giturlparser", function() assert_eq(actual.repo_pos.end_pos, 49) end) end) - describe("[ssh]", function() + describe("[parse ssh]", function() it("ssh://user@host.xz:org/repo.git", function() local actual = giturlparser.parse("ssh://user@host.xz:org/repo.git") assert_eq(type(actual), "table") diff --git a/src/giturlparser.lua b/src/giturlparser.lua index b9f0366..34d89f1 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -130,7 +130,11 @@ end M._make_path = function(p, start) assert(type(start) == "number") assert(M._startswith(p, "/")) - assert(not M._endswith(p, "/")) + + local endswith_slash = M._endswith(p, "/") + if endswith_slash then + p = string.sub(p, 1, #p - 1) + end local org = nil local org_pos = nil @@ -154,6 +158,13 @@ M._make_path = function(p, start) end path, path_pos = M._make(p, start, plen) + if endswith_slash then + repo = repo .. "/" + repo_pos.end_pos = repo_pos.end_pos + 1 + path = path .. "/" + path_pos.end_pos = path_pos.end_pos + 1 + end + return { org = org, org_pos = org_pos, From 6ec66e9484021e3e944a52256ed34132b5753730 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 11:14:22 +0800 Subject: [PATCH 16/61] chore --- spec/giturlparser_spec.lua | 43 +++++++++++++++++++++++++++++++++++++- src/giturlparser.lua | 20 +++++------------- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 61ea611..d8021ab 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -8,7 +8,48 @@ describe("giturlparser", function() local giturlparser = require("giturlparser") describe("[_make_path]", function() it("repo.git", function() - local actual = giturlparser._make_path("repo.git") + local actual = giturlparser._make_path("repo.git", 1) + assert_eq(actual.org, nil) + assert_eq(actual.org_pos, nil) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 1) + assert_eq(actual.repo_pos.end_pos, 8) + assert_eq(actual.path, "repo.git") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 8) + end) + it("repo.git/", function() + local actual = giturlparser._make_path("repo.git/", 1) + assert_eq(actual.org, nil) + assert_eq(actual.org_pos, nil) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 1) + assert_eq(actual.repo_pos.end_pos, 8) + assert_eq(actual.path, "repo.git/") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 9) + end) + it("/repo.git/", function() + local actual = giturlparser._make_path("/repo.git/", 1) + assert_eq(actual.org, nil) + assert_eq(actual.org_pos, nil) + assert_eq(actual.repo, "/repo.git") + assert_eq(actual.repo_pos.start_pos, 1) + assert_eq(actual.repo_pos.end_pos, 8) + assert_eq(actual.path, "/repo.git/") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 10) + end) + it("/repo.git", function() + local actual = giturlparser._make_path("/repo.git", 1) + assert_eq(actual.org, nil) + assert_eq(actual.org_pos, nil) + assert_eq(actual.repo, "/repo.git") + assert_eq(actual.repo_pos.start_pos, 1) + assert_eq(actual.repo_pos.end_pos, 8) + assert_eq(actual.path, "/repo.git/") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 10) end) end) diff --git a/src/giturlparser.lua b/src/giturlparser.lua index 34d89f1..0bf785f 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -129,12 +129,8 @@ end --- @return giturlparser._GitUrlPath M._make_path = function(p, start) assert(type(start) == "number") - assert(M._startswith(p, "/")) local endswith_slash = M._endswith(p, "/") - if endswith_slash then - p = string.sub(p, 1, #p - 1) - end local org = nil local org_pos = nil @@ -142,29 +138,23 @@ M._make_path = function(p, start) local repo_pos = nil local path = nil local path_pos = nil - local plen = string.len(p) - local last_slash_pos = M._rfind(p, "/") + + local last_slash_pos = M._rfind(p, "/", endswith_slash and plen - 1 or plen) if type(last_slash_pos) == "number" and last_slash_pos > start and last_slash_pos < plen then org, org_pos = M._make(p, start, last_slash_pos - 1) - repo, repo_pos = M._make(p, last_slash_pos, plen) + repo, repo_pos = + M._make(p, last_slash_pos, endswith_slash and plen - 1 or plen) else -- no slash found, only 1 path component - repo, repo_pos = M._make(p, start, plen) + repo, repo_pos = M._make(p, start, endswith_slash and plen - 1 or plen) end path, path_pos = M._make(p, start, plen) - if endswith_slash then - repo = repo .. "/" - repo_pos.end_pos = repo_pos.end_pos + 1 - path = path .. "/" - path_pos.end_pos = path_pos.end_pos + 1 - end - return { org = org, org_pos = org_pos, From d27c7a3407e4ea3350dfb20d82562af04939c472 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 11:15:02 +0800 Subject: [PATCH 17/61] chore --- spec/giturlparser_spec.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index d8021ab..b92b50e 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -35,7 +35,7 @@ describe("giturlparser", function() assert_eq(actual.org_pos, nil) assert_eq(actual.repo, "/repo.git") assert_eq(actual.repo_pos.start_pos, 1) - assert_eq(actual.repo_pos.end_pos, 8) + assert_eq(actual.repo_pos.end_pos, 9) assert_eq(actual.path, "/repo.git/") assert_eq(actual.path_pos.start_pos, 1) assert_eq(actual.path_pos.end_pos, 10) @@ -46,10 +46,10 @@ describe("giturlparser", function() assert_eq(actual.org_pos, nil) assert_eq(actual.repo, "/repo.git") assert_eq(actual.repo_pos.start_pos, 1) - assert_eq(actual.repo_pos.end_pos, 8) - assert_eq(actual.path, "/repo.git/") + assert_eq(actual.repo_pos.end_pos, 9) + assert_eq(actual.path, "/repo.git") assert_eq(actual.path_pos.start_pos, 1) - assert_eq(actual.path_pos.end_pos, 10) + assert_eq(actual.path_pos.end_pos, 9) end) end) From 1f6d0634e89e492dff3a565a0e2c4a7503ad214c Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 11:24:36 +0800 Subject: [PATCH 18/61] chore --- spec/giturlparser_spec.lua | 396 ++++++++++++++++++++----------------- 1 file changed, 215 insertions(+), 181 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index b92b50e..ad7580b 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -51,193 +51,227 @@ describe("giturlparser", function() assert_eq(actual.path_pos.start_pos, 1) assert_eq(actual.path_pos.end_pos, 9) end) - end) - - describe("[parse http(s)]", function() - it("http://host.xz/path/to/repo.git/", function() - local actual = giturlparser.parse("http://host.xz/path/to/repo.git/") - assert_eq(type(actual), "table") - assert_eq(actual.protocol, "http") - assert_eq(actual.protocol_pos.start_pos, 1) - assert_eq(actual.protocol_pos.end_pos, 4) - assert_eq(actual.user, nil) - assert_eq(actual.user_pos, nil) - assert_eq(actual.password, nil) - assert_eq(actual.password_pos, nil) - assert_eq(actual.host, "host.xz") - assert_eq(actual.host_pos.start_pos, 8) - assert_eq(actual.host_pos.end_pos, 14) - assert_eq(actual.org, "path/to") - assert_eq(actual.org_pos.start_pos, 16) - assert_eq(actual.org_pos.end_pos, 22) - assert_eq(actual.repo, "repo.git") - assert_eq(actual.repo_pos.start_pos, 24) - assert_eq(actual.repo_pos.end_pos, 31) - end) - it("http://host.xz/path/to/repo.git", function() - local actual = giturlparser.parse("http://host.xz/path/to/repo.git") - assert_eq(type(actual), "table") - assert_eq(actual.protocol, "http") - assert_eq(actual.protocol_pos.start_pos, 1) - assert_eq(actual.protocol_pos.end_pos, 4) - assert_eq(actual.user, nil) - assert_eq(actual.user_pos, nil) - assert_eq(actual.password, nil) - assert_eq(actual.password_pos, nil) - assert_eq(actual.host, "host.xz") - assert_eq(actual.host_pos.start_pos, 8) - assert_eq(actual.host_pos.end_pos, 14) - assert_eq(actual.org, "path/to") - assert_eq(actual.org_pos.start_pos, 16) - assert_eq(actual.org_pos.end_pos, 22) - assert_eq(actual.repo, "repo.git") - assert_eq(actual.repo_pos.start_pos, 24) - assert_eq(actual.repo_pos.end_pos, 31) - end) - it("https://host.xz/path/to/repo.git/", function() - local actual = giturlparser.parse("https://host.xz/path/to/repo.git/") - assert_eq(type(actual), "table") - assert_eq(actual.protocol, "https") - assert_eq(actual.protocol_pos.start_pos, 1) - assert_eq(actual.protocol_pos.end_pos, 5) - assert_eq(actual.user, nil) - assert_eq(actual.user_pos, nil) - assert_eq(actual.password, nil) - assert_eq(actual.password_pos, nil) - assert_eq(actual.host, "host.xz") - assert_eq(actual.host_pos.start_pos, 9) - assert_eq(actual.host_pos.end_pos, 15) - assert_eq(actual.org, "path/to") - assert_eq(actual.org_pos.start_pos, 17) - assert_eq(actual.org_pos.end_pos, 23) - assert_eq(actual.repo, "repo.git") - assert_eq(actual.repo_pos.start_pos, 25) - assert_eq(actual.repo_pos.end_pos, 32) - end) - it("https://host.xz/path/to/repo.git", function() - local actual = giturlparser.parse("https://host.xz/path/to/repo.git") - assert_eq(type(actual), "table") - assert_eq(actual.protocol, "https") - assert_eq(actual.protocol_pos.start_pos, 1) - assert_eq(actual.protocol_pos.end_pos, 5) - assert_eq(actual.user, nil) - assert_eq(actual.user_pos, nil) - assert_eq(actual.password, nil) - assert_eq(actual.password_pos, nil) - assert_eq(actual.host, "host.xz") - assert_eq(actual.host_pos.start_pos, 9) - assert_eq(actual.host_pos.end_pos, 15) - assert_eq(actual.org, "path/to") - assert_eq(actual.org_pos.start_pos, 17) - assert_eq(actual.org_pos.end_pos, 23) - assert_eq(actual.repo, "repo.git") - assert_eq(actual.repo_pos.start_pos, 25) - assert_eq(actual.repo_pos.end_pos, 32) - end) - it("https://git.samba.com/samba.git", function() - local actual = giturlparser.parse("https://git.samba.com/samba.git") - assert_eq(type(actual), "table") - assert_eq(actual.protocol, "https") - assert_eq(actual.protocol_pos.start_pos, 1) - assert_eq(actual.protocol_pos.end_pos, 5) - assert_eq(actual.user, nil) - assert_eq(actual.user_pos, nil) - assert_eq(actual.password, nil) - assert_eq(actual.password_pos, nil) - assert_eq(actual.host, "git.samba.com") - assert_eq(actual.host_pos.start_pos, 9) - assert_eq(actual.host_pos.end_pos, 21) + it("prefix/repo.git", function() + local actual = giturlparser._make_path("prefix/repo.git", 7) assert_eq(actual.org, nil) assert_eq(actual.org_pos, nil) - assert_eq(actual.repo, "samba.git") - assert_eq(actual.repo_pos.start_pos, 23) - assert_eq(actual.repo_pos.end_pos, 31) - end) - it("https://git.samba.com/samba.git/", function() - local actual = giturlparser.parse("https://git.samba.com/samba.git/") - assert_eq(type(actual), "table") - assert_eq(actual.protocol, "https") - assert_eq(actual.protocol_pos.start_pos, 1) - assert_eq(actual.protocol_pos.end_pos, 5) - assert_eq(actual.user, nil) - assert_eq(actual.user_pos, nil) - assert_eq(actual.password, nil) - assert_eq(actual.password_pos, nil) - assert_eq(actual.host, "git.samba.com") - assert_eq(actual.host_pos.start_pos, 9) - assert_eq(actual.host_pos.end_pos, 21) - assert_eq(actual.org, nil) - assert_eq(actual.org_pos, nil) - assert_eq(actual.repo, "samba.git") - assert_eq(actual.repo_pos.start_pos, 23) - assert_eq(actual.repo_pos.end_pos, 31) - end) - it("https://git.samba.org/bbaumbach/samba.git", function() - local actual = - giturlparser.parse("https://git.samba.org/bbaumbach/samba.git") - assert_eq(type(actual), "table") - assert_eq(actual.protocol, "https") - assert_eq(actual.protocol_pos.start_pos, 1) - assert_eq(actual.protocol_pos.end_pos, 5) - assert_eq(actual.user, nil) - assert_eq(actual.user_pos, nil) - assert_eq(actual.password, nil) - assert_eq(actual.password_pos, nil) - assert_eq(actual.host, "git.samba.org") - assert_eq(actual.host_pos.start_pos, 9) - assert_eq(actual.host_pos.end_pos, 21) - assert_eq(actual.org, "bbaumbach") - assert_eq(actual.org_pos.start_pos, 23) - assert_eq(actual.org_pos.end_pos, 31) - assert_eq(actual.repo, "samba.git") - assert_eq(actual.repo_pos.start_pos, 33) - assert_eq(actual.repo_pos.end_pos, 41) + assert_eq(actual.repo, "/repo.git") + assert_eq(actual.repo_pos.start_pos, 7) + assert_eq(actual.repo_pos.end_pos, 15) + assert_eq(actual.path, "/repo.git") + assert_eq(actual.path_pos.start_pos, 7) + assert_eq(actual.path_pos.end_pos, 15) end) - it("https://username:password@git.samba.com/samba.git", function() - local actual = - giturlparser.parse("https://username:password@git.samba.com/samba.git") - assert_eq(type(actual), "table") - assert_eq(actual.protocol, "https") - assert_eq(actual.protocol_pos.start_pos, 1) - assert_eq(actual.protocol_pos.end_pos, 5) - assert_eq(actual.user, "username") - assert_eq(actual.user_pos.start_pos, 9) - assert_eq(actual.user_pos.end_pos, 16) - assert_eq(actual.password, "password") - assert_eq(actual.password_pos.start_pos, 18) - assert_eq(actual.password_pos.end_pos, 25) - assert_eq(actual.host, "git.samba.com") - assert_eq(actual.host_pos.start_pos, 27) - assert_eq(actual.host_pos.end_pos, 39) + it("prefix/repo.git/", function() + local actual = giturlparser._make_path("prefix/repo.git/", 7) assert_eq(actual.org, nil) assert_eq(actual.org_pos, nil) - assert_eq(actual.repo, "samba.git") - assert_eq(actual.repo_pos.start_pos, 41) - assert_eq(actual.repo_pos.end_pos, 49) + assert_eq(actual.repo, "/repo.git") + assert_eq(actual.repo_pos.start_pos, 7) + assert_eq(actual.repo_pos.end_pos, 15) + assert_eq(actual.path, "/repo.git/") + assert_eq(actual.path_pos.start_pos, 7) + assert_eq(actual.path_pos.end_pos, 16) end) - end) - describe("[parse ssh]", function() - it("ssh://user@host.xz:org/repo.git", function() - local actual = giturlparser.parse("ssh://user@host.xz:org/repo.git") - assert_eq(type(actual), "table") - assert_eq(actual.protocol, "ssh") - assert_eq(actual.protocol_pos.start_pos, 1) - assert_eq(actual.protocol_pos.end_pos, 3) - assert_eq(actual.user, "user") - assert_eq(actual.user_pos.start_pos, 9) - assert_eq(actual.user_pos.end_pos, 16) - assert_eq(actual.password, nil) - assert_eq(actual.password_pos, nil) - assert_eq(actual.host, "host.xz") - assert_eq(actual.host_pos.start_pos, 27) - assert_eq(actual.host_pos.end_pos, 39) - assert_eq(actual.org, "org") - assert_eq(actual.org_pos.start_pos, 41) - assert_eq(actual.org_pos.end_pos, 53) - assert_eq(actual.repo, "repo.git") - assert_eq(actual.repo_pos.start_pos, 41) - assert_eq(actual.repo_pos.end_pos, 49) + it("path/to/the/repo.git", function() + local actual = giturlparser._make_path("path/to/the/repo.git", 1) + assert_eq(actual.org, "path/to/the") + assert_eq(actual.org_pos.start_pos, 1) + assert_eq(actual.org_pos.end_pos, 11) + assert_eq(actual.repo, "/repo.git") + assert_eq(actual.repo_pos.start_pos, 12) + assert_eq(actual.repo_pos.end_pos, 20) + assert_eq(actual.path, "path/to/the/repo.git") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 20) end) - it("ssh://git@github:linrongbin16/giturlparser.lua.git", function() end) end) + + -- describe("[parse http(s)]", function() + -- it("http://host.xz/path/to/repo.git/", function() + -- local actual = giturlparser.parse("http://host.xz/path/to/repo.git/") + -- assert_eq(type(actual), "table") + -- assert_eq(actual.protocol, "http") + -- assert_eq(actual.protocol_pos.start_pos, 1) + -- assert_eq(actual.protocol_pos.end_pos, 4) + -- assert_eq(actual.user, nil) + -- assert_eq(actual.user_pos, nil) + -- assert_eq(actual.password, nil) + -- assert_eq(actual.password_pos, nil) + -- assert_eq(actual.host, "host.xz") + -- assert_eq(actual.host_pos.start_pos, 8) + -- assert_eq(actual.host_pos.end_pos, 14) + -- assert_eq(actual.org, "path/to") + -- assert_eq(actual.org_pos.start_pos, 16) + -- assert_eq(actual.org_pos.end_pos, 22) + -- assert_eq(actual.repo, "repo.git") + -- assert_eq(actual.repo_pos.start_pos, 24) + -- assert_eq(actual.repo_pos.end_pos, 31) + -- end) + -- it("http://host.xz/path/to/repo.git", function() + -- local actual = giturlparser.parse("http://host.xz/path/to/repo.git") + -- assert_eq(type(actual), "table") + -- assert_eq(actual.protocol, "http") + -- assert_eq(actual.protocol_pos.start_pos, 1) + -- assert_eq(actual.protocol_pos.end_pos, 4) + -- assert_eq(actual.user, nil) + -- assert_eq(actual.user_pos, nil) + -- assert_eq(actual.password, nil) + -- assert_eq(actual.password_pos, nil) + -- assert_eq(actual.host, "host.xz") + -- assert_eq(actual.host_pos.start_pos, 8) + -- assert_eq(actual.host_pos.end_pos, 14) + -- assert_eq(actual.org, "path/to") + -- assert_eq(actual.org_pos.start_pos, 16) + -- assert_eq(actual.org_pos.end_pos, 22) + -- assert_eq(actual.repo, "repo.git") + -- assert_eq(actual.repo_pos.start_pos, 24) + -- assert_eq(actual.repo_pos.end_pos, 31) + -- end) + -- it("https://host.xz/path/to/repo.git/", function() + -- local actual = giturlparser.parse("https://host.xz/path/to/repo.git/") + -- assert_eq(type(actual), "table") + -- assert_eq(actual.protocol, "https") + -- assert_eq(actual.protocol_pos.start_pos, 1) + -- assert_eq(actual.protocol_pos.end_pos, 5) + -- assert_eq(actual.user, nil) + -- assert_eq(actual.user_pos, nil) + -- assert_eq(actual.password, nil) + -- assert_eq(actual.password_pos, nil) + -- assert_eq(actual.host, "host.xz") + -- assert_eq(actual.host_pos.start_pos, 9) + -- assert_eq(actual.host_pos.end_pos, 15) + -- assert_eq(actual.org, "path/to") + -- assert_eq(actual.org_pos.start_pos, 17) + -- assert_eq(actual.org_pos.end_pos, 23) + -- assert_eq(actual.repo, "repo.git") + -- assert_eq(actual.repo_pos.start_pos, 25) + -- assert_eq(actual.repo_pos.end_pos, 32) + -- end) + -- it("https://host.xz/path/to/repo.git", function() + -- local actual = giturlparser.parse("https://host.xz/path/to/repo.git") + -- assert_eq(type(actual), "table") + -- assert_eq(actual.protocol, "https") + -- assert_eq(actual.protocol_pos.start_pos, 1) + -- assert_eq(actual.protocol_pos.end_pos, 5) + -- assert_eq(actual.user, nil) + -- assert_eq(actual.user_pos, nil) + -- assert_eq(actual.password, nil) + -- assert_eq(actual.password_pos, nil) + -- assert_eq(actual.host, "host.xz") + -- assert_eq(actual.host_pos.start_pos, 9) + -- assert_eq(actual.host_pos.end_pos, 15) + -- assert_eq(actual.org, "path/to") + -- assert_eq(actual.org_pos.start_pos, 17) + -- assert_eq(actual.org_pos.end_pos, 23) + -- assert_eq(actual.repo, "repo.git") + -- assert_eq(actual.repo_pos.start_pos, 25) + -- assert_eq(actual.repo_pos.end_pos, 32) + -- end) + -- it("https://git.samba.com/samba.git", function() + -- local actual = giturlparser.parse("https://git.samba.com/samba.git") + -- assert_eq(type(actual), "table") + -- assert_eq(actual.protocol, "https") + -- assert_eq(actual.protocol_pos.start_pos, 1) + -- assert_eq(actual.protocol_pos.end_pos, 5) + -- assert_eq(actual.user, nil) + -- assert_eq(actual.user_pos, nil) + -- assert_eq(actual.password, nil) + -- assert_eq(actual.password_pos, nil) + -- assert_eq(actual.host, "git.samba.com") + -- assert_eq(actual.host_pos.start_pos, 9) + -- assert_eq(actual.host_pos.end_pos, 21) + -- assert_eq(actual.org, nil) + -- assert_eq(actual.org_pos, nil) + -- assert_eq(actual.repo, "samba.git") + -- assert_eq(actual.repo_pos.start_pos, 23) + -- assert_eq(actual.repo_pos.end_pos, 31) + -- end) + -- it("https://git.samba.com/samba.git/", function() + -- local actual = giturlparser.parse("https://git.samba.com/samba.git/") + -- assert_eq(type(actual), "table") + -- assert_eq(actual.protocol, "https") + -- assert_eq(actual.protocol_pos.start_pos, 1) + -- assert_eq(actual.protocol_pos.end_pos, 5) + -- assert_eq(actual.user, nil) + -- assert_eq(actual.user_pos, nil) + -- assert_eq(actual.password, nil) + -- assert_eq(actual.password_pos, nil) + -- assert_eq(actual.host, "git.samba.com") + -- assert_eq(actual.host_pos.start_pos, 9) + -- assert_eq(actual.host_pos.end_pos, 21) + -- assert_eq(actual.org, nil) + -- assert_eq(actual.org_pos, nil) + -- assert_eq(actual.repo, "samba.git") + -- assert_eq(actual.repo_pos.start_pos, 23) + -- assert_eq(actual.repo_pos.end_pos, 31) + -- end) + -- it("https://git.samba.org/bbaumbach/samba.git", function() + -- local actual = + -- giturlparser.parse("https://git.samba.org/bbaumbach/samba.git") + -- assert_eq(type(actual), "table") + -- assert_eq(actual.protocol, "https") + -- assert_eq(actual.protocol_pos.start_pos, 1) + -- assert_eq(actual.protocol_pos.end_pos, 5) + -- assert_eq(actual.user, nil) + -- assert_eq(actual.user_pos, nil) + -- assert_eq(actual.password, nil) + -- assert_eq(actual.password_pos, nil) + -- assert_eq(actual.host, "git.samba.org") + -- assert_eq(actual.host_pos.start_pos, 9) + -- assert_eq(actual.host_pos.end_pos, 21) + -- assert_eq(actual.org, "bbaumbach") + -- assert_eq(actual.org_pos.start_pos, 23) + -- assert_eq(actual.org_pos.end_pos, 31) + -- assert_eq(actual.repo, "samba.git") + -- assert_eq(actual.repo_pos.start_pos, 33) + -- assert_eq(actual.repo_pos.end_pos, 41) + -- end) + -- it("https://username:password@git.samba.com/samba.git", function() + -- local actual = + -- giturlparser.parse("https://username:password@git.samba.com/samba.git") + -- assert_eq(type(actual), "table") + -- assert_eq(actual.protocol, "https") + -- assert_eq(actual.protocol_pos.start_pos, 1) + -- assert_eq(actual.protocol_pos.end_pos, 5) + -- assert_eq(actual.user, "username") + -- assert_eq(actual.user_pos.start_pos, 9) + -- assert_eq(actual.user_pos.end_pos, 16) + -- assert_eq(actual.password, "password") + -- assert_eq(actual.password_pos.start_pos, 18) + -- assert_eq(actual.password_pos.end_pos, 25) + -- assert_eq(actual.host, "git.samba.com") + -- assert_eq(actual.host_pos.start_pos, 27) + -- assert_eq(actual.host_pos.end_pos, 39) + -- assert_eq(actual.org, nil) + -- assert_eq(actual.org_pos, nil) + -- assert_eq(actual.repo, "samba.git") + -- assert_eq(actual.repo_pos.start_pos, 41) + -- assert_eq(actual.repo_pos.end_pos, 49) + -- end) + -- end) + -- describe("[parse ssh]", function() + -- it("ssh://user@host.xz:org/repo.git", function() + -- local actual = giturlparser.parse("ssh://user@host.xz:org/repo.git") + -- assert_eq(type(actual), "table") + -- assert_eq(actual.protocol, "ssh") + -- assert_eq(actual.protocol_pos.start_pos, 1) + -- assert_eq(actual.protocol_pos.end_pos, 3) + -- assert_eq(actual.user, "user") + -- assert_eq(actual.user_pos.start_pos, 9) + -- assert_eq(actual.user_pos.end_pos, 16) + -- assert_eq(actual.password, nil) + -- assert_eq(actual.password_pos, nil) + -- assert_eq(actual.host, "host.xz") + -- assert_eq(actual.host_pos.start_pos, 27) + -- assert_eq(actual.host_pos.end_pos, 39) + -- assert_eq(actual.org, "org") + -- assert_eq(actual.org_pos.start_pos, 41) + -- assert_eq(actual.org_pos.end_pos, 53) + -- assert_eq(actual.repo, "repo.git") + -- assert_eq(actual.repo_pos.start_pos, 41) + -- assert_eq(actual.repo_pos.end_pos, 49) + -- end) + -- it("ssh://git@github:linrongbin16/giturlparser.lua.git", function() end) + -- end) end) From 5f235d6163860445da84c1bddd1084b6c0346d06 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 11:33:26 +0800 Subject: [PATCH 19/61] chore --- spec/giturlparser_spec.lua | 76 ++++++++++++++++++++++++++++++++++---- src/giturlparser.lua | 29 +++++++++++++-- 2 files changed, 94 insertions(+), 11 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index ad7580b..2ec8a35 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -33,8 +33,8 @@ describe("giturlparser", function() local actual = giturlparser._make_path("/repo.git/", 1) assert_eq(actual.org, nil) assert_eq(actual.org_pos, nil) - assert_eq(actual.repo, "/repo.git") - assert_eq(actual.repo_pos.start_pos, 1) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 2) assert_eq(actual.repo_pos.end_pos, 9) assert_eq(actual.path, "/repo.git/") assert_eq(actual.path_pos.start_pos, 1) @@ -44,8 +44,8 @@ describe("giturlparser", function() local actual = giturlparser._make_path("/repo.git", 1) assert_eq(actual.org, nil) assert_eq(actual.org_pos, nil) - assert_eq(actual.repo, "/repo.git") - assert_eq(actual.repo_pos.start_pos, 1) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 2) assert_eq(actual.repo_pos.end_pos, 9) assert_eq(actual.path, "/repo.git") assert_eq(actual.path_pos.start_pos, 1) @@ -55,8 +55,8 @@ describe("giturlparser", function() local actual = giturlparser._make_path("prefix/repo.git", 7) assert_eq(actual.org, nil) assert_eq(actual.org_pos, nil) - assert_eq(actual.repo, "/repo.git") - assert_eq(actual.repo_pos.start_pos, 7) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 8) assert_eq(actual.repo_pos.end_pos, 15) assert_eq(actual.path, "/repo.git") assert_eq(actual.path_pos.start_pos, 7) @@ -66,8 +66,8 @@ describe("giturlparser", function() local actual = giturlparser._make_path("prefix/repo.git/", 7) assert_eq(actual.org, nil) assert_eq(actual.org_pos, nil) - assert_eq(actual.repo, "/repo.git") - assert_eq(actual.repo_pos.start_pos, 7) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 8) assert_eq(actual.repo_pos.end_pos, 15) assert_eq(actual.path, "/repo.git/") assert_eq(actual.path_pos.start_pos, 7) @@ -78,6 +78,54 @@ describe("giturlparser", function() assert_eq(actual.org, "path/to/the") assert_eq(actual.org_pos.start_pos, 1) assert_eq(actual.org_pos.end_pos, 11) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 13) + assert_eq(actual.repo_pos.end_pos, 20) + assert_eq(actual.path, "path/to/the/repo.git") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 20) + end) + it("path/to/the/repo.git/", function() + local actual = giturlparser._make_path("path/to/the/repo.git/", 1) + assert_eq(actual.org, "path/to/the") + assert_eq(actual.org_pos.start_pos, 1) + assert_eq(actual.org_pos.end_pos, 11) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 13) + assert_eq(actual.repo_pos.end_pos, 20) + assert_eq(actual.path, "path/to/the/repo.git/") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 21) + end) + it("/abspath/to/the/repo.git", function() + local actual = giturlparser._make_path("/abspath/to/the/repo.git", 1) + assert_eq(actual.org, "abspath/to/the") + assert_eq(actual.org_pos.start_pos, 2) + assert_eq(actual.org_pos.end_pos, 15) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 17) + assert_eq(actual.repo_pos.end_pos, 24) + assert_eq(actual.path, "/abspath/to/the/repo.git") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 24) + end) + it("/abspath/to/the/repo.git/", function() + local actual = giturlparser._make_path("/abspath/to/the/repo.git/", 1) + assert_eq(actual.org, "/abspath/to/the") + assert_eq(actual.org_pos.start_pos, 1) + assert_eq(actual.org_pos.end_pos, 11) + assert_eq(actual.repo, "/repo.git") + assert_eq(actual.repo_pos.start_pos, 12) + assert_eq(actual.repo_pos.end_pos, 20) + assert_eq(actual.path, "path/to/the/repo.git/") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 21) + end) + it("prefix/path/to/the/repo.git", function() + local actual = giturlparser._make_path("prefix/path/to/the/repo.git", 7) + assert_eq(actual.org, "path/to/the") + assert_eq(actual.org_pos.start_pos, 1) + assert_eq(actual.org_pos.end_pos, 11) assert_eq(actual.repo, "/repo.git") assert_eq(actual.repo_pos.start_pos, 12) assert_eq(actual.repo_pos.end_pos, 20) @@ -85,6 +133,18 @@ describe("giturlparser", function() assert_eq(actual.path_pos.start_pos, 1) assert_eq(actual.path_pos.end_pos, 20) end) + it("path/to/the/repo.git/", function() + local actual = giturlparser._make_path("path/to/the/repo.git/", 1) + assert_eq(actual.org, "path/to/the") + assert_eq(actual.org_pos.start_pos, 1) + assert_eq(actual.org_pos.end_pos, 11) + assert_eq(actual.repo, "/repo.git") + assert_eq(actual.repo_pos.start_pos, 12) + assert_eq(actual.repo_pos.end_pos, 20) + assert_eq(actual.path, "path/to/the/repo.git/") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 21) + end) end) -- describe("[parse http(s)]", function() diff --git a/src/giturlparser.lua b/src/giturlparser.lua index 0bf785f..15aa031 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -122,6 +122,23 @@ M._make = function(url, start_pos, end_pos) return component, pos end +--- @param val string +--- @param pos giturlparser.GitUrlPos +--- @return string, giturlparser.GitUrlPos +M._trim_slash = function(val, pos) + assert(type(val) == "string") + if val and M._startswith(val, "/") then + val = string.sub(val, 2) + pos.start_pos = pos.start_pos + 1 + end + if val and M._endswith(val, "/") then + val = string.sub(val, 1, string.len(val) - 1) + pos.end_pos = pos.end_pos - 1 + end + + return val, pos +end + --- @alias giturlparser._GitUrlPath {org:string?,org_pos:giturlparser.GitUrlPos?,repo:string?,repo_pos:giturlparser.GitUrlPos?,path:string?,path_pos:giturlparser.GitUrlPos?} -- --- @param p string @@ -147,14 +164,20 @@ M._make_path = function(p, start) and last_slash_pos < plen then org, org_pos = M._make(p, start, last_slash_pos - 1) - repo, repo_pos = - M._make(p, last_slash_pos, endswith_slash and plen - 1 or plen) + repo, repo_pos = M._make(p, last_slash_pos, plen) else -- no slash found, only 1 path component - repo, repo_pos = M._make(p, start, endswith_slash and plen - 1 or plen) + repo, repo_pos = M._make(p, start, plen) end path, path_pos = M._make(p, start, plen) + if repo and repo_pos then + repo, repo_pos = M._trim_slash(repo, repo_pos) + end + if org and org_pos then + org, org_pos = M._trim_slash(org, org_pos) + end + return { org = org, org_pos = org_pos, From 0a3472e8d607683cc132fa2e3d66a477a85b0bd6 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 11:34:54 +0800 Subject: [PATCH 20/61] chore --- spec/giturlparser_spec.lua | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 2ec8a35..4371e6a 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -111,15 +111,15 @@ describe("giturlparser", function() end) it("/abspath/to/the/repo.git/", function() local actual = giturlparser._make_path("/abspath/to/the/repo.git/", 1) - assert_eq(actual.org, "/abspath/to/the") - assert_eq(actual.org_pos.start_pos, 1) - assert_eq(actual.org_pos.end_pos, 11) - assert_eq(actual.repo, "/repo.git") - assert_eq(actual.repo_pos.start_pos, 12) - assert_eq(actual.repo_pos.end_pos, 20) - assert_eq(actual.path, "path/to/the/repo.git/") + assert_eq(actual.org, "abspath/to/the") + assert_eq(actual.org_pos.start_pos, 2) + assert_eq(actual.org_pos.end_pos, 15) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 17) + assert_eq(actual.repo_pos.end_pos, 24) + assert_eq(actual.path, "/abspath/to/the/repo.git/") assert_eq(actual.path_pos.start_pos, 1) - assert_eq(actual.path_pos.end_pos, 21) + assert_eq(actual.path_pos.end_pos, 25) end) it("prefix/path/to/the/repo.git", function() local actual = giturlparser._make_path("prefix/path/to/the/repo.git", 7) From 4743690ca6fde07724ba3c0b7cf6122fc2702235 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 11:43:08 +0800 Subject: [PATCH 21/61] chore --- spec/giturlparser_spec.lua | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 4371e6a..a41a3b1 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -124,26 +124,26 @@ describe("giturlparser", function() it("prefix/path/to/the/repo.git", function() local actual = giturlparser._make_path("prefix/path/to/the/repo.git", 7) assert_eq(actual.org, "path/to/the") - assert_eq(actual.org_pos.start_pos, 1) - assert_eq(actual.org_pos.end_pos, 11) - assert_eq(actual.repo, "/repo.git") - assert_eq(actual.repo_pos.start_pos, 12) - assert_eq(actual.repo_pos.end_pos, 20) - assert_eq(actual.path, "path/to/the/repo.git") - assert_eq(actual.path_pos.start_pos, 1) - assert_eq(actual.path_pos.end_pos, 20) + assert_eq(actual.org_pos.start_pos, 8) + assert_eq(actual.org_pos.end_pos, 18) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 20) + assert_eq(actual.repo_pos.end_pos, 27) + assert_eq(actual.path, "/path/to/the/repo.git") + assert_eq(actual.path_pos.start_pos, 7) + assert_eq(actual.path_pos.end_pos, 27) end) - it("path/to/the/repo.git/", function() - local actual = giturlparser._make_path("path/to/the/repo.git/", 1) + it("prefix/path/to/the/repo.git/", function() + local actual = giturlparser._make_path("prefix/path/to/the/repo.git/", 8) assert_eq(actual.org, "path/to/the") - assert_eq(actual.org_pos.start_pos, 1) - assert_eq(actual.org_pos.end_pos, 11) - assert_eq(actual.repo, "/repo.git") - assert_eq(actual.repo_pos.start_pos, 12) - assert_eq(actual.repo_pos.end_pos, 20) + assert_eq(actual.org_pos.start_pos, 8) + assert_eq(actual.org_pos.end_pos, 18) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 20) + assert_eq(actual.repo_pos.end_pos, 27) assert_eq(actual.path, "path/to/the/repo.git/") - assert_eq(actual.path_pos.start_pos, 1) - assert_eq(actual.path_pos.end_pos, 21) + assert_eq(actual.path_pos.start_pos, 8) + assert_eq(actual.path_pos.end_pos, 28) end) end) From 001378514bdc0a6e2e1b16539c206e770184c19d Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 11:46:45 +0800 Subject: [PATCH 22/61] chore --- spec/giturlparser_spec.lua | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index a41a3b1..d19944a 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -145,6 +145,30 @@ describe("giturlparser", function() assert_eq(actual.path_pos.start_pos, 8) assert_eq(actual.path_pos.end_pos, 28) end) + it("~/path/to/the/repo.git", function() + local actual = giturlparser._make_path("~/path/to/the/repo.git", 1) + assert_eq(actual.org, "~/path/to/the") + assert_eq(actual.org_pos.start_pos, 1) + assert_eq(actual.org_pos.end_pos, 13) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 15) + assert_eq(actual.repo_pos.end_pos, 22) + assert_eq(actual.path, "~/path/to/the/repo.git") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 22) + end) + it("~/path/to/the/repo.git/", function() + local actual = giturlparser._make_path("~/path/to/the/repo.git/", 1) + assert_eq(actual.org, "~/path/to/the") + assert_eq(actual.org_pos.start_pos, 1) + assert_eq(actual.org_pos.end_pos, 13) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 15) + assert_eq(actual.repo_pos.end_pos, 22) + assert_eq(actual.path, "~/path/to/the/repo.git/") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 23) + end) end) -- describe("[parse http(s)]", function() From 166dbe04b42c9a596f80936c6e2ab4f94c8f3ebb Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 11:48:01 +0800 Subject: [PATCH 23/61] chore --- spec/giturlparser_spec.lua | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index d19944a..f619fd7 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -169,6 +169,30 @@ describe("giturlparser", function() assert_eq(actual.path_pos.start_pos, 1) assert_eq(actual.path_pos.end_pos, 23) end) + it("./path/to/the/repo.git", function() + local actual = giturlparser._make_path("./path/to/the/repo.git", 1) + assert_eq(actual.org, "./path/to/the") + assert_eq(actual.org_pos.start_pos, 1) + assert_eq(actual.org_pos.end_pos, 13) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 15) + assert_eq(actual.repo_pos.end_pos, 22) + assert_eq(actual.path, "./path/to/the/repo.git") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 22) + end) + it("../path/to/the/repo.git/", function() + local actual = giturlparser._make_path("../path/to/the/repo.git/", 1) + assert_eq(actual.org, "../path/to/the") + assert_eq(actual.org_pos.start_pos, 1) + assert_eq(actual.org_pos.end_pos, 14) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 16) + assert_eq(actual.repo_pos.end_pos, 23) + assert_eq(actual.path, "../path/to/the/repo.git/") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 24) + end) end) -- describe("[parse http(s)]", function() From f3a5ce83cf1c4c9cd4f3d4212711d669641db2c6 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 12:34:28 +0800 Subject: [PATCH 24/61] chore --- spec/giturlparser_spec.lua | 18 ++++++ src/giturlparser.lua | 118 +++++++++++++++++++++---------------- 2 files changed, 85 insertions(+), 51 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index f619fd7..5275a84 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -195,6 +195,24 @@ describe("giturlparser", function() end) end) + describe("[_make_host]", function() + it("github.com/org/repo.git", function() + local actual = giturlparser._make_host("github.com/org/repo.git", 1) + assert_eq(actual.host, "github.com") + assert_eq(actual.host_pos.start_pos, 1) + assert_eq(actual.host_pos.end_pos, 10) + assert_eq(actual.org, "../path/to/the") + assert_eq(actual.org_pos.start_pos, 1) + assert_eq(actual.org_pos.end_pos, 14) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 16) + assert_eq(actual.repo_pos.end_pos, 23) + assert_eq(actual.path, "../path/to/the/repo.git/") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 24) + end) + end) + -- describe("[parse http(s)]", function() -- it("http://host.xz/path/to/repo.git/", function() -- local actual = giturlparser.parse("http://host.xz/path/to/repo.git/") diff --git a/src/giturlparser.lua b/src/giturlparser.lua index 15aa031..ccc25c6 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -250,16 +250,58 @@ M._make_host = function(p, start) } end +-- with omitted ssh protocol, host end with ':' +-- +--- @param p string +--- @param start integer +--- @return giturlparser._GitUrlHost +M._make_host_with_omitted_ssh = function(p, start) + assert(type(start) == "number") + assert(not M._startswith(p, "/")) + assert(not M._endswith(p, "/")) + + local host = nil + local host_pos = nil + local port = nil + local port_pos = nil + --- @type giturlparser._GitUrlPath + local path_obj = {} + + local plen = string.len(p) + + -- find ':', the end position of host, start position of path + local first_colon_pos = M._find(p, ":", start) + if type(first_colon_pos) == "number" and first_colon_pos > start then + -- host end with ':', path start with ':' + host, host_pos = M._make(p, 1, first_colon_pos - 1) + path_obj = M._make_path(p, first_colon_pos) + else + -- host not found, path start with '/' + path_obj = M._make_path(p, start) + end + + return { + host = host, + host_pos = host_pos, + port = port, + port_pos = port_pos, + path_obj = path_obj, + } +end + --- @alias giturlparser._GitUrlUser {user:string?,user_pos:giturlparser.GitUrlPos?,password:string?,password_pos:giturlparser.GitUrlPos?,host_obj:giturlparser._GitUrlHost} -- --- @param p string --- @param start integer +--- @param ssh_protocol_omitted boolean? --- @return giturlparser._GitUrlUser -M._make_user = function(p, start) +M._make_user = function(p, start, ssh_protocol_omitted) assert(type(start) == "number") assert(not M._startswith(p, "/")) assert(not M._endswith(p, "/")) + ssh_protocol_omitted = ssh_protocol_omitted or false + local user = nil local user_pos = nil local password = nil @@ -298,7 +340,9 @@ M._make_user = function(p, start) -- host start from beginning end - host_obj = M._make_host(p, host_start_pos) + host_obj = ssh_protocol_omitted + and M._make_host_with_omitted_ssh(p, host_start_pos) + or M._make_host(p, host_start_pos) return { user = user, @@ -359,10 +403,10 @@ M.parse = function(url) else -- protocol not found, either ssh/local file path - -- find first '@', user (and password) end position - local first_at_pos = M._find(url, "@") - if type(first_at_pos) == "number" and first_at_pos > 1 then - local user_obj = M._make_user(url, 1) + -- find first ':', host end position on omitted ssh protocol + local first_colon_pos = M._find(url, ":") + if type(first_colon_pos) == "number" and first_colon_pos > 1 then + local user_obj = M._make_user(url, 1, true) local host_obj = user_obj.host_obj local path_obj = host_obj.path_obj @@ -390,51 +434,23 @@ M.parse = function(url) path_pos = path_obj.path_pos, } else - -- user not found - - -- find first ':', host end position, port start position - local first_colon_pos = M._find(url, ":") - if type(first_colon_pos) == "number" and first_colon_pos > 1 then - -- host end with ':', port start with ':' - - local host_obj = M._make_host(url, 1) - local path_obj = host_obj.path_obj - return { - -- no protocol - -- no user - - -- host - host = host_obj.host, - host_pos = host_obj.host_pos, - port = host_obj.port, - port_pos = host_obj.port_pos, - - -- path - org = path_obj.org, - org_pos = path_obj.org_pos, - repo = path_obj.repo, - repo_pos = path_obj.repo_pos, - path = path_obj.path, - path_pos = path_obj.path_pos, - } - else - -- port not found, treat as path, either absolute/relative - - local path_obj = M._make_path(url, 1) - return { - -- no protocol - -- no user - -- no host - - -- path - org = path_obj.org, - org_pos = path_obj.org_pos, - repo = path_obj.repo, - repo_pos = path_obj.repo_pos, - path = path_obj.path, - path_pos = path_obj.path_pos, - } - end + -- host not found + + -- treat as local file path, either absolute/relative + local path_obj = M._make_path(url, 1) + return { + -- no protocol + -- no user + -- no host + + -- path + org = path_obj.org, + org_pos = path_obj.org_pos, + repo = path_obj.repo, + repo_pos = path_obj.repo_pos, + path = path_obj.path, + path_pos = path_obj.path_pos, + } end end end From 0d1f9402acccb78d33bebca58c61124d24d3ad3c Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 12:35:19 +0800 Subject: [PATCH 25/61] chore --- spec/giturlparser_spec.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 5275a84..fac9a65 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -201,8 +201,8 @@ describe("giturlparser", function() assert_eq(actual.host, "github.com") assert_eq(actual.host_pos.start_pos, 1) assert_eq(actual.host_pos.end_pos, 10) - assert_eq(actual.org, "../path/to/the") - assert_eq(actual.org_pos.start_pos, 1) + assert_eq(actual.org, "/org/repo.git") + assert_eq(actual.org_pos.start_pos, 11) assert_eq(actual.org_pos.end_pos, 14) assert_eq(actual.repo, "repo.git") assert_eq(actual.repo_pos.start_pos, 16) From 2dde3494de584a432e35098342bbfaf7311854f3 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 12:45:53 +0800 Subject: [PATCH 26/61] chore --- .github/workflows/ci.yml | 1 + Makefile | 6 ------ spec/giturlparser_spec.lua | 4 +++- src/giturlparser.lua | 6 ++++-- 4 files changed, 8 insertions(+), 9 deletions(-) delete mode 100644 Makefile diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 335caea..9bdc5d2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,6 +64,7 @@ jobs: ls -lha luarocks install luacov luarocks install cluacov + luarocks install inspect luarocks install busted busted --coverage . # - uses: lunarmodules/busted@v2 diff --git a/Makefile b/Makefile deleted file mode 100644 index 4a2cd3e..0000000 --- a/Makefile +++ /dev/null @@ -1,6 +0,0 @@ -install_deps: - luarocks install --local luacov - luarocks install --local busted - -test: - busted --coverage diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index fac9a65..31661fc 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -5,6 +5,7 @@ describe("giturlparser", function() before_each(function() end) + local inspect = require("inspect") local giturlparser = require("giturlparser") describe("[_make_path]", function() it("repo.git", function() @@ -198,10 +199,11 @@ describe("giturlparser", function() describe("[_make_host]", function() it("github.com/org/repo.git", function() local actual = giturlparser._make_host("github.com/org/repo.git", 1) + print(string.format("_make_host-1:%s\n", inspect(actual))) assert_eq(actual.host, "github.com") assert_eq(actual.host_pos.start_pos, 1) assert_eq(actual.host_pos.end_pos, 10) - assert_eq(actual.org, "/org/repo.git") + assert_eq(actual.org, "org") assert_eq(actual.org_pos.start_pos, 11) assert_eq(actual.org_pos.end_pos, 14) assert_eq(actual.repo, "repo.git") diff --git a/src/giturlparser.lua b/src/giturlparser.lua index ccc25c6..68789dc 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -188,6 +188,8 @@ M._make_path = function(p, start) } end +-- without omitted ssh protocol, host (and port end with ':') end with '/' +-- --- @alias giturlparser._GitUrlHost {host:string?,host_pos:giturlparser.GitUrlPos?,port:string?,port_pos:giturlparser.GitUrlPos?,path_obj:giturlparser._GitUrlPath} -- --- @param p string @@ -236,8 +238,8 @@ M._make_host = function(p, start) host, host_pos = M._make(p, start, first_slash_pos - 1) path_obj = M._make_path(p, first_slash_pos) else - -- first slash not found, host end until url end - host, host_pos = M._make(p, start, plen) + -- first slash not found, host is omitted, path end until url end + path_obj = M._make_path(p, start) end end From ab1afcf8c1652bbd70c3e56b27f221e93d3d68cf Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 12:50:52 +0800 Subject: [PATCH 27/61] chore --- giturlparser-scm-1.rockspec | 3 +++ spec/giturlparser_spec.lua | 21 +++++++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/giturlparser-scm-1.rockspec b/giturlparser-scm-1.rockspec index 18b1e1a..a9b930a 100644 --- a/giturlparser-scm-1.rockspec +++ b/giturlparser-scm-1.rockspec @@ -11,6 +11,9 @@ description = { dependencies = { "lua >= 5.1, luajit >= 2.0.0", } +test_dependencies = { + "inspect", +} build = { type = "builtin", modules = { diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 31661fc..2b3c67b 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -199,19 +199,20 @@ describe("giturlparser", function() describe("[_make_host]", function() it("github.com/org/repo.git", function() local actual = giturlparser._make_host("github.com/org/repo.git", 1) - print(string.format("_make_host-1:%s\n", inspect(actual))) + -- print(string.format("_make_host-1:%s\n", inspect(actual))) assert_eq(actual.host, "github.com") assert_eq(actual.host_pos.start_pos, 1) assert_eq(actual.host_pos.end_pos, 10) - assert_eq(actual.org, "org") - assert_eq(actual.org_pos.start_pos, 11) - assert_eq(actual.org_pos.end_pos, 14) - assert_eq(actual.repo, "repo.git") - assert_eq(actual.repo_pos.start_pos, 16) - assert_eq(actual.repo_pos.end_pos, 23) - assert_eq(actual.path, "../path/to/the/repo.git/") - assert_eq(actual.path_pos.start_pos, 1) - assert_eq(actual.path_pos.end_pos, 24) + local path_obj = actual.path_obj + assert_eq(path_obj.org, "org") + assert_eq(path_obj.org_pos.start_pos, 12) + assert_eq(path_obj.org_pos.end_pos, 14) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 16) + assert_eq(path_obj.repo_pos.end_pos, 23) + assert_eq(path_obj.path, "/org/repo.git") + assert_eq(path_obj.path_pos.start_pos, 11) + assert_eq(path_obj.path_pos.end_pos, 23) end) end) From 62080aedfc0b42b354095dd314a35766d89b4e95 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 12:51:36 +0800 Subject: [PATCH 28/61] chore --- spec/giturlparser_spec.lua | 17 +++++++++++++++++ src/giturlparser.lua | 1 - 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 2b3c67b..88cc05a 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -214,6 +214,23 @@ describe("giturlparser", function() assert_eq(path_obj.path_pos.start_pos, 11) assert_eq(path_obj.path_pos.end_pos, 23) end) + it("github.com/org/repo.git/", function() + local actual = giturlparser._make_host("github.com/org/repo.git/", 1) + -- print(string.format("_make_host-1:%s\n", inspect(actual))) + assert_eq(actual.host, "github.com") + assert_eq(actual.host_pos.start_pos, 1) + assert_eq(actual.host_pos.end_pos, 10) + local path_obj = actual.path_obj + assert_eq(path_obj.org, "org") + assert_eq(path_obj.org_pos.start_pos, 12) + assert_eq(path_obj.org_pos.end_pos, 14) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 16) + assert_eq(path_obj.repo_pos.end_pos, 23) + assert_eq(path_obj.path, "/org/repo.git/") + assert_eq(path_obj.path_pos.start_pos, 11) + assert_eq(path_obj.path_pos.end_pos, 24) + end) end) -- describe("[parse http(s)]", function() diff --git a/src/giturlparser.lua b/src/giturlparser.lua index 68789dc..77cdc88 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -198,7 +198,6 @@ end M._make_host = function(p, start) assert(type(start) == "number") assert(not M._startswith(p, "/")) - assert(not M._endswith(p, "/")) local host = nil local host_pos = nil From 40ba5fcf2abe240965429feb6377d115337aec65 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 12:55:38 +0800 Subject: [PATCH 29/61] chore --- spec/giturlparser_spec.lua | 46 +++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 88cc05a..782f968 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -203,6 +203,8 @@ describe("giturlparser", function() assert_eq(actual.host, "github.com") assert_eq(actual.host_pos.start_pos, 1) assert_eq(actual.host_pos.end_pos, 10) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) local path_obj = actual.path_obj assert_eq(path_obj.org, "org") assert_eq(path_obj.org_pos.start_pos, 12) @@ -216,10 +218,12 @@ describe("giturlparser", function() end) it("github.com/org/repo.git/", function() local actual = giturlparser._make_host("github.com/org/repo.git/", 1) - -- print(string.format("_make_host-1:%s\n", inspect(actual))) + -- print(string.format("_make_host-2:%s\n", inspect(actual))) assert_eq(actual.host, "github.com") assert_eq(actual.host_pos.start_pos, 1) assert_eq(actual.host_pos.end_pos, 10) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) local path_obj = actual.path_obj assert_eq(path_obj.org, "org") assert_eq(path_obj.org_pos.start_pos, 12) @@ -231,6 +235,46 @@ describe("giturlparser", function() assert_eq(path_obj.path_pos.start_pos, 11) assert_eq(path_obj.path_pos.end_pos, 24) end) + it("github.com:port/org/repo.git", function() + local actual = giturlparser._make_host("github.com:port/org/repo.git", 1) + -- print(string.format("_make_host-3:%s\n", inspect(actual))) + assert_eq(actual.host, "github.com") + assert_eq(actual.host_pos.start_pos, 1) + assert_eq(actual.host_pos.end_pos, 10) + assert_eq(actual.port, "port") + assert_eq(actual.port_pos.start_pos, 12) + assert_eq(actual.port_pos.end_pos, 15) + local path_obj = actual.path_obj + assert_eq(path_obj.org, "org") + assert_eq(path_obj.org_pos.start_pos, 17) + assert_eq(path_obj.org_pos.end_pos, 19) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 21) + assert_eq(path_obj.repo_pos.end_pos, 28) + assert_eq(path_obj.path, "/org/repo.git") + assert_eq(path_obj.path_pos.start_pos, 16) + assert_eq(path_obj.path_pos.end_pos, 28) + end) + it("127.0.0.1:12345/org/repo.git/", function() + local actual = giturlparser._make_host("127.0.0.1:12345/org/repo.git/", 1) + -- print(string.format("_make_host-4:%s\n", inspect(actual))) + assert_eq(actual.host, "127.0.0.1") + assert_eq(actual.host_pos.start_pos, 1) + assert_eq(actual.host_pos.end_pos, 9) + assert_eq(actual.port, "12345") + assert_eq(actual.port_pos.start_pos, 11) + assert_eq(actual.port_pos.end_pos, 15) + local path_obj = actual.path_obj + assert_eq(path_obj.org, "org") + assert_eq(path_obj.org_pos.start_pos, 17) + assert_eq(path_obj.org_pos.end_pos, 19) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 21) + assert_eq(path_obj.repo_pos.end_pos, 28) + assert_eq(path_obj.path, "/org/repo.git/") + assert_eq(path_obj.path_pos.start_pos, 16) + assert_eq(path_obj.path_pos.end_pos, 29) + end) end) -- describe("[parse http(s)]", function() From 00690423c7375e1f4ab5a087d8f3dc935bea7b91 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 13:05:16 +0800 Subject: [PATCH 30/61] chore --- README.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index eb98fc3..f44b97f 100644 --- a/README.md +++ b/README.md @@ -42,20 +42,18 @@ Pure Lua implemented git URL parsing library, e.g. the output of git remot The git url syntax contains many use cases: -1. `{protocol}://host[:{port}]/[{org}/]*{repo}` +1. `{protocol}://[[{user}[:{password}]@]{host}[:{port}]]/[{org}/]*{repo}` - `http://host.xyz/repo.git` - - `ssh://host.xyz:port/path/to/the/repo.git` -2. `{protocol}://[{user}[:{password}]@]host[:{port}]/[{org}/]*{repo}` - - `https://git@host.xyz/repo.git` + - `https://git@127.0.0.1:12345/repo.git` - `ssh://username:password@host.xyz:port/path/to/the/repo.git` -3. `{protocol}://[[{user}[:{password}]@]host[:{port}]]/[{org}/]*{repo}` + - `ssh://host.xyz:port/path/to/the/repo.git` - `file:///repo.git` - - `file://user:passwd@host.xyz:port/repo.git` + - `file://user:passwd@host.xyz:port/path/to/the/repo.git` - `file://~/home/to/the/repo.git` -4. `[{protocol}://][[{user}[:{password}]@]host[:{port}]]/[{org}/]*{repo}` +2. `[{protocol}://][[{user}[:{password}]@]host[:{port}]]/[{org}/]*{repo}` - `git@host.xyz/repo.git` - `user:passwd@host.xyz:port/path/to/the/repo.git` -5. `[~][/{org}]*/{repo}` +3. `[~][/{org}]*/{repo}` - `repo.git` - `./repo.git` - `../path/to/the/repo.git` From ff5c442eb3a75ad76b4e3b4783beb9951959d22c Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 13:06:00 +0800 Subject: [PATCH 31/61] chore --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f44b97f..8771c7c 100644 --- a/README.md +++ b/README.md @@ -50,9 +50,9 @@ The git url syntax contains many use cases: - `file:///repo.git` - `file://user:passwd@host.xyz:port/path/to/the/repo.git` - `file://~/home/to/the/repo.git` -2. `[{protocol}://][[{user}[:{password}]@]host[:{port}]]/[{org}/]*{repo}` - - `git@host.xyz/repo.git` - - `user:passwd@host.xyz:port/path/to/the/repo.git` +2. `[{user}[:{password}]@]{host}:[{org}/]*{repo}` + - `git@host.xyz:repo.git` + - `user:passwd@host.xyz:path/to/the/repo.git` 3. `[~][/{org}]*/{repo}` - `repo.git` - `./repo.git` From 3633b16ece2d0fdb46895ca47ad5a54724332516 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 13:09:33 +0800 Subject: [PATCH 32/61] chore --- spec/giturlparser_spec.lua | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 782f968..525aeaf 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -275,6 +275,43 @@ describe("giturlparser", function() assert_eq(path_obj.path_pos.start_pos, 16) assert_eq(path_obj.path_pos.end_pos, 29) end) + it("github.com/repo.git", function() + local actual = giturlparser._make_host("github.com/repo.git", 1) + -- print(string.format("_make_host-3:%s\n", inspect(actual))) + assert_eq(actual.host, "github.com") + assert_eq(actual.host_pos.start_pos, 1) + assert_eq(actual.host_pos.end_pos, 10) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) + local path_obj = actual.path_obj + assert_eq(path_obj.org, nil) + assert_eq(path_obj.org_pos, nil) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 12) + assert_eq(path_obj.repo_pos.end_pos, 19) + assert_eq(path_obj.path, "/repo.git") + assert_eq(path_obj.path_pos.start_pos, 11) + assert_eq(path_obj.path_pos.end_pos, 19) + end) + it("127.0.0.1:12345/repo.git/", function() + local actual = giturlparser._make_host("127.0.0.1:12345/repo.git/", 1) + -- print(string.format("_make_host-4:%s\n", inspect(actual))) + assert_eq(actual.host, "127.0.0.1") + assert_eq(actual.host_pos.start_pos, 1) + assert_eq(actual.host_pos.end_pos, 9) + assert_eq(actual.port, "12345") + assert_eq(actual.port_pos.start_pos, 11) + assert_eq(actual.port_pos.end_pos, 15) + local path_obj = actual.path_obj + assert_eq(path_obj.org, nil) + assert_eq(path_obj.org_pos, nil) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 17) + assert_eq(path_obj.repo_pos.end_pos, 24) + assert_eq(path_obj.path, "/repo.git/") + assert_eq(path_obj.path_pos.start_pos, 16) + assert_eq(path_obj.path_pos.end_pos, 25) + end) end) -- describe("[parse http(s)]", function() From b9f4feaa77c02c60926f46eae884b2c813fd885c Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 13:12:12 +0800 Subject: [PATCH 33/61] chore --- spec/giturlparser_spec.lua | 119 +++++++++++++++++++++++++++++++++++++ src/giturlparser.lua | 8 +-- 2 files changed, 123 insertions(+), 4 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 525aeaf..b04ce66 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -314,6 +314,125 @@ describe("giturlparser", function() end) end) + describe("[_make_host_with_omit_ssh]", function() + it("github.com:org/repo.git", function() + local actual = + giturlparser._make_host_with_omit_ssh("github.com:org/repo.git", 1) + -- print(string.format("_make_host_with_omit_ssh-1:%s\n", inspect(actual))) + assert_eq(actual.host, "github.com") + assert_eq(actual.host_pos.start_pos, 1) + assert_eq(actual.host_pos.end_pos, 10) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) + local path_obj = actual.path_obj + assert_eq(path_obj.org, "org") + assert_eq(path_obj.org_pos.start_pos, 12) + assert_eq(path_obj.org_pos.end_pos, 14) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 16) + assert_eq(path_obj.repo_pos.end_pos, 23) + assert_eq(path_obj.path, "org/repo.git") + assert_eq(path_obj.path_pos.start_pos, 12) + assert_eq(path_obj.path_pos.end_pos, 23) + end) + it("github.com/org/repo.git/", function() + local actual = giturlparser._make_host("github.com/org/repo.git/", 1) + -- print(string.format("_make_host-2:%s\n", inspect(actual))) + assert_eq(actual.host, "github.com") + assert_eq(actual.host_pos.start_pos, 1) + assert_eq(actual.host_pos.end_pos, 10) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) + local path_obj = actual.path_obj + assert_eq(path_obj.org, "org") + assert_eq(path_obj.org_pos.start_pos, 12) + assert_eq(path_obj.org_pos.end_pos, 14) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 16) + assert_eq(path_obj.repo_pos.end_pos, 23) + assert_eq(path_obj.path, "/org/repo.git/") + assert_eq(path_obj.path_pos.start_pos, 11) + assert_eq(path_obj.path_pos.end_pos, 24) + end) + it("github.com:port/org/repo.git", function() + local actual = giturlparser._make_host("github.com:port/org/repo.git", 1) + -- print(string.format("_make_host-3:%s\n", inspect(actual))) + assert_eq(actual.host, "github.com") + assert_eq(actual.host_pos.start_pos, 1) + assert_eq(actual.host_pos.end_pos, 10) + assert_eq(actual.port, "port") + assert_eq(actual.port_pos.start_pos, 12) + assert_eq(actual.port_pos.end_pos, 15) + local path_obj = actual.path_obj + assert_eq(path_obj.org, "org") + assert_eq(path_obj.org_pos.start_pos, 17) + assert_eq(path_obj.org_pos.end_pos, 19) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 21) + assert_eq(path_obj.repo_pos.end_pos, 28) + assert_eq(path_obj.path, "/org/repo.git") + assert_eq(path_obj.path_pos.start_pos, 16) + assert_eq(path_obj.path_pos.end_pos, 28) + end) + it("127.0.0.1:12345/org/repo.git/", function() + local actual = giturlparser._make_host("127.0.0.1:12345/org/repo.git/", 1) + -- print(string.format("_make_host-4:%s\n", inspect(actual))) + assert_eq(actual.host, "127.0.0.1") + assert_eq(actual.host_pos.start_pos, 1) + assert_eq(actual.host_pos.end_pos, 9) + assert_eq(actual.port, "12345") + assert_eq(actual.port_pos.start_pos, 11) + assert_eq(actual.port_pos.end_pos, 15) + local path_obj = actual.path_obj + assert_eq(path_obj.org, "org") + assert_eq(path_obj.org_pos.start_pos, 17) + assert_eq(path_obj.org_pos.end_pos, 19) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 21) + assert_eq(path_obj.repo_pos.end_pos, 28) + assert_eq(path_obj.path, "/org/repo.git/") + assert_eq(path_obj.path_pos.start_pos, 16) + assert_eq(path_obj.path_pos.end_pos, 29) + end) + it("github.com/repo.git", function() + local actual = giturlparser._make_host("github.com/repo.git", 1) + -- print(string.format("_make_host-3:%s\n", inspect(actual))) + assert_eq(actual.host, "github.com") + assert_eq(actual.host_pos.start_pos, 1) + assert_eq(actual.host_pos.end_pos, 10) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) + local path_obj = actual.path_obj + assert_eq(path_obj.org, nil) + assert_eq(path_obj.org_pos, nil) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 12) + assert_eq(path_obj.repo_pos.end_pos, 19) + assert_eq(path_obj.path, "/repo.git") + assert_eq(path_obj.path_pos.start_pos, 11) + assert_eq(path_obj.path_pos.end_pos, 19) + end) + it("127.0.0.1:12345/repo.git/", function() + local actual = giturlparser._make_host("127.0.0.1:12345/repo.git/", 1) + -- print(string.format("_make_host-4:%s\n", inspect(actual))) + assert_eq(actual.host, "127.0.0.1") + assert_eq(actual.host_pos.start_pos, 1) + assert_eq(actual.host_pos.end_pos, 9) + assert_eq(actual.port, "12345") + assert_eq(actual.port_pos.start_pos, 11) + assert_eq(actual.port_pos.end_pos, 15) + local path_obj = actual.path_obj + assert_eq(path_obj.org, nil) + assert_eq(path_obj.org_pos, nil) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 17) + assert_eq(path_obj.repo_pos.end_pos, 24) + assert_eq(path_obj.path, "/repo.git/") + assert_eq(path_obj.path_pos.start_pos, 16) + assert_eq(path_obj.path_pos.end_pos, 25) + end) + end) + -- describe("[parse http(s)]", function() -- it("http://host.xz/path/to/repo.git/", function() -- local actual = giturlparser.parse("http://host.xz/path/to/repo.git/") diff --git a/src/giturlparser.lua b/src/giturlparser.lua index 77cdc88..df83533 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -256,7 +256,7 @@ end --- @param p string --- @param start integer --- @return giturlparser._GitUrlHost -M._make_host_with_omitted_ssh = function(p, start) +M._make_host_with_omit_ssh = function(p, start) assert(type(start) == "number") assert(not M._startswith(p, "/")) assert(not M._endswith(p, "/")) @@ -275,9 +275,9 @@ M._make_host_with_omitted_ssh = function(p, start) if type(first_colon_pos) == "number" and first_colon_pos > start then -- host end with ':', path start with ':' host, host_pos = M._make(p, 1, first_colon_pos - 1) - path_obj = M._make_path(p, first_colon_pos) + path_obj = M._make_path(p, first_colon_pos + 1) else - -- host not found, path start with '/' + -- host not found, path start from beginning path_obj = M._make_path(p, start) end @@ -342,7 +342,7 @@ M._make_user = function(p, start, ssh_protocol_omitted) end host_obj = ssh_protocol_omitted - and M._make_host_with_omitted_ssh(p, host_start_pos) + and M._make_host_with_omit_ssh(p, host_start_pos) or M._make_host(p, host_start_pos) return { From 05e2a552f4d17025588eefbba4d3bd85c9bceb1e Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 13:13:44 +0800 Subject: [PATCH 34/61] chore --- spec/giturlparser_spec.lua | 11 ++++++----- src/giturlparser.lua | 1 - 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index b04ce66..0665e05 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -335,9 +335,10 @@ describe("giturlparser", function() assert_eq(path_obj.path_pos.start_pos, 12) assert_eq(path_obj.path_pos.end_pos, 23) end) - it("github.com/org/repo.git/", function() - local actual = giturlparser._make_host("github.com/org/repo.git/", 1) - -- print(string.format("_make_host-2:%s\n", inspect(actual))) + it("github.com:org/repo.git/", function() + local actual = + giturlparser._make_host_with_omit_ssh("github.com:org/repo.git/", 1) + -- print(string.format("_make_host_with_omit_ssh-2:%s\n", inspect(actual))) assert_eq(actual.host, "github.com") assert_eq(actual.host_pos.start_pos, 1) assert_eq(actual.host_pos.end_pos, 10) @@ -350,8 +351,8 @@ describe("giturlparser", function() assert_eq(path_obj.repo, "repo.git") assert_eq(path_obj.repo_pos.start_pos, 16) assert_eq(path_obj.repo_pos.end_pos, 23) - assert_eq(path_obj.path, "/org/repo.git/") - assert_eq(path_obj.path_pos.start_pos, 11) + assert_eq(path_obj.path, "org/repo.git/") + assert_eq(path_obj.path_pos.start_pos, 12) assert_eq(path_obj.path_pos.end_pos, 24) end) it("github.com:port/org/repo.git", function() diff --git a/src/giturlparser.lua b/src/giturlparser.lua index df83533..3350426 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -259,7 +259,6 @@ end M._make_host_with_omit_ssh = function(p, start) assert(type(start) == "number") assert(not M._startswith(p, "/")) - assert(not M._endswith(p, "/")) local host = nil local host_pos = nil From 185f7f65b261741538c02362fb24debcdc6e037b Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 13:16:51 +0800 Subject: [PATCH 35/61] chore --- spec/giturlparser_spec.lua | 73 +++++++++----------------------------- 1 file changed, 17 insertions(+), 56 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 0665e05..6d3fbd4 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -355,49 +355,10 @@ describe("giturlparser", function() assert_eq(path_obj.path_pos.start_pos, 12) assert_eq(path_obj.path_pos.end_pos, 24) end) - it("github.com:port/org/repo.git", function() - local actual = giturlparser._make_host("github.com:port/org/repo.git", 1) - -- print(string.format("_make_host-3:%s\n", inspect(actual))) - assert_eq(actual.host, "github.com") - assert_eq(actual.host_pos.start_pos, 1) - assert_eq(actual.host_pos.end_pos, 10) - assert_eq(actual.port, "port") - assert_eq(actual.port_pos.start_pos, 12) - assert_eq(actual.port_pos.end_pos, 15) - local path_obj = actual.path_obj - assert_eq(path_obj.org, "org") - assert_eq(path_obj.org_pos.start_pos, 17) - assert_eq(path_obj.org_pos.end_pos, 19) - assert_eq(path_obj.repo, "repo.git") - assert_eq(path_obj.repo_pos.start_pos, 21) - assert_eq(path_obj.repo_pos.end_pos, 28) - assert_eq(path_obj.path, "/org/repo.git") - assert_eq(path_obj.path_pos.start_pos, 16) - assert_eq(path_obj.path_pos.end_pos, 28) - end) - it("127.0.0.1:12345/org/repo.git/", function() - local actual = giturlparser._make_host("127.0.0.1:12345/org/repo.git/", 1) - -- print(string.format("_make_host-4:%s\n", inspect(actual))) - assert_eq(actual.host, "127.0.0.1") - assert_eq(actual.host_pos.start_pos, 1) - assert_eq(actual.host_pos.end_pos, 9) - assert_eq(actual.port, "12345") - assert_eq(actual.port_pos.start_pos, 11) - assert_eq(actual.port_pos.end_pos, 15) - local path_obj = actual.path_obj - assert_eq(path_obj.org, "org") - assert_eq(path_obj.org_pos.start_pos, 17) - assert_eq(path_obj.org_pos.end_pos, 19) - assert_eq(path_obj.repo, "repo.git") - assert_eq(path_obj.repo_pos.start_pos, 21) - assert_eq(path_obj.repo_pos.end_pos, 28) - assert_eq(path_obj.path, "/org/repo.git/") - assert_eq(path_obj.path_pos.start_pos, 16) - assert_eq(path_obj.path_pos.end_pos, 29) - end) - it("github.com/repo.git", function() - local actual = giturlparser._make_host("github.com/repo.git", 1) - -- print(string.format("_make_host-3:%s\n", inspect(actual))) + it("github.com:repo.git", function() + local actual = + giturlparser._make_host_with_omit_ssh("github.com:repo.git", 1) + -- print(string.format("_make_host_with_omit_ssh-3:%s\n", inspect(actual))) assert_eq(actual.host, "github.com") assert_eq(actual.host_pos.start_pos, 1) assert_eq(actual.host_pos.end_pos, 10) @@ -409,28 +370,28 @@ describe("giturlparser", function() assert_eq(path_obj.repo, "repo.git") assert_eq(path_obj.repo_pos.start_pos, 12) assert_eq(path_obj.repo_pos.end_pos, 19) - assert_eq(path_obj.path, "/repo.git") - assert_eq(path_obj.path_pos.start_pos, 11) + assert_eq(path_obj.path, "repo.git") + assert_eq(path_obj.path_pos.start_pos, 12) assert_eq(path_obj.path_pos.end_pos, 19) end) - it("127.0.0.1:12345/repo.git/", function() - local actual = giturlparser._make_host("127.0.0.1:12345/repo.git/", 1) - -- print(string.format("_make_host-4:%s\n", inspect(actual))) + it("127.0.0.1:repo.git/", function() + local actual = + giturlparser._make_host_with_omit_ssh("127.0.0.1:repo.git/", 1) + -- print(string.format("_make_host_with_omit_ssh-4:%s\n", inspect(actual))) assert_eq(actual.host, "127.0.0.1") assert_eq(actual.host_pos.start_pos, 1) assert_eq(actual.host_pos.end_pos, 9) - assert_eq(actual.port, "12345") - assert_eq(actual.port_pos.start_pos, 11) - assert_eq(actual.port_pos.end_pos, 15) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) local path_obj = actual.path_obj assert_eq(path_obj.org, nil) assert_eq(path_obj.org_pos, nil) assert_eq(path_obj.repo, "repo.git") - assert_eq(path_obj.repo_pos.start_pos, 17) - assert_eq(path_obj.repo_pos.end_pos, 24) - assert_eq(path_obj.path, "/repo.git/") - assert_eq(path_obj.path_pos.start_pos, 16) - assert_eq(path_obj.path_pos.end_pos, 25) + assert_eq(path_obj.repo_pos.start_pos, 11) + assert_eq(path_obj.repo_pos.end_pos, 18) + assert_eq(path_obj.path, "repo.git/") + assert_eq(path_obj.path_pos.start_pos, 11) + assert_eq(path_obj.path_pos.end_pos, 19) end) end) From af55d6f8eb99988e6d7b60392ba2232e15927ce8 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 13:37:41 +0800 Subject: [PATCH 36/61] chore --- spec/giturlparser_spec.lua | 294 +++++++++++++++++++++++++++++++------ src/giturlparser.lua | 25 +++- 2 files changed, 270 insertions(+), 49 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 6d3fbd4..916c772 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -52,28 +52,6 @@ describe("giturlparser", function() assert_eq(actual.path_pos.start_pos, 1) assert_eq(actual.path_pos.end_pos, 9) end) - it("prefix/repo.git", function() - local actual = giturlparser._make_path("prefix/repo.git", 7) - assert_eq(actual.org, nil) - assert_eq(actual.org_pos, nil) - assert_eq(actual.repo, "repo.git") - assert_eq(actual.repo_pos.start_pos, 8) - assert_eq(actual.repo_pos.end_pos, 15) - assert_eq(actual.path, "/repo.git") - assert_eq(actual.path_pos.start_pos, 7) - assert_eq(actual.path_pos.end_pos, 15) - end) - it("prefix/repo.git/", function() - local actual = giturlparser._make_path("prefix/repo.git/", 7) - assert_eq(actual.org, nil) - assert_eq(actual.org_pos, nil) - assert_eq(actual.repo, "repo.git") - assert_eq(actual.repo_pos.start_pos, 8) - assert_eq(actual.repo_pos.end_pos, 15) - assert_eq(actual.path, "/repo.git/") - assert_eq(actual.path_pos.start_pos, 7) - assert_eq(actual.path_pos.end_pos, 16) - end) it("path/to/the/repo.git", function() local actual = giturlparser._make_path("path/to/the/repo.git", 1) assert_eq(actual.org, "path/to/the") @@ -122,30 +100,6 @@ describe("giturlparser", function() assert_eq(actual.path_pos.start_pos, 1) assert_eq(actual.path_pos.end_pos, 25) end) - it("prefix/path/to/the/repo.git", function() - local actual = giturlparser._make_path("prefix/path/to/the/repo.git", 7) - assert_eq(actual.org, "path/to/the") - assert_eq(actual.org_pos.start_pos, 8) - assert_eq(actual.org_pos.end_pos, 18) - assert_eq(actual.repo, "repo.git") - assert_eq(actual.repo_pos.start_pos, 20) - assert_eq(actual.repo_pos.end_pos, 27) - assert_eq(actual.path, "/path/to/the/repo.git") - assert_eq(actual.path_pos.start_pos, 7) - assert_eq(actual.path_pos.end_pos, 27) - end) - it("prefix/path/to/the/repo.git/", function() - local actual = giturlparser._make_path("prefix/path/to/the/repo.git/", 8) - assert_eq(actual.org, "path/to/the") - assert_eq(actual.org_pos.start_pos, 8) - assert_eq(actual.org_pos.end_pos, 18) - assert_eq(actual.repo, "repo.git") - assert_eq(actual.repo_pos.start_pos, 20) - assert_eq(actual.repo_pos.end_pos, 27) - assert_eq(actual.path, "path/to/the/repo.git/") - assert_eq(actual.path_pos.start_pos, 8) - assert_eq(actual.path_pos.end_pos, 28) - end) it("~/path/to/the/repo.git", function() local actual = giturlparser._make_path("~/path/to/the/repo.git", 1) assert_eq(actual.org, "~/path/to/the") @@ -395,6 +349,254 @@ describe("giturlparser", function() end) end) + describe("[_make_user]", function() + it("github.com/org/repo.git", function() + local actual = giturlparser._make_user("github.com/org/repo.git", 1) + -- print(string.format("_make_user-1:%s\n", inspect(actual))) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "github.com") + assert_eq(host_obj.host_pos.start_pos, 1) + assert_eq(host_obj.host_pos.end_pos, 10) + assert_eq(host_obj.port, nil) + assert_eq(host_obj.port_pos, nil) + local path_obj = host_obj.path_obj + assert_eq(path_obj.org, "org") + assert_eq(path_obj.org_pos.start_pos, 12) + assert_eq(path_obj.org_pos.end_pos, 14) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 16) + assert_eq(path_obj.repo_pos.end_pos, 23) + assert_eq(path_obj.path, "/org/repo.git") + assert_eq(path_obj.path_pos.start_pos, 11) + assert_eq(path_obj.path_pos.end_pos, 23) + end) + it("github.com/org/repo.git/", function() + local actual = giturlparser._make_user("github.com/org/repo.git/", 1) + -- print(string.format("_make_user-2:%s\n", inspect(actual))) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "github.com") + assert_eq(host_obj.host_pos.start_pos, 1) + assert_eq(host_obj.host_pos.end_pos, 10) + assert_eq(host_obj.port, nil) + assert_eq(host_obj.port_pos, nil) + local path_obj = host_obj.path_obj + assert_eq(path_obj.org, "org") + assert_eq(path_obj.org_pos.start_pos, 12) + assert_eq(path_obj.org_pos.end_pos, 14) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 16) + assert_eq(path_obj.repo_pos.end_pos, 23) + assert_eq(path_obj.path, "/org/repo.git/") + assert_eq(path_obj.path_pos.start_pos, 11) + assert_eq(path_obj.path_pos.end_pos, 24) + end) + it("user@github.com/org/repo.git", function() + local actual = giturlparser._make_user("user@github.com/org/repo.git", 1) + -- print(string.format("_make_user-3:%s\n", inspect(actual))) + assert_eq(actual.user, "user") + assert_eq(actual.user_pos.start_pos, 1) + assert_eq(actual.user_pos.end_pos, 4) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "github.com") + assert_eq(host_obj.host_pos.start_pos, 6) + assert_eq(host_obj.host_pos.end_pos, 15) + assert_eq(host_obj.port, nil) + assert_eq(host_obj.port_pos, nil) + local path_obj = host_obj.path_obj + assert_eq(path_obj.org, "org") + assert_eq(path_obj.org_pos.start_pos, 17) + assert_eq(path_obj.org_pos.end_pos, 19) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 21) + assert_eq(path_obj.repo_pos.end_pos, 28) + assert_eq(path_obj.path, "/org/repo.git") + assert_eq(path_obj.path_pos.start_pos, 16) + assert_eq(path_obj.path_pos.end_pos, 28) + end) + it("user:passwd@github.com/org/repo.git/", function() + local actual = + giturlparser._make_user("user:passwd@github.com/org/repo.git/", 1) + -- print(string.format("_make_user-4:%s\n", inspect(actual))) + assert_eq(actual.user, "user") + assert_eq(actual.user_pos.start_pos, 1) + assert_eq(actual.user_pos.end_pos, 4) + assert_eq(actual.password, "passwd") + assert_eq(actual.password_pos.start_pos, 6) + assert_eq(actual.password_pos.end_pos, 11) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "github.com") + assert_eq(host_obj.host_pos.start_pos, 13) + assert_eq(host_obj.host_pos.end_pos, 22) + assert_eq(host_obj.port, nil) + assert_eq(host_obj.port_pos, nil) + local path_obj = host_obj.path_obj + assert_eq(path_obj.org, "org") + assert_eq(path_obj.org_pos.start_pos, 24) + assert_eq(path_obj.org_pos.end_pos, 26) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 28) + assert_eq(path_obj.repo_pos.end_pos, 35) + assert_eq(path_obj.path, "/org/repo.git/") + assert_eq(path_obj.path_pos.start_pos, 23) + assert_eq(path_obj.path_pos.end_pos, 36) + end) + it("github.com:port/org/repo.git", function() + local actual = giturlparser._make_user("github.com:port/org/repo.git", 1) + -- print(string.format("_make_user-5:%s\n", inspect(actual))) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "github.com") + assert_eq(host_obj.host_pos.start_pos, 1) + assert_eq(host_obj.host_pos.end_pos, 10) + assert_eq(host_obj.port, "port") + assert_eq(host_obj.port_pos.start_pos, 12) + assert_eq(host_obj.port_pos.end_pos, 15) + local path_obj = host_obj.path_obj + assert_eq(path_obj.org, "org") + assert_eq(path_obj.org_pos.start_pos, 17) + assert_eq(path_obj.org_pos.end_pos, 19) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 21) + assert_eq(path_obj.repo_pos.end_pos, 28) + assert_eq(path_obj.path, "/org/repo.git") + assert_eq(path_obj.path_pos.start_pos, 16) + assert_eq(path_obj.path_pos.end_pos, 28) + end) + it("127.0.0.1:12345/org/repo.git/", function() + local actual = giturlparser._make_user("127.0.0.1:12345/org/repo.git/", 1) + -- print(string.format("_make_user-6:%s\n", inspect(actual))) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "127.0.0.1") + assert_eq(host_obj.host_pos.start_pos, 1) + assert_eq(host_obj.host_pos.end_pos, 9) + assert_eq(host_obj.port, "12345") + assert_eq(host_obj.port_pos.start_pos, 11) + assert_eq(host_obj.port_pos.end_pos, 15) + local path_obj = host_obj.path_obj + assert_eq(path_obj.org, "org") + assert_eq(path_obj.org_pos.start_pos, 17) + assert_eq(path_obj.org_pos.end_pos, 19) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 21) + assert_eq(path_obj.repo_pos.end_pos, 28) + assert_eq(path_obj.path, "/org/repo.git/") + assert_eq(path_obj.path_pos.start_pos, 16) + assert_eq(path_obj.path_pos.end_pos, 29) + end) + it("user2:passwd2@github.com:port/org/repo.git", function() + local actual = + giturlparser._make_user("user2:passwd2@github.com:port/org/repo.git", 1) + -- print(string.format("_make_user-7:%s\n", inspect(actual))) + assert_eq(actual.user, "user2") + assert_eq(actual.user_pos.start_pos, 1) + assert_eq(actual.user_pos.end_pos, 5) + assert_eq(actual.password, "passwd2") + assert_eq(actual.password_pos.start_pos, 7) + assert_eq(actual.password_pos.end_pos, 13) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "github.com") + assert_eq(host_obj.host_pos.start_pos, 15) + assert_eq(host_obj.host_pos.end_pos, 24) + assert_eq(host_obj.port, "port") + assert_eq(host_obj.port_pos.start_pos, 12) + assert_eq(host_obj.port_pos.end_pos, 15) + local path_obj = host_obj.path_obj + assert_eq(path_obj.org, "org") + assert_eq(path_obj.org_pos.start_pos, 17) + assert_eq(path_obj.org_pos.end_pos, 19) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 21) + assert_eq(path_obj.repo_pos.end_pos, 28) + assert_eq(path_obj.path, "/org/repo.git") + assert_eq(path_obj.path_pos.start_pos, 16) + assert_eq(path_obj.path_pos.end_pos, 28) + end) + it("git@127.0.0.1:12345/org/repo.git/", function() + local actual = + giturlparser._make_user("git@127.0.0.1:12345/org/repo.git/", 1) + -- print(string.format("_make_user-6:%s\n", inspect(actual))) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "127.0.0.1") + assert_eq(host_obj.host_pos.start_pos, 1) + assert_eq(host_obj.host_pos.end_pos, 9) + assert_eq(host_obj.port, "12345") + assert_eq(host_obj.port_pos.start_pos, 11) + assert_eq(host_obj.port_pos.end_pos, 15) + local path_obj = host_obj.path_obj + assert_eq(path_obj.org, "org") + assert_eq(path_obj.org_pos.start_pos, 17) + assert_eq(path_obj.org_pos.end_pos, 19) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 21) + assert_eq(path_obj.repo_pos.end_pos, 28) + assert_eq(path_obj.path, "/org/repo.git/") + assert_eq(path_obj.path_pos.start_pos, 16) + assert_eq(path_obj.path_pos.end_pos, 29) + end) + it("github.com/repo.git", function() + local actual = giturlparser._make_host("github.com/repo.git", 1) + -- print(string.format("_make_host-3:%s\n", inspect(actual))) + assert_eq(actual.host, "github.com") + assert_eq(actual.host_pos.start_pos, 1) + assert_eq(actual.host_pos.end_pos, 10) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) + local path_obj = actual.path_obj + assert_eq(path_obj.org, nil) + assert_eq(path_obj.org_pos, nil) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 12) + assert_eq(path_obj.repo_pos.end_pos, 19) + assert_eq(path_obj.path, "/repo.git") + assert_eq(path_obj.path_pos.start_pos, 11) + assert_eq(path_obj.path_pos.end_pos, 19) + end) + it("127.0.0.1:12345/repo.git/", function() + local actual = giturlparser._make_host("127.0.0.1:12345/repo.git/", 1) + -- print(string.format("_make_host-4:%s\n", inspect(actual))) + assert_eq(actual.host, "127.0.0.1") + assert_eq(actual.host_pos.start_pos, 1) + assert_eq(actual.host_pos.end_pos, 9) + assert_eq(actual.port, "12345") + assert_eq(actual.port_pos.start_pos, 11) + assert_eq(actual.port_pos.end_pos, 15) + local path_obj = actual.path_obj + assert_eq(path_obj.org, nil) + assert_eq(path_obj.org_pos, nil) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 17) + assert_eq(path_obj.repo_pos.end_pos, 24) + assert_eq(path_obj.path, "/repo.git/") + assert_eq(path_obj.path_pos.start_pos, 16) + assert_eq(path_obj.path_pos.end_pos, 25) + end) + end) + + describe("[_make_user with omitted ssh]", function() + it("") + end) + -- describe("[parse http(s)]", function() -- it("http://host.xz/path/to/repo.git/", function() -- local actual = giturlparser.parse("http://host.xz/path/to/repo.git/") diff --git a/src/giturlparser.lua b/src/giturlparser.lua index 3350426..7c3ad67 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -212,7 +212,7 @@ M._make_host = function(p, start) local first_colon_pos = M._find(p, ":", start) if type(first_colon_pos) == "number" and first_colon_pos > start then -- host end with ':', port start with ':' - host, host_pos = M._make(p, 1, first_colon_pos - 1) + host, host_pos = M._make(p, start, first_colon_pos - 1) -- find first slash '/' (after second ':'), the end position of port, start position of path local first_slash_pos = M._find(p, "/", first_colon_pos + 1) @@ -298,7 +298,8 @@ end M._make_user = function(p, start, ssh_protocol_omitted) assert(type(start) == "number") assert(not M._startswith(p, "/")) - assert(not M._endswith(p, "/")) + + local inspect = require("inspect") ssh_protocol_omitted = ssh_protocol_omitted or false @@ -311,10 +312,20 @@ M._make_user = function(p, start, ssh_protocol_omitted) local plen = string.len(p) - local host_start_pos = 1 + local host_start_pos = start -- find first '@', the end position of user and password local first_at_pos = M._find(p, "@", start) + print( + string.format( + "|_make_user-1| p:%s, start:%s, ssh_protocol_omitted:%s, first_at_pos:%s, host_start_pos:%s\n", + inspect(p), + inspect(start), + inspect(ssh_protocol_omitted), + inspect(first_at_pos), + inspect(host_start_pos) + ) + ) if type(first_at_pos) == "number" and first_at_pos > start then -- user (and password) end with '@' @@ -340,6 +351,14 @@ M._make_user = function(p, start, ssh_protocol_omitted) -- host start from beginning end + print( + string.format( + "|_make_user-2| ssh_protocol_omitted:%s, host_start_pos:%s, first_at_pos:%s\n", + inspect(ssh_protocol_omitted), + inspect(host_start_pos), + inspect(first_at_pos) + ) + ) host_obj = ssh_protocol_omitted and M._make_host_with_omit_ssh(p, host_start_pos) or M._make_host(p, host_start_pos) From 2348a87030c022315054e6a5856a399e5211e0fa Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 13:39:47 +0800 Subject: [PATCH 37/61] chore --- spec/giturlparser_spec.lua | 41 +++++++++++++++++++------------------- src/giturlparser.lua | 38 +++++++++++++++++------------------ 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 916c772..f01d950 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -515,44 +515,45 @@ describe("giturlparser", function() assert_eq(host_obj.host_pos.start_pos, 15) assert_eq(host_obj.host_pos.end_pos, 24) assert_eq(host_obj.port, "port") - assert_eq(host_obj.port_pos.start_pos, 12) - assert_eq(host_obj.port_pos.end_pos, 15) + assert_eq(host_obj.port_pos.start_pos, 26) + assert_eq(host_obj.port_pos.end_pos, 29) local path_obj = host_obj.path_obj assert_eq(path_obj.org, "org") - assert_eq(path_obj.org_pos.start_pos, 17) - assert_eq(path_obj.org_pos.end_pos, 19) + assert_eq(path_obj.org_pos.start_pos, 31) + assert_eq(path_obj.org_pos.end_pos, 33) assert_eq(path_obj.repo, "repo.git") - assert_eq(path_obj.repo_pos.start_pos, 21) - assert_eq(path_obj.repo_pos.end_pos, 28) + assert_eq(path_obj.repo_pos.start_pos, 35) + assert_eq(path_obj.repo_pos.end_pos, 42) assert_eq(path_obj.path, "/org/repo.git") - assert_eq(path_obj.path_pos.start_pos, 16) - assert_eq(path_obj.path_pos.end_pos, 28) + assert_eq(path_obj.path_pos.start_pos, 30) + assert_eq(path_obj.path_pos.end_pos, 42) end) it("git@127.0.0.1:12345/org/repo.git/", function() local actual = giturlparser._make_user("git@127.0.0.1:12345/org/repo.git/", 1) -- print(string.format("_make_user-6:%s\n", inspect(actual))) - assert_eq(actual.user, nil) - assert_eq(actual.user_pos, nil) + assert_eq(actual.user, "git") + assert_eq(actual.user_pos.start_pos, 1) + assert_eq(actual.user_pos.end_pos, 3) assert_eq(actual.password, nil) assert_eq(actual.password_pos, nil) local host_obj = actual.host_obj assert_eq(host_obj.host, "127.0.0.1") - assert_eq(host_obj.host_pos.start_pos, 1) - assert_eq(host_obj.host_pos.end_pos, 9) + assert_eq(host_obj.host_pos.start_pos, 5) + assert_eq(host_obj.host_pos.end_pos, 13) assert_eq(host_obj.port, "12345") - assert_eq(host_obj.port_pos.start_pos, 11) - assert_eq(host_obj.port_pos.end_pos, 15) + assert_eq(host_obj.port_pos.start_pos, 15) + assert_eq(host_obj.port_pos.end_pos, 19) local path_obj = host_obj.path_obj assert_eq(path_obj.org, "org") - assert_eq(path_obj.org_pos.start_pos, 17) - assert_eq(path_obj.org_pos.end_pos, 19) + assert_eq(path_obj.org_pos.start_pos, 21) + assert_eq(path_obj.org_pos.end_pos, 23) assert_eq(path_obj.repo, "repo.git") - assert_eq(path_obj.repo_pos.start_pos, 21) - assert_eq(path_obj.repo_pos.end_pos, 28) + assert_eq(path_obj.repo_pos.start_pos, 25) + assert_eq(path_obj.repo_pos.end_pos, 33) assert_eq(path_obj.path, "/org/repo.git/") - assert_eq(path_obj.path_pos.start_pos, 16) - assert_eq(path_obj.path_pos.end_pos, 29) + assert_eq(path_obj.path_pos.start_pos, 20) + assert_eq(path_obj.path_pos.end_pos, 34) end) it("github.com/repo.git", function() local actual = giturlparser._make_host("github.com/repo.git", 1) diff --git a/src/giturlparser.lua b/src/giturlparser.lua index 7c3ad67..ff848ea 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -299,7 +299,7 @@ M._make_user = function(p, start, ssh_protocol_omitted) assert(type(start) == "number") assert(not M._startswith(p, "/")) - local inspect = require("inspect") + -- local inspect = require("inspect") ssh_protocol_omitted = ssh_protocol_omitted or false @@ -316,16 +316,16 @@ M._make_user = function(p, start, ssh_protocol_omitted) -- find first '@', the end position of user and password local first_at_pos = M._find(p, "@", start) - print( - string.format( - "|_make_user-1| p:%s, start:%s, ssh_protocol_omitted:%s, first_at_pos:%s, host_start_pos:%s\n", - inspect(p), - inspect(start), - inspect(ssh_protocol_omitted), - inspect(first_at_pos), - inspect(host_start_pos) - ) - ) + -- print( + -- string.format( + -- "|_make_user-1| p:%s, start:%s, ssh_protocol_omitted:%s, first_at_pos:%s, host_start_pos:%s\n", + -- inspect(p), + -- inspect(start), + -- inspect(ssh_protocol_omitted), + -- inspect(first_at_pos), + -- inspect(host_start_pos) + -- ) + -- ) if type(first_at_pos) == "number" and first_at_pos > start then -- user (and password) end with '@' @@ -351,14 +351,14 @@ M._make_user = function(p, start, ssh_protocol_omitted) -- host start from beginning end - print( - string.format( - "|_make_user-2| ssh_protocol_omitted:%s, host_start_pos:%s, first_at_pos:%s\n", - inspect(ssh_protocol_omitted), - inspect(host_start_pos), - inspect(first_at_pos) - ) - ) + -- print( + -- string.format( + -- "|_make_user-2| ssh_protocol_omitted:%s, host_start_pos:%s, first_at_pos:%s\n", + -- inspect(ssh_protocol_omitted), + -- inspect(host_start_pos), + -- inspect(first_at_pos) + -- ) + -- ) host_obj = ssh_protocol_omitted and M._make_host_with_omit_ssh(p, host_start_pos) or M._make_host(p, host_start_pos) From 7b7943964feec69070ea57a329d5de7f3787b306 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 13:40:04 +0800 Subject: [PATCH 38/61] chore --- spec/giturlparser_spec.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index f01d950..2c30308 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -550,10 +550,10 @@ describe("giturlparser", function() assert_eq(path_obj.org_pos.end_pos, 23) assert_eq(path_obj.repo, "repo.git") assert_eq(path_obj.repo_pos.start_pos, 25) - assert_eq(path_obj.repo_pos.end_pos, 33) + assert_eq(path_obj.repo_pos.end_pos, 32) assert_eq(path_obj.path, "/org/repo.git/") assert_eq(path_obj.path_pos.start_pos, 20) - assert_eq(path_obj.path_pos.end_pos, 34) + assert_eq(path_obj.path_pos.end_pos, 33) end) it("github.com/repo.git", function() local actual = giturlparser._make_host("github.com/repo.git", 1) From 17a9970d5f4967ffb559001531bd63fd92237853 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 13:42:25 +0800 Subject: [PATCH 39/61] chore --- .luacheckrc | 2 +- spec/giturlparser_spec.lua | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index 7e585f4..810574b 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -3,4 +3,4 @@ max_line_length = 500 unused = false unused_args = false exclude_files = {} -ignore = { 542 } +ignore = { "542" } diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 2c30308..455706e 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -556,8 +556,8 @@ describe("giturlparser", function() assert_eq(path_obj.path_pos.end_pos, 33) end) it("github.com/repo.git", function() - local actual = giturlparser._make_host("github.com/repo.git", 1) - -- print(string.format("_make_host-3:%s\n", inspect(actual))) + local actual = giturlparser._make_user("github.com/repo.git", 1) + -- print(string.format("_make_user-7:%s\n", inspect(actual))) assert_eq(actual.host, "github.com") assert_eq(actual.host_pos.start_pos, 1) assert_eq(actual.host_pos.end_pos, 10) @@ -574,8 +574,8 @@ describe("giturlparser", function() assert_eq(path_obj.path_pos.end_pos, 19) end) it("127.0.0.1:12345/repo.git/", function() - local actual = giturlparser._make_host("127.0.0.1:12345/repo.git/", 1) - -- print(string.format("_make_host-4:%s\n", inspect(actual))) + local actual = giturlparser._make_user("127.0.0.1:12345/repo.git/", 1) + -- print(string.format("_make_user-8:%s\n", inspect(actual))) assert_eq(actual.host, "127.0.0.1") assert_eq(actual.host_pos.start_pos, 1) assert_eq(actual.host_pos.end_pos, 9) From 13041cac0fa14609e277f367e1ea08fd813477d6 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 13:45:55 +0800 Subject: [PATCH 40/61] chore --- spec/giturlparser_spec.lua | 84 ++++++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 13 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 455706e..e9be5bc 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -558,12 +558,17 @@ describe("giturlparser", function() it("github.com/repo.git", function() local actual = giturlparser._make_user("github.com/repo.git", 1) -- print(string.format("_make_user-7:%s\n", inspect(actual))) - assert_eq(actual.host, "github.com") - assert_eq(actual.host_pos.start_pos, 1) - assert_eq(actual.host_pos.end_pos, 10) - assert_eq(actual.port, nil) - assert_eq(actual.port_pos, nil) - local path_obj = actual.path_obj + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "github.com") + assert_eq(host_obj.host_pos.start_pos, 1) + assert_eq(host_obj.host_pos.end_pos, 10) + assert_eq(host_obj.port, nil) + assert_eq(host_obj.port_pos, nil) + local path_obj = host_obj.path_obj assert_eq(path_obj.org, nil) assert_eq(path_obj.org_pos, nil) assert_eq(path_obj.repo, "repo.git") @@ -576,13 +581,66 @@ describe("giturlparser", function() it("127.0.0.1:12345/repo.git/", function() local actual = giturlparser._make_user("127.0.0.1:12345/repo.git/", 1) -- print(string.format("_make_user-8:%s\n", inspect(actual))) - assert_eq(actual.host, "127.0.0.1") - assert_eq(actual.host_pos.start_pos, 1) - assert_eq(actual.host_pos.end_pos, 9) - assert_eq(actual.port, "12345") - assert_eq(actual.port_pos.start_pos, 11) - assert_eq(actual.port_pos.end_pos, 15) - local path_obj = actual.path_obj + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "127.0.0.1") + assert_eq(host_obj.host_pos.start_pos, 1) + assert_eq(host_obj.host_pos.end_pos, 9) + assert_eq(host_obj.port, "12345") + assert_eq(host_obj.port_pos.start_pos, 11) + assert_eq(host_obj.port_pos.end_pos, 15) + local path_obj = host_obj.path_obj + assert_eq(path_obj.org, nil) + assert_eq(path_obj.org_pos, nil) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 17) + assert_eq(path_obj.repo_pos.end_pos, 24) + assert_eq(path_obj.path, "/repo.git/") + assert_eq(path_obj.path_pos.start_pos, 16) + assert_eq(path_obj.path_pos.end_pos, 25) + end) + it("git@github.com/repo.git", function() + local actual = giturlparser._make_user("git@github.com/repo.git", 1) + -- print(string.format("_make_user-9:%s\n", inspect(actual))) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "github.com") + assert_eq(host_obj.host_pos.start_pos, 1) + assert_eq(host_obj.host_pos.end_pos, 10) + assert_eq(host_obj.port, nil) + assert_eq(host_obj.port_pos, nil) + local path_obj = host_obj.path_obj + assert_eq(path_obj.org, nil) + assert_eq(path_obj.org_pos, nil) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 12) + assert_eq(path_obj.repo_pos.end_pos, 19) + assert_eq(path_obj.path, "/repo.git") + assert_eq(path_obj.path_pos.start_pos, 11) + assert_eq(path_obj.path_pos.end_pos, 19) + end) + it("linrongbin:123456@127.0.0.1:12345/repo.git/", function() + local actual = + giturlparser._make_user("linrongbin:123@127.0.0.1:12345/repo.git/", 1) + -- print(string.format("_make_user-8:%s\n", inspect(actual))) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "127.0.0.1") + assert_eq(host_obj.host_pos.start_pos, 1) + assert_eq(host_obj.host_pos.end_pos, 9) + assert_eq(host_obj.port, "12345") + assert_eq(host_obj.port_pos.start_pos, 11) + assert_eq(host_obj.port_pos.end_pos, 15) + local path_obj = host_obj.path_obj assert_eq(path_obj.org, nil) assert_eq(path_obj.org_pos, nil) assert_eq(path_obj.repo, "repo.git") From 41cfe1e92717877e78b71030c2b73563c325fbbf Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 13:47:58 +0800 Subject: [PATCH 41/61] chore --- spec/giturlparser_spec.lua | 43 ++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index e9be5bc..ae4e584 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -605,50 +605,53 @@ describe("giturlparser", function() it("git@github.com/repo.git", function() local actual = giturlparser._make_user("git@github.com/repo.git", 1) -- print(string.format("_make_user-9:%s\n", inspect(actual))) - assert_eq(actual.user, nil) - assert_eq(actual.user_pos, nil) + assert_eq(actual.user, "git") + assert_eq(actual.user_pos.start_pos, 1) + assert_eq(actual.user_pos.end_pos, 3) assert_eq(actual.password, nil) assert_eq(actual.password_pos, nil) local host_obj = actual.host_obj assert_eq(host_obj.host, "github.com") - assert_eq(host_obj.host_pos.start_pos, 1) - assert_eq(host_obj.host_pos.end_pos, 10) + assert_eq(host_obj.host_pos.start_pos, 5) + assert_eq(host_obj.host_pos.end_pos, 14) assert_eq(host_obj.port, nil) assert_eq(host_obj.port_pos, nil) local path_obj = host_obj.path_obj assert_eq(path_obj.org, nil) assert_eq(path_obj.org_pos, nil) assert_eq(path_obj.repo, "repo.git") - assert_eq(path_obj.repo_pos.start_pos, 12) - assert_eq(path_obj.repo_pos.end_pos, 19) + assert_eq(path_obj.repo_pos.start_pos, 16) + assert_eq(path_obj.repo_pos.end_pos, 23) assert_eq(path_obj.path, "/repo.git") - assert_eq(path_obj.path_pos.start_pos, 11) - assert_eq(path_obj.path_pos.end_pos, 19) + assert_eq(path_obj.path_pos.start_pos, 15) + assert_eq(path_obj.path_pos.end_pos, 23) end) it("linrongbin:123456@127.0.0.1:12345/repo.git/", function() local actual = giturlparser._make_user("linrongbin:123@127.0.0.1:12345/repo.git/", 1) -- print(string.format("_make_user-8:%s\n", inspect(actual))) - assert_eq(actual.user, nil) - assert_eq(actual.user_pos, nil) - assert_eq(actual.password, nil) - assert_eq(actual.password_pos, nil) + assert_eq(actual.user, "linrongbin") + assert_eq(actual.user_pos.start_pos, 1) + assert_eq(actual.user_pos.end_pos, 10) + assert_eq(actual.password, "123") + assert_eq(actual.password_pos.start_pos, 12) + assert_eq(actual.password_pos.end_pos, 14) local host_obj = actual.host_obj assert_eq(host_obj.host, "127.0.0.1") - assert_eq(host_obj.host_pos.start_pos, 1) - assert_eq(host_obj.host_pos.end_pos, 9) + assert_eq(host_obj.host_pos.start_pos, 16) + assert_eq(host_obj.host_pos.end_pos, 24) assert_eq(host_obj.port, "12345") - assert_eq(host_obj.port_pos.start_pos, 11) - assert_eq(host_obj.port_pos.end_pos, 15) + assert_eq(host_obj.port_pos.start_pos, 26) + assert_eq(host_obj.port_pos.end_pos, 30) local path_obj = host_obj.path_obj assert_eq(path_obj.org, nil) assert_eq(path_obj.org_pos, nil) assert_eq(path_obj.repo, "repo.git") - assert_eq(path_obj.repo_pos.start_pos, 17) - assert_eq(path_obj.repo_pos.end_pos, 24) + assert_eq(path_obj.repo_pos.start_pos, 32) + assert_eq(path_obj.repo_pos.end_pos, 39) assert_eq(path_obj.path, "/repo.git/") - assert_eq(path_obj.path_pos.start_pos, 16) - assert_eq(path_obj.path_pos.end_pos, 25) + assert_eq(path_obj.path_pos.start_pos, 31) + assert_eq(path_obj.path_pos.end_pos, 40) end) end) From 314c85b5f8660ae1d4484133908af5962dfbb4da Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 14:05:51 +0800 Subject: [PATCH 42/61] chore --- spec/giturlparser_spec.lua | 101 ++++++++++++++++++++++++++++++++++++- src/giturlparser.lua | 2 +- 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index ae4e584..2c215ff 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -656,7 +656,106 @@ describe("giturlparser", function() end) describe("[_make_user with omitted ssh]", function() - it("") + it("github.com:org/repo.git", function() + local actual = giturlparser._make_user("github.com:org/repo.git", 1, true) + -- print(string.format("_make_user_omit-1:%s\n", inspect(actual))) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "github.com") + assert_eq(host_obj.host_pos.start_pos, 1) + assert_eq(host_obj.host_pos.end_pos, 10) + assert_eq(host_obj.port, nil) + assert_eq(host_obj.port_pos, nil) + local path_obj = host_obj.path_obj + assert_eq(path_obj.org, "org") + assert_eq(path_obj.org_pos.start_pos, 12) + assert_eq(path_obj.org_pos.end_pos, 14) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 16) + assert_eq(path_obj.repo_pos.end_pos, 23) + assert_eq(path_obj.path, "org/repo.git") + assert_eq(path_obj.path_pos.start_pos, 12) + assert_eq(path_obj.path_pos.end_pos, 23) + end) + it("github.com:org/repo.git/", function() + local actual = + giturlparser._make_user("github.com:org/repo.git/", 1, true) + -- print(string.format("_make_user_omit-2:%s\n", inspect(actual))) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "github.com") + assert_eq(host_obj.host_pos.start_pos, 1) + assert_eq(host_obj.host_pos.end_pos, 10) + assert_eq(host_obj.port, nil) + assert_eq(host_obj.port_pos, nil) + local path_obj = host_obj.path_obj + assert_eq(path_obj.org, "org") + assert_eq(path_obj.org_pos.start_pos, 12) + assert_eq(path_obj.org_pos.end_pos, 14) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 16) + assert_eq(path_obj.repo_pos.end_pos, 23) + assert_eq(path_obj.path, "org/repo.git/") + assert_eq(path_obj.path_pos.start_pos, 12) + assert_eq(path_obj.path_pos.end_pos, 24) + end) + it("user@github.com:repo.git", function() + local actual = + giturlparser._make_user("user@github.com:repo.git", 1, true) + -- print(string.format("_make_user_omit-3:%s\n", inspect(actual))) + assert_eq(actual.user, "user") + assert_eq(actual.user_pos.start_pos, 1) + assert_eq(actual.user_pos.end_pos, 4) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "github.com") + assert_eq(host_obj.host_pos.start_pos, 6) + assert_eq(host_obj.host_pos.end_pos, 15) + assert_eq(host_obj.port, nil) + assert_eq(host_obj.port_pos, nil) + local path_obj = host_obj.path_obj + assert_eq(path_obj.org, nil) + assert_eq(path_obj.org_pos, nil) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 17) + assert_eq(path_obj.repo_pos.end_pos, 24) + assert_eq(path_obj.path, "repo.git") + assert_eq(path_obj.path_pos.start_pos, 17) + assert_eq(path_obj.path_pos.end_pos, 24) + end) + it("user:passwd@127.0.0.1:repo.git/", function() + local actual = + giturlparser._make_user("user:passwd@127.0.0.1:repo.git/", 1, true) + -- print(string.format("_make_user_omit-4:%s\n", inspect(actual))) + assert_eq(actual.user, "user") + assert_eq(actual.user_pos.start_pos, 1) + assert_eq(actual.user_pos.end_pos, 4) + assert_eq(actual.password, "passwd") + assert_eq(actual.password_pos.start_pos, 6) + assert_eq(actual.password_pos.end_pos, 11) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "127.0.0.1") + assert_eq(host_obj.host_pos.start_pos, 13) + assert_eq(host_obj.host_pos.end_pos, 21) + assert_eq(host_obj.port, nil) + assert_eq(host_obj.port_pos, nil) + local path_obj = host_obj.path_obj + assert_eq(path_obj.org, nil) + assert_eq(path_obj.org_pos, nil) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 23) + assert_eq(path_obj.repo_pos.end_pos, 30) + assert_eq(path_obj.path, "repo.git/") + assert_eq(path_obj.path_pos.start_pos, 23) + assert_eq(path_obj.path_pos.end_pos, 31) + end) end) -- describe("[parse http(s)]", function() diff --git a/src/giturlparser.lua b/src/giturlparser.lua index ff848ea..6ffa06f 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -273,7 +273,7 @@ M._make_host_with_omit_ssh = function(p, start) local first_colon_pos = M._find(p, ":", start) if type(first_colon_pos) == "number" and first_colon_pos > start then -- host end with ':', path start with ':' - host, host_pos = M._make(p, 1, first_colon_pos - 1) + host, host_pos = M._make(p, start, first_colon_pos - 1) path_obj = M._make_path(p, first_colon_pos + 1) else -- host not found, path start from beginning From dad0461a12a4cda3733df75f6e72ae2483d632ba Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 14:12:52 +0800 Subject: [PATCH 43/61] chore --- spec/giturlparser_spec.lua | 97 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 2c215ff..7f247be 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -756,6 +756,103 @@ describe("giturlparser", function() assert_eq(path_obj.path_pos.start_pos, 23) assert_eq(path_obj.path_pos.end_pos, 31) end) + it("github.com:repo.git", function() + local actual = giturlparser._make_user("github.com:repo.git", 1, true) + -- print(string.format("_make_user_omit-5:%s\n", inspect(actual))) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "github.com") + assert_eq(host_obj.host_pos.start_pos, 1) + assert_eq(host_obj.host_pos.end_pos, 10) + assert_eq(host_obj.port, nil) + assert_eq(host_obj.port_pos, nil) + local path_obj = host_obj.path_obj + assert_eq(path_obj.org, nil) + assert_eq(path_obj.org_pos, nil) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 12) + assert_eq(path_obj.repo_pos.end_pos, 19) + assert_eq(path_obj.path, "repo.git") + assert_eq(path_obj.path_pos.start_pos, 12) + assert_eq(path_obj.path_pos.end_pos, 19) + end) + it("127.0.0.1:repo.git/", function() + local actual = giturlparser._make_user("127.0.0.1:repo.git/", 1, true) + -- print(string.format("_make_user_omit-6:%s\n", inspect(actual))) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "127.0.0.1") + assert_eq(host_obj.host_pos.start_pos, 1) + assert_eq(host_obj.host_pos.end_pos, 9) + assert_eq(host_obj.port, nil) + assert_eq(host_obj.port_pos, nil) + local path_obj = host_obj.path_obj + assert_eq(path_obj.org, nil) + assert_eq(path_obj.org_pos, nil) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 11) + assert_eq(path_obj.repo_pos.end_pos, 18) + assert_eq(path_obj.path, "repo.git/") + assert_eq(path_obj.path_pos.start_pos, 11) + assert_eq(path_obj.path_pos.end_pos, 19) + end) + it("user@github.com:repo.git", function() + local actual = + giturlparser._make_user("user@github.com:repo.git", 1, true) + -- print(string.format("_make_user_omit-7:%s\n", inspect(actual))) + assert_eq(actual.user, "user") + assert_eq(actual.user_pos.start_pos, 1) + assert_eq(actual.user_pos.end_pos, 4) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "github.com") + assert_eq(host_obj.host_pos.start_pos, 6) + assert_eq(host_obj.host_pos.end_pos, 15) + assert_eq(host_obj.port, nil) + assert_eq(host_obj.port_pos, nil) + local path_obj = host_obj.path_obj + assert_eq(path_obj.org, nil) + assert_eq(path_obj.org_pos, nil) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 17) + assert_eq(path_obj.repo_pos.end_pos, 24) + assert_eq(path_obj.path, "repo.git") + assert_eq(path_obj.path_pos.start_pos, 17) + assert_eq(path_obj.path_pos.end_pos, 24) + end) + it("git:pass@127.0.0.1:repo.git/", function() + local actual = + giturlparser._make_user("git:pass@127.0.0.1:repo.git/", 1, true) + -- print(string.format("_make_user_omit-9:%s\n", inspect(actual))) + assert_eq(actual.user, "git") + assert_eq(actual.user_pos.start_pos, 1) + assert_eq(actual.user_pos.end_pos, 3) + assert_eq(actual.password, "pass") + assert_eq(actual.password_pos.start_pos, 5) + assert_eq(actual.password_pos.end_pos, 8) + local host_obj = actual.host_obj + assert_eq(host_obj.host, "127.0.0.1") + assert_eq(host_obj.host_pos.start_pos, 10) + assert_eq(host_obj.host_pos.end_pos, 18) + assert_eq(host_obj.port, nil) + assert_eq(host_obj.port_pos, nil) + local path_obj = host_obj.path_obj + assert_eq(path_obj.org, nil) + assert_eq(path_obj.org_pos, nil) + assert_eq(path_obj.repo, "repo.git") + assert_eq(path_obj.repo_pos.start_pos, 20) + assert_eq(path_obj.repo_pos.end_pos, 27) + assert_eq(path_obj.path, "repo.git/") + assert_eq(path_obj.path_pos.start_pos, 20) + assert_eq(path_obj.path_pos.end_pos, 28) + end) end) -- describe("[parse http(s)]", function() From e4214f6feb8a43936fe8c23215721d1fdaaa0bed Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 14:17:13 +0800 Subject: [PATCH 44/61] chore --- spec/giturlparser_spec.lua | 376 +++++++++++++++++++------------------ 1 file changed, 189 insertions(+), 187 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 7f247be..732c663 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -855,191 +855,193 @@ describe("giturlparser", function() end) end) - -- describe("[parse http(s)]", function() - -- it("http://host.xz/path/to/repo.git/", function() - -- local actual = giturlparser.parse("http://host.xz/path/to/repo.git/") - -- assert_eq(type(actual), "table") - -- assert_eq(actual.protocol, "http") - -- assert_eq(actual.protocol_pos.start_pos, 1) - -- assert_eq(actual.protocol_pos.end_pos, 4) - -- assert_eq(actual.user, nil) - -- assert_eq(actual.user_pos, nil) - -- assert_eq(actual.password, nil) - -- assert_eq(actual.password_pos, nil) - -- assert_eq(actual.host, "host.xz") - -- assert_eq(actual.host_pos.start_pos, 8) - -- assert_eq(actual.host_pos.end_pos, 14) - -- assert_eq(actual.org, "path/to") - -- assert_eq(actual.org_pos.start_pos, 16) - -- assert_eq(actual.org_pos.end_pos, 22) - -- assert_eq(actual.repo, "repo.git") - -- assert_eq(actual.repo_pos.start_pos, 24) - -- assert_eq(actual.repo_pos.end_pos, 31) - -- end) - -- it("http://host.xz/path/to/repo.git", function() - -- local actual = giturlparser.parse("http://host.xz/path/to/repo.git") - -- assert_eq(type(actual), "table") - -- assert_eq(actual.protocol, "http") - -- assert_eq(actual.protocol_pos.start_pos, 1) - -- assert_eq(actual.protocol_pos.end_pos, 4) - -- assert_eq(actual.user, nil) - -- assert_eq(actual.user_pos, nil) - -- assert_eq(actual.password, nil) - -- assert_eq(actual.password_pos, nil) - -- assert_eq(actual.host, "host.xz") - -- assert_eq(actual.host_pos.start_pos, 8) - -- assert_eq(actual.host_pos.end_pos, 14) - -- assert_eq(actual.org, "path/to") - -- assert_eq(actual.org_pos.start_pos, 16) - -- assert_eq(actual.org_pos.end_pos, 22) - -- assert_eq(actual.repo, "repo.git") - -- assert_eq(actual.repo_pos.start_pos, 24) - -- assert_eq(actual.repo_pos.end_pos, 31) - -- end) - -- it("https://host.xz/path/to/repo.git/", function() - -- local actual = giturlparser.parse("https://host.xz/path/to/repo.git/") - -- assert_eq(type(actual), "table") - -- assert_eq(actual.protocol, "https") - -- assert_eq(actual.protocol_pos.start_pos, 1) - -- assert_eq(actual.protocol_pos.end_pos, 5) - -- assert_eq(actual.user, nil) - -- assert_eq(actual.user_pos, nil) - -- assert_eq(actual.password, nil) - -- assert_eq(actual.password_pos, nil) - -- assert_eq(actual.host, "host.xz") - -- assert_eq(actual.host_pos.start_pos, 9) - -- assert_eq(actual.host_pos.end_pos, 15) - -- assert_eq(actual.org, "path/to") - -- assert_eq(actual.org_pos.start_pos, 17) - -- assert_eq(actual.org_pos.end_pos, 23) - -- assert_eq(actual.repo, "repo.git") - -- assert_eq(actual.repo_pos.start_pos, 25) - -- assert_eq(actual.repo_pos.end_pos, 32) - -- end) - -- it("https://host.xz/path/to/repo.git", function() - -- local actual = giturlparser.parse("https://host.xz/path/to/repo.git") - -- assert_eq(type(actual), "table") - -- assert_eq(actual.protocol, "https") - -- assert_eq(actual.protocol_pos.start_pos, 1) - -- assert_eq(actual.protocol_pos.end_pos, 5) - -- assert_eq(actual.user, nil) - -- assert_eq(actual.user_pos, nil) - -- assert_eq(actual.password, nil) - -- assert_eq(actual.password_pos, nil) - -- assert_eq(actual.host, "host.xz") - -- assert_eq(actual.host_pos.start_pos, 9) - -- assert_eq(actual.host_pos.end_pos, 15) - -- assert_eq(actual.org, "path/to") - -- assert_eq(actual.org_pos.start_pos, 17) - -- assert_eq(actual.org_pos.end_pos, 23) - -- assert_eq(actual.repo, "repo.git") - -- assert_eq(actual.repo_pos.start_pos, 25) - -- assert_eq(actual.repo_pos.end_pos, 32) - -- end) - -- it("https://git.samba.com/samba.git", function() - -- local actual = giturlparser.parse("https://git.samba.com/samba.git") - -- assert_eq(type(actual), "table") - -- assert_eq(actual.protocol, "https") - -- assert_eq(actual.protocol_pos.start_pos, 1) - -- assert_eq(actual.protocol_pos.end_pos, 5) - -- assert_eq(actual.user, nil) - -- assert_eq(actual.user_pos, nil) - -- assert_eq(actual.password, nil) - -- assert_eq(actual.password_pos, nil) - -- assert_eq(actual.host, "git.samba.com") - -- assert_eq(actual.host_pos.start_pos, 9) - -- assert_eq(actual.host_pos.end_pos, 21) - -- assert_eq(actual.org, nil) - -- assert_eq(actual.org_pos, nil) - -- assert_eq(actual.repo, "samba.git") - -- assert_eq(actual.repo_pos.start_pos, 23) - -- assert_eq(actual.repo_pos.end_pos, 31) - -- end) - -- it("https://git.samba.com/samba.git/", function() - -- local actual = giturlparser.parse("https://git.samba.com/samba.git/") - -- assert_eq(type(actual), "table") - -- assert_eq(actual.protocol, "https") - -- assert_eq(actual.protocol_pos.start_pos, 1) - -- assert_eq(actual.protocol_pos.end_pos, 5) - -- assert_eq(actual.user, nil) - -- assert_eq(actual.user_pos, nil) - -- assert_eq(actual.password, nil) - -- assert_eq(actual.password_pos, nil) - -- assert_eq(actual.host, "git.samba.com") - -- assert_eq(actual.host_pos.start_pos, 9) - -- assert_eq(actual.host_pos.end_pos, 21) - -- assert_eq(actual.org, nil) - -- assert_eq(actual.org_pos, nil) - -- assert_eq(actual.repo, "samba.git") - -- assert_eq(actual.repo_pos.start_pos, 23) - -- assert_eq(actual.repo_pos.end_pos, 31) - -- end) - -- it("https://git.samba.org/bbaumbach/samba.git", function() - -- local actual = - -- giturlparser.parse("https://git.samba.org/bbaumbach/samba.git") - -- assert_eq(type(actual), "table") - -- assert_eq(actual.protocol, "https") - -- assert_eq(actual.protocol_pos.start_pos, 1) - -- assert_eq(actual.protocol_pos.end_pos, 5) - -- assert_eq(actual.user, nil) - -- assert_eq(actual.user_pos, nil) - -- assert_eq(actual.password, nil) - -- assert_eq(actual.password_pos, nil) - -- assert_eq(actual.host, "git.samba.org") - -- assert_eq(actual.host_pos.start_pos, 9) - -- assert_eq(actual.host_pos.end_pos, 21) - -- assert_eq(actual.org, "bbaumbach") - -- assert_eq(actual.org_pos.start_pos, 23) - -- assert_eq(actual.org_pos.end_pos, 31) - -- assert_eq(actual.repo, "samba.git") - -- assert_eq(actual.repo_pos.start_pos, 33) - -- assert_eq(actual.repo_pos.end_pos, 41) - -- end) - -- it("https://username:password@git.samba.com/samba.git", function() - -- local actual = - -- giturlparser.parse("https://username:password@git.samba.com/samba.git") - -- assert_eq(type(actual), "table") - -- assert_eq(actual.protocol, "https") - -- assert_eq(actual.protocol_pos.start_pos, 1) - -- assert_eq(actual.protocol_pos.end_pos, 5) - -- assert_eq(actual.user, "username") - -- assert_eq(actual.user_pos.start_pos, 9) - -- assert_eq(actual.user_pos.end_pos, 16) - -- assert_eq(actual.password, "password") - -- assert_eq(actual.password_pos.start_pos, 18) - -- assert_eq(actual.password_pos.end_pos, 25) - -- assert_eq(actual.host, "git.samba.com") - -- assert_eq(actual.host_pos.start_pos, 27) - -- assert_eq(actual.host_pos.end_pos, 39) - -- assert_eq(actual.org, nil) - -- assert_eq(actual.org_pos, nil) - -- assert_eq(actual.repo, "samba.git") - -- assert_eq(actual.repo_pos.start_pos, 41) - -- assert_eq(actual.repo_pos.end_pos, 49) - -- end) - -- end) - -- describe("[parse ssh]", function() - -- it("ssh://user@host.xz:org/repo.git", function() - -- local actual = giturlparser.parse("ssh://user@host.xz:org/repo.git") - -- assert_eq(type(actual), "table") - -- assert_eq(actual.protocol, "ssh") - -- assert_eq(actual.protocol_pos.start_pos, 1) - -- assert_eq(actual.protocol_pos.end_pos, 3) - -- assert_eq(actual.user, "user") - -- assert_eq(actual.user_pos.start_pos, 9) - -- assert_eq(actual.user_pos.end_pos, 16) - -- assert_eq(actual.password, nil) - -- assert_eq(actual.password_pos, nil) - -- assert_eq(actual.host, "host.xz") - -- assert_eq(actual.host_pos.start_pos, 27) - -- assert_eq(actual.host_pos.end_pos, 39) - -- assert_eq(actual.org, "org") - -- assert_eq(actual.org_pos.start_pos, 41) - -- assert_eq(actual.org_pos.end_pos, 53) - -- assert_eq(actual.repo, "repo.git") - -- assert_eq(actual.repo_pos.start_pos, 41) - -- assert_eq(actual.repo_pos.end_pos, 49) - -- end) - -- it("ssh://git@github:linrongbin16/giturlparser.lua.git", function() end) - -- end) + describe("[parse http(s)]", function() + it("http://host.xz/path/to/repo.git/", function() + local actual = giturlparser.parse("http://host.xz/path/to/repo.git/") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "http") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 4) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "host.xz") + assert_eq(actual.host_pos.start_pos, 8) + assert_eq(actual.host_pos.end_pos, 14) + assert_eq(actual.org, "path/to") + assert_eq(actual.org_pos.start_pos, 16) + assert_eq(actual.org_pos.end_pos, 22) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 24) + assert_eq(actual.repo_pos.end_pos, 31) + end) + it("http://host.xz/path/to/repo.git", function() + local actual = giturlparser.parse("http://host.xz/path/to/repo.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "http") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 4) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "host.xz") + assert_eq(actual.host_pos.start_pos, 8) + assert_eq(actual.host_pos.end_pos, 14) + assert_eq(actual.org, "path/to") + assert_eq(actual.org_pos.start_pos, 16) + assert_eq(actual.org_pos.end_pos, 22) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 24) + assert_eq(actual.repo_pos.end_pos, 31) + end) + it("https://host.xz/path/to/repo.git/", function() + local actual = giturlparser.parse("https://host.xz/path/to/repo.git/") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "https") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 5) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "host.xz") + assert_eq(actual.host_pos.start_pos, 9) + assert_eq(actual.host_pos.end_pos, 15) + assert_eq(actual.org, "path/to") + assert_eq(actual.org_pos.start_pos, 17) + assert_eq(actual.org_pos.end_pos, 23) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 25) + assert_eq(actual.repo_pos.end_pos, 32) + end) + it("https://host.xz/path/to/repo.git", function() + local actual = giturlparser.parse("https://host.xz/path/to/repo.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "https") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 5) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "host.xz") + assert_eq(actual.host_pos.start_pos, 9) + assert_eq(actual.host_pos.end_pos, 15) + assert_eq(actual.org, "path/to") + assert_eq(actual.org_pos.start_pos, 17) + assert_eq(actual.org_pos.end_pos, 23) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 25) + assert_eq(actual.repo_pos.end_pos, 32) + end) + it("https://git.samba.com/samba.git", function() + local actual = giturlparser.parse("https://git.samba.com/samba.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "https") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 5) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "git.samba.com") + assert_eq(actual.host_pos.start_pos, 9) + assert_eq(actual.host_pos.end_pos, 21) + assert_eq(actual.org, nil) + assert_eq(actual.org_pos, nil) + assert_eq(actual.repo, "samba.git") + assert_eq(actual.repo_pos.start_pos, 23) + assert_eq(actual.repo_pos.end_pos, 31) + end) + it("https://git.samba.com/samba.git/", function() + local actual = giturlparser.parse("https://git.samba.com/samba.git/") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "https") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 5) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "git.samba.com") + assert_eq(actual.host_pos.start_pos, 9) + assert_eq(actual.host_pos.end_pos, 21) + assert_eq(actual.org, nil) + assert_eq(actual.org_pos, nil) + assert_eq(actual.repo, "samba.git") + assert_eq(actual.repo_pos.start_pos, 23) + assert_eq(actual.repo_pos.end_pos, 31) + end) + it("https://git.samba.org/bbaumbach/samba.git", function() + local actual = + giturlparser.parse("https://git.samba.org/bbaumbach/samba.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "https") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 5) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "git.samba.org") + assert_eq(actual.host_pos.start_pos, 9) + assert_eq(actual.host_pos.end_pos, 21) + assert_eq(actual.org, "bbaumbach") + assert_eq(actual.org_pos.start_pos, 23) + assert_eq(actual.org_pos.end_pos, 31) + assert_eq(actual.repo, "samba.git") + assert_eq(actual.repo_pos.start_pos, 33) + assert_eq(actual.repo_pos.end_pos, 41) + end) + it("https://username:password@git.samba.com/samba.git", function() + local actual = + giturlparser.parse("https://username:password@git.samba.com/samba.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "https") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 5) + assert_eq(actual.user, "username") + assert_eq(actual.user_pos.start_pos, 9) + assert_eq(actual.user_pos.end_pos, 16) + assert_eq(actual.password, "password") + assert_eq(actual.password_pos.start_pos, 18) + assert_eq(actual.password_pos.end_pos, 25) + assert_eq(actual.host, "git.samba.com") + assert_eq(actual.host_pos.start_pos, 27) + assert_eq(actual.host_pos.end_pos, 39) + assert_eq(actual.org, nil) + assert_eq(actual.org_pos, nil) + assert_eq(actual.repo, "samba.git") + assert_eq(actual.repo_pos.start_pos, 41) + assert_eq(actual.repo_pos.end_pos, 49) + end) + end) + describe("[parse ssh]", function() + it("ssh://user@host.xz/repo.git", function() + local actual = giturlparser.parse("ssh://user@host.xz/repo.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "ssh") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 3) + assert_eq(actual.user, "user") + assert_eq(actual.user_pos.start_pos, 7) + assert_eq(actual.user_pos.end_pos, 10) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "host.xz") + assert_eq(actual.host_pos.start_pos, 12) + assert_eq(actual.host_pos.end_pos, 18) + assert_eq(actual.org, nil) + assert_eq(actual.org_pos, nil) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 20) + assert_eq(actual.repo_pos.end_pos, 27) + assert_eq(actual.path, "/repo.git") + assert_eq(actual.path_pos.start_pos, 19) + assert_eq(actual.repo_pos.end_pos, 27) + end) + it("ssh://git@github:linrongbin16/giturlparser.lua.git", function() end) + end) end) From f7c7c11ab40eda1ba17ead1ff0bf85d8d1074afb Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 14:20:30 +0800 Subject: [PATCH 45/61] chore --- spec/giturlparser_spec.lua | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 732c663..c449468 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -1042,6 +1042,31 @@ describe("giturlparser", function() assert_eq(actual.path_pos.start_pos, 19) assert_eq(actual.repo_pos.end_pos, 27) end) - it("ssh://git@github:linrongbin16/giturlparser.lua.git", function() end) + it("ssh://git@github.com/linrongbin16/giturlparser.lua.git", function() + local actual = giturlparser.parse( + "ssh://git@github.com/linrongbin16/giturlparser.lua.git" + ) + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "ssh") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 3) + assert_eq(actual.user, "git") + assert_eq(actual.user_pos.start_pos, 7) + assert_eq(actual.user_pos.end_pos, 9) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "github.com") + assert_eq(actual.host_pos.start_pos, 11) + assert_eq(actual.host_pos.end_pos, 20) + assert_eq(actual.org, "linrongbin16") + assert_eq(actual.org_pos.start_pos, 22) + assert_eq(actual.org_pos.end_pos, 33) + assert_eq(actual.repo, "giturlparser.lua.git") + assert_eq(actual.repo_pos.start_pos, 35) + assert_eq(actual.repo_pos.end_pos, 54) + assert_eq(actual.path, "/linrongbin16/giturlparser.lua.git") + assert_eq(actual.path_pos.start_pos, 21) + assert_eq(actual.repo_pos.end_pos, 54) + end) end) end) From f160d623e3077fb3502861e7bf58e1a9ad42921d Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 14:24:43 +0800 Subject: [PATCH 46/61] chore --- spec/giturlparser_spec.lua | 100 +++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index c449468..62e585b 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -1069,4 +1069,104 @@ describe("giturlparser", function() assert_eq(actual.repo_pos.end_pos, 54) end) end) + describe("[parse ssh omit]", function() + it("git:pass@host.xz:repo.git", function() + local actual = giturlparser.parse("git:pass@host.xz:repo.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, nil) + assert_eq(actual.protocol_pos, nil) + assert_eq(actual.user, "git") + assert_eq(actual.user_pos.start_pos, 1) + assert_eq(actual.user_pos.end_pos, 3) + assert_eq(actual.password, "pass") + assert_eq(actual.password_pos.start_pos, 5) + assert_eq(actual.password_pos.end_pos, 8) + assert_eq(actual.host, "host.xz") + assert_eq(actual.host_pos.start_pos, 10) + assert_eq(actual.host_pos.end_pos, 16) + assert_eq(actual.org, nil) + assert_eq(actual.org_pos, nil) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 18) + assert_eq(actual.repo_pos.end_pos, 25) + assert_eq(actual.path, "repo.git") + assert_eq(actual.path_pos.start_pos, 18) + assert_eq(actual.repo_pos.end_pos, 25) + end) + it("git@github.com:linrongbin16/giturlparser.lua.git", function() + local actual = + giturlparser.parse("git@github.com:linrongbin16/giturlparser.lua.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, nil) + assert_eq(actual.protocol_pos, nil) + assert_eq(actual.user, "git") + assert_eq(actual.user_pos.start_pos, 1) + assert_eq(actual.user_pos.end_pos, 3) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "github.com") + assert_eq(actual.host_pos.start_pos, 5) + assert_eq(actual.host_pos.end_pos, 14) + assert_eq(actual.org, "linrongbin16") + assert_eq(actual.org_pos.start_pos, 16) + assert_eq(actual.org_pos.end_pos, 27) + assert_eq(actual.repo, "giturlparser.lua.git") + assert_eq(actual.repo_pos.start_pos, 29) + assert_eq(actual.repo_pos.end_pos, 48) + assert_eq(actual.path, "linrongbin16/giturlparser.lua.git") + assert_eq(actual.path_pos.start_pos, 16) + assert_eq(actual.repo_pos.end_pos, 48) + end) + end) + describe("[parse git]", function() + it("git://user@host.xz/repo.git", function() + local actual = giturlparser.parse("git://user@host.xz/repo.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "git") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 3) + assert_eq(actual.user, "user") + assert_eq(actual.user_pos.start_pos, 7) + assert_eq(actual.user_pos.end_pos, 10) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "host.xz") + assert_eq(actual.host_pos.start_pos, 12) + assert_eq(actual.host_pos.end_pos, 18) + assert_eq(actual.org, nil) + assert_eq(actual.org_pos, nil) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 20) + assert_eq(actual.repo_pos.end_pos, 27) + assert_eq(actual.path, "/repo.git") + assert_eq(actual.path_pos.start_pos, 19) + assert_eq(actual.repo_pos.end_pos, 27) + end) + it("git://git@github.com/linrongbin16/giturlparser.lua.git", function() + local actual = giturlparser.parse( + "git://git@github.com/linrongbin16/giturlparser.lua.git" + ) + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "git") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 3) + assert_eq(actual.user, "git") + assert_eq(actual.user_pos.start_pos, 7) + assert_eq(actual.user_pos.end_pos, 9) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "github.com") + assert_eq(actual.host_pos.start_pos, 11) + assert_eq(actual.host_pos.end_pos, 20) + assert_eq(actual.org, "linrongbin16") + assert_eq(actual.org_pos.start_pos, 22) + assert_eq(actual.org_pos.end_pos, 33) + assert_eq(actual.repo, "giturlparser.lua.git") + assert_eq(actual.repo_pos.start_pos, 35) + assert_eq(actual.repo_pos.end_pos, 54) + assert_eq(actual.path, "/linrongbin16/giturlparser.lua.git") + assert_eq(actual.path_pos.start_pos, 21) + assert_eq(actual.repo_pos.end_pos, 54) + end) + end) end) From 0bb4a1c73a7d7b3072854286659ea3015b02291d Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 14:28:30 +0800 Subject: [PATCH 47/61] chore --- spec/giturlparser_spec.lua | 97 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 62e585b..d29a7c7 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -1168,5 +1168,102 @@ describe("giturlparser", function() assert_eq(actual.path_pos.start_pos, 21) assert_eq(actual.repo_pos.end_pos, 54) end) + it("git://host.xz/repo.git", function() + local actual = giturlparser.parse("git://host.xz/repo.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "git") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 3) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "host.xz") + assert_eq(actual.host_pos.start_pos, 7) + assert_eq(actual.host_pos.end_pos, 13) + assert_eq(actual.org, nil) + assert_eq(actual.org_pos, nil) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 15) + assert_eq(actual.repo_pos.end_pos, 22) + assert_eq(actual.path, "/repo.git") + assert_eq(actual.path_pos.start_pos, 14) + assert_eq(actual.repo_pos.end_pos, 22) + end) + it("git://github.com/linrongbin16/giturlparser.lua.git", function() + local actual = + giturlparser.parse("git://github.com/linrongbin16/giturlparser.lua.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "git") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 3) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "github.com") + assert_eq(actual.host_pos.start_pos, 7) + assert_eq(actual.host_pos.end_pos, 16) + assert_eq(actual.org, "linrongbin16") + assert_eq(actual.org_pos.start_pos, 18) + assert_eq(actual.org_pos.end_pos, 29) + assert_eq(actual.repo, "giturlparser.lua.git") + assert_eq(actual.repo_pos.start_pos, 31) + assert_eq(actual.repo_pos.end_pos, 50) + assert_eq(actual.path, "/linrongbin16/giturlparser.lua.git") + assert_eq(actual.path_pos.start_pos, 17) + assert_eq(actual.repo_pos.end_pos, 50) + end) + end) + describe("[parse file remote]", function() + it("file://user@host.xz/repo.git", function() + local actual = giturlparser.parse("file://user@host.xz/repo.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "file") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 4) + assert_eq(actual.user, "user") + assert_eq(actual.user_pos.start_pos, 8) + assert_eq(actual.user_pos.end_pos, 11) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "host.xz") + assert_eq(actual.host_pos.start_pos, 13) + assert_eq(actual.host_pos.end_pos, 19) + assert_eq(actual.org, nil) + assert_eq(actual.org_pos, nil) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 21) + assert_eq(actual.repo_pos.end_pos, 28) + assert_eq(actual.path, "/repo.git") + assert_eq(actual.path_pos.start_pos, 20) + assert_eq(actual.repo_pos.end_pos, 28) + end) + it("file://git@github.com/linrongbin16/giturlparser.lua.git", function() + local actual = giturlparser.parse( + "file://git@github.com/linrongbin16/giturlparser.lua.git" + ) + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "file") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 4) + assert_eq(actual.user, "git") + assert_eq(actual.user_pos.start_pos, 8) + assert_eq(actual.user_pos.end_pos, 10) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "github.com") + assert_eq(actual.host_pos.start_pos, 12) + assert_eq(actual.host_pos.end_pos, 21) + assert_eq(actual.org, "linrongbin16") + assert_eq(actual.org_pos.start_pos, 23) + assert_eq(actual.org_pos.end_pos, 34) + assert_eq(actual.repo, "giturlparser.lua.git") + assert_eq(actual.repo_pos.start_pos, 36) + assert_eq(actual.repo_pos.end_pos, 55) + assert_eq(actual.path, "/linrongbin16/giturlparser.lua.git") + assert_eq(actual.path_pos.start_pos, 22) + assert_eq(actual.repo_pos.end_pos, 55) + end) end) end) From d3e0c5fa4d4ae6ea3b0bce302f1b373588dd395f Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 14:31:20 +0800 Subject: [PATCH 48/61] cohre --- spec/giturlparser_spec.lua | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index d29a7c7..67ae501 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -1265,5 +1265,52 @@ describe("giturlparser", function() assert_eq(actual.path_pos.start_pos, 22) assert_eq(actual.repo_pos.end_pos, 55) end) + it("file://host.xz/repo.git", function() + local actual = giturlparser.parse("file://host.xz/repo.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "file") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 4) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "host.xz") + assert_eq(actual.host_pos.start_pos, 8) + assert_eq(actual.host_pos.end_pos, 14) + assert_eq(actual.org, nil) + assert_eq(actual.org_pos, nil) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 16) + assert_eq(actual.repo_pos.end_pos, 23) + assert_eq(actual.path, "/repo.git") + assert_eq(actual.path_pos.start_pos, 15) + assert_eq(actual.repo_pos.end_pos, 23) + end) + it("file://github.com/linrongbin16/giturlparser.lua.git", function() + local actual = giturlparser.parse( + "file://github.com/linrongbin16/giturlparser.lua.git" + ) + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "file") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 4) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, "github.com") + assert_eq(actual.host_pos.start_pos, 8) + assert_eq(actual.host_pos.end_pos, 17) + assert_eq(actual.org, "linrongbin16") + assert_eq(actual.org_pos.start_pos, 19) + assert_eq(actual.org_pos.end_pos, 30) + assert_eq(actual.repo, "giturlparser.lua.git") + assert_eq(actual.repo_pos.start_pos, 32) + assert_eq(actual.repo_pos.end_pos, 51) + assert_eq(actual.path, "/linrongbin16/giturlparser.lua.git") + assert_eq(actual.path_pos.start_pos, 18) + assert_eq(actual.repo_pos.end_pos, 51) + end) end) end) From f9db5b235f2bbc3c32e8371ec68a00473c4f5028 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 14:36:52 +0800 Subject: [PATCH 49/61] chore --- spec/giturlparser_spec.lua | 60 ++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 67ae501..b0771f4 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -875,6 +875,9 @@ describe("giturlparser", function() assert_eq(actual.repo, "repo.git") assert_eq(actual.repo_pos.start_pos, 24) assert_eq(actual.repo_pos.end_pos, 31) + assert_eq(actual.path, "/path/to/repo.git") + assert_eq(actual.path_pos.start_pos, 15) + assert_eq(actual.path_pos.end_pos, 31) end) it("http://host.xz/path/to/repo.git", function() local actual = giturlparser.parse("http://host.xz/path/to/repo.git") @@ -895,6 +898,9 @@ describe("giturlparser", function() assert_eq(actual.repo, "repo.git") assert_eq(actual.repo_pos.start_pos, 24) assert_eq(actual.repo_pos.end_pos, 31) + assert_eq(actual.path, "/path/to/repo.git") + assert_eq(actual.path_pos.start_pos, 15) + assert_eq(actual.path_pos.end_pos, 31) end) it("https://host.xz/path/to/repo.git/", function() local actual = giturlparser.parse("https://host.xz/path/to/repo.git/") @@ -1016,6 +1022,9 @@ describe("giturlparser", function() assert_eq(actual.repo, "samba.git") assert_eq(actual.repo_pos.start_pos, 41) assert_eq(actual.repo_pos.end_pos, 49) + assert_eq(actual.path, "/samba.git") + assert_eq(actual.path_pos.start_pos, 40) + assert_eq(actual.path_pos.end_pos, 49) end) end) describe("[parse ssh]", function() @@ -1040,7 +1049,7 @@ describe("giturlparser", function() assert_eq(actual.repo_pos.end_pos, 27) assert_eq(actual.path, "/repo.git") assert_eq(actual.path_pos.start_pos, 19) - assert_eq(actual.repo_pos.end_pos, 27) + assert_eq(actual.path_pos.end_pos, 27) end) it("ssh://git@github.com/linrongbin16/giturlparser.lua.git", function() local actual = giturlparser.parse( @@ -1066,7 +1075,7 @@ describe("giturlparser", function() assert_eq(actual.repo_pos.end_pos, 54) assert_eq(actual.path, "/linrongbin16/giturlparser.lua.git") assert_eq(actual.path_pos.start_pos, 21) - assert_eq(actual.repo_pos.end_pos, 54) + assert_eq(actual.path_pos.end_pos, 54) end) end) describe("[parse ssh omit]", function() @@ -1091,7 +1100,7 @@ describe("giturlparser", function() assert_eq(actual.repo_pos.end_pos, 25) assert_eq(actual.path, "repo.git") assert_eq(actual.path_pos.start_pos, 18) - assert_eq(actual.repo_pos.end_pos, 25) + assert_eq(actual.path_pos.end_pos, 25) end) it("git@github.com:linrongbin16/giturlparser.lua.git", function() local actual = @@ -1115,7 +1124,7 @@ describe("giturlparser", function() assert_eq(actual.repo_pos.end_pos, 48) assert_eq(actual.path, "linrongbin16/giturlparser.lua.git") assert_eq(actual.path_pos.start_pos, 16) - assert_eq(actual.repo_pos.end_pos, 48) + assert_eq(actual.path_pos.end_pos, 48) end) end) describe("[parse git]", function() @@ -1140,7 +1149,7 @@ describe("giturlparser", function() assert_eq(actual.repo_pos.end_pos, 27) assert_eq(actual.path, "/repo.git") assert_eq(actual.path_pos.start_pos, 19) - assert_eq(actual.repo_pos.end_pos, 27) + assert_eq(actual.path_pos.end_pos, 27) end) it("git://git@github.com/linrongbin16/giturlparser.lua.git", function() local actual = giturlparser.parse( @@ -1166,7 +1175,7 @@ describe("giturlparser", function() assert_eq(actual.repo_pos.end_pos, 54) assert_eq(actual.path, "/linrongbin16/giturlparser.lua.git") assert_eq(actual.path_pos.start_pos, 21) - assert_eq(actual.repo_pos.end_pos, 54) + assert_eq(actual.path_pos.end_pos, 54) end) it("git://host.xz/repo.git", function() local actual = giturlparser.parse("git://host.xz/repo.git") @@ -1188,7 +1197,7 @@ describe("giturlparser", function() assert_eq(actual.repo_pos.end_pos, 22) assert_eq(actual.path, "/repo.git") assert_eq(actual.path_pos.start_pos, 14) - assert_eq(actual.repo_pos.end_pos, 22) + assert_eq(actual.path_pos.end_pos, 22) end) it("git://github.com/linrongbin16/giturlparser.lua.git", function() local actual = @@ -1212,7 +1221,7 @@ describe("giturlparser", function() assert_eq(actual.repo_pos.end_pos, 50) assert_eq(actual.path, "/linrongbin16/giturlparser.lua.git") assert_eq(actual.path_pos.start_pos, 17) - assert_eq(actual.repo_pos.end_pos, 50) + assert_eq(actual.path_pos.end_pos, 50) end) end) describe("[parse file remote]", function() @@ -1237,7 +1246,7 @@ describe("giturlparser", function() assert_eq(actual.repo_pos.end_pos, 28) assert_eq(actual.path, "/repo.git") assert_eq(actual.path_pos.start_pos, 20) - assert_eq(actual.repo_pos.end_pos, 28) + assert_eq(actual.path_pos.end_pos, 28) end) it("file://git@github.com/linrongbin16/giturlparser.lua.git", function() local actual = giturlparser.parse( @@ -1263,7 +1272,7 @@ describe("giturlparser", function() assert_eq(actual.repo_pos.end_pos, 55) assert_eq(actual.path, "/linrongbin16/giturlparser.lua.git") assert_eq(actual.path_pos.start_pos, 22) - assert_eq(actual.repo_pos.end_pos, 55) + assert_eq(actual.path_pos.end_pos, 55) end) it("file://host.xz/repo.git", function() local actual = giturlparser.parse("file://host.xz/repo.git") @@ -1285,7 +1294,7 @@ describe("giturlparser", function() assert_eq(actual.repo_pos.end_pos, 23) assert_eq(actual.path, "/repo.git") assert_eq(actual.path_pos.start_pos, 15) - assert_eq(actual.repo_pos.end_pos, 23) + assert_eq(actual.path_pos.end_pos, 23) end) it("file://github.com/linrongbin16/giturlparser.lua.git", function() local actual = giturlparser.parse( @@ -1310,7 +1319,34 @@ describe("giturlparser", function() assert_eq(actual.repo_pos.end_pos, 51) assert_eq(actual.path, "/linrongbin16/giturlparser.lua.git") assert_eq(actual.path_pos.start_pos, 18) - assert_eq(actual.repo_pos.end_pos, 51) + assert_eq(actual.path_pos.end_pos, 51) + end) + end) + describe("[parse file local]", function() + it("file:///path/to/repo.git", function() + local actual = giturlparser.parse("file:///path/to/repo.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "file") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 4) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, nil) + assert_eq(actual.host_pos, nil) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) + assert_eq(actual.org, "path/to") + assert_eq(actual.org_pos.start_pos, 9) + assert_eq(actual.org_pos.end_pos, 15) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 17) + assert_eq(actual.repo_pos.end_pos, 24) + assert_eq(actual.path, "/path/to/repo.git") + assert_eq(actual.path_pos.start_pos, 8) + assert_eq(actual.path_pos.end_pos, 24) end) + it("file:///repo.git", function() end) end) end) From c0fd8dda3976cdb8d97bee6f042f28273ba5b7a3 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 14:46:14 +0800 Subject: [PATCH 50/61] chore --- .github/workflows/ci.yml | 1 - spec/giturlparser_spec.lua | 118 ++++++++++++++++++++++++++++++++++++- src/giturlparser.lua | 15 +++-- 3 files changed, 126 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9bdc5d2..335caea 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,6 @@ jobs: ls -lha luarocks install luacov luarocks install cluacov - luarocks install inspect luarocks install busted busted --coverage . # - uses: lunarmodules/busted@v2 diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index b0771f4..2b8449d 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -875,9 +875,9 @@ describe("giturlparser", function() assert_eq(actual.repo, "repo.git") assert_eq(actual.repo_pos.start_pos, 24) assert_eq(actual.repo_pos.end_pos, 31) - assert_eq(actual.path, "/path/to/repo.git") + assert_eq(actual.path, "/path/to/repo.git/") assert_eq(actual.path_pos.start_pos, 15) - assert_eq(actual.path_pos.end_pos, 31) + assert_eq(actual.path_pos.end_pos, 32) end) it("http://host.xz/path/to/repo.git", function() local actual = giturlparser.parse("http://host.xz/path/to/repo.git") @@ -1347,6 +1347,118 @@ describe("giturlparser", function() assert_eq(actual.path_pos.start_pos, 8) assert_eq(actual.path_pos.end_pos, 24) end) - it("file:///repo.git", function() end) + it("file:///repo.git", function() + local actual = giturlparser.parse("file:///repo.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, "file") + assert_eq(actual.protocol_pos.start_pos, 1) + assert_eq(actual.protocol_pos.end_pos, 4) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, nil) + assert_eq(actual.host_pos, nil) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) + assert_eq(actual.org, nil) + assert_eq(actual.org_pos, nil) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 9) + assert_eq(actual.repo_pos.end_pos, 16) + assert_eq(actual.path, "/repo.git") + assert_eq(actual.path_pos.start_pos, 8) + assert_eq(actual.path_pos.end_pos, 16) + end) + end) + describe("[parse local path]", function() + it("repo.git", function() + local actual = giturlparser.parse("repo.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, nil) + assert_eq(actual.protocol_pos, nil) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, nil) + assert_eq(actual.host_pos, nil) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) + assert_eq(actual.org, nil) + assert_eq(actual.org_pos, nil) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 1) + assert_eq(actual.repo_pos.end_pos, 8) + assert_eq(actual.path, "repo.git") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 8) + end) + it("repo.git/", function() + local actual = giturlparser.parse("repo.git/") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, nil) + assert_eq(actual.protocol_pos, nil) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, nil) + assert_eq(actual.host_pos, nil) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) + assert_eq(actual.org, nil) + assert_eq(actual.org_pos, nil) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 1) + assert_eq(actual.repo_pos.end_pos, 8) + assert_eq(actual.path, "repo.git/") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 9) + end) + it("/repo.git", function() + local actual = giturlparser.parse("/repo.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, nil) + assert_eq(actual.protocol_pos, nil) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, nil) + assert_eq(actual.host_pos, nil) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) + assert_eq(actual.org, nil) + assert_eq(actual.org_pos, nil) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 2) + assert_eq(actual.repo_pos.end_pos, 9) + assert_eq(actual.path, "/repo.git") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 9) + end) + it("/repo.git/", function() + local actual = giturlparser.parse("/repo.git/") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, nil) + assert_eq(actual.protocol_pos, nil) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, nil) + assert_eq(actual.host_pos, nil) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) + assert_eq(actual.org, nil) + assert_eq(actual.org_pos, nil) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 2) + assert_eq(actual.repo_pos.end_pos, 9) + assert_eq(actual.path, "/repo.git/") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 10) + end) end) end) diff --git a/src/giturlparser.lua b/src/giturlparser.lua index 6ffa06f..639dd81 100644 --- a/src/giturlparser.lua +++ b/src/giturlparser.lua @@ -147,6 +147,8 @@ end M._make_path = function(p, start) assert(type(start) == "number") + -- local inspect = require("inspect") + local endswith_slash = M._endswith(p, "/") local org = nil @@ -169,6 +171,15 @@ M._make_path = function(p, start) -- no slash found, only 1 path component repo, repo_pos = M._make(p, start, plen) end + + -- print( + -- string.format( + -- "|_make_path| p:%s, start:%s, plen:%s\n", + -- inspect(p), + -- inspect(start), + -- inspect(plen) + -- ) + -- ) path, path_pos = M._make(p, start, plen) if repo and repo_pos then @@ -379,10 +390,6 @@ M.parse = function(url) return nil, "empty string" end - if M._endswith(url, "/") then - url = string.sub(url, 1, #url - 1) - end - -- find first '://', the end position of protocol local protocol_delimiter_pos = M._find(url, "://") if From 8b656f469d6ade4e4cc514b6b067da86637b1463 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 14:48:09 +0800 Subject: [PATCH 51/61] chore --- spec/giturlparser_spec.lua | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index 2b8449d..a0acfa9 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -1460,5 +1460,51 @@ describe("giturlparser", function() assert_eq(actual.path_pos.start_pos, 1) assert_eq(actual.path_pos.end_pos, 10) end) + it("./repo.git", function() + local actual = giturlparser.parse("./repo.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, nil) + assert_eq(actual.protocol_pos, nil) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, nil) + assert_eq(actual.host_pos, nil) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) + assert_eq(actual.org, ".") + assert_eq(actual.org_pos.start_pos, 1) + assert_eq(actual.org_pos.end_pos, 1) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 3) + assert_eq(actual.repo_pos.end_pos, 10) + assert_eq(actual.path, "./repo.git") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 10) + end) + it("../repo.git/", function() + local actual = giturlparser.parse("../repo.git/") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, nil) + assert_eq(actual.protocol_pos, nil) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, nil) + assert_eq(actual.host_pos, nil) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) + assert_eq(actual.org, "..") + assert_eq(actual.org_pos.start_pos, 1) + assert_eq(actual.org_pos.end_pos, 2) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 4) + assert_eq(actual.repo_pos.end_pos, 11) + assert_eq(actual.path, "../repo.git/") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 12) + end) end) end) From d3649d2b6d693e98af19b9ed99d59765299b6210 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 14:50:38 +0800 Subject: [PATCH 52/61] chore --- spec/giturlparser_spec.lua | 92 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index a0acfa9..c0b95fd 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -1506,5 +1506,97 @@ describe("giturlparser", function() assert_eq(actual.path_pos.start_pos, 1) assert_eq(actual.path_pos.end_pos, 12) end) + it("~/path/to/repo.git", function() + local actual = giturlparser.parse("~/path/to/repo.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, nil) + assert_eq(actual.protocol_pos, nil) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, nil) + assert_eq(actual.host_pos, nil) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) + assert_eq(actual.org, "~/path/to") + assert_eq(actual.org_pos.start_pos, 1) + assert_eq(actual.org_pos.end_pos, 9) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 11) + assert_eq(actual.repo_pos.end_pos, 18) + assert_eq(actual.path, "~/path/to/repo.git") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 18) + end) + it("~/path/to/repo.git/", function() + local actual = giturlparser.parse("~/path/to/repo.git/") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, nil) + assert_eq(actual.protocol_pos, nil) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, nil) + assert_eq(actual.host_pos, nil) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) + assert_eq(actual.org, "~/path/to") + assert_eq(actual.org_pos.start_pos, 1) + assert_eq(actual.org_pos.end_pos, 9) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 11) + assert_eq(actual.repo_pos.end_pos, 18) + assert_eq(actual.path, "~/path/to/repo.git/") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 19) + end) + it("/absolute/path/to/repo.git", function() + local actual = giturlparser.parse("/absolute/path/to/repo.git") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, nil) + assert_eq(actual.protocol_pos, nil) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, nil) + assert_eq(actual.host_pos, nil) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) + assert_eq(actual.org, "absolute/path/to") + assert_eq(actual.org_pos.start_pos, 2) + assert_eq(actual.org_pos.end_pos, 17) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 19) + assert_eq(actual.repo_pos.end_pos, 26) + assert_eq(actual.path, "/absolute/path/to/repo.git") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 26) + end) + it("/absolute/path/to/repo.git/", function() + local actual = giturlparser.parse("/absolute/path/to/repo.git/") + assert_eq(type(actual), "table") + assert_eq(actual.protocol, nil) + assert_eq(actual.protocol_pos, nil) + assert_eq(actual.user, nil) + assert_eq(actual.user_pos, nil) + assert_eq(actual.password, nil) + assert_eq(actual.password_pos, nil) + assert_eq(actual.host, nil) + assert_eq(actual.host_pos, nil) + assert_eq(actual.port, nil) + assert_eq(actual.port_pos, nil) + assert_eq(actual.org, "absolute/path/to") + assert_eq(actual.org_pos.start_pos, 2) + assert_eq(actual.org_pos.end_pos, 17) + assert_eq(actual.repo, "repo.git") + assert_eq(actual.repo_pos.start_pos, 19) + assert_eq(actual.repo_pos.end_pos, 26) + assert_eq(actual.path, "/absolute/path/to/repo.git/") + assert_eq(actual.path_pos.start_pos, 1) + assert_eq(actual.path_pos.end_pos, 27) + end) end) end) From c32e741bf40fa7f5edb9c153dad4293d9cc4263c Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 14:51:12 +0800 Subject: [PATCH 53/61] chore --- spec/giturlparser_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/giturlparser_spec.lua b/spec/giturlparser_spec.lua index c0b95fd..d096cee 100644 --- a/spec/giturlparser_spec.lua +++ b/spec/giturlparser_spec.lua @@ -5,7 +5,7 @@ describe("giturlparser", function() before_each(function() end) - local inspect = require("inspect") + -- local inspect = require("inspect") local giturlparser = require("giturlparser") describe("[_make_path]", function() it("repo.git", function() From aeed00131bb0060cb25fc227d3e31b2481fdad58 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 14:52:55 +0800 Subject: [PATCH 54/61] chore --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8771c7c..1859521 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Pure Lua implemented git URL parsing library, e.g. the output of git remot - [Requirements](#requirements) - [Features](#features) - [Install](#install) -- [Documents](#documents) +- [API](#api) - [Types](#types) - [`giturlparser.GitUrlPos`](#giturlparsergiturlpos) - [`giturlparser.GitUrlInfo`](#giturlparsergiturlinfo) @@ -34,12 +34,6 @@ Pure Lua implemented git URL parsing library, e.g. the output of git remot - Single file & zero dependency. - Full [Git Protocols](https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols) support. -## Install - -`luarocks install giturlparser` - -## Documents - The git url syntax contains many use cases: 1. `{protocol}://[[{user}[:{password}]@]{host}[:{port}]]/[{org}/]*{repo}` @@ -73,6 +67,12 @@ All of above can be written by: 1. `[{protocol}://][[{user}[:{password}]@]host[:{port}]]/[{org}/]*{repo}` 2. `[~][/{org}]*/{repo}` +## Install + +`luarocks install giturlparser` + +## API + ### Types #### `giturlparser.GitUrlPos` From 363e97a619ca2ac2981dd20ef0ea0a7e39a8f1d5 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 14:54:29 +0800 Subject: [PATCH 55/61] chore --- README.md | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 1859521..42bff44 100644 --- a/README.md +++ b/README.md @@ -31,12 +31,21 @@ Pure Lua implemented git URL parsing library, e.g. the output of git remot ## Features -- Single file & zero dependency. -- Full [Git Protocols](https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols) support. +Single file & zero dependency. -The git url syntax contains many use cases: +Full [Git Protocols](https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols) support: -1. `{protocol}://[[{user}[:{password}]@]{host}[:{port}]]/[{org}/]*{repo}` +> [!NOTE] +> +> In below code snippets: +> +> 1. The `{}` contains parsed components returned from [`giturlparser.GitUrlInfo`](#giturlparsergiturlinfo). +> 2. The `[]` contains optional (0 or 1) component. +> 3. The `[]*` contains zero or more (≥ 0) component. +> 4. The `[]+` contains 1 or more (≥ 1) component. +> 5. The `|` inside `[]` is **_or_** operator. + +1. `{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` @@ -44,24 +53,16 @@ The git url syntax contains many use cases: - `file:///repo.git` - `file://user:passwd@host.xyz:port/path/to/the/repo.git` - `file://~/home/to/the/repo.git` -2. `[{user}[:{password}]@]{host}:[{org}/]*{repo}` +2. `[{user}[:{password}]@]{host}:[{org}/]*{repo}`, for example: - `git@host.xyz:repo.git` - `user:passwd@host.xyz:path/to/the/repo.git` -3. `[~][/{org}]*/{repo}` +3. `[.|..|~][/{org}]*/{repo}`, for example: - `repo.git` - `./repo.git` - `../path/to/the/repo.git` - `~/home/to/the/repo.git` - `/usr/home/to/the/repo.git` -> [!NOTE] -> -> 1. The `{}` contains parsed components returned from [`giturlparser.GitUrlInfo`](#giturlparsergiturlinfo). -> 2. The `[]` contains optional (0 or 1) component. -> 3. The `[]*` contains zero or more (≥ 0) component. -> 4. The `[]+` contains 1 or more (≥ 1) component. -> 5. The `|` inside `[]` is **_or_** operator. - All of above can be written by: 1. `[{protocol}://][[{user}[:{password}]@]host[:{port}]]/[{org}/]*{repo}` From 757ba8d91c6622e702c1e05c60787277ee036e93 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 14:54:45 +0800 Subject: [PATCH 56/61] chore --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 42bff44..96c338a 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ Full [Git Protocols](https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protoc All of above can be written by: 1. `[{protocol}://][[{user}[:{password}]@]host[:{port}]]/[{org}/]*{repo}` -2. `[~][/{org}]*/{repo}` +2. `[.|..|~][/{org}]*/{repo}` ## Install From 2517e4085be0096941d48afc39bef6547888ede3 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 14:55:46 +0800 Subject: [PATCH 57/61] chore --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 96c338a..6e39d8d 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Full [Git Protocols](https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protoc > [!NOTE] > -> In below code snippets: +> Below code snippets are (just easier to help explain the parsing algorithm) written in a regex-like syntax: > > 1. The `{}` contains parsed components returned from [`giturlparser.GitUrlInfo`](#giturlparsergiturlinfo). > 2. The `[]` contains optional (0 or 1) component. From f39444f93e49943f51f515b0b08f08f5c9209d87 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 14:56:01 +0800 Subject: [PATCH 58/61] chore --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6e39d8d..322cd4a 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Full [Git Protocols](https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protoc > [!NOTE] > -> Below code snippets are (just easier to help explain the parsing algorithm) written in a regex-like syntax: +> Below pattern are (just easier to help explain the parsing algorithm) written in a regex-like syntax: > > 1. The `{}` contains parsed components returned from [`giturlparser.GitUrlInfo`](#giturlparsergiturlinfo). > 2. The `[]` contains optional (0 or 1) component. From a05d684221ba52eec2faff0a40c55c0dbd1772e3 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 15:03:11 +0800 Subject: [PATCH 59/61] chore --- README.md | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 322cd4a..4f5b3f5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ + + # giturlparser.lua

@@ -99,20 +101,17 @@ Parsed information. It contains below fields: -- `protocol`: Protocol, e.g. `http` (`http://`), `https` (`https://`), `ssh` (`ssh://`), `file` (`file://`). -- `protocol_pos`: Protocol position. -- `user`: User name, e.g. `username` in `ssh://username@githost.com`. -- `user_pos`: User name position. -- `password`: Password, e.g. `password` in `ssh://username:password@githost.com`. -- `password_pos`: Password position. -- `host`: Host name, e.g. `githost.com` in `ssh://githost.com`. -- `host_pos`: Host name position. -- `path`: All the left parts after `host/`, e.g. `linrongbin16/giturlparser.lua.git` in `https://github.com/linrongbin16/giturlparser.lua.git`. -- `path_pos`: Path position. -- `repo`: Repository (the left parts after the last slash `/`, if exists), e.g. `giturlparser.lua.git` in `https://github.com/linrongbin16/giturlparser.lua.git`. -- `repo_pos`: Repository position. -- `org`: , Organization (the parts after `host/` and before the last slash `/`, if exists), e.g. `linrongbin16` in `https://github.com/linrongbin16/giturlparser.lua.git`. -- `org_pos`: Organization position. +- `protocol`/`protocol_pos`: Protocol, e.g. `http` in `http://github.com`, and its position. +- `user`/`user_pos`: User name, e.g. `username` in `ssh://username@githost.com`, and its position. +- `password`/`password_pos`: Password, e.g. `password` in `ssh://username:password@githost.com`, and its position. +- `host`/`host_pos`: Host name, e.g. `githost.com` in `ssh://githost.com`, and its position. +- `port`/`port_pos`: Port, e.g. `12345` in `ssh://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` in `https://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` in `https://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` in `https://github.com/linrongbin16/giturlparser.lua.git`, and its position. > [!NOTE] > From ddbb380660cd683ccce6aed906c50f47c2ac4fb7 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 15:04:11 +0800 Subject: [PATCH 60/61] chore --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4f5b3f5..62ff410 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,8 @@ There're 2 more sugar fields: > [!NOTE] > -> The `{path}` component is equivalent to `{org}/{repo}`. +> - The `{path}` component is (almost) equivalent to `/{org}/{repo}`. +> - The `{org}` and `{repo}` component are trimmed from around slashes if there's any. > [!IMPORTANT] > From 47611d459a0a84e7f7c96b1e94b341986686a652 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Mon, 8 Jan 2024 15:04:44 +0800 Subject: [PATCH 61/61] chore --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index 62ff410..11b3837 100644 --- a/README.md +++ b/README.md @@ -117,10 +117,7 @@ There're 2 more sugar fields: > > - The `{path}` component is (almost) equivalent to `/{org}/{repo}`. > - The `{org}` and `{repo}` component are trimmed from around slashes if there's any. - -> [!IMPORTANT] -> -> If there's only 1 slash, the `org` component is omitted. +> - If there's only 1 slash, the `org` component is omitted. ### Functions