Skip to content

Commit fba1b63

Browse files
committed
feature: supports to change submodule's branch (#1513)
Signed-off-by: leo <[email protected]>
1 parent 7672218 commit fba1b63

File tree

12 files changed

+213
-12
lines changed

12 files changed

+213
-12
lines changed

src/Commands/QuerySubmodules.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public QuerySubmodules(string repo)
7070
{
7171
var modules = new Dictionary<string, ModuleInfo>();
7272
lines = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
73+
7374
foreach (var line in lines)
7475
{
7576
var match = REG_FORMAT_MODULE_INFO().Match(line);
@@ -81,21 +82,39 @@ public QuerySubmodules(string repo)
8182

8283
if (!modules.TryGetValue(name, out var m))
8384
{
84-
m = new ModuleInfo();
85-
modules.Add(name, m);
85+
// Find name alias.
86+
foreach (var kv in modules)
87+
{
88+
if (kv.Value.Path.Equals(name, StringComparison.Ordinal))
89+
{
90+
m = kv.Value;
91+
break;
92+
}
93+
}
94+
95+
if (m == null)
96+
{
97+
m = new ModuleInfo();
98+
modules.Add(name, m);
99+
}
86100
}
87101

88102
if (key.Equals("path", StringComparison.Ordinal))
89103
m.Path = val;
90104
else if (key.Equals("url", StringComparison.Ordinal))
91105
m.URL = val;
106+
else if (key.Equals("branch", StringComparison.Ordinal))
107+
m.Branch = val;
92108
}
93109
}
94110

95111
foreach (var kv in modules)
96112
{
97113
if (map.TryGetValue(kv.Value.Path, out var m))
114+
{
98115
m.URL = kv.Value.URL;
116+
m.Branch = kv.Value.Branch;
117+
}
99118
}
100119
}
101120
}
@@ -138,6 +157,7 @@ private class ModuleInfo
138157
{
139158
public string Path { get; set; } = string.Empty;
140159
public string URL { get; set; } = string.Empty;
160+
public string Branch { get; set; } = string.Empty;
141161
}
142162
}
143163
}

src/Commands/Submodule.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,22 @@ public async Task<bool> AddAsync(string url, string relativePath, bool recursive
2727
return await ExecAsync().ConfigureAwait(false);
2828
}
2929

30-
public async Task<bool> SetURL(string path, string url)
30+
public async Task<bool> SetURLAsync(string path, string url)
3131
{
3232
Args = $"submodule set-url -- \"{path}\" \"{url}\"";
3333
return await ExecAsync().ConfigureAwait(false);
3434
}
3535

36+
public async Task<bool> SetBranchAsync(string path, string branch)
37+
{
38+
if (string.IsNullOrEmpty(branch))
39+
Args = $"submodule set-branch -d -- \"{path}\"";
40+
else
41+
Args = $"submodule set-branch -b \"{branch}\" -- \"{path}\"";
42+
43+
return await ExecAsync().ConfigureAwait(false);
44+
}
45+
3646
public async Task<bool> UpdateAsync(List<string> modules, bool init, bool recursive, bool useRemote = false)
3747
{
3848
var builder = new StringBuilder();

src/Models/Submodule.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class Submodule
1414
public string Path { get; set; } = string.Empty;
1515
public string SHA { get; set; } = string.Empty;
1616
public string URL { get; set; } = string.Empty;
17+
public string Branch { get; set; } = string.Empty;
1718
public SubmoduleStatus Status { get; set; } = SubmoduleStatus.Normal;
1819
public bool IsDirty => Status > SubmoduleStatus.NotInited;
1920
}

