From 2605a4febeaeded79599c4a48bd2d49b2f4e4e6c Mon Sep 17 00:00:00 2001 From: jinzhongjia Date: Sat, 2 Aug 2025 21:39:19 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(git):=20=E4=BC=98=E5=8C=96=20push=20?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BC=98=E5=85=88?= =?UTF-8?q?=E6=8E=A8=E9=80=81=E5=8D=95=E4=B8=AA=20tag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fixed the processing of tag_name taking priority over tags when pushing - Standardize tag push command generation to avoid confusion of parameters - Ensure the logic of branch and tag push is clear --- .../_extensions/gitcommit/tools/git.lua | 64 +++++++++++++------ 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/lua/codecompanion/_extensions/gitcommit/tools/git.lua b/lua/codecompanion/_extensions/gitcommit/tools/git.lua index ce1c695..db34630 100644 --- a/lua/codecompanion/_extensions/gitcommit/tools/git.lua +++ b/lua/codecompanion/_extensions/gitcommit/tools/git.lua @@ -497,18 +497,30 @@ function GitTool.push(remote, branch, force, tags, tag_name) 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" - 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 +540,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 = {} From a106e7f2302c78dbcb31276b8fe47585692ab706 Mon Sep 17 00:00:00 2001 From: jinzhongjia Date: Sat, 2 Aug 2025 22:13:11 +0800 Subject: [PATCH 2/2] feat(git): add set_upstream option to push command - introduce set_upstream parameter for git push to enable automatic remote tracking - default set_upstream to true for push operations when not specified - update type definitions and documentation for push function - improve code comments for operation execution safety --- lua/codecompanion/_extensions/gitcommit/init.lua | 12 ++++++++++++ .../_extensions/gitcommit/tools/git.lua | 8 ++++++-- .../_extensions/gitcommit/tools/git_edit.lua | 6 +++++- .../_extensions/gitcommit/tools/git_read.lua | 2 +- lua/codecompanion/_extensions/gitcommit/types.lua | 3 +-- 5 files changed, 25 insertions(+), 6 deletions(-) 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 db34630..74eb86e 100644 --- a/lua/codecompanion/_extensions/gitcommit/tools/git.lua +++ b/lua/codecompanion/_extensions/gitcommit/tools/git.lua @@ -489,14 +489,18 @@ 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 set_upstream then + cmd = cmd .. " --set-upstream" + end -- Handle tag pushing - single tag takes priority over all tags if tag_name and vim.trim(tag_name) ~= "" then 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