From 755c6b69d2026294f127192e3f1ddf5f44d7dd5f Mon Sep 17 00:00:00 2001 From: Patrick Ruddiman <86851465+PatrickRuddiman@users.noreply.github.com> Date: Mon, 7 Jul 2025 10:22:22 -0400 Subject: [PATCH 1/4] feat: add context retrieval for small diffs --- Program.cs | 19 ++++++++++++++++++- README.md | 1 + Services/GitService.cs | 15 +++++++++++++-- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Program.cs b/Program.cs index 14dbd69..d76c526 100644 --- a/Program.cs +++ b/Program.cs @@ -140,7 +140,24 @@ static async Task GenerateCommitMessage( } // Get staged changes - var stagedChanges = await gitService.GetStagedChangesAsync(); + var stagedChanges = await gitService.GetStagedChangesAsync(verbose); + + // If the diff is very small, grab a few extra lines of context + var fileCount = System.Text.RegularExpressions.Regex.Matches( + stagedChanges, + "^diff --git", + System.Text.RegularExpressions.RegexOptions.Multiline + ).Count; + var lineCount = stagedChanges.Split('\n').Length; + + if (fileCount <= 2 && lineCount < 20) + { + if (verbose) + { + Console.WriteLine("Small diff detected, gathering additional context..."); + } + stagedChanges = await gitService.GetStagedChangesWithContextAsync(10, verbose); + } if (string.IsNullOrWhiteSpace(stagedChanges)) { Console.WriteLine( diff --git a/README.md b/README.md index 0a7827f..e0c03b6 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ A cross-platform .NET tool that generates AI-powered commit messages using OpenA - 📝 **Verbose output** - Detailed logging for debugging and transparency - ⚡ **Fast and lightweight** - Direct OpenAI API integration for quick responses - 📋 **Smart chunking** - Handles large diffs by intelligently splitting them into semantic chunks +- 🔍 **Context-aware** - Adds surrounding code lines when diffs are very small for better summaries ## 🚀 Quick Start diff --git a/Services/GitService.cs b/Services/GitService.cs index 5f5543f..3b134bd 100644 --- a/Services/GitService.cs +++ b/Services/GitService.cs @@ -18,9 +18,9 @@ public async Task IsInGitRepositoryAsync() } } - public async Task GetStagedChangesAsync() + public async Task GetStagedChangesAsync(bool verbose = false) { - var result = await RunCommandAsync("git", "--no-pager diff --staged", false); + var result = await RunCommandAsync("git", "--no-pager diff --staged", verbose); if (result.ExitCode != 0) { throw new InvalidOperationException($"Failed to get staged changes: {result.Error}"); @@ -28,6 +28,17 @@ public async Task GetStagedChangesAsync() return result.Output; } + public async Task GetStagedChangesWithContextAsync(int contextLines, bool verbose = false) + { + var args = $"--no-pager diff --staged --unified={contextLines}"; + var result = await RunCommandAsync("git", args, verbose); + if (result.ExitCode != 0) + { + throw new InvalidOperationException($"Failed to get staged changes with context: {result.Error}"); + } + return result.Output; + } + public async Task CommitChangesAsync(string commitMessage, bool verbose) { // Create temporary file for commit message From 2a45cce9f4f8df6950121776ea1eee1526a3764f Mon Sep 17 00:00:00 2001 From: Patrick Ruddiman <86851465+PatrickRuddiman@users.noreply.github.com> Date: Mon, 7 Jul 2025 10:25:48 -0400 Subject: [PATCH 2/4] Update Program.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Program.cs b/Program.cs index d76c526..649640e 100644 --- a/Program.cs +++ b/Program.cs @@ -150,7 +150,7 @@ static async Task GenerateCommitMessage( ).Count; var lineCount = stagedChanges.Split('\n').Length; - if (fileCount <= 2 && lineCount < 20) + if (fileCount <= SMALL_DIFF_FILE_THRESHOLD && lineCount < SMALL_DIFF_LINE_THRESHOLD) { if (verbose) { From 876e624de7343620bdb563654c322c7b76432534 Mon Sep 17 00:00:00 2001 From: Patrick Ruddiman <86851465+PatrickRuddiman@users.noreply.github.com> Date: Mon, 7 Jul 2025 10:26:08 -0400 Subject: [PATCH 3/4] Update Program.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- Program.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Program.cs b/Program.cs index 649640e..4353562 100644 --- a/Program.cs +++ b/Program.cs @@ -156,7 +156,8 @@ static async Task GenerateCommitMessage( { Console.WriteLine("Small diff detected, gathering additional context..."); } - stagedChanges = await gitService.GetStagedChangesWithContextAsync(10, verbose); + const int DefaultContextLines = 10; + stagedChanges = await gitService.GetStagedChangesWithContextAsync(DefaultContextLines, verbose); } if (string.IsNullOrWhiteSpace(stagedChanges)) { From 7296100af6d0f4eab4e309702e6abfaa7bef2ca6 Mon Sep 17 00:00:00 2001 From: Patrick Ruddiman <86851465+PatrickRuddiman@users.noreply.github.com> Date: Mon, 7 Jul 2025 14:43:04 +0000 Subject: [PATCH 4/4] Update constants --- Constants/DiffContextDefaults.cs | 22 ++++++++++++++++++++++ Program.cs | 11 ++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 Constants/DiffContextDefaults.cs diff --git a/Constants/DiffContextDefaults.cs b/Constants/DiffContextDefaults.cs new file mode 100644 index 0000000..07d3c84 --- /dev/null +++ b/Constants/DiffContextDefaults.cs @@ -0,0 +1,22 @@ +namespace WriteCommit.Constants; + +/// +/// Default thresholds used when analyzing diffs. +/// +public static class DiffContextDefaults +{ + /// + /// Maximum number of files considered a "small" diff. + /// + public const int SmallDiffFileThreshold = 2; + + /// + /// Maximum total line count considered a "small" diff. + /// + public const int SmallDiffLineThreshold = 20; + + /// + /// Extra context lines to fetch around changes in small diffs. + /// + public const int ExtraContextLines = 10; +} diff --git a/Program.cs b/Program.cs index 4353562..4b09e63 100644 --- a/Program.cs +++ b/Program.cs @@ -150,14 +150,19 @@ static async Task GenerateCommitMessage( ).Count; var lineCount = stagedChanges.Split('\n').Length; - if (fileCount <= SMALL_DIFF_FILE_THRESHOLD && lineCount < SMALL_DIFF_LINE_THRESHOLD) + if ( + fileCount <= DiffContextDefaults.SmallDiffFileThreshold + && lineCount < DiffContextDefaults.SmallDiffLineThreshold + ) { if (verbose) { Console.WriteLine("Small diff detected, gathering additional context..."); } - const int DefaultContextLines = 10; - stagedChanges = await gitService.GetStagedChangesWithContextAsync(DefaultContextLines, verbose); + stagedChanges = await gitService.GetStagedChangesWithContextAsync( + DiffContextDefaults.ExtraContextLines, + verbose + ); } if (string.IsNullOrWhiteSpace(stagedChanges)) {