src/Resources/Locales/en_US.axaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,11 @@
704704
<x:String x:Key="Text.SelfUpdate.IgnoreThisVersion" xml:space="preserve">Skip This Version</x:String>
705705
<x:String x:Key="Text.SelfUpdate.Title" xml:space="preserve">Software Update</x:String>
706706
<x:String x:Key="Text.SelfUpdate.UpToDate" xml:space="preserve">There are currently no updates available.</x:String>
707+
<x:String x:Key="Text.SetSubmoduleBranch" xml:space="preserve">Set Submodule's Branch</x:String>
708+
<x:String x:Key="Text.SetSubmoduleBranch.Submodule" xml:space="preserve">Submodule:</x:String>
709+
<x:String x:Key="Text.SetSubmoduleBranch.Current" xml:space="preserve">Current:</x:String>
710+
<x:String x:Key="Text.SetSubmoduleBranch.New" xml:space="preserve">Change To:</x:String>
711+
<x:String x:Key="Text.SetSubmoduleBranch.New.Tip" xml:space="preserve">Optional. Set to default when it is empty.</x:String>
707712
<x:String x:Key="Text.SetUpstream" xml:space="preserve">Set Tracking Branch</x:String>
708713
<x:String x:Key="Text.SetUpstream.Local" xml:space="preserve">Branch:</x:String>
709714
<x:String x:Key="Text.SetUpstream.Unset" xml:space="preserve">Unset upstream</x:String>
@@ -742,6 +747,7 @@
742747
<x:String x:Key="Text.Statistics.TotalCommits" xml:space="preserve">COMMITS: </x:String>
743748
<x:String x:Key="Text.Submodule" xml:space="preserve">SUBMODULES</x:String>
744749
<x:String x:Key="Text.Submodule.Add" xml:space="preserve">Add Submodule</x:String>
750+
<x:String x:Key="Text.Submodule.Branch" xml:space="preserve">Branch</x:String>
745751
<x:String x:Key="Text.Submodule.CopyPath" xml:space="preserve">Relative Path</x:String>
746752
<x:String x:Key="Text.Submodule.Deinit" xml:space="preserve">De-initialize</x:String>
747753
<x:String x:Key="Text.Submodule.FetchNested" xml:space="preserve">Fetch nested submodules</x:String>
@@ -751,6 +757,7 @@
751757
<x:String x:Key="Text.Submodule.RelativePath" xml:space="preserve">Relative Path:</x:String>
752758
<x:String x:Key="Text.Submodule.RelativePath.Placeholder" xml:space="preserve">Relative folder to store this module.</x:String>
753759
<x:String x:Key="Text.Submodule.Remove" xml:space="preserve">Delete</x:String>
760+
<x:String x:Key="Text.Submodule.SetBranch" xml:space="preserve">Set Branch</x:String>
754761
<x:String x:Key="Text.Submodule.SetURL" xml:space="preserve">Change URL</x:String>
755762
<x:String x:Key="Text.Submodule.Status" xml:space="preserve">STATUS</x:String>
756763
<x:String x:Key="Text.Submodule.Status.Modified" xml:space="preserve">modified</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,11 @@
708708
<x:String x:Key="Text.SelfUpdate.IgnoreThisVersion" xml:space="preserve">忽略此版本</x:String>
709709
<x:String x:Key="Text.SelfUpdate.Title" xml:space="preserve">软件更新</x:String>
710710
<x:String x:Key="Text.SelfUpdate.UpToDate" xml:space="preserve">当前已是最新版本。</x:String>
711+
<x:String x:Key="Text.SetSubmoduleBranch" xml:space="preserve">修改子模块追踪分支</x:String>
712+
<x:String x:Key="Text.SetSubmoduleBranch.Submodule" xml:space="preserve">子模块 :</x:String>
713+
<x:String x:Key="Text.SetSubmoduleBranch.Current" xml:space="preserve">当前追踪分支 :</x:String>
714+
<x:String x:Key="Text.SetSubmoduleBranch.New" xml:space="preserve">修改为 :</x:String>
715+
<x:String x:Key="Text.SetSubmoduleBranch.New.Tip" xml:space="preserve">可选。当不填写时,恢复默认追踪分支。</x:String>
711716
<x:String x:Key="Text.SetUpstream" xml:space="preserve">切换上游分支</x:String>
712717
<x:String x:Key="Text.SetUpstream.Local" xml:space="preserve">本地分支 :</x:String>
713718
<x:String x:Key="Text.SetUpstream.Unset" xml:space="preserve">取消追踪</x:String>
@@ -746,6 +751,7 @@
746751
<x:String x:Key="Text.Statistics.TotalCommits" xml:space="preserve">提交次数: </x:String>
747752
<x:String x:Key="Text.Submodule" xml:space="preserve">子模块</x:String>
748753
<x:String x:Key="Text.Submodule.Add" xml:space="preserve">添加子模块</x:String>
754+
<x:String x:Key="Text.Submodule.Branch" xml:space="preserve">跟踪分支</x:String>
749755
<x:String x:Key="Text.Submodule.CopyPath" xml:space="preserve">相对路径</x:String>
750756
<x:String x:Key="Text.Submodule.Deinit" xml:space="preserve">取消初始化</x:String>
751757
<x:String x:Key="Text.Submodule.FetchNested" xml:space="preserve">拉取子孙模块</x:String>
@@ -755,6 +761,7 @@
755761
<x:String x:Key="Text.Submodule.RelativePath" xml:space="preserve">相对仓库路径 :</x:String>
756762
<x:String x:Key="Text.Submodule.RelativePath.Placeholder" xml:space="preserve">本地存放的相对路径。</x:String>
757763
<x:String x:Key="Text.Submodule.Remove" xml:space="preserve">删除</x:String>
764+
<x:String x:Key="Text.Submodule.SetBranch" xml:space="preserve">修改跟踪分支</x:String>
758765
<x:String x:Key="Text.Submodule.SetURL" xml:space="preserve">修改远程地址</x:String>
759766
<x:String x:Key="Text.Submodule.Status" xml:space="preserve">状态</x:String>
760767
<x:String x:Key="Text.Submodule.Status.Modified" xml:space="preserve">未提交修改</x:String>

