Skip to content

Commit 1855b43

Browse files
committed
feature: allow empty commit (#587)
1 parent a8a7775 commit 1855b43

File tree

8 files changed

+147
-20
lines changed

8 files changed

+147
-20
lines changed

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@
637637
<x:String x:Key="Text.WorkingCopy.CommitMessageHelper" xml:space="preserve">Template/Histories</x:String>
638638
<x:String x:Key="Text.WorkingCopy.CommitTip" xml:space="preserve">Trigger click event</x:String>
639639
<x:String x:Key="Text.WorkingCopy.CommitWithAutoStage" xml:space="preserve">Stage all changes and commit</x:String>
640+
<x:String x:Key="Text.WorkingCopy.ConfirmCommitWithoutFiles" xml:space="preserve">Empty commit detected! Do you want to continue (--allow-empty)?</x:String>
640641
<x:String x:Key="Text.WorkingCopy.Conflicts" xml:space="preserve">CONFLICTS DETECTED</x:String>
641642
<x:String x:Key="Text.WorkingCopy.Conflicts.Resolved" xml:space="preserve">FILE CONFLICTS ARE RESOLVED</x:String>
642643
<x:String x:Key="Text.WorkingCopy.IncludeUntracked" xml:space="preserve">INCLUDE UNTRACKED FILES</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@
635635
<x:String x:Key="Text.WorkingCopy.CommitMessageHelper" xml:space="preserve">历史输入/模板</x:String>
636636
<x:String x:Key="Text.WorkingCopy.CommitTip" xml:space="preserve">触发点击事件</x:String>
637637
<x:String x:Key="Text.WorkingCopy.CommitWithAutoStage" xml:space="preserve">自动暂存所有变更并提交</x:String>
638+
<x:String x:Key="Text.WorkingCopy.ConfirmCommitWithoutFiles" xml:space="preserve">提交未包含变更文件!是否继续(--allow-empty)?</x:String>
638639
<x:String x:Key="Text.WorkingCopy.Conflicts" xml:space="preserve">检测到冲突</x:String>
639640
<x:String x:Key="Text.WorkingCopy.Conflicts.Resolved" xml:space="preserve">文件冲突已解决</x:String>
640641
<x:String x:Key="Text.WorkingCopy.IncludeUntracked" xml:space="preserve">显示未跟踪文件</x:String>

src/Resources/Locales/zh_TW.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@
640640
<x:String x:Key="Text.WorkingCopy.CommitMessageHelper" xml:space="preserve">歷史輸入/範本</x:String>
641641
<x:String x:Key="Text.WorkingCopy.CommitTip" xml:space="preserve">觸發點擊事件</x:String>
642642
<x:String x:Key="Text.WorkingCopy.CommitWithAutoStage" xml:space="preserve">自動暫存全部變更並提交</x:String>
643+
<x:String x:Key="Text.WorkingCopy.ConfirmCommitWithoutFiles" xml:space="preserve">提交未包含變更檔案!是否繼續(--allow-empty)?</x:String>
643644
<x:String x:Key="Text.WorkingCopy.Conflicts" xml:space="preserve">檢測到衝突</x:String>
644645
<x:String x:Key="Text.WorkingCopy.Conflicts.Resolved" xml:space="preserve">檔案衝突已解決</x:String>
645646
<x:String x:Key="Text.WorkingCopy.IncludeUntracked" xml:space="preserve">顯示未追蹤檔案</x:String>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace SourceGit.ViewModels
2+
{
3+
public class ConfirmCommitWithoutFiles
4+
{
5+
public ConfirmCommitWithoutFiles(WorkingCopy wc, bool autoPush)
6+
{
7+
_wc = wc;
8+
_autoPush = autoPush;
9+
}
10+
11+
public void Continue()
12+
{
13+
_wc.CommitWithoutFiles(_autoPush);
14+
}
15+
16+
private readonly WorkingCopy _wc;
17+
private bool _autoPush;
18+
}
19+
}

src/ViewModels/WorkingCopy.cs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -410,17 +410,22 @@ public void Discard(List<Models.Change> changes)
410410

411411
public void Commit()
412412
{
413-
DoCommit(false, false);
413+
DoCommit(false, false, false);
414414
}
415415

416416
public void CommitWithAutoStage()
417417
{
418-
DoCommit(true, false);
418+
DoCommit(true, false, false);
419419
}
420420

421421
public void CommitWithPush()
422422
{
423-
DoCommit(false, true);
423+
DoCommit(false, true, false);
424+
}
425+
426+
public void CommitWithoutFiles(bool autoPush)
427+
{
428+
DoCommit(false, autoPush, true);
424429
}
425430

426431
public ContextMenu CreateContextMenuForUnstagedChanges()
@@ -1268,7 +1273,7 @@ private async void UseExternalMergeTool(Models.Change change)
12681273
_repo.SetWatcherEnabled(true);
12691274
}
12701275

1271-
private void DoCommit(bool autoStage, bool autoPush)
1276+
private void DoCommit(bool autoStage, bool autoPush, bool allowEmpty)
12721277
{
12731278
if (!PopupHost.CanCreatePopup())
12741279
{
@@ -1282,23 +1287,16 @@ private void DoCommit(bool autoStage, bool autoPush)
12821287
return;
12831288
}
12841289

1285-
if (!_useAmend)
1290+
if (!_useAmend && !allowEmpty)
12861291
{
1287-
if (autoStage)
1288-
{
1289-
if (_count == 0)
1290-
{
1291-
App.RaiseException(_repo.FullPath, "No files added to commit!");
1292-
return;
1293-
}
1294-
}
1295-
else
1292+
if ((autoStage && _count == 0) || (!autoStage && _staged.Count == 0))
12961293
{
1297-
if (_staged.Count == 0)
1294+
App.OpenDialog(new Views.ConfirmCommitWithoutFiles()
12981295
{
1299-
App.RaiseException(_repo.FullPath, "No files added to commit!");
1300-
return;
1301-
}
1296+
DataContext = new ConfirmCommitWithoutFiles(this, autoPush)
1297+
});
1298+
1299+
return;
13021300
}
13031301
}
13041302

