Skip to content

Commit

Permalink
feat: optimize not in git root error (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
linrongbin16 authored Jun 15, 2023
1 parent 3735844 commit b2b8c5b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 36 deletions.
9 changes: 5 additions & 4 deletions lua/gitlinker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local git = require("gitlinker.git")
local util = require("gitlinker.util")
local keys = require("gitlinker.keys")
local logger = require("gitlinker.logger")
local path = require("plenary.path")

local Defaults = {
-- system/clipboard
Expand Down Expand Up @@ -95,9 +96,9 @@ local function new_linker(remote_url, rev, file, lstart, lend, file_changed)
end

local function make_link_data()
local root = git.get_root()
if not root then
logger.error("Error! Not in a git repository")
local root_result = git.get_root()
if not git.result_has_out(root_result) then
git.result_print_err(root_result, "not in a git repository")
return nil
end

Expand All @@ -121,6 +122,7 @@ local function make_link_data()
return nil
end

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",
Expand Down Expand Up @@ -240,7 +242,6 @@ end
local M = {
setup = setup,
link = link,
map_remote_to_host = map_remote_to_host,
}

return M
45 changes: 13 additions & 32 deletions lua/gitlinker/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,29 +109,11 @@ local function is_file_in_rev(file, revspec)
vim.inspect(result)
)
return result
-- return not result_has_err(result)
end

-- local function string_split(s, sep)
-- -- by default, split by whitespace
-- if sep == nil then
-- sep = "%s"
-- end
-- local splits = {}
-- for i in string.gmatch(s, "([^" .. sep .. "]+)") do
-- table.insert(splits, i)
-- end
-- return splits
-- end
--
-- local function to_positive(n)
-- if n < 0 then
-- return -n
-- else
-- return n
-- end
-- end

--- @param file string
--- @param rev string
--- @return boolean
local function has_file_changed(file, rev)
local result = cmd({ "diff", rev, "--", file })
logger.debug(
Expand All @@ -143,7 +125,10 @@ local function has_file_changed(file, rev)
return result_has_out(result)
end

local function is_rev_in_remote(revspec, remote)
--- @param revspec string
--- @param remote string
--- @return boolean
local function _is_rev_in_remote(revspec, remote)
local result = cmd({ "branch", "--remotes", "--contains", revspec })
logger.debug(
"[git.is_rev_in_remote] revspec:%s, remote:%s, result:%s",
Expand All @@ -162,6 +147,8 @@ end

local UpstreamBranchAllowedChars = "[_%-%w%.]+"

--- @param remote string
--- @return string|nil
local function get_closest_remote_compatible_rev(remote)
assert(remote, "remote cannot be nil")

Expand All @@ -172,7 +159,7 @@ local function get_closest_remote_compatible_rev(remote)
end

-- try HEAD
if is_rev_in_remote("HEAD", remote) then
if _is_rev_in_remote("HEAD", remote) then
local head_rev = _get_rev("HEAD")
if head_rev then
return head_rev
Expand All @@ -182,7 +169,7 @@ local function get_closest_remote_compatible_rev(remote)
-- try last 50 parent commits
for i = 1, 50 do
local revspec = "HEAD~" .. i
if is_rev_in_remote(revspec, remote) then
if _is_rev_in_remote(revspec, remote) then
local rev = _get_rev(revspec)
if rev then
return rev
Expand All @@ -203,6 +190,7 @@ local function get_closest_remote_compatible_rev(remote)
return nil
end

--- @return JobResult
local function get_root()
local buf_path = path:new(vim.api.nvim_buf_get_name(0))
local buf_dir = tostring(buf_path:parent())
Expand All @@ -213,14 +201,7 @@ local function get_root()
vim.inspect(buf_dir),
vim.inspect(result)
)
if result_has_out(result) then
local root = result.stdout[1]
logger.debug("[git.root] current_folder:%s, root:%s", buf_dir, root)
return tostring(path:new(root))
else
logger.debug("[git.root] current_folder:%s, root is nil", buf_dir)
return nil
end
return result
end

local function get_branch_remote()
Expand Down

0 comments on commit b2b8c5b

Please sign in to comment.