src/Resources/Locales/zh_TW.axaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,11 @@
708708
<x:String x:Key="Text.SelfUpdate.IgnoreThisVersion" xml:space="preserve">忽略此版本</x:String>
709709
<x:String x:Key="Text.SelfUpdate.Title" xml:space="preserve">軟體更新</x:String>
710710
<x:String x:Key="Text.SelfUpdate.UpToDate" xml:space="preserve">目前已是最新版本。</x:String>
711+
<x:String x:Key="Text.SetSubmoduleBranch" xml:space="preserve">設定子模組的追蹤分支</x:String>
712+
<x:String x:Key="Text.SetSubmoduleBranch.Submodule" xml:space="preserve">子模組:</x:String>
713+
<x:String x:Key="Text.SetSubmoduleBranch.Current" xml:space="preserve">目前追蹤分支:</x:String>
714+
<x:String x:Key="Text.SetSubmoduleBranch.New" xml:space="preserve">變更為:</x:String>
715+
<x:String x:Key="Text.SetSubmoduleBranch.New.Tip" xml:space="preserve">選購。為空時設定為預設值。</x:String>
711716
<x:String x:Key="Text.SetUpstream" xml:space="preserve">切換上游分支</x:String>
712717
<x:String x:Key="Text.SetUpstream.Local" xml:space="preserve">本機分支:</x:String>
713718
<x:String x:Key="Text.SetUpstream.Unset" xml:space="preserve">取消設定上游分支</x:String>
@@ -746,6 +751,7 @@
746751
<x:String x:Key="Text.Statistics.TotalCommits" xml:space="preserve">提交次數:</x:String>
747752
<x:String x:Key="Text.Submodule" xml:space="preserve">子模組</x:String>
748753
<x:String x:Key="Text.Submodule.Add" xml:space="preserve">新增子模組</x:String>
754+
<x:String x:Key="Text.Submodule.Branch" xml:space="preserve">追蹤分支</x:String>
749755
<x:String x:Key="Text.Submodule.CopyPath" xml:space="preserve">相對路徑</x:String>
750756
<x:String x:Key="Text.Submodule.Deinit" xml:space="preserve">取消初始化</x:String>
751757
<x:String x:Key="Text.Submodule.FetchNested" xml:space="preserve">提取子模組</x:String>
@@ -755,6 +761,7 @@
755761
<x:String x:Key="Text.Submodule.RelativePath" xml:space="preserve">相對存放庫路徑:</x:String>
756762
<x:String x:Key="Text.Submodule.RelativePath.Placeholder" xml:space="preserve">本機存放的相對路徑。</x:String>
757763
<x:String x:Key="Text.Submodule.Remove" xml:space="preserve">刪除</x:String>
764+
<x:String x:Key="Text.Submodule.SetBranch" xml:space="preserve">更改追蹤分支</x:String>
758765
<x:String x:Key="Text.Submodule.SetURL" xml:space="preserve">更改遠端網址</x:String>
759766
<x:String x:Key="Text.Submodule.Status" xml:space="preserve">狀態</x:String>
760767
<x:String x:Key="Text.Submodule.Status.Modified" xml:space="preserve">未提交變更</x:String>

