Skip to content

Commit

Permalink
fix: clean up DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
m42e committed Jul 6, 2024
1 parent 08e8ef9 commit 2e44fa3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
12 changes: 3 additions & 9 deletions lua/lgh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,8 @@ local function backup_file(dirname, filename)
end
end
if M.config.fix_dangling then
local backupdir = vim.fn.fnameescape(utils.get_backup_dir(M.config))
table.insert(commands, cmds.build_git_command(M.config, 'add', backupdir))
table.insert(commands, '&&')
table.insert(commands,
cmds.build_git_command(M.config, 'commit', '-m', '"Backup danlging files ' .. backupdir .. '"'))
local fix_dangling_command = cmds.get_fix_dangling_command(M.config, dirname)
table.insert(commands, fix_dangling_command)
end
table.insert(commands, "exit 0")
table.insert(commands, 1, cmds.get_copy_command(M.config, dirname, filename))
Expand All @@ -423,10 +420,7 @@ M.backup_file = backup_file
--- Fix dangling commits
-- In case some file has been copied but not commited
local function fix_dangling()
local commands = {}
local backupdir = vim.fn.fnameescape(utils.get_backup_dir(M.config))
table.insert(commands, cmds.build_git_command(M.config, 'add', backupdir))
table.insert(commands, cmds.build_git_command(M.config, 'commit', '-m', '"Backup danlging files ' .. backupdir .. '"'))
local commands = cmds.get_fix_dangling_command(M.config)
run_command(cmds.shell_cmd(unpack(commands)), M.handle_exit, nil, M.handle_stderr)
end
M.fix_dangling = fix_dangling
Expand Down
13 changes: 13 additions & 0 deletions lua/lgh/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ function M.build_git_command(opts, ...)
return command
end

--- Get the whole commit chain (add and commit if changed)
-- @opts The options for lgh.nvim
-- @dirname The directory name of the file to be commited
-- @filename The filename name of the file to be commited
function M.get_fix_dangling_command(opts)
local backupdir = utils.get_backup_dir(opts)
return M.multiple_commands(
M.build_git_command(opts, 'add', backupdir),
M.cmd_and(),
M.build_git_command(opts, 'commit', '-m', '"Backup danlging files ' .. backupdir .. '"')
)
end

--- Get the whole commit chain (add and commit if changed)
-- @opts The options for lgh.nvim
-- @dirname The directory name of the file to be commited
Expand Down

0 comments on commit 2e44fa3

Please sign in to comment.