From 06fd49ba928e67dfbfab3ce4493ef45b01d06503 Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 23 Oct 2024 09:45:52 +0800 Subject: [PATCH] feature: support `--signoff` for `git commit` command (#591) Signed-off-by: leo --- src/Commands/Commit.cs | 28 +++++++++++++++++++++++---- src/Models/RepositorySettings.cs | 6 ++++++ src/Resources/Locales/en_US.axaml | 1 + src/Resources/Locales/zh_CN.axaml | 1 + src/Resources/Locales/zh_TW.axaml | 1 + src/ViewModels/RepositoryConfigure.cs | 6 ++++++ src/ViewModels/Reword.cs | 2 +- src/ViewModels/Squash.cs | 2 +- src/ViewModels/WorkingCopy.cs | 2 +- src/Views/RepositoryConfigure.axaml | 8 ++++++-- 10 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/Commands/Commit.cs b/src/Commands/Commit.cs index 5927da51..6406ee90 100644 --- a/src/Commands/Commit.cs +++ b/src/Commands/Commit.cs @@ -4,17 +4,37 @@ namespace SourceGit.Commands { public class Commit : Command { - public Commit(string repo, string message, bool amend) + public Commit(string repo, string message, bool amend, bool signOff) { - var file = Path.GetTempFileName(); - File.WriteAllText(file, message); + _tmpFile = Path.GetTempFileName(); + File.WriteAllText(_tmpFile, message); WorkingDirectory = repo; Context = repo; TraitErrorAsOutput = true; - Args = $"commit --allow-empty --file=\"{file}\""; + Args = $"commit --allow-empty --file=\"{_tmpFile}\""; if (amend) Args += " --amend --no-edit"; + if (signOff) + Args += " --signoff"; } + + public bool Run() + { + var succ = Exec(); + + try + { + File.Delete(_tmpFile); + } + catch + { + // Ignore + } + + return succ; + } + + private string _tmpFile = string.Empty; } } diff --git a/src/Models/RepositorySettings.cs b/src/Models/RepositorySettings.cs index c48e3c0c..74c07418 100644 --- a/src/Models/RepositorySettings.cs +++ b/src/Models/RepositorySettings.cs @@ -106,6 +106,12 @@ namespace SourceGit.Models set; } = 10; + public bool EnableSignOffForCommit + { + get; + set; + } = false; + public void PushCommitMessage(string message) { var existIdx = CommitMessages.IndexOf(message); diff --git a/src/Resources/Locales/en_US.axaml b/src/Resources/Locales/en_US.axaml index dfdcc6b0..919c071b 100644 --- a/src/Resources/Locales/en_US.axaml +++ b/src/Resources/Locales/en_US.axaml @@ -143,6 +143,7 @@ Fetch remotes automatically Minute(s) Default Remote + Enable --signoff for commit ISSUE TRACKER Add Sample Github Rule Add Sample Jira Rule diff --git a/src/Resources/Locales/zh_CN.axaml b/src/Resources/Locales/zh_CN.axaml index c09fa75b..6938ffce 100644 --- a/src/Resources/Locales/zh_CN.axaml +++ b/src/Resources/Locales/zh_CN.axaml @@ -146,6 +146,7 @@ 启用定时自动拉取远程更新 分钟 默认远程 + 提交信息追加署名 (--signoff) ISSUE追踪 新增匹配Github Issue规则 新增匹配Jira规则 diff --git a/src/Resources/Locales/zh_TW.axaml b/src/Resources/Locales/zh_TW.axaml index 2c3367a7..f1612150 100644 --- a/src/Resources/Locales/zh_TW.axaml +++ b/src/Resources/Locales/zh_TW.axaml @@ -146,6 +146,7 @@ 啟用定時自動提取 (fetch) 遠端更新 分鐘 預設遠端存放庫 + 提交資訊追加署名 (--signoff) Issue 追蹤 新增符合 GitHub Issue 規則 新增符合 Jira 規則 diff --git a/src/ViewModels/RepositoryConfigure.cs b/src/ViewModels/RepositoryConfigure.cs index 1f490316..8b724383 100644 --- a/src/ViewModels/RepositoryConfigure.cs +++ b/src/ViewModels/RepositoryConfigure.cs @@ -60,6 +60,12 @@ namespace SourceGit.ViewModels set => SetProperty(ref _httpProxy, value); } + public bool EnableSignOffForCommit + { + get => _repo.Settings.EnableSignOffForCommit; + set => _repo.Settings.EnableSignOffForCommit = value; + } + public bool EnableAutoFetch { get => _repo.Settings.EnableAutoFetch; diff --git a/src/ViewModels/Reword.cs b/src/ViewModels/Reword.cs index 3cd85d72..955a0d38 100644 --- a/src/ViewModels/Reword.cs +++ b/src/ViewModels/Reword.cs @@ -39,7 +39,7 @@ namespace SourceGit.ViewModels return Task.Run(() => { - var succ = new Commands.Commit(_repo.FullPath, _message, true).Exec(); + var succ = new Commands.Commit(_repo.FullPath, _message, true, _repo.Settings.EnableSignOffForCommit).Run(); CallUIThread(() => _repo.SetWatcherEnabled(true)); return succ; }); diff --git a/src/ViewModels/Squash.cs b/src/ViewModels/Squash.cs index b4c2fa31..198dbe9a 100644 --- a/src/ViewModels/Squash.cs +++ b/src/ViewModels/Squash.cs @@ -35,7 +35,7 @@ namespace SourceGit.ViewModels { var succ = new Commands.Reset(_repo.FullPath, Target.SHA, "--soft").Exec(); if (succ) - succ = new Commands.Commit(_repo.FullPath, _message, true).Exec(); + succ = new Commands.Commit(_repo.FullPath, _message, true, _repo.Settings.EnableSignOffForCommit).Run(); CallUIThread(() => _repo.SetWatcherEnabled(true)); return succ; }); diff --git a/src/ViewModels/WorkingCopy.cs b/src/ViewModels/WorkingCopy.cs index e45a1749..10ea126b 100644 --- a/src/ViewModels/WorkingCopy.cs +++ b/src/ViewModels/WorkingCopy.cs @@ -1311,7 +1311,7 @@ namespace SourceGit.ViewModels succ = new Commands.Add(_repo.FullPath, _repo.IncludeUntracked).Exec(); if (succ) - succ = new Commands.Commit(_repo.FullPath, _commitMessage, _useAmend).Exec(); + succ = new Commands.Commit(_repo.FullPath, _commitMessage, _useAmend, _repo.Settings.EnableSignOffForCommit).Run(); Dispatcher.UIThread.Post(() => { diff --git a/src/Views/RepositoryConfigure.axaml b/src/Views/RepositoryConfigure.axaml index 1a9c3235..706fa2ef 100644 --- a/src/Views/RepositoryConfigure.axaml +++ b/src/Views/RepositoryConfigure.axaml @@ -51,7 +51,7 @@ - + + + - +