src/ViewModels/ChangeSubmoduleUrl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public override async Task<bool> Sure()
4747

4848
var succ = await new Commands.Submodule(_repo.FullPath)
4949
.Use(log)
50-
.SetURL(Submodule.Path, _url);
50+
.SetURLAsync(Submodule.Path, _url);
5151

5252
log.Complete();
5353
_repo.SetWatcherEnabled(true);

src/ViewModels/Repository.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,8 +1272,9 @@ public void RefreshSubmodules()
12721272
}
12731273

12741274
hasChanged = !exist.SHA.Equals(module.SHA, StringComparison.Ordinal) ||
1275-
!exist.URL.Equals(module.URL, StringComparison.Ordinal) ||
1276-
exist.Status != module.Status;
1275+
!exist.Branch.Equals(module.Branch, StringComparison.Ordinal) ||
1276+
!exist.URL.Equals(module.URL, StringComparison.Ordinal) ||
1277+
exist.Status != module.Status;
12771278

12781279
if (hasChanged)
12791280
break;
@@ -2584,6 +2585,16 @@ public ContextMenu CreateContextMenuForSubmodule(Models.Submodule submodule)
25842585
ev.Handled = true;
25852586
};
25862587

2588+
var setBranch = new MenuItem();
2589+
setBranch.Header = App.Text("Submodule.SetBranch");
2590+
setBranch.Icon = App.CreateMenuIcon("Icons.Track");
2591+
setBranch.Click += (_, ev) =>
2592+
{
2593+
if (CanCreatePopup())
2594+
ShowPopup(new SetSubmoduleBranch(this, submodule));
2595+
ev.Handled = true;
2596+
};
2597+
25872598
var deinit = new MenuItem();
25882599
deinit.Header = App.Text("Submodule.Deinit");
25892600
deinit.Icon = App.CreateMenuIcon("Icons.Undo");
@@ -2641,10 +2652,20 @@ public ContextMenu CreateContextMenuForSubmodule(Models.Submodule submodule)
26412652
ev.Handled = true;
26422653
};
26432654

2655+
var copyBranch = new MenuItem();
2656+
copyBranch.Header = App.Text("Submodule.Branch");
2657+
copyBranch.Icon = App.CreateMenuIcon("Icons.Branch");
2658+
copyBranch.Click += async (_, ev) =>
2659+
{
2660+
await App.CopyTextAsync(submodule.Branch);
2661+
ev.Handled = true;
2662+
};
2663+
26442664
var copy = new MenuItem();
26452665
copy.Header = App.Text("Copy");
26462666
copy.Icon = App.CreateMenuIcon("Icons.Copy");
26472667
copy.Items.Add(copySHA);
2668+
copy.Items.Add(copyBranch);
26482669
copy.Items.Add(copyRelativePath);
26492670
copy.Items.Add(copyURL);
26502671

