Skip to content

Commit 6f27c87

Browse files
committed
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
1 parent 98dd5b2 commit 6f27c87

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

lua/codecompanion/_extensions/gitcommit/init.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,18 @@ return {
482482
return GitTool.merge(branch)
483483
end,
484484

485+
---Push changes to remote repository
486+
---@param remote? string The remote to push to (e.g., origin)
487+
---@param branch? string The branch to push (defaults to current branch)
488+
---@param force? boolean Force push (DANGEROUS)
489+
---@param set_upstream? boolean Set upstream branch (default: true for auto-linking)
490+
---@param tags? boolean Push all tags
491+
---@param tag_name? string Single tag to push
492+
push = function(remote, branch, force, set_upstream, tags, tag_name)
493+
local GitTool = require("codecompanion._extensions.gitcommit.tools.git").GitTool
494+
return GitTool.push(remote, branch, force, set_upstream, tags, tag_name)
495+
end,
496+
485497
---Generate release notes between tags
486498
---@param from_tag? string Starting tag (if not provided, uses second latest tag)
487499
---@param to_tag? string Ending tag (if not provided, uses latest tag)

lua/codecompanion/_extensions/gitcommit/tools/git.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,14 +489,18 @@ end
489489
---@param remote? string The name of the remote to push to (e.g., origin)
490490
---@param branch? string The name of the branch to push (defaults to current branch)
491491
---@param force? boolean Force push (DANGEROUS: overwrites remote history)
492+
---@param set_upstream? boolean Set the upstream branch for the current local branch
492493
---@param tags? boolean Push all tags
493-
---@param tag_name? string The name of a single tag to push
494+
---@param tag_name? string The name of a single tag to push (takes priority over tags parameter)
494495
---@return boolean success, string output
495-
function GitTool.push(remote, branch, force, tags, tag_name)
496+
function GitTool.push(remote, branch, force, set_upstream, tags, tag_name)
496497
local cmd = "git push"
497498
if force then
498499
cmd = cmd .. " --force"
499500
end
501+
if set_upstream then
502+
cmd = cmd .. " --set-upstream"
503+
end
500504

501505
-- Handle tag pushing - single tag takes priority over all tags
502506
if tag_name and vim.trim(tag_name) ~= "" then

lua/codecompanion/_extensions/gitcommit/tools/git_edit.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ Available write-access Git operations:
207207
end
208208

209209
if operation == "push" then
210+
-- If set_upstream is not explicitly specified, default to true for automatic remote tracking
211+
if op_args.set_upstream == nil then
212+
op_args.set_upstream = true
213+
end
210214
return GitTool.push_async(
211215
op_args.remote,
212216
op_args.branch,
@@ -218,7 +222,7 @@ Available write-access Git operations:
218222
)
219223
end
220224

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

lua/codecompanion/_extensions/gitcommit/tools/git_read.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ GitRead.cmds = {
155155

156156
local success, output, user_msg, llm_msg
157157

158-
-- 通过 pcall 安全执行操作,确保始终有响应
158+
-- Safely execute operations through pcall to ensure there's always a response
159159
local ok, result = pcall(function()
160160
if operation == "status" then
161161
success, output, user_msg, llm_msg = GitTool.get_status()

lua/codecompanion/_extensions/gitcommit/types.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
---@field use_commit_history? boolean Enable using commit history as context (default: true)
1+
---@field push fun(remote?: string, branch?: string, force?: boolean, set_upstream?: boolean, tags?: boolean, tag_name?: string): boolean, string
22

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

0 commit comments

Comments
 (0)