feature: add conventional commit message generator (#574)

This commit is contained in:
leo 2024-10-18 16:20:45 +08:00
parent 2821bab77c
commit 9153bbe07f
No known key found for this signature in database
10 changed files with 369 additions and 8 deletions

View file

@ -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<ConventionalCommitType> Supported = new List<ConventionalCommitType>()
{
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;
}
}
}

View file

@ -16,6 +16,7 @@
<StreamGeometry x:Key="Icons.Code">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</StreamGeometry>
<StreamGeometry x:Key="Icons.ColorPicker">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</StreamGeometry>
<StreamGeometry x:Key="Icons.Commit">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</StreamGeometry>
<StreamGeometry x:Key="Icons.CommitMessageGenerator">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</StreamGeometry>
<StreamGeometry x:Key="Icons.Compare">M645 448l64 64 220-221L704 64l-64 64 115 115H128v90h628zM375 576l-64-64-220 224L314 960l64-64-116-115H896v-90H262z</StreamGeometry>
<StreamGeometry x:Key="Icons.Conflict">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</StreamGeometry>
<StreamGeometry x:Key="Icons.Copy">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</StreamGeometry>

View file

@ -158,6 +158,13 @@
<x:String x:Key="Text.ConfigureWorkspace" xml:space="preserve">Workspaces</x:String>
<x:String x:Key="Text.ConfigureWorkspace.Color" xml:space="preserve">Color</x:String>
<x:String x:Key="Text.ConfigureWorkspace.Restore" xml:space="preserve">Restore tabs on startup</x:String>
<x:String x:Key="Text.ConventionalCommit" xml:space="preserve">Conventional Commit Helper</x:String>
<x:String x:Key="Text.ConventionalCommit.BreakingChanges" xml:space="preserve">Breaking Change:</x:String>
<x:String x:Key="Text.ConventionalCommit.ClosedIssue" xml:space="preserve">Closed Issue:</x:String>
<x:String x:Key="Text.ConventionalCommit.Detail" xml:space="preserve">Detail Changes:</x:String>
<x:String x:Key="Text.ConventionalCommit.Scope" xml:space="preserve">Scope:</x:String>
<x:String x:Key="Text.ConventionalCommit.ShortDescription" xml:space="preserve">Short Description:</x:String>
<x:String x:Key="Text.ConventionalCommit.Type" xml:space="preserve">Type of Change:</x:String>
<x:String x:Key="Text.Copy" xml:space="preserve">Copy</x:String>
<x:String x:Key="Text.CopyAllText" xml:space="preserve">Copy All Text</x:String>
<x:String x:Key="Text.CopyMessage" xml:space="preserve">COPY MESSAGE</x:String>

View file

@ -161,6 +161,13 @@
<x:String x:Key="Text.ConfigureWorkspace" xml:space="preserve">工作区</x:String>
<x:String x:Key="Text.ConfigureWorkspace.Color" xml:space="preserve">颜色</x:String>
<x:String x:Key="Text.ConfigureWorkspace.Restore" xml:space="preserve">启动时恢复打开的仓库</x:String>
<x:String x:Key="Text.ConventionalCommit" xml:space="preserve">规范化提交信息生成工具</x:String>
<x:String x:Key="Text.ConventionalCommit.BreakingChanges" xml:space="preserve">破壞性變更說明:</x:String>
<x:String x:Key="Text.ConventionalCommit.ClosedIssue" xml:space="preserve">關閉的ISSUE</x:String>
<x:String x:Key="Text.ConventionalCommit.Detail" xml:space="preserve">詳細說明:</x:String>
<x:String x:Key="Text.ConventionalCommit.Scope" xml:space="preserve">變更模組:</x:String>
<x:String x:Key="Text.ConventionalCommit.ShortDescription" xml:space="preserve">變更簡述:</x:String>
<x:String x:Key="Text.ConventionalCommit.Type" xml:space="preserve">變更類型:</x:String>
<x:String x:Key="Text.Copy" xml:space="preserve">复制</x:String>
<x:String x:Key="Text.CopyAllText" xml:space="preserve">复制全部文本</x:String>
<x:String x:Key="Text.CopyMessage" xml:space="preserve">复制内容</x:String>

