Skip to content

Commit

Permalink
fix: 'plenary.path' on Windows (#54)
Browse files Browse the repository at this point in the history
* refactor: add log

* fix: plenary.path on Windows
  • Loading branch information
linrongbin16 committed Jul 15, 2023
1 parent e81acd0 commit 565f186
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 39 deletions.
57 changes: 47 additions & 10 deletions lua/gitlinker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,22 @@ local function make_link_data()
git.result_print_err(root_result, "not in a git repository")
return nil
end
logger.debug(
"|make_link_data| root_result(%s):%s",
vim.inspect(type(root_result)),
vim.inspect(root_result)
)

--- @type string|nil
local remote = git.get_branch_remote()
if not remote then
return nil
end
logger.debug(
"|make_link_data| remote(%s):%s",
vim.inspect(type(remote)),
vim.inspect(remote)
)

--- @type JobResult
local remote_url_result = git.get_remote_url(remote)
Expand All @@ -188,20 +198,32 @@ local function make_link_data()
)
return nil
end
logger.debug(
"|make_link_data| remote_url_result(%s):%s",
vim.inspect(type(remote_url_result)),
vim.inspect(remote_url_result)
)

--- @type string|nil
local rev = git.get_closest_remote_compatible_rev(remote)
if not rev then
return nil
end
logger.debug(
"|make_link_data| rev(%s):%s",
vim.inspect(type(rev)),
vim.inspect(rev)
)

local root = tostring(path:new(root_result.stdout[1]))
local buf_path_on_root = util.relative_path(root)
-- logger.debug(
-- "[make_link_data] buf_path_on_root: %s, git_root: %s",
-- vim.inspect(buf_path_on_root),
-- vim.inspect(root)
-- )
logger.debug(
"|make_link_data| root(%s):%s, buf_path_on_root(%s):%s",
vim.inspect(type(root)),
vim.inspect(root),
vim.inspect(type(buf_path_on_root)),
vim.inspect(buf_path_on_root)
)

--- @type JobResult
local file_in_rev_result = git.is_file_in_rev(buf_path_on_root, rev)
Expand All @@ -212,18 +234,33 @@ local function make_link_data()
)
return nil
end
logger.debug(
"|make_link_data| file_in_rev_result(%s):%s",
vim.inspect(type(file_in_rev_result)),
vim.inspect(file_in_rev_result)
)

local buf_path_on_cwd = util.relative_path()
logger.debug(
"|make_link_data| buf_path_on_cwd(%s):%s",
vim.inspect(type(buf_path_on_cwd)),
vim.inspect(buf_path_on_cwd)
)

--- @type LineRange
local range = util.line_range()
-- logger.debug(
-- "[make_link_data] buf_path_on_cwd:%s, range:%s",
-- vim.inspect(buf_path_on_cwd),
-- vim.inspect(range)
-- )
logger.debug(
"[make_link_data] range(%s):%s",
vim.inspect(type(range)),
vim.inspect(range)
)

local remote_url = remote_url_result.stdout[1]
logger.debug(
"[make_link_data] remote_url(%s):%s",
vim.inspect(type(remote_url)),
vim.inspect(remote_url)
)
return new_linker(
remote_url,
rev,
Expand Down
15 changes: 9 additions & 6 deletions lua/gitlinker/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,15 @@ end
--- @return JobResult
local function is_file_in_rev(file, revspec)
local result = cmd({ "cat-file", "-e", revspec .. ":" .. file })
-- logger.debug(
-- "[git.is_file_in_rev] file:%s, revspec:%s, result:%s",
-- vim.inspect(file),
-- vim.inspect(revspec),
-- vim.inspect(result)
-- )
logger.debug(
"|git.is_file_in_rev| file(%s):%s, revspec(%s):%s, result(%s):%s",
vim.inspect(type(file)),
vim.inspect(file),
vim.inspect(type(revspec)),
vim.inspect(revspec),
vim.inspect(type(result)),
vim.inspect(result)
)
return result
end

Expand Down
72 changes: 49 additions & 23 deletions lua/gitlinker/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ local os = vim.loop.os_uname().sysname

--- @return boolean
local function is_macos()
return os == "Darwin"
return vim.fn.has("mac") > 0
end

--- @return boolean
local function is_windows()
if os:match("Windows") then
return true
else
return false
end
return vim.fn.has("win32") > 0 or vim.fn.has("win64") > 0
end

--- @param cwd string|nil
Expand All @@ -24,28 +20,58 @@ local function relative_path(cwd)
-- This will lead us to the wrong relative path because plenary.path don't recoginize them
-- So here we replace '/' to '\\' for plenary.path

-- logger.debug("[util.get_relative_path] cwd:%s", vim.inspect(cwd))
if cwd ~= nil and is_windows() then
if cwd:find("/") then
cwd = cwd:gsub("/", "\\")
end
end
logger.debug(
"|util.relative_path| cwd1(%s):%s",
vim.inspect(type(cwd)),
vim.inspect(cwd)
)

local buf_path = path:new(vim.api.nvim_buf_get_name(0))
local relpath = buf_path:make_relative(cwd)
-- logger.debug(
-- "[util.get_relative_path] buf_path:%s, cwd:%s, relpath:%s",
-- vim.inspect(buf_path),
-- vim.inspect(cwd),
-- vim.inspect(relpath)
-- )
local relpath = nil

-- Then we translate '\\' back to '/'
if relpath ~= nil and is_windows() then
if relpath:find("\\") then
relpath = relpath:gsub("\\", "/")
if is_windows() and cwd ~= nil then
local buf_path_filename = tostring(buf_path)
if buf_path_filename:sub(1, #cwd) == cwd then
relpath = buf_path_filename:sub(#cwd + 1, -1)
logger.debug(
"|util.relative_path| relpath1(%s):%s",
vim.inspect(type(relpath)),
vim.inspect(relpath)
)
if relpath:sub(1, 1) == "/" or relpath:sub(1, 1) == "\\" then
relpath = relpath:sub(2, -1)
logger.debug(
"|util.relative_path| relpath1.1(%s):%s",
vim.inspect(type(relpath)),
vim.inspect(relpath)
)
end
else
relpath = buf_path:make_relative(cwd)
end
else
relpath = buf_path:make_relative(cwd)
end

logger.debug(
"|util.relative_path| buf_path(%s):%s, relpath(%s):%s",
vim.inspect(type(buf_path)),
vim.inspect(buf_path),
vim.inspect(type(relpath)),
vim.inspect(relpath)
)

-- Then we translate '\\' back to '/'
-- if relpath ~= nil and is_windows() then
-- if relpath:find("\\") then
-- relpath = relpath:gsub("\\", "/")
-- end
-- end
logger.debug(
"|util.relative_path| relpath2(%s):%s",
vim.inspect(type(relpath)),
vim.inspect(relpath)
)
return relpath
end

Expand Down

0 comments on commit 565f186

Please sign in to comment.