@@ -1313,7 +1311,7 @@ private void DoCommit(bool autoStage, bool autoPush)
13131311
succ = new Commands.Add(_repo.FullPath, _repo.IncludeUntracked).Exec();
13141312

13151313
if (succ)
1316-
succ = new Commands.Commit(_repo.FullPath, _commitMessage, _useAmend).Exec();
1314+
succ = new Commands.Commit(_repo.FullPath, _commitMessage, _useAmend, true).Exec();
13171315

13181316
Dispatcher.UIThread.Post(() =>
13191317
{
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<v:ChromelessWindow xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:v="using:SourceGit.Views"
6+
xmlns:vm="using:SourceGit.ViewModels"
7+
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
8+
x:Class="SourceGit.Views.ConfirmCommitWithoutFiles"
9+
x:DataType="vm:ConfirmCommitWithoutFiles"
10+
x:Name="ThisControl"
11+
Icon="/App.ico"
12+
Title="{DynamicResource Text.Warn}"
13+
SizeToContent="WidthAndHeight"
14+
CanResize="False"
15+
WindowStartupLocation="CenterOwner">
16+
<Grid RowDefinitions="Auto,Auto,Auto">
17+
<!-- TitleBar -->
18+
<Grid Grid.Row="0" ColumnDefinitions="Auto,*,Auto" Height="30" IsVisible="{Binding !#ThisControl.UseSystemWindowFrame}">
19+
<Border Grid.Column="0" Grid.ColumnSpan="3"
20+
Background="{DynamicResource Brush.TitleBar}"
21+
BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border0}"
22+
PointerPressed="BeginMoveWindow"/>
23+
24+
<Path Grid.Column="0"
25+
Width="14" Height="14"
26+
Data="{StaticResource Icons.Error}"
27+
Margin="10,0,0,0"
28+
IsVisible="{OnPlatform True, macOS=False}"/>
29+
30+
<v:CaptionButtonsMacOS Grid.Column="0"
31+
Margin="0,2,0,0"
32+
IsCloseButtonOnly="True"
33+
IsVisible="{OnPlatform False, macOS=True}"/>
34+
35+
<TextBlock Grid.Column="0" Grid.ColumnSpan="3"
36+
Classes="bold"
37+
Text="{DynamicResource Text.Warn}"
38+
HorizontalAlignment="Center" VerticalAlignment="Center"
39+
IsHitTestVisible="False"/>
40+
41+
<v:CaptionButtons Grid.Column="2"
42+
IsCloseButtonOnly="True"
43+
IsVisible="{OnPlatform True, macOS=False}"/>
44+
</Grid>
45+
46+
<!-- Body -->
47+
<Border Grid.Row="1" Margin="16">
48+
<TextBlock Text="{DynamicResource Text.WorkingCopy.ConfirmCommitWithoutFiles}"/>
49+
</Border>
50+
51+
<!-- Buttons -->
52+
<StackPanel Grid.Row="2" Margin="0,0,0,16" Orientation="Horizontal" HorizontalAlignment="Center">
53+
<Button Classes="flat"
54+
Width="80"
55+
Height="30"
56+
Margin="4,0"
57+
Click="Sure"
58+
Content="{DynamicResource Text.Sure}"
59+
HorizontalAlignment="Center"
60+
HorizontalContentAlignment="Center"
61+
VerticalContentAlignment="Center"/>
62+
63+
<Button Classes="flat primary"
64+
Width="80"
65+
Height="30"
66+
Margin="4,0"
67+
Click="CloseWindow"
68+
Content="{DynamicResource Text.Cancel}"
69+
HorizontalAlignment="Center"
70+
HorizontalContentAlignment="Center"
71+
VerticalContentAlignment="Center"/>
72+
</StackPanel>
73+
</Grid>
74+
</v:ChromelessWindow>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Avalonia.Input;
2+
using Avalonia.Interactivity;
3+
4+
namespace SourceGit.Views
5+
{
6+
public partial class ConfirmCommitWithoutFiles : ChromelessWindow
7+
{
8+
public ConfirmCommitWithoutFiles()
9+
{
10+
InitializeComponent();
11+
}
12+
13+
private void BeginMoveWindow(object _, PointerPressedEventArgs e)
14+
{
15+
BeginMoveDrag(e);
16+
}
17+
18+
private void Sure(object _1, RoutedEventArgs _2)
19+
{
20+
if (DataContext is ViewModels.ConfirmCommitWithoutFiles vm)
21+
{
22+
vm.Continue();
23+
}
24+
25+
Close();
26+
}
27+
28+
private void CloseWindow(object _1, RoutedEventArgs _2)
29+
{
30+
Close();
31+
}
32+
}
33+
}

src/Views/ConventionalCommitMessageBuilder.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
SizeToContent="Height"
1717
CanResize="False"
1818
WindowStartupLocation="CenterOwner">
19-
<Grid RowDefinitions="Auto,Auto,Auto" MinWidth="494">
19+
<Grid RowDefinitions="Auto,Auto,Auto">
2020
<!-- TitleBar -->
2121
<Grid Grid.Row="0" ColumnDefinitions="Auto,*,Auto" Height="30" IsVisible="{Binding !#ThisControl.UseSystemWindowFrame}">
2222
<Border Grid.Column="0" Grid.ColumnSpan="3"

0 commit comments

Comments
 (0)