View file

@ -161,6 +161,13 @@
<x:String x:Key="Text.ConfigureWorkspace" xml:space="preserve">工作區</x:String>
<x:String x:Key="Text.ConfigureWorkspace.Color" xml:space="preserve">顏色</x:String>
<x:String x:Key="Text.ConfigureWorkspace.Restore" xml:space="preserve">啟動時還原上次開啟的存放庫</x:String>
<x:String x:Key="Text.ConventionalCommit" xml:space="preserve">規範化提交資訊生成工具</x:String>
<x:String x:Key="Text.ConventionalCommit.BreakingChanges" xml:space="preserve">破坏性更新:</x:String>
<x:String x:Key="Text.ConventionalCommit.ClosedIssue" xml:space="preserve">关闭的ISSUE</x:String>
<x:String x:Key="Text.ConventionalCommit.Detail" xml:space="preserve">详细说明:</x:String>
<x:String x:Key="Text.ConventionalCommit.Scope" xml:space="preserve">变更模块:</x:String>
<x:String x:Key="Text.ConventionalCommit.ShortDescription" xml:space="preserve">变更简述:</x:String>
<x:String x:Key="Text.ConventionalCommit.Type" xml:space="preserve">变更类型:</x:String>
<x:String x:Key="Text.Copy" xml:space="preserve">複製</x:String>
<x:String x:Key="Text.CopyAllText" xml:space="preserve">複製全部內容</x:String>
<x:String x:Key="Text.CopyMessage" xml:space="preserve">複製內容</x:String>

View file

@ -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;
}
}

View file

