refactor(git): extract util.split_lines helper
This commit is contained in:
+8
-11
@@ -1,5 +1,6 @@
|
|||||||
local log = require("log")
|
local log = require("log")
|
||||||
local repo = require("git.repo")
|
local repo = require("git.repo")
|
||||||
|
local util = require("util")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
@@ -58,16 +59,6 @@ local function git_cmds()
|
|||||||
return cached_cmds or {}
|
return cached_cmds or {}
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param content string
|
|
||||||
---@return string[]
|
|
||||||
local function split_lines(content)
|
|
||||||
local lines = vim.split(content, "\n", { plain = true, trimempty = false })
|
|
||||||
if #lines > 0 and lines[#lines] == "" then
|
|
||||||
table.remove(lines)
|
|
||||||
end
|
|
||||||
return lines
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param args string[]
|
---@param args string[]
|
||||||
---@param start integer
|
---@param start integer
|
||||||
---@return string?
|
---@return string?
|
||||||
@@ -116,7 +107,13 @@ local function run_in_split(worktree, args, conf)
|
|||||||
end
|
end
|
||||||
local content = (obj.stdout or "") .. (obj.stderr or "")
|
local content = (obj.stdout or "") .. (obj.stderr or "")
|
||||||
vim.bo[buf].modifiable = true
|
vim.bo[buf].modifiable = true
|
||||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, split_lines(content))
|
vim.api.nvim_buf_set_lines(
|
||||||
|
buf,
|
||||||
|
0,
|
||||||
|
-1,
|
||||||
|
false,
|
||||||
|
util.split_lines(content)
|
||||||
|
)
|
||||||
vim.bo[buf].modifiable = false
|
vim.bo[buf].modifiable = false
|
||||||
vim.bo[buf].modified = false
|
vim.bo[buf].modified = false
|
||||||
if obj.code ~= 0 then
|
if obj.code ~= 0 then
|
||||||
|
|||||||
+2
-9
@@ -1,5 +1,6 @@
|
|||||||
local log = require("log")
|
local log = require("log")
|
||||||
local repo = require("git.repo")
|
local repo = require("git.repo")
|
||||||
|
local util = require("util")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
@@ -70,15 +71,7 @@ local function read_show(worktree, revspec)
|
|||||||
)
|
)
|
||||||
return {}
|
return {}
|
||||||
end
|
end
|
||||||
local lines = vim.split(
|
return util.split_lines(result.stdout or "")
|
||||||
result.stdout or "",
|
|
||||||
"\n",
|
|
||||||
{ plain = true, trimempty = false }
|
|
||||||
)
|
|
||||||
if #lines > 0 and lines[#lines] == "" then
|
|
||||||
table.remove(lines)
|
|
||||||
end
|
|
||||||
return lines
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param worktree string
|
---@param worktree string
|
||||||
|
|||||||
+2
-8
@@ -1,5 +1,6 @@
|
|||||||
local log = require("log")
|
local log = require("log")
|
||||||
local repo = require("git.repo")
|
local repo = require("git.repo")
|
||||||
|
local util = require("util")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
@@ -30,14 +31,7 @@ function M.show()
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local lines = vim.split(
|
local lines = util.split_lines(result.stdout or "")
|
||||||
result.stdout or "",
|
|
||||||
"\n",
|
|
||||||
{ plain = true, trimempty = false }
|
|
||||||
)
|
|
||||||
if #lines > 0 and lines[#lines] == "" then
|
|
||||||
table.remove(lines)
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.cmd("new")
|
vim.cmd("new")
|
||||||
local buf = vim.api.nvim_get_current_buf()
|
local buf = vim.api.nvim_get_current_buf()
|
||||||
|
|||||||
+2
-5
@@ -1,6 +1,7 @@
|
|||||||
local diff = require("git.diff")
|
local diff = require("git.diff")
|
||||||
local log = require("log")
|
local log = require("log")
|
||||||
local repo = require("git.repo")
|
local repo = require("git.repo")
|
||||||
|
local util = require("util")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
@@ -153,11 +154,7 @@ function M.open_commit(worktree, ref)
|
|||||||
log.error("git show %s failed: %s", ref, result.stderr or "")
|
log.error("git show %s failed: %s", ref, result.stderr or "")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local content = result.stdout or ""
|
local lines = util.split_lines(result.stdout or "")
|
||||||
local lines = vim.split(content, "\n", { plain = true, trimempty = false })
|
|
||||||
if #lines > 0 and lines[#lines] == "" then
|
|
||||||
table.remove(lines)
|
|
||||||
end
|
|
||||||
local sha = repo.rev_parse(worktree, ref, true) or ref
|
local sha = repo.rev_parse(worktree, ref, true) or ref
|
||||||
local parent = repo.rev_parse(worktree, ref .. "^", true)
|
local parent = repo.rev_parse(worktree, ref .. "^", true)
|
||||||
local buf = vim.api.nvim_create_buf(false, true)
|
local buf = vim.api.nvim_create_buf(false, true)
|
||||||
|
|||||||
@@ -428,4 +428,17 @@ function M.get_hl_source(name)
|
|||||||
return hl
|
return hl
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Split a string on newlines, dropping the trailing empty element that an
|
||||||
|
---input ending in `\n` produces. Convenient for slicing subprocess stdout
|
||||||
|
---into a list of lines without a phantom blank at the end.
|
||||||
|
---@param content string
|
||||||
|
---@return string[]
|
||||||
|
function M.split_lines(content)
|
||||||
|
local lines = vim.split(content, "\n", { plain = true, trimempty = false })
|
||||||
|
if #lines > 0 and lines[#lines] == "" then
|
||||||
|
table.remove(lines)
|
||||||
|
end
|
||||||
|
return lines
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
Reference in New Issue
Block a user