Skip to content

Commit aed9dde

Browse files
committed
feature: allows to customize conventional commit types (#1861)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 11d66c6 commit aed9dde

16 files changed

+179
-37
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* Custom Action
4646
* Create PR on GitHub/Gitlab/Gitea/Gitee/Bitbucket...
4747
* Using AI to generate commit message (C# port of [anjerodev/commitollama](https://github.com/anjerodev/commitollama))
48+
* Built-in conventional commit message helper.
4849

4950
> [!WARNING]
5051
> **Linux** only tested on **Debian 12** on both **X11** & **Wayland**.
@@ -173,6 +174,27 @@ This app supports open repository in external tools listed in the table below.
173174
> [!NOTE]
174175
> This app also supports a lot of `JetBrains` IDEs, installing `JetBrains Toolbox` will help this app to find them.
175176
177+
## Conventional Commit Helper
178+
179+
You can define your own conventional commit types (per-repository) by following steps:
180+
181+
1. Create a json file with your own conventional commit type definitions. For example:
182+
```json
183+
[
184+
{
185+
"Name": "New Feature",
186+
"Type": "Feature",
187+
"Description": "Adding a new feature"
188+
},
189+
{
190+
"Name": "Bug Fixes",
191+
"Type": "Fix",
192+
"Description": "Fixing a bug"
193+
}
194+
]
195+
```
196+
2. Configure the `Conventional Commit Types` in repository configuration window.
197+
176198
## Screenshots
177199

178200
* Dark Theme

src/App.JsonCodeGen.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public override void Write(Utf8JsonWriter writer, DataGridLength value, JsonSeri
6565
[JsonSerializable(typeof(Models.ThemeOverrides))]
6666
[JsonSerializable(typeof(Models.Version))]
6767
[JsonSerializable(typeof(Models.RepositorySettings))]
68+
[JsonSerializable(typeof(List<Models.ConventionalCommitType>))]
6869
[JsonSerializable(typeof(List<Models.LFSLock>))]
6970
[JsonSerializable(typeof(List<Models.VisualStudioInstance>))]
7071
[JsonSerializable(typeof(ViewModels.Preferences))]
Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Text.Json;
24

35
namespace SourceGit.Models
46
{
@@ -8,26 +10,39 @@ public class ConventionalCommitType
810
public string Type { get; set; }
911
public string Description { get; set; }
1012

11-
public static readonly List<ConventionalCommitType> Supported = [
12-
new("Features", "feat", "Adding a new feature"),
13-
new("Bug Fixes", "fix", "Fixing a bug"),
14-
new("Work In Progress", "wip", "Still being developed and not yet complete"),
15-
new("Reverts", "revert", "Undoing a previous commit"),
16-
new("Code Refactoring", "refactor", "Restructuring code without changing its external behavior"),
17-
new("Performance Improvements", "perf", "Improves performance"),
18-
new("Builds", "build", "Changes that affect the build system or external dependencies"),
19-
new("Continuous Integrations", "ci", "Changes to CI configuration files and scripts"),
20-
new("Documentations", "docs", "Updating documentation"),
21-
new("Styles", "style", "Elements or code styles without changing the code logic"),
22-
new("Tests", "test", "Adding or updating tests"),
23-
new("Chores", "chore", "Other changes that don't modify src or test files"),
24-
];
25-
2613
public ConventionalCommitType(string name, string type, string description)
2714
{
2815
Name = name;
2916
Type = type;
3017
Description = description;
3118
}
19+
20+
public static List<ConventionalCommitType> Load(string storageFile)
21+
{
22+
try
23+
{
24+
if (!string.IsNullOrEmpty(storageFile) && File.Exists(storageFile))
25+
return JsonSerializer.Deserialize(File.ReadAllText(storageFile), JsonCodeGen.Default.ListConventionalCommitType) ?? [];
26+
}
27+
catch
28+
{
29+
// Ignore errors.
30+
}
31+
32+
return new List<ConventionalCommitType> {
33+
new("Features", "feat", "Adding a new feature"),
34+
new("Bug Fixes", "fix", "Fixing a bug"),
35+
new("Work In Progress", "wip", "Still being developed and not yet complete"),
36+
new("Reverts", "revert", "Undoing a previous commit"),
37+
new("Code Refactoring", "refactor", "Restructuring code without changing its external behavior"),
38+
new("Performance Improvements", "perf", "Improves performance"),
39+
new("Builds", "build", "Changes that affect the build system or external dependencies"),
40+
new("Continuous Integrations", "ci", "Changes to CI configuration files and scripts"),
41+
new("Documentations", "docs", "Updating documentation"),
42+
new("Styles", "style", "Elements or code styles without changing the code logic"),
43+
new("Tests", "test", "Adding or updating tests"),
44+
new("Chores", "chore", "Other changes that don't modify src or test files"),
45+
};
46+
}
3247
}
3348
}

src/Models/RepositorySettings.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ public string LastCommitMessage
236236
set;
237237
} = string.Empty;
238238

239+
public string ConventionalTypesOverride
240+
{
241+
get;
242+
set;
243+
} = string.Empty;
244+
239245
public Dictionary<string, FilterMode> CollectHistoriesFilters()
240246
{
241247
var map = new Dictionary<string, FilterMode>();

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
<x:String x:Key="Text.Configure.Git" xml:space="preserve">GIT</x:String>
223223
<x:String x:Key="Text.Configure.Git.AutoFetch" xml:space="preserve">Fetch remotes automatically</x:String>
224224
<x:String x:Key="Text.Configure.Git.AutoFetchIntervalSuffix" xml:space="preserve">Minute(s)</x:String>
225+
<x:String x:Key="Text.Configure.Git.ConventionalTypesOverride" xml:space="preserve">Conventional Commit Types</x:String>
225226
<x:String x:Key="Text.Configure.Git.DefaultRemote" xml:space="preserve">Default Remote</x:String>
226227
<x:String x:Key="Text.Configure.Git.PreferredMergeMode" xml:space="preserve">Preferred Merge Mode</x:String>
227228
<x:String x:Key="Text.Configure.IssueTracker" xml:space="preserve">ISSUE TRACKER</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@
226226
<x:String x:Key="Text.Configure.Git" xml:space="preserve">GIT配置</x:String>
227227
<x:String x:Key="Text.Configure.Git.AutoFetch" xml:space="preserve">启用定时自动拉取远程更新</x:String>
228228
<x:String x:Key="Text.Configure.Git.AutoFetchIntervalSuffix" xml:space="preserve">分钟</x:String>
229+
<x:String x:Key="Text.Configure.Git.ConventionalTypesOverride" xml:space="preserve">自定义规范化提交类型</x:String>
229230
<x:String x:Key="Text.Configure.Git.DefaultRemote" xml:space="preserve">默认远程</x:String>
230231
<x:String x:Key="Text.Configure.Git.PreferredMergeMode" xml:space="preserve">默认合并方式</x:String>
231232
<x:String x:Key="Text.Configure.IssueTracker" xml:space="preserve">ISSUE追踪</x:String>

src/Resources/Locales/zh_TW.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@
226226
<x:String x:Key="Text.Configure.Git" xml:space="preserve">Git 設定</x:String>
227227
<x:String x:Key="Text.Configure.Git.AutoFetch" xml:space="preserve">啟用定時自動提取 (fetch) 遠端更新</x:String>
228228
<x:String x:Key="Text.Configure.Git.AutoFetchIntervalSuffix" xml:space="preserve">分鐘</x:String>
229+
<x:String x:Key="Text.Configure.Git.ConventionalTypesOverride" xml:space="preserve">自訂約定式提交類型</x:String>
229230
<x:String x:Key="Text.Configure.Git.DefaultRemote" xml:space="preserve">預設遠端存放庫</x:String>
230231
<x:String x:Key="Text.Configure.Git.PreferredMergeMode" xml:space="preserve">預設合併模式</x:String>
231232
<x:String x:Key="Text.Configure.IssueTracker" xml:space="preserve">Issue 追蹤</x:String>

src/ViewModels/ConventionalCommitMessageBuilder.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel.DataAnnotations;
34
using System.Diagnostics.CodeAnalysis;
45
using System.Text;
@@ -9,11 +10,17 @@ namespace SourceGit.ViewModels
910
{
1011
public class ConventionalCommitMessageBuilder : ObservableValidator
1112
{
13+
public List<Models.ConventionalCommitType> Types
14+
{
15+
get;
16+
private set;
17+
} = [];
18+
1219
[Required(ErrorMessage = "Type of changes can not be null")]
13-
public Models.ConventionalCommitType Type
20+
public Models.ConventionalCommitType SelectedType
1421
{
15-
get => _type;
16-
set => SetProperty(ref _type, value, true);
22+
get => _selectedType;
23+
set => SetProperty(ref _selectedType, value, true);
1724
}
1825

1926
public string Scope
@@ -47,8 +54,10 @@ public string ClosedIssue
4754
set => SetProperty(ref _closedIssue, value);
4855
}
4956

50-
public ConventionalCommitMessageBuilder(Action<string> onApply)
57+
public ConventionalCommitMessageBuilder(string conventionalTypesOverride, Action<string> onApply)
5158
{
59+
Types = Models.ConventionalCommitType.Load(conventionalTypesOverride);
60+
SelectedType = Types.Count > 0 ? Types[0] : null;
5261
_onApply = onApply;
5362
}
5463

@@ -63,7 +72,7 @@ public bool Apply()
6372
return false;
6473

6574
var builder = new StringBuilder();
66-
builder.Append(_type.Type);
75+
builder.Append(_selectedType.Type);
6776

6877
if (!string.IsNullOrEmpty(_scope))
6978
{
@@ -103,7 +112,7 @@ public bool Apply()
103112
}
104113

105114
private Action<string> _onApply = null;
106-
private Models.ConventionalCommitType _type = Models.ConventionalCommitType.Supported[0];
115+
private Models.ConventionalCommitType _selectedType = null;
107116
private string _scope = string.Empty;
108117
private string _description = string.Empty;
109118
private string _detail = string.Empty;

src/ViewModels/InteractiveRebase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ public AvaloniaList<Models.IssueTracker> IssueTrackers
9696
get => _repo.IssueTrackers;
9797
}
9898

99+
public string ConventionalTypesOverride
100+
{
101+
get => _repo.Settings.ConventionalTypesOverride;
102+
}
103+
99104
public bool IsLoading
100105
{
101106
get => _isLoading;

src/ViewModels/RepositoryConfigure.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ public string HttpProxy
7676
set => SetProperty(ref _httpProxy, value);
7777
}
7878

79+
public string ConventionalTypesOverride
80+
{
81+
get => _repo.Settings.ConventionalTypesOverride;
82+
set
83+
{
84+
if (_repo.Settings.ConventionalTypesOverride != value)
85+
{
86+
_repo.Settings.ConventionalTypesOverride = value;
87+
OnPropertyChanged();
88+
}
89+
}
90+
}
91+
7992
public bool EnablePruneOnFetch
8093
{
8194
get;

0 commit comments

Comments
 (0)