@ -0,0 +1,146 @@
<v:ChromelessWindow xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:m="using:SourceGit.Models"
xmlns:vm="using:SourceGit.ViewModels"
xmlns:v="using:SourceGit.Views"
xmlns:c="using:SourceGit.Converters"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SourceGit.Views.ConventionalCommitMessageBuilder"
x:DataType="vm:ConventionalCommitMessageBuilder"
x:Name="ThisControl"
Icon="/App.ico"
Title="{DynamicResource Text.ConventionalCommit}"
Width="600"
SizeToContent="Height"
CanResize="False"
WindowStartupLocation="CenterOwner">
<Grid RowDefinitions="Auto,Auto,Auto" MinWidth="494">
<!-- TitleBar -->
<Grid Grid.Row="0" ColumnDefinitions="Auto,*,Auto" Height="30" IsVisible="{Binding !#ThisControl.UseSystemWindowFrame}">
<Border Grid.Column="0" Grid.ColumnSpan="3"
Background="{DynamicResource Brush.TitleBar}"
BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border0}"
PointerPressed="BeginMoveWindow"/>
<Path Grid.Column="0"
Width="14" Height="14"
Data="{StaticResource Icons.Code}"
Margin="10,0,0,0"
IsVisible="{OnPlatform True, macOS=False}"/>
<v:CaptionButtonsMacOS Grid.Column="0"
Margin="0,2,0,0"
IsCloseButtonOnly="True"
IsVisible="{OnPlatform False, macOS=True}"/>
<TextBlock Grid.Column="0" Grid.ColumnSpan="3"
Classes="bold"
Text="{DynamicResource Text.ConventionalCommit}"
HorizontalAlignment="Center" VerticalAlignment="Center"
IsHitTestVisible="False"/>
<v:CaptionButtons Grid.Column="2"
IsCloseButtonOnly="True"
IsVisible="{OnPlatform True, macOS=False}"/>
</Grid>
<!-- Body -->
<Grid Grid.Row="1" Margin="16,8" RowDefinitions="32,32,32,100,100,32" ColumnDefinitions="Auto,*">
<TextBlock Grid.Row="0" Grid.Column="0"
HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="0,6,8,0"
Text="{DynamicResource Text.ConventionalCommit.Type}"/>
<ComboBox Grid.Row="0" Grid.Column="1"
Height="28" Padding="8,0"
VerticalAlignment="Center" HorizontalAlignment="Stretch"
ItemsSource="{Binding Source={x:Static m:ConventionalCommitType.Supported}}"
SelectedItem="{Binding Type, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="{x:Type m:ConventionalCommitType}">
<StackPanel Orientation="Horizontal" Height="20" VerticalAlignment="Center">
<TextBlock Text="{Binding Type}"/>
<TextBlock Margin="6,0,0,0" Text="{Binding Description}" Foreground="{DynamicResource Brush.FG2}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock Grid.Row="1" Grid.Column="0"
HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="0,6,8,0"
Text="{DynamicResource Text.ConventionalCommit.Scope}"/>
<TextBox Grid.Row="1" Grid.Column="1"
Height="26"
VerticalAlignment="Center"
CornerRadius="2"
Watermark="{DynamicResource Text.Optional}"
Text="{Binding Scope, Mode=TwoWay}"/>
<TextBlock Grid.Row="2" Grid.Column="0"
HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="0,6,8,0"
Text="{DynamicResource Text.ConventionalCommit.ShortDescription}"/>
<TextBox Grid.Row="2" Grid.Column="1"
Height="26"
VerticalAlignment="Center"
CornerRadius="2"
Text="{Binding Description, Mode=TwoWay}"/>
<TextBlock Grid.Row="3" Grid.Column="0"
HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="0,6,8,0"
Text="{DynamicResource Text.ConventionalCommit.Detail}"/>
<TextBox Grid.Row="3" Grid.Column="1"
Height="96"
AcceptsReturn="True"
AcceptsTab="True"
TextWrapping="Wrap"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
VerticalAlignment="Center"
VerticalContentAlignment="Top"
CornerRadius="2"
Watermark="{DynamicResource Text.Optional}"
Text="{Binding Detail, Mode=TwoWay}"/>
<TextBlock Grid.Row="4" Grid.Column="0"
HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="0,6,8,0"
Text="{DynamicResource Text.ConventionalCommit.BreakingChanges}"/>
<TextBox Grid.Row="4" Grid.Column="1"
Height="96"
AcceptsReturn="True"
AcceptsTab="True"
TextWrapping="Wrap"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
VerticalAlignment="Center"
VerticalContentAlignment="Top"
CornerRadius="2"
Watermark="{DynamicResource Text.Optional}"
Text="{Binding BreakingChanges, Mode=TwoWay}"/>
<TextBlock Grid.Row="5" Grid.Column="0"
HorizontalAlignment="Right" VerticalAlignment="Top"
Margin="0,6,8,0"
Text="{DynamicResource Text.ConventionalCommit.ClosedIssue}"/>
<TextBox Grid.Row="5" Grid.Column="1"
Height="26"
VerticalAlignment="Center"
CornerRadius="2"
Watermark="{DynamicResource Text.Optional}"
Text="{Binding ClosedIssue, Mode=TwoWay}"/>
</Grid>
<!-- Apply Button -->
<Button Grid.Row="2"
Classes="flat primary"
Height="32" Width="80"
Margin="0,8,0,16"
HorizontalAlignment="Center"
Content="{DynamicResource Text.Sure}"
Click="OnApplyClicked"/>
</Grid>
</v:ChromelessWindow>

View file

@ -0,0 +1,29 @@
using Avalonia.Input;
using Avalonia.Interactivity;
namespace SourceGit.Views
{
public partial class ConventionalCommitMessageBuilder : ChromelessWindow
{
public ConventionalCommitMessageBuilder()
{
InitializeComponent();
}
private void BeginMoveWindow(object _, PointerPressedEventArgs e)
{
BeginMoveDrag(e);
}
private void OnApplyClicked(object _, RoutedEventArgs e)
{
if (DataContext is ViewModels.ConventionalCommitMessageBuilder builder)
{
if (builder.Apply())
Close();
}
e.Handled = true;
}
}
}

