Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lua/codecompanion/_extensions/gitcommit/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,18 @@ return {
return GitTool.merge(branch)
end,

---Push changes to remote repository
---@param remote? string The remote to push to (e.g., origin)
---@param branch? string The branch to push (defaults to current branch)
---@param force? boolean Force push (DANGEROUS)
---@param set_upstream? boolean Set upstream branch (default: true for auto-linking)
---@param tags? boolean Push all tags
---@param tag_name? string Single tag to push
push = function(remote, branch, force, set_upstream, tags, tag_name)
local GitTool = require("codecompanion._extensions.gitcommit.tools.git").GitTool
return GitTool.push(remote, branch, force, set_upstream, tags, tag_name)
end,

---Generate release notes between tags
---@param from_tag? string Starting tag (if not provided, uses second latest tag)
---@param to_tag? string Ending tag (if not provided, uses latest tag)
Expand Down
70 changes: 48 additions & 22 deletions lua/codecompanion/_extensions/gitcommit/tools/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -489,26 +489,42 @@ end
---@param remote? string The name of the remote to push to (e.g., origin)
---@param branch? string The name of the branch to push (defaults to current branch)
---@param force? boolean Force push (DANGEROUS: overwrites remote history)
---@param set_upstream? boolean Set the upstream branch for the current local branch
---@param tags? boolean Push all tags
---@param tag_name? string The name of a single tag to push
---@param tag_name? string The name of a single tag to push (takes priority over tags parameter)
---@return boolean success, string output
function GitTool.push(remote, branch, force, tags, tag_name)
function GitTool.push(remote, branch, force, set_upstream, tags, tag_name)
local cmd = "git push"
if force then
cmd = cmd .. " --force"
end
if remote then
cmd = cmd .. " " .. vim.fn.shellescape(remote)
end
if branch then
cmd = cmd .. " " .. vim.fn.shellescape(branch)
end
if tags then
cmd = cmd .. " --tags"
if set_upstream then
cmd = cmd .. " --set-upstream"
end
if tag_name then

-- Handle tag pushing - single tag takes priority over all tags
if tag_name and vim.trim(tag_name) ~= "" then
-- Push single tag: git push origin tag_name
if remote then
cmd = cmd .. " " .. vim.fn.shellescape(remote)
end
cmd = cmd .. " " .. vim.fn.shellescape(tag_name)
elseif tags then
-- Push all tags: git push origin --tags
if remote then
cmd = cmd .. " " .. vim.fn.shellescape(remote)
end
cmd = cmd .. " --tags"
else
-- Regular branch push: git push origin branch
if remote then
cmd = cmd .. " " .. vim.fn.shellescape(remote)
end
if branch then
cmd = cmd .. " " .. vim.fn.shellescape(branch)
end
end

return execute_git_command(cmd)
end

Expand All @@ -528,18 +544,28 @@ function GitTool.push_async(remote, branch, force, set_upstream, tags, tag_name,
if set_upstream then
table.insert(cmd, "--set-upstream")
end
if tags then
table.insert(cmd, "--tags")
end
if tag_name then
table.insert(cmd, "tag")

-- Handle tag pushing - single tag takes priority over all tags
if tag_name and vim.trim(tag_name) ~= "" then
-- Push single tag: git push origin tag_name
if remote then
table.insert(cmd, remote)
end
table.insert(cmd, tag_name)
end
if remote then
table.insert(cmd, remote)
end
if branch then
table.insert(cmd, branch)
elseif tags then
-- Push all tags: git push origin --tags
if remote then
table.insert(cmd, remote)
end
table.insert(cmd, "--tags")
else
-- Regular branch push with optional upstream setting
if remote then
table.insert(cmd, remote)
end
if branch then
table.insert(cmd, branch)
end
end

local stdout_lines = {}
Expand Down
6 changes: 5 additions & 1 deletion lua/codecompanion/_extensions/gitcommit/tools/git_edit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ Available write-access Git operations:
end

if operation == "push" then
-- If set_upstream is not explicitly specified, default to true for automatic remote tracking
if op_args.set_upstream == nil then
op_args.set_upstream = true
end
return GitTool.push_async(
op_args.remote,
op_args.branch,
Expand All @@ -218,7 +222,7 @@ Available write-access Git operations:
)
end

-- 通过 pcall 安全执行操作,确保始终有响应
-- Safely execute operations through pcall to ensure there's always a response
local ok, result = pcall(function()
local success, output

Expand Down
2 changes: 1 addition & 1 deletion lua/codecompanion/_extensions/gitcommit/tools/git_read.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ GitRead.cmds = {

local success, output, user_msg, llm_msg

-- 通过 pcall 安全执行操作,确保始终有响应
-- Safely execute operations through pcall to ensure there's always a response
local ok, result = pcall(function()
if operation == "status" then
success, output, user_msg, llm_msg = GitTool.get_status()
Expand Down
3 changes: 1 addition & 2 deletions lua/codecompanion/_extensions/gitcommit/types.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---@field use_commit_history? boolean Enable using commit history as context (default: true)
---@field push fun(remote?: string, branch?: string, force?: boolean, set_upstream?: boolean, tags?: boolean, tag_name?: string): boolean, string

---@class CodeCompanion.GitCommit.ExtensionOpts.Buffer
---@field enabled boolean Enable buffer-specific keymap for git commit
---@field keymap string Keymap for generating commit message in git commit buffer
---@field auto_generate? boolean Automatically generate commit message on entering gitcommit buffer
Expand Down