Skip to content

Commit b519ead

Browse files
committed
feature: supports to add ignored file(s) locally (saved in $GIT_DIR/info/exclude) (#1447)
Signed-off-by: leo <[email protected]>
1 parent 5b6e980 commit b519ead

File tree

9 files changed

+168
-29
lines changed

9 files changed

+168
-29
lines changed

src/Commands/GitIgnore.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/Models/GitIgnoreFile.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
4+
namespace SourceGit.Models
5+
{
6+
public class GitIgnoreFile
7+
{
8+
public static readonly List<GitIgnoreFile> Supported = [new(true), new(false)];
9+
10+
public bool IsShared { get; set; }
11+
public string File => IsShared ? ".gitignore" : "$GIT_DIR/info/exclude";
12+
public string Desc => IsShared ? "Shared" : "Private";
13+
14+
public GitIgnoreFile(bool isShared)
15+
{
16+
IsShared = isShared;
17+
}
18+
19+
public string GetFullPath(string repoPath, string gitDir)
20+
{
21+
return IsShared ? Path.Combine(repoPath, ".gitignore") : Path.Combine(gitDir, "info", "exclude");
22+
}
23+
}
24+
}

src/Resources/Locales/en_US.axaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
<x:String x:Key="Text.About" xml:space="preserve">About</x:String>
33
<x:String x:Key="Text.About.Menu" xml:space="preserve">About SourceGit</x:String>
44
<x:String x:Key="Text.About.SubTitle" xml:space="preserve">Opensource &amp; Free Git GUI Client</x:String>
5+
<x:String x:Key="Text.AddToIgnore" xml:space="preserve">Add File(s) To Ignore</x:String>
6+
<x:String x:Key="Text.AddToIgnore.Pattern" xml:space="preserve">Pattern:</x:String>
7+
<x:String x:Key="Text.AddToIgnore.Storage" xml:space="preserve">Storage File:</x:String>
58
<x:String x:Key="Text.AddWorktree" xml:space="preserve">Add Worktree</x:String>
69
<x:String x:Key="Text.AddWorktree.Location" xml:space="preserve">Location:</x:String>
710
<x:String x:Key="Text.AddWorktree.Location.Placeholder" xml:space="preserve">Path for this worktree. Relative path is supported.</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
<x:String x:Key="Text.About" xml:space="preserve">关于软件</x:String>
77
<x:String x:Key="Text.About.Menu" xml:space="preserve">关于本软件</x:String>
88
<x:String x:Key="Text.About.SubTitle" xml:space="preserve">开源免费的Git客户端</x:String>
9+
<x:String x:Key="Text.AddToIgnore" xml:space="preserve">新增忽略文件</x:String>
10+
<x:String x:Key="Text.AddToIgnore.Pattern" xml:space="preserve">匹配模式 :</x:String>
11+
<x:String x:Key="Text.AddToIgnore.Storage" xml:space="preserve">保存位置 :</x:String>
912
<x:String x:Key="Text.AddWorktree" xml:space="preserve">新增工作树</x:String>
1013
<x:String x:Key="Text.AddWorktree.Location" xml:space="preserve">工作树路径 :</x:String>
1114
<x:String x:Key="Text.AddWorktree.Location.Placeholder" xml:space="preserve">填写该工作树的路径。支持相对路径。</x:String>

src/Resources/Locales/zh_TW.axaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
<x:String x:Key="Text.About" xml:space="preserve">關於</x:String>
77
<x:String x:Key="Text.About.Menu" xml:space="preserve">關於 SourceGit</x:String>
88
<x:String x:Key="Text.About.SubTitle" xml:space="preserve">開源免費的 Git 客戶端</x:String>
9+
<x:String x:Key="Text.AddToIgnore" xml:space="preserve">新增忽略檔案</x:String>
10+
<x:String x:Key="Text.AddToIgnore.Pattern" xml:space="preserve">匹配模式 :</x:String>
11+
<x:String x:Key="Text.AddToIgnore.Storage" xml:space="preserve">儲存路徑 :</x:String>
912
<x:String x:Key="Text.AddWorktree" xml:space="preserve">新增工作區</x:String>
1013
<x:String x:Key="Text.AddWorktree.Location" xml:space="preserve">工作區路徑:</x:String>
1114
<x:String x:Key="Text.AddWorktree.Location.Placeholder" xml:space="preserve">填寫該工作區的路徑。支援相對路徑。</x:String>

src/ViewModels/AddToIgnore.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using System.IO;
3+
using System.Threading.Tasks;
4+
5+
namespace SourceGit.ViewModels
6+
{
7+
public class AddToIgnore : Popup
8+
{
9+
[Required(ErrorMessage = "Ignore pattern is required!")]
10+
public string Pattern
11+
{
12+
get => _pattern;
13+
set => SetProperty(ref _pattern, value, true);
14+
}
15+
16+
[Required(ErrorMessage = "Storage file is required!!!")]
17+
public Models.GitIgnoreFile StorageFile
18+
{
19+
get;
20+
set;
21+
}
22+
23+
public AddToIgnore(Repository repo, string pattern)
24+
{
25+
_repo = repo;
26+
_pattern = pattern;
27+
StorageFile = Models.GitIgnoreFile.Supported[0];
28+
}
29+
30+
public override Task<bool> Sure()
31+
{
32+
_repo.SetWatcherEnabled(false);
33+
ProgressDescription = "Adding Ignored File(s) ...";
34+
35+
return Task.Run(() =>
36+
{
37+
var file = StorageFile.GetFullPath(_repo.FullPath, _repo.GitDir);
38+
if (!File.Exists(file))
39+
{
40+
File.WriteAllLines(file, [_pattern]);
41+
}
42+
else
43+
{
44+
var org = File.ReadAllText(file);
45+
if (!org.EndsWith('\n'))
46+
File.AppendAllLines(file, ["", _pattern]);
47+
else
48+
File.AppendAllLines(file, [_pattern]);
49+
}
50+
51+
CallUIThread(() =>
52+
{
53+
_repo.MarkWorkingCopyDirtyManually();
54+
_repo.SetWatcherEnabled(true);
55+
});
56+
57+
return true;
58+
});
59+
}
60+
61+
private readonly Repository _repo;
62+
private string _pattern;
63+
}
64+
}

src/ViewModels/WorkingCopy.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,8 @@ public ContextMenu CreateContextMenuForUnstagedChanges(string selectedSingleFold
771771
ignoreFolder.Header = App.Text("WorkingCopy.AddToGitIgnore.InFolder");
772772
ignoreFolder.Click += (_, e) =>
773773
{
774-
Commands.GitIgnore.Add(_repo.FullPath, $"{selectedSingleFolder}/");
774+
if (_repo.CanCreatePopup())
775+
_repo.ShowPopup(new AddToIgnore(_repo, $"{selectedSingleFolder}/"));
775776
e.Handled = true;
776777
};
777778
addToIgnore.Items.Add(ignoreFolder);
@@ -783,7 +784,8 @@ public ContextMenu CreateContextMenuForUnstagedChanges(string selectedSingleFold
783784
singleFile.Header = App.Text("WorkingCopy.AddToGitIgnore.SingleFile");
784785
singleFile.Click += (_, e) =>
785786
{
786-
Commands.GitIgnore.Add(_repo.FullPath, change.Path);
787+
if (_repo.CanCreatePopup())
788+
_repo.ShowPopup(new AddToIgnore(_repo, change.Path));
787789
e.Handled = true;
788790
};
789791
addToIgnore.Items.Add(singleFile);
@@ -794,7 +796,8 @@ public ContextMenu CreateContextMenuForUnstagedChanges(string selectedSingleFold
794796
byExtension.Header = App.Text("WorkingCopy.AddToGitIgnore.Extension", extension);
795797
byExtension.Click += (_, e) =>
796798
{
797-
Commands.GitIgnore.Add(_repo.FullPath, $"*{extension}");
799+
if (_repo.CanCreatePopup())
800+
_repo.ShowPopup(new AddToIgnore(_repo, $"*{extension}"));
798801
e.Handled = true;
799802
};
800803
addToIgnore.Items.Add(byExtension);
@@ -805,7 +808,8 @@ public ContextMenu CreateContextMenuForUnstagedChanges(string selectedSingleFold
805808
byExtensionInSameFolder.Click += (_, e) =>
806809
{
807810
var dir = Path.GetDirectoryName(change.Path)!.Replace('\\', '/').TrimEnd('/');
808-
Commands.GitIgnore.Add(_repo.FullPath, $"{dir}/*{extension}");
811+
if (_repo.CanCreatePopup())
812+
_repo.ShowPopup(new AddToIgnore(_repo, $"{dir}/*{extension}"));
809813
e.Handled = true;
810814
};
811815
addToIgnore.Items.Add(byExtensionInSameFolder);
@@ -827,7 +831,8 @@ public ContextMenu CreateContextMenuForUnstagedChanges(string selectedSingleFold
827831
ignoreFolder.Header = App.Text("WorkingCopy.AddToGitIgnore.InFolder");
828832
ignoreFolder.Click += (_, e) =>
829833
{
830-
Commands.GitIgnore.Add(_repo.FullPath, $"{selectedSingleFolder}/");
834+
if (_repo.CanCreatePopup())
835+
_repo.ShowPopup(new AddToIgnore(_repo, $"{selectedSingleFolder}/"));
831836
e.Handled = true;
832837
};
833838
addToIgnore.Items.Add(ignoreFolder);
@@ -1133,7 +1138,8 @@ public ContextMenu CreateContextMenuForUnstagedChanges(string selectedSingleFold
11331138
ignoreFolder.Header = App.Text("WorkingCopy.AddToGitIgnore.InFolder");
11341139
ignoreFolder.Click += (_, e) =>
11351140
{
1136-
Commands.GitIgnore.Add(_repo.FullPath, $"{selectedSingleFolder}/");
1141+
if (_repo.CanCreatePopup())
1142+
_repo.ShowPopup(new AddToIgnore(_repo, $"{selectedSingleFolder}/"));
11371143
e.Handled = true;
11381144
};
11391145

src/Views/AddToIgnore.axaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<UserControl 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:m="using:SourceGit.Models"
6+
xmlns:vm="using:SourceGit.ViewModels"
7+
xmlns:v="using:SourceGit.Views"
8+
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
9+
x:Class="SourceGit.Views.AddToIgnore"
10+
x:DataType="vm:AddToIgnore">
11+
<StackPanel Orientation="Vertical" Margin="8,0">
12+
<TextBlock FontSize="18"
13+
Classes="bold"
14+
Text="{DynamicResource Text.AddToIgnore}"/>
15+
<Grid Margin="0,16,0,0" RowDefinitions="32,32" ColumnDefinitions="120,*">
16+
<TextBlock Grid.Row="0" Grid.Column="0"
17+
HorizontalAlignment="Right" VerticalAlignment="Center"
18+
Margin="0,0,8,0"
19+
Text="{DynamicResource Text.AddToIgnore.Pattern}"/>
20+
<TextBox Grid.Row="0" Grid.Column="1"
21+
Height="28"
22+
CornerRadius="3"
23+
Text="{Binding Pattern, Mode=TwoWay}"
24+
v:AutoFocusBehaviour.IsEnabled="True"/>
25+
26+
<TextBlock Grid.Row="1" Grid.Column="0"
27+
HorizontalAlignment="Right" VerticalAlignment="Center"
28+
Margin="0,0,8,0"
29+
Text="{DynamicResource Text.AddToIgnore.Storage}"/>
30+
<ComboBox Grid.Row="1" Grid.Column="1"
31+
Height="28" Padding="8,0"
32+
VerticalAlignment="Center" HorizontalAlignment="Stretch"
33+
ItemsSource="{Binding Source={x:Static m:GitIgnoreFile.Supported}}"
34+
SelectedItem="{Binding StorageFile, Mode=TwoWay}">
35+
<ComboBox.ItemTemplate>
36+
<DataTemplate DataType="m:GitIgnoreFile">
37+
<TextBlock VerticalAlignment="Center">
38+
<Run Text="{Binding File, Mode=OneWay}"/>
39+
<Run Text="" Foreground="{DynamicResource Brush.FG2}"/>
40+
<Run Text="{Binding Desc, Mode=OneWay}" Foreground="{DynamicResource Brush.FG2}"/>
41+
</TextBlock>
42+
</DataTemplate>
43+
</ComboBox.ItemTemplate>
44+
</ComboBox>
45+
</Grid>
46+
</StackPanel>
47+
</UserControl>

src/Views/AddToIgnore.axaml.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Avalonia.Controls;
2+
3+
namespace SourceGit.Views
4+
{
5+
public partial class AddToIgnore : UserControl
6+
{
7+
public AddToIgnore()
8+
{
9+
InitializeComponent();
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)