View file

@ -185,7 +185,7 @@
<v:CommitMessageTextBox Grid.Row="2" Text="{Binding CommitMessage, Mode=TwoWay}"/>
<!-- Commit Options -->
<Grid Grid.Row="3" Margin="0,6,0,0" ColumnDefinitions="Auto,Auto,Auto,Auto,*,Auto,Auto,Auto,Auto">
<Grid Grid.Row="3" Margin="0,6,0,0" ColumnDefinitions="Auto,Auto,Auto,Auto,Auto,*,Auto,Auto,Auto,Auto">
<Button Grid.Column="0"
Classes="icon_button"
Margin="4,0,0,0" Padding="0"
@ -198,7 +198,6 @@
<Button Grid.Column="1"
Classes="icon_button"
Width="32"
Margin="4,2,0,0"
Click="OnOpenAIAssist"
ToolTip.Tip="{DynamicResource Text.AIAssistant.Tip}"
@ -207,23 +206,33 @@
<Path Width="15" Height="15" Data="{StaticResource Icons.AIAssist}"/>
</Button>
<CheckBox Grid.Column="2"
<Button Grid.Column="2"
Classes="icon_button"
Margin="0,2,0,0"
Click="OnOpenConventionalCommitHelper"
ToolTip.Tip="{DynamicResource Text.ConventionalCommit}"
ToolTip.Placement="Top"
ToolTip.VerticalOffset="0">
<Path Width="15" Height="15" Data="{StaticResource Icons.CommitMessageGenerator}"/>
</Button>
<CheckBox Grid.Column="3"
Height="24"
Margin="4,0,0,0"
HorizontalAlignment="Left"
IsChecked="{Binding AutoStageBeforeCommit, Mode=TwoWay}"
Content="{DynamicResource Text.WorkingCopy.AutoStage}"/>
<CheckBox Grid.Column="3"
<CheckBox Grid.Column="4"
Height="24"
Margin="8,0,0,0"
HorizontalAlignment="Left"
IsChecked="{Binding UseAmend, Mode=TwoWay}"
Content="{DynamicResource Text.WorkingCopy.Amend}"/>
<v:LoadingIcon Grid.Column="5" Width="18" Height="18" IsVisible="{Binding IsCommitting}"/>
<v:LoadingIcon Grid.Column="6" Width="18" Height="18" IsVisible="{Binding IsCommitting}"/>
<Button Grid.Column="6"
<Button Grid.Column="7"
Classes="flat primary"
Content="{DynamicResource Text.WorkingCopy.Commit}"
Height="28"
@ -248,13 +257,13 @@
</Button>
<!-- Invisible button just to add another hotkey `Ctrl+Shift+Enter` to commit with auto-stage -->
<Button Grid.Column="7"
<Button Grid.Column="8"
Width="0" Height="0"
Background="Transparent"
Command="{Binding CommitWithAutoStage}"
HotKey="{OnPlatform Ctrl+Shift+Enter, macOS=⌘+Shift+Enter}"/>
<Button Grid.Column="8"
<Button Grid.Column="9"
Classes="flat"
Content="{DynamicResource Text.WorkingCopy.CommitAndPush}"
Height="28"

View file

@ -144,5 +144,20 @@ namespace SourceGit.Views
e.Handled = true;
}
private void OnOpenConventionalCommitHelper(object _, RoutedEventArgs e)
{
if (DataContext is ViewModels.WorkingCopy vm)
{
var dialog = new ConventionalCommitMessageBuilder()
{
DataContext = new ViewModels.ConventionalCommitMessageBuilder(vm)
};
App.OpenDialog(dialog);
}
e.Handled = true;
}
}
}