Skip to content

Commit d2d0fd3

Browse files
committed
fix: account for amend when generating AI commit message
When "Amend" was checked, the AI commit-message generator ignored that context: it read each file's diff against HEAD, so it only saw the new staged changes and missed the content of the commit being amended, producing a message that discarded the previous intent. Detect amend from the staged changes (which carry the previous commit's parent SHA in amend mode) and pass that parent down to the file-diff tool, so the model reads each file against the parent and sees the full resulting commit.
1 parent 9a9b62c commit d2d0fd3

4 files changed

Lines changed: 13 additions & 7 deletions

File tree

‎src/AI/Agent.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public Agent(Service service)
1414
_service = service;
1515
}
1616

17-
public async Task GenerateCommitMessageAsync(string repo, string currentBranch, string changeList, Action<string> onUpdate, CancellationToken cancellation)
17+
public async Task GenerateCommitMessageAsync(string repo, string currentBranch, string changeList, string amendParent, Action<string> onUpdate, CancellationToken cancellation)
1818
{
1919
var chatClient = _service.GetChatClient();
2020
if (chatClient == null)
@@ -95,7 +95,7 @@ public async Task GenerateCommitMessageAsync(string repo, string currentBranch,
9595

9696
foreach (var call in completion.ToolCalls)
9797
{
98-
var result = await ChatTools.ProcessAsync(call, onUpdate);
98+
var result = await ChatTools.ProcessAsync(call, onUpdate, amendParent);
9999
messages.Add(result);
100100
}
101101

‎src/AI/ChatTools.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static class ChatTools
3232
}
3333
""")), false);
3434

35-
public static async Task<ToolChatMessage> ProcessAsync(ChatToolCall call, Action<string> output)
35+
public static async Task<ToolChatMessage> ProcessAsync(ChatToolCall call, Action<string> output, string amendParent)
3636
{
3737
using var doc = JsonDocument.Parse(call.FunctionArguments);
3838

@@ -49,7 +49,7 @@ public static async Task<ToolChatMessage> ProcessAsync(ChatToolCall call, Action
4949
output?.Invoke($"Read changes in file: {filePath.GetString()}");
5050

5151
var orgFilePath = hasOriginalFile ? originalFilePath.GetString() : string.Empty;
52-
var rs = await new Commands.GetFileChangeForAI(repoPath.GetString(), filePath.GetString(), orgFilePath).ReadAsync();
52+
var rs = await new Commands.GetFileChangeForAI(repoPath.GetString(), filePath.GetString(), orgFilePath, amendParent).ReadAsync();
5353
var message = rs.IsSuccess ? rs.StdOut : string.Empty;
5454
return new ToolChatMessage(call.Id, message);
5555
}

‎src/Commands/GetFileChangeForAI.cs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ namespace SourceGit.Commands
66
{
77
public class GetFileChangeForAI : Command
88
{
9-
public GetFileChangeForAI(string repo, string file, string originalFile)
9+
public GetFileChangeForAI(string repo, string file, string originalFile, string amendParent)
1010
{
1111
WorkingDirectory = repo;
1212
Context = repo;
1313

1414
var builder = new StringBuilder();
15-
builder.Append("diff --no-color --no-ext-diff --diff-algorithm=minimal --cached -- ");
15+
builder.Append("diff --no-color --no-ext-diff --diff-algorithm=minimal --cached ");
16+
if (!string.IsNullOrEmpty(amendParent))
17+
builder.Append(amendParent).Append(' ');
18+
builder.Append("-- ");
1619
if (!string.IsNullOrEmpty(originalFile) && !file.Equals(originalFile, StringComparison.Ordinal))
1720
builder.Append(originalFile.Quoted()).Append(' ');
1821
builder.Append(file.Quoted());

‎src/ViewModels/AIAssistant.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public AIAssistant(Repository repo, AI.Service service, List<Models.Change> chan
4949
foreach (var c in changes)
5050
SerializeChange(c, builder);
5151
_changeList = builder.ToString();
52+
53+
_amendParent = changes.Count > 0 ? changes[0].DataForAmend?.ParentSHA : null;
5254
}
5355

5456
public async Task GenAsync()
@@ -71,7 +73,7 @@ public async Task GenAsync()
7173

7274
try
7375
{
74-
await agent.GenerateCommitMessageAsync(_repo.FullPath, currentBranchName, _changeList, message =>
76+
await agent.GenerateCommitMessageAsync(_repo.FullPath, currentBranchName, _changeList, _amendParent, message =>
7577
{
7678
builder.AppendLine(message);
7779

@@ -145,6 +147,7 @@ private void SerializeChange(Models.Change c, StringBuilder builder)
145147
private readonly Repository _repo = null;
146148
private readonly AI.Service _service = null;
147149
private readonly string _changeList = null;
150+
private readonly string _amendParent = null;
148151
private CancellationTokenSource _cancel = null;
149152
private bool _isGenerating = false;
150153
private string _text = string.Empty;

0 commit comments

Comments
 (0)