@@ -2653,6 +2674,7 @@ public ContextMenu CreateContextMenuForSubmodule(Models.Submodule submodule)
26532674
menu.Items.Add(new MenuItem() { Header = "-" });
26542675
menu.Items.Add(update);
26552676
menu.Items.Add(setURL);
2677+
menu.Items.Add(setBranch);
26562678
menu.Items.Add(move);
26572679
menu.Items.Add(deinit);
26582680
menu.Items.Add(rm);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace SourceGit.ViewModels
5+
{
6+
public class SetSubmoduleBranch : Popup
7+
{
8+
public Models.Submodule Submodule
9+
{
10+
get;
11+
}
12+
13+
public string ChangeTo
14+
{
15+
get => _changeTo;
16+
set => SetProperty(ref _changeTo, value);
17+
}
18+
19+
public SetSubmoduleBranch(Repository repo, Models.Submodule submodule)
20+
{
21+
_repo = repo;
22+
_changeTo = submodule.Branch;
23+
Submodule = submodule;
24+
}
25+
26+
public override async Task<bool> Sure()
27+
{
28+
ProgressDescription = "Set submodule's branch ...";
29+
30+
if (_changeTo.Equals(Submodule.Branch, StringComparison.Ordinal))
31+
return true;
32+
33+
var log = _repo.CreateLog("Set Submodule's Branch");
34+
_repo.SetWatcherEnabled(false);
35+
Use(log);
36+
37+
var succ = await new Commands.Submodule(_repo.FullPath)
38+
.Use(log)
39+
.SetBranchAsync(Submodule.Path, _changeTo);
40+
41+
log.Complete();
42+
_repo.SetWatcherEnabled(true);
43+
return succ;
44+
}
45+
46+
private readonly Repository _repo;
47+
private string _changeTo;
48+
}
49+
}

src/Views/SetSubmoduleBranch.axaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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:vm="using:SourceGit.ViewModels"
6+
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
7+
x:Class="SourceGit.Views.SetSubmoduleBranch"
8+
x:DataType="vm:SetSubmoduleBranch">
9+
<StackPanel Orientation="Vertical" Margin="8,0">
10+
<TextBlock FontSize="18"
11+
Classes="bold"
12+
Text="{DynamicResource Text.SetSubmoduleBranch}"/>
13+
<Grid Margin="0,16,0,0" RowDefinitions="32,32,32" ColumnDefinitions="120,*">
14+
<TextBlock Grid.Row="0" Grid.Column="0"
15+
HorizontalAlignment="Right" VerticalAlignment="Center"
16+
Margin="0,0,8,0"
17+
Text="{DynamicResource Text.SetSubmoduleBranch.Submodule}"/>
18+
<StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal">
19+
<Path Width="14" Height="14" Data="{StaticResource Icons.Submodule}"/>
20+
<TextBlock Margin="6,0,0,0" Text="{Binding Submodule.Path}"/>
21+
</StackPanel>
22+
23+
<TextBlock Grid.Row="1" Grid.Column="0"
24+
HorizontalAlignment="Right" VerticalAlignment="Center"
25+
Margin="0,0,8,0"
26+
Text="{DynamicResource Text.SetSubmoduleBranch.Current}"/>
27+
<StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal">
28+
<Path Width="14" Height="14" Data="{StaticResource Icons.Branch}"/>
29+
<TextBlock Margin="6,0,0,0"
30+
Text="{Binding Submodule.Branch, Mode=OneWay}"
31+
IsVisible="{Binding Submodule.Branch, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"/>
32+
<TextBlock Margin="6,0,0,0"
33+
Text="--"
34+
IsVisible="{Binding Submodule.Branch, Converter={x:Static StringConverters.IsNullOrEmpty}}"/>
35+
</StackPanel>
36+
37+
<TextBlock Grid.Row="2" Grid.Column="0"
38+
HorizontalAlignment="Right" VerticalAlignment="Center"
39+
Margin="0,0,8,0"
40+
Text="{DynamicResource Text.SetSubmoduleBranch.New}"/>
41+
<TextBox Grid.Row="2" Grid.Column="1"
42+
Height="26"
43+
VerticalAlignment="Center"
44+
CornerRadius="2"
45+
Watermark="{DynamicResource Text.SetSubmoduleBranch.New.Tip}"
46+
Text="{Binding ChangeTo, Mode=TwoWay}"/>
47+
</Grid>
48+
</StackPanel>
49+
</UserControl>

0 commit comments

Comments
 (0)