From 9153bbe07f90935b9661aed75df5f44421d65215 Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 18 Oct 2024 16:20:45 +0800 Subject: [PATCH] feature: add conventional commit message generator (#574) --- src/Models/ConventionalCommitType.cs | 28 ++++ src/Resources/Icons.axaml | 1 + src/Resources/Locales/en_US.axaml | 7 + src/Resources/Locales/zh_CN.axaml | 7 + src/Resources/Locales/zh_TW.axaml | 7 + .../ConventionalCommitMessageBuilder.cs | 112 ++++++++++++++ .../ConventionalCommitMessageBuilder.axaml | 146 ++++++++++++++++++ .../ConventionalCommitMessageBuilder.axaml.cs | 29 ++++ src/Views/WorkingCopy.axaml | 25 ++- src/Views/WorkingCopy.axaml.cs | 15 ++ 10 files changed, 369 insertions(+), 8 deletions(-) create mode 100644 src/Models/ConventionalCommitType.cs create mode 100644 src/ViewModels/ConventionalCommitMessageBuilder.cs create mode 100644 src/Views/ConventionalCommitMessageBuilder.axaml create mode 100644 src/Views/ConventionalCommitMessageBuilder.axaml.cs diff --git a/src/Models/ConventionalCommitType.cs b/src/Models/ConventionalCommitType.cs new file mode 100644 index 00000000..4fb61d87 --- /dev/null +++ b/src/Models/ConventionalCommitType.cs @@ -0,0 +1,28 @@ +using System.Collections.Generic; + +namespace SourceGit.Models +{ + public class ConventionalCommitType + { + public string Type { get; set; } = string.Empty; + public string Description { get; set; } = string.Empty; + + public static readonly List Supported = new List() + { + new ConventionalCommitType("feat", "Adding a new feature"), + new ConventionalCommitType("fix", "Fixing a bug"), + new ConventionalCommitType("docs", "Updating documentation"), + new ConventionalCommitType("style", "Elements or code styles without changing the code logic"), + new ConventionalCommitType("test", "Adding or updating tests"), + new ConventionalCommitType("chore", "Making changes to the build process or auxiliary tools and libraries"), + new ConventionalCommitType("revert", "Undoing a previous commit"), + new ConventionalCommitType("refactor", "Restructuring code without changing its external behavior") + }; + + public ConventionalCommitType(string type, string description) + { + Type = type; + Description = description; + } + } +} diff --git a/src/Resources/Icons.axaml b/src/Resources/Icons.axaml index b21f1592..1a84597e 100644 --- a/src/Resources/Icons.axaml +++ b/src/Resources/Icons.axaml @@ -16,6 +16,7 @@ M853 102H171C133 102 102 133 102 171v683C102 891 133 922 171 922h683C891 922 922 891 922 853V171C922 133 891 102 853 102zM390 600l-48 48L205 512l137-137 48 48L301 512l88 88zM465 819l-66-18L559 205l66 18L465 819zm218-171L634 600 723 512l-88-88 48-48L819 512 683 649z M128 854h768v86H128zM390 797c13 13 29 19 48 19s35-6 45-19l291-288c26-22 26-64 0-90L435 83l-61 61L426 192l-272 269c-22 22-22 64 0 90l237 246zm93-544 211 211-32 32H240l243-243zM707 694c0 48 38 86 86 86 48 0 86-38 86-86 0-22-10-45-26-61L794 576l-61 61c-13 13-26 35-26 58z M796 471A292 292 0 00512 256a293 293 0 00-284 215H0v144h228A293 293 0 00512 832a291 291 0 00284-217H1024V471h-228M512 688A146 146 0 01366 544A145 145 0 01512 400c80 0 146 63 146 144A146 146 0 01512 688 + M796 561a5 5 0 014 7l-39 90a5 5 0 004 7h100a5 5 0 014 8l-178 247a5 5 0 01-9-4l32-148a5 5 0 00-5-6h-89a5 5 0 01-4-7l86-191a5 5 0 014-3h88zM731 122a73 73 0 0173 73v318a54 54 0 00-8-1H731V195H244v634h408l-16 73H244a73 73 0 01-73-73V195a73 73 0 0173-73h488zm-219 366v73h-195v-73h195zm146-146v73H317v-73h341z M645 448l64 64 220-221L704 64l-64 64 115 115H128v90h628zM375 576l-64-64-220 224L314 960l64-64-116-115H896v-90H262z M608 0q48 0 88 23t63 63 23 87v70h55q35 0 67 14t57 38 38 57 14 67V831q0 34-14 66t-38 57-57 38-67 13H426q-34 0-66-13t-57-38-38-57-14-66v-70h-56q-34 0-66-14t-57-38-38-57-13-67V174q0-47 23-87T109 23 196 0h412m175 244H426q-46 0-86 22T278 328t-26 85v348H608q47 0 86-22t63-62 25-85l1-348m-269 318q18 0 31 13t13 31-13 31-31 13-31-13-13-31 13-31 31-13m0-212q13 0 22 9t11 22v125q0 14-9 23t-22 10-23-7-11-22l-1-126q0-13 10-23t23-10z M896 811l-128 0c-23 0-43-19-43-43 0-23 19-43 43-43l107 0c13 0 21-9 21-21L896 107c0-13-9-21-21-21L448 85c-13 0-21 9-21 21l0 21c0 23-19 43-43 43-23 0-43-19-43-43L341 85c0-47 38-85 85-85l469 0c47 0 85 38 85 85l0 640C981 772 943 811 896 811zM683 299l0 640c0 47-38 85-85 85L128 1024c-47 0-85-38-85-85L43 299c0-47 38-85 85-85l469 0C644 213 683 252 683 299zM576 299 149 299c-13 0-21 9-21 21l0 597c0 13 9 21 21 21l427 0c13 0 21-9 21-21L597 320C597 307 589 299 576 299z diff --git a/src/Resources/Locales/en_US.axaml b/src/Resources/Locales/en_US.axaml index cf8caef1..bfb0ace4 100644 --- a/src/Resources/Locales/en_US.axaml +++ b/src/Resources/Locales/en_US.axaml @@ -158,6 +158,13 @@ Workspaces Color Restore tabs on startup + Conventional Commit Helper + Breaking Change: + Closed Issue: + Detail Changes: + Scope: + Short Description: + Type of Change: Copy Copy All Text COPY MESSAGE diff --git a/src/Resources/Locales/zh_CN.axaml b/src/Resources/Locales/zh_CN.axaml index c8e768ba..4a98c4d1 100644 --- a/src/Resources/Locales/zh_CN.axaml +++ b/src/Resources/Locales/zh_CN.axaml @@ -161,6 +161,13 @@ 工作区 颜色 启动时恢复打开的仓库 + 规范化提交信息生成工具 + 破壞性變更說明: + 關閉的ISSUE: + 詳細說明: + 變更模組: + 變更簡述: + 變更類型: 复制 复制全部文本 复制内容 diff --git a/src/Resources/Locales/zh_TW.axaml b/src/Resources/Locales/zh_TW.axaml index 973da041..063139c4 100644 --- a/src/Resources/Locales/zh_TW.axaml +++ b/src/Resources/Locales/zh_TW.axaml @@ -161,6 +161,13 @@ 工作區 顏色 啟動時還原上次開啟的存放庫 + 規範化提交資訊生成工具 + 破坏性更新: + 关闭的ISSUE: + 详细说明: + 变更模块: + 变更简述: + 变更类型: 複製 複製全部內容 複製內容 diff --git a/src/ViewModels/ConventionalCommitMessageBuilder.cs b/src/ViewModels/ConventionalCommitMessageBuilder.cs new file mode 100644 index 00000000..ab48bc30 --- /dev/null +++ b/src/ViewModels/ConventionalCommitMessageBuilder.cs @@ -0,0 +1,112 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; +using System.Text; + +using CommunityToolkit.Mvvm.ComponentModel; + +namespace SourceGit.ViewModels +{ + public class ConventionalCommitMessageBuilder : ObservableValidator + { + [Required(ErrorMessage = "Type of changes can not be null")] + public Models.ConventionalCommitType Type + { + get => _type; + set => SetProperty(ref _type, value, true); + } + + public string Scope + { + get => _scope; + set => SetProperty(ref _scope, value); + } + + [Required(ErrorMessage = "Short description can not be empty")] + public string Description + { + get => _description; + set => SetProperty(ref _description, value, true); + } + + public string Detail + { + get => _detail; + set => SetProperty(ref _detail, value); + } + + public string BreakingChanges + { + get => _breakingChanges; + set => SetProperty(ref _breakingChanges, value); + } + + public string ClosedIssue + { + get => _closedIssue; + set => SetProperty(ref _closedIssue, value); + } + + public ConventionalCommitMessageBuilder(WorkingCopy wc) + { + _wc = wc; + } + + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2026:RequiresUnreferencedCode")] + public bool Apply() + { + if (HasErrors) + return false; + + ValidateAllProperties(); + if (HasErrors) + return false; + + var builder = new StringBuilder(); + builder.Append(_type.Type); + + if (!string.IsNullOrEmpty(_scope)) + { + builder.Append("("); + builder.Append(_scope); + builder.Append("): "); + } + else + { + builder.Append(": "); + } + + builder.Append(_description); + builder.Append("\n\n"); + + if (!string.IsNullOrEmpty(_detail)) + { + builder.Append(_detail); + builder.Append("\n\n"); + } + + if (!string.IsNullOrEmpty(_breakingChanges)) + { + builder.Append("BREAKING CHANGE: "); + builder.Append(_breakingChanges); + builder.Append("\n\n"); + } + + if (!string.IsNullOrEmpty(_closedIssue)) + { + builder.Append("Closed "); + builder.Append(_closedIssue); + } + + _wc.CommitMessage = builder.ToString(); + return true; + } + + private WorkingCopy _wc = null; + private Models.ConventionalCommitType _type = Models.ConventionalCommitType.Supported[0]; + private string _scope = string.Empty; + private string _description = string.Empty; + private string _detail = string.Empty; + private string _breakingChanges = string.Empty; + private string _closedIssue = string.Empty; + } +} diff --git a/src/Views/ConventionalCommitMessageBuilder.axaml b/src/Views/ConventionalCommitMessageBuilder.axaml new file mode 100644 index 00000000..c15f0a2c --- /dev/null +++ b/src/Views/ConventionalCommitMessageBuilder.axaml @@ -0,0 +1,146 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + - - + -