|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using System.Threading; |
| 5 | + |
| 6 | +namespace SourceGit.Commands |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// A C# version of https://github.com/anjerodev/commitollama |
| 10 | + /// </summary> |
| 11 | + public class GenerateCommitMessage |
| 12 | + { |
| 13 | + public class GetDiffContent : Command |
| 14 | + { |
| 15 | + public GetDiffContent(string repo, Models.DiffOption opt) |
| 16 | + { |
| 17 | + WorkingDirectory = repo; |
| 18 | + Context = repo; |
| 19 | + Args = $"diff --diff-algorithm=minimal {opt}"; |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public GenerateCommitMessage(string repo, List<Models.Change> changes, CancellationToken cancelToken, Action<string> onProgress) |
| 24 | + { |
| 25 | + _repo = repo; |
| 26 | + _changes = changes; |
| 27 | + _cancelToken = cancelToken; |
| 28 | + _onProgress = onProgress; |
| 29 | + } |
| 30 | + |
| 31 | + public string Result() |
| 32 | + { |
| 33 | + try |
| 34 | + { |
| 35 | + var summaries = new List<string>(); |
| 36 | + foreach (var change in _changes) |
| 37 | + { |
| 38 | + if (_cancelToken.IsCancellationRequested) |
| 39 | + return ""; |
| 40 | + |
| 41 | + _onProgress?.Invoke($"Analyzing {change.Path}..."); |
| 42 | + var summary = GenerateChangeSummary(change); |
| 43 | + summaries.Add(summary); |
| 44 | + } |
| 45 | + |
| 46 | + if (_cancelToken.IsCancellationRequested) |
| 47 | + return ""; |
| 48 | + |
| 49 | + _onProgress?.Invoke($"Generating commit message..."); |
| 50 | + var builder = new StringBuilder(); |
| 51 | + builder.Append(GenerateSubject(string.Join("", summaries))); |
| 52 | + builder.Append("\n"); |
| 53 | + foreach (var summary in summaries) |
| 54 | + { |
| 55 | + builder.Append("\n- "); |
| 56 | + builder.Append(summary.Trim()); |
| 57 | + } |
| 58 | + |
| 59 | + return builder.ToString(); |
| 60 | + } |
| 61 | + catch (Exception e) |
| 62 | + { |
| 63 | + App.RaiseException(_repo, $"Failed to generate commit message: {e}"); |
| 64 | + return ""; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private string GenerateChangeSummary(Models.Change change) |
| 69 | + { |
| 70 | + var rs = new GetDiffContent(_repo, new Models.DiffOption(change, false)).ReadToEnd(); |
| 71 | + var diff = rs.IsSuccess ? rs.StdOut : "unknown change"; |
| 72 | + |
| 73 | + var prompt = new StringBuilder(); |
| 74 | + prompt.AppendLine("You are an expert developer specialist in creating commits."); |
| 75 | + prompt.AppendLine("Provide a super concise one sentence overall changes summary of the user `git diff` output following strictly the next rules:"); |
| 76 | + prompt.AppendLine("- Do not use any code snippets, imports, file routes or bullets points."); |
| 77 | + prompt.AppendLine("- Do not mention the route of file that has been change."); |
| 78 | + prompt.AppendLine("- Simply describe the MAIN GOAL of the changes."); |
| 79 | + prompt.AppendLine("- Output directly the summary in plain text.`"); |
| 80 | + |
| 81 | + var rsp = Models.OpenAI.Chat(prompt.ToString(), $"Here is the `git diff` output: {diff}"); |
| 82 | + if (rsp != null && rsp.Choices.Count > 0) |
| 83 | + return rsp.Choices[0].Message.Content; |
| 84 | + |
| 85 | + return string.Empty; |
| 86 | + } |
| 87 | + |
| 88 | + private string GenerateSubject(string summary) |
| 89 | + { |
| 90 | + var prompt = new StringBuilder(); |
| 91 | + prompt.AppendLine("You are an expert developer specialist in creating commits messages."); |
| 92 | + prompt.AppendLine("Your only goal is to retrieve a single commit message."); |
| 93 | + prompt.AppendLine("Based on the provided user changes, combine them in ONE SINGLE commit message retrieving the global idea, following strictly the next rules:"); |
| 94 | + prompt.AppendLine("- Assign the commit {type} according to the next conditions:"); |
| 95 | + prompt.AppendLine(" feat: Only when adding a new feature."); |
| 96 | + prompt.AppendLine(" fix: When fixing a bug."); |
| 97 | + prompt.AppendLine(" docs: When updating documentation."); |
| 98 | + prompt.AppendLine(" style: When changing elements styles or design and/or making changes to the code style (formatting, missing semicolons, etc.) without changing the code logic."); |
| 99 | + prompt.AppendLine(" test: When adding or updating tests. "); |
| 100 | + prompt.AppendLine(" chore: When making changes to the build process or auxiliary tools and libraries. "); |
| 101 | + prompt.AppendLine(" revert: When undoing a previous commit."); |
| 102 | + prompt.AppendLine(" refactor: When restructuring code without changing its external behavior, or is any of the other refactor types."); |
| 103 | + prompt.AppendLine("- Do not add any issues numeration, explain your output nor introduce your answer."); |
| 104 | + prompt.AppendLine("- Output directly only one commit message in plain text with the next format: {type}: {commit_message}."); |
| 105 | + prompt.AppendLine("- Be as concise as possible, keep the message under 50 characters."); |
| 106 | + |
| 107 | + var rsp = Models.OpenAI.Chat(prompt.ToString(), $"Here are the summaries changes: {summary}"); |
| 108 | + if (rsp != null && rsp.Choices.Count > 0) |
| 109 | + return rsp.Choices[0].Message.Content; |
| 110 | + |
| 111 | + return string.Empty; |
| 112 | + } |
| 113 | + |
| 114 | + private string _repo; |
| 115 | + private List<Models.Change> _changes; |
| 116 | + private CancellationToken _cancelToken; |
| 117 | + private Action<string> _onProgress; |
| 118 | + } |
| 119 | +} |
0 commit comments