Skip to content

Commit

Permalink
perf(gitweb): correct parsing remote url! (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
linrongbin16 authored Nov 26, 2023
1 parent 8530e1a commit 382b31e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 39 deletions.
27 changes: 9 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,9 @@ require('gitlinker').setup({
-- example:
-- main repo: https://git.samba.org/?p=samba.git;a=blob;f=wscript;hb=83e8971c0f1c1db8c3574f83107190ac1ac23db0#l6
-- dev repo: https://git.samba.org/?p=bbaumbach/samba.git;a=blob;f=wscript;hb=8de348e9d025d336a7985a9025fe08b7096c0394#l7
["^git%.samba%.org"] = "https://git.samba.org/?"
.. "p={string.len(_A.REPO) == 0 and _A.USER or (_A.USER .. '/' .. _A.REPO .. '.git')};a=blob;" -- 'p=samba.git' or 'p=bbaumbach/samba.git'
["^git%.samba%.org"] = "https://git.samba.org/?p="
.. "{string.len(_A.USER) > 0 and (_A.USER .. '/') or ''}" -- 'p=samba.git' or 'p=bbaumbach/samba.git'
.. "{_A.REPO .. '.git'};a=blob;"
.. "f={_A.FILE};"
.. "hb={_A.REV}"
.. "#l{_A.LSTART}",
Expand Down Expand Up @@ -406,34 +407,24 @@ The available variables are the same with the `lk` parameter passing to hook fun

For [GitWeb](https://git-scm.com/book/en/v2/Git-on-the-Server-GitWeb), there're two types of urls: the main repository and the user's dev repository. For example on [git.samba.org](https://git.samba.org/):

- Main repo: https://git.samba.org/?p=samba.git;a=blob;f=wscript;hb=83e8971c0f1c1db8c3574f83107190ac1ac23db0#l7.
- User's dev repo: https://git.samba.org/?p=bbaumbach/samba.git;a=blob;f=wscript;hb=8de348e9d025d336a7985a9025fe08b7096c0394#l7.

Take a closer look at them:

```bash
# main repo
https://git.samba.org/samba.git (`git remote get-url origin`)
https://git.samba.org/?p=samba.git;a=blob;f=wscript;hb=83e8971c0f1c1db8c3574f83107190ac1ac23db0#l7
| | | | | |
protocol host repo file rev line number

# user dev repo
# user's dev repo
https://git.samba.org/bbaumbach/samba.git (`git remote get-url origin`)
https://git.samba.org/?p=bbaumbach/samba.git;a=blob;f=wscript;hb=8de348e9d025d336a7985a9025fe08b7096c0394#l7
| | | | | | |
protocol host user repo file rev line number
```

> Also see difference between `h` and `hb` in gitweb url:
>
> 1. https://stackoverflow.com/q/14444593/4438921.
> 2. https://stackoverflow.com/a/14444767/4438921.
The difference is: the main repo doesn't have the `user` component, it's just `https://git.samba.org/?p=samba.git`. To support such case, `user` and `repo` components have a little bit different:

- `lk.user` (`_A.USER`): the value is `samba.git`.
- `lk.repo` (`_A.REPO`): the value is `` (empty string).
The difference is: the main repo doesn't have the `user` component, it's just `https://git.samba.org/?p=samba.git`. To support such case, `user` and `repo` components have a little bit different when facing the main repo:

> Actually it should be more likely the `lk.user` is empty string, and `lk.repo` is `samba.git`, but I'm just parsing it in this way.
- `lk.user` (`_A.USER`): the value is `` (empty string).
- `lk.repo` (`_A.REPO`): the value is `samba.git`.

### More Router Types

Expand Down
15 changes: 6 additions & 9 deletions lua/gitlinker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ local Defaults = {
-- example:
-- main repo: https://git.samba.org/?p=samba.git;a=blob;f=wscript;hb=83e8971c0f1c1db8c3574f83107190ac1ac23db0#l6
-- dev repo: https://git.samba.org/?p=bbaumbach/samba.git;a=blob;f=wscript;hb=8de348e9d025d336a7985a9025fe08b7096c0394#l7
["^git%.samba%.org"] = "https://git.samba.org/?"
.. "p={string.len(_A.REPO) == 0 and _A.USER or (_A.USER .. '/' .. _A.REPO .. '.git')};a=blob;" -- 'p=samba.git' or 'p=bbaumbach/samba.git'
["^git%.samba%.org"] = "https://git.samba.org/?p="
.. "{string.len(_A.USER) > 0 and (_A.USER .. '/') or ''}" -- 'p=samba.git;' or 'p=bbaumbach/samba.git;'
.. "{_A.REPO .. '.git'};a=blob;"
.. "f={_A.FILE};"
.. "hb={_A.REV}"
.. "#l{_A.LSTART}",
Expand Down Expand Up @@ -205,17 +206,13 @@ local function _url_template_engine(lk, template)
if exp.plain then
table.insert(results, exp.body)
else
local repo = lk.repo or ""
if type(repo) == "string" and string.len(repo) > 0 then
if utils.string_endswith(repo, ".git") then
repo = repo:sub(1, #repo - 4)
end
end
local evaluated = vim.fn.luaeval(exp.body, {
PROTOCOL = lk.protocol,
HOST = lk.host,
USER = lk.user,
REPO = repo,
REPO = utils.string_endswith(lk.repo, ".git")
and lk.repo:sub(1, #lk.repo - 4)
or lk.repo,
REV = lk.rev,
FILE = lk.file,
LSTART = lk.lstart,
Expand Down
7 changes: 6 additions & 1 deletion lua/gitlinker/linker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ local function _parse_remote_url(remote_url)
user = remote_url:sub(host_end_pos + 1, user_end_pos - 1)
repo = remote_url:sub(user_end_pos + 1)
else
user = remote_url:sub(host_end_pos + 1)
-- if no slash '/', then don't have 'user', but only 'repo'
-- example:
-- * main repo: https://git.samba.org/?p=samba.git
-- * user dev repo: https://git.samba.org/?p=bbaumbach/samba.git
repo = remote_url:sub(host_end_pos + 1)
user = ""
end
local result = {
protocol = protocol,
Expand Down
12 changes: 5 additions & 7 deletions lua/gitlinker/routers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,12 @@ end
--- @return string
local function samba_browse(lk)
logger.debug("|routers.samba_browse| lk:%s", vim.inspect(lk))
local builder = "https://git.samba.org/?"
-- user/repo
local builder = "https://git.samba.org/?p="
-- user
builder = builder
.. (
(lk.repo == nil or string.len(lk.repo) == 0)
and (string.format("p=%s;a=blob;", lk.user))
or (string.format("p=%s/%s;a=blob;", lk.user, lk.repo))
)
.. (string.len(lk.user) > 0 and string.format("%s/", lk.user) or "")
-- user
builder = builder .. string.format("%s;a=blob;", lk.repo)
-- file: 'wscript'
builder = builder .. string.format("f=%s;", lk.file)
-- rev
Expand Down
8 changes: 4 additions & 4 deletions test/gitlinker_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ describe("gitlinker", function()
protocol = "git@",
host = "git.samba.org",
host_delimiter = ":",
user = "samba.git",
repo = nil,
user = "",
repo = "samba.git",
rev = "399b1d05473c711fc5592a6ffc724e231c403486",
file = "wscript",
file_changed = false,
Expand All @@ -67,8 +67,8 @@ describe("gitlinker", function()
protocol = "https://",
host = "git.samba.org",
host_delimiter = "/",
user = "samba.git",
repo = "",
user = "",
repo = "samba.git",
rev = "399b1d05473c711fc5592a6ffc724e231c403486",
file = "wscript",
file_changed = false,
Expand Down

0 comments on commit 382b31e

Please sign in to comment.