Skip to content

Commit

Permalink
chore: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Dec 7, 2024
1 parent 413997e commit 02ff063
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lua/tinygit/commands/commit-and-amend.lua
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ local function setupInputField(commitType)
if commitType == "smartCommit" then
vim.api.nvim_create_autocmd("WinClosed", {
callback = function(ctx)
local ft = vim.api.nvim_get_option_value("filetype", { buf = ctx.buf })
if not (ft == "gitcommit" or ft == "DressingInput") then return end
local ft = vim.bo[ctx.buf].filetype
if ft ~= "gitcommit" and ft ~= "DressingInput" then return end

local cwd = vim.uv.cwd() or ""
M.state.abortedCommitMsg[cwd] = vim.api.nvim_buf_get_lines(ctx.buf, 0, 1, false)[1]
Expand Down
20 changes: 11 additions & 9 deletions lua/tinygit/commands/push-pull.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,27 @@ end
local function pushCmd(opts)
local config = require("tinygit.config").config.push
local gitCommand = { "git", "push" }
local title = opts.forceWithLease and "Force push" or "Push"
if opts.forceWithLease then table.insert(gitCommand, "--force-with-lease") end

vim.system(
gitCommand,
{ detach = true, text = true },
{ detach = true },
vim.schedule_wrap(function(result)
local out = vim.trim((result.stdout or "") .. (result.stderr or "")):gsub("\n%s+", "\n") -- fix leading spacing
local out = vim.trim((result.stdout or "") .. (result.stderr or ""))
out = out:gsub("\n%s+", "\n") -- remove padding
local commitRange = out:match("%x+%.%.%x+")

-- notify
if result.code == 0 then
local numOfPushedCommits = u.syncShellCmd { "git", "rev-list", "--count", commitRange }
if numOfPushedCommits ~= "" then
local plural = numOfPushedCommits ~= "1" and "s" or ""
-- `[]` together with `ft=markdown` -> simple highlighting for `snacks.nvim`
out = out .. ("\n[%s commit%s]"):format(numOfPushedCommits, plural)
-- `[]` -> simple highlighting for `snacks.nvim`
out = out .. ("\n[%d commit%s]"):format(numOfPushedCommits, plural)
end
end
u.notify(out, result.code == 0 and "info" or "error", { ft = ft, title = "Push" })
u.notify(out, result.code == 0 and "info" or "error", { title = title })

-- sound
if config.confirmationSound and jit.os == "OSX" then
Expand All @@ -67,6 +69,8 @@ end
---@param calledByCommitFunc? boolean
function M.push(opts, calledByCommitFunc)
local config = require("tinygit.config").config.push
if not opts then opts = {} end
local title = opts.forceWithLease and "Force push" or "Push"

-- GUARD
if u.notInGitRepo() then return end
Expand All @@ -75,12 +79,10 @@ function M.push(opts, calledByCommitFunc)
u.syncShellCmd { "git", "log", "--oneline", "--grep=^fixup!", "--grep=^squash!" }
if fixupOrSquashCommits ~= "" then
local msg = "Aborting: There are fixup or squash commits.\n\n" .. fixupOrSquashCommits
u.notify(msg, "warn", { title = "Push" })
u.notify(msg, "warn", { title = title })
return
end
end
if not opts then opts = {} end
local title = opts.forceWithLease and "Force push" or "Push"

-- extra notification when called by user
if not calledByCommitFunc then
Expand Down Expand Up @@ -115,7 +117,7 @@ function M.push(opts, calledByCommitFunc)
-- Pull & Push
vim.system(
{ "git", "pull" },
{ detach = true, text = true },
{ detach = true },
vim.schedule_wrap(function(result)
-- Git messaging is weird and sometimes puts normal messages into
-- stderr, thus we need to merge stdout and stderr.
Expand Down

0 comments on commit 02ff063

Please sign in to comment.