diff --git a/lua/codecompanion/_extensions/gitcommit/init.lua b/lua/codecompanion/_extensions/gitcommit/init.lua index ac60535..51d5ed0 100644 --- a/lua/codecompanion/_extensions/gitcommit/init.lua +++ b/lua/codecompanion/_extensions/gitcommit/init.lua @@ -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) diff --git a/lua/codecompanion/_extensions/gitcommit/tools/git.lua b/lua/codecompanion/_extensions/gitcommit/tools/git.lua index ce1c695..74eb86e 100644 --- a/lua/codecompanion/_extensions/gitcommit/tools/git.lua +++ b/lua/codecompanion/_extensions/gitcommit/tools/git.lua @@ -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 @@ -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 = {} diff --git a/lua/codecompanion/_extensions/gitcommit/tools/git_edit.lua b/lua/codecompanion/_extensions/gitcommit/tools/git_edit.lua index 1df0d20..041a714 100644 --- a/lua/codecompanion/_extensions/gitcommit/tools/git_edit.lua +++ b/lua/codecompanion/_extensions/gitcommit/tools/git_edit.lua @@ -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, @@ -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 diff --git a/lua/codecompanion/_extensions/gitcommit/tools/git_read.lua b/lua/codecompanion/_extensions/gitcommit/tools/git_read.lua index 990004d..19ca694 100644 --- a/lua/codecompanion/_extensions/gitcommit/tools/git_read.lua +++ b/lua/codecompanion/_extensions/gitcommit/tools/git_read.lua @@ -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() diff --git a/lua/codecompanion/_extensions/gitcommit/types.lua b/lua/codecompanion/_extensions/gitcommit/types.lua index 259c198..726cff8 100644 --- a/lua/codecompanion/_extensions/gitcommit/types.lua +++ b/lua/codecompanion/_extensions/gitcommit/types.lua @@ -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