Skip to content

Commit 1442dcf

Browse files
committed
feature: allow fetch the latest remote changes into local branch which is not current branch (#617)
Signed-off-by: leo <[email protected]>
1 parent 489b578 commit 1442dcf

File tree

8 files changed

+102
-2
lines changed

8 files changed

+102
-2
lines changed

src/Commands/Fetch.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ public Fetch(string repo, string remote, bool noTags, Action<string> outputHandl
2121
Args += remote;
2222
}
2323

24+
public Fetch(string repo, Models.Branch local, Models.Branch remote, Action<string> outputHandler)
25+
{
26+
_outputHandler = outputHandler;
27+
WorkingDirectory = repo;
28+
Context = repo;
29+
TraitErrorAsOutput = true;
30+
SSHKey = new Config(repo).Get($"remote.{remote.Remote}.sshkey");
31+
Args = $"fetch --progress --verbose {remote.Remote} {remote.Name}:{local.Name}";
32+
}
33+
2434
protected override void OnReadline(string line)
2535
{
2636
_outputHandler?.Invoke(line);

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<x:String x:Key="Text.BranchCM.DeleteMultiBranches" xml:space="preserve">Delete selected {0} branches</x:String>
5656
<x:String x:Key="Text.BranchCM.DiscardAll" xml:space="preserve">Discard all changes</x:String>
5757
<x:String x:Key="Text.BranchCM.FastForward" xml:space="preserve">Fast-Forward to ${0}$</x:String>
58+
<x:String x:Key="Text.BranchCM.FetchInto" xml:space="preserve">Fetch ${0}$ into ${1}$...</x:String>
5859
<x:String x:Key="Text.BranchCM.Finish" xml:space="preserve">Git Flow - Finish ${0}$</x:String>
5960
<x:String x:Key="Text.BranchCM.Merge" xml:space="preserve">Merge ${0}$ into ${1}$...</x:String>
6061
<x:String x:Key="Text.BranchCM.Pull" xml:space="preserve">Pull ${0}$</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<x:String x:Key="Text.BranchCM.DeleteMultiBranches" xml:space="preserve">删除选中的 {0} 个分支</x:String>
5959
<x:String x:Key="Text.BranchCM.DiscardAll" xml:space="preserve">放弃所有更改</x:String>
6060
<x:String x:Key="Text.BranchCM.FastForward" xml:space="preserve">快进(fast-forward)到 ${0}$</x:String>
61+
<x:String x:Key="Text.BranchCM.FetchInto" xml:space="preserve">拉取(fetch) ${0}$ 至 ${1}$...</x:String>
6162
<x:String x:Key="Text.BranchCM.Finish" xml:space="preserve">GIT工作流 - 完成 ${0}$</x:String>
6263
<x:String x:Key="Text.BranchCM.Merge" xml:space="preserve">合并 ${0}$ 到 ${1}$...</x:String>
6364
<x:String x:Key="Text.BranchCM.Pull" xml:space="preserve">拉回(pull) ${0}$</x:String>

src/Resources/Locales/zh_TW.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<x:String x:Key="Text.BranchCM.DeleteMultiBranches" xml:space="preserve">刪除所選的 {0} 個分支</x:String>
5959
<x:String x:Key="Text.BranchCM.DiscardAll" xml:space="preserve">捨棄所有變更</x:String>
6060
<x:String x:Key="Text.BranchCM.FastForward" xml:space="preserve">快轉 (fast-forward) 到 ${0}$</x:String>
61+
<x:String x:Key="Text.BranchCM.FetchInto" xml:space="preserve">提取(fetch) ${0}$ 至 ${1}$...</x:String>
6162
<x:String x:Key="Text.BranchCM.Finish" xml:space="preserve">Git 工作流 - 完成 ${0}$</x:String>
6263
<x:String x:Key="Text.BranchCM.Merge" xml:space="preserve">合併 ${0}$ 到 ${1}$...</x:String>
6364
<x:String x:Key="Text.BranchCM.Pull" xml:space="preserve">拉取 (pull) ${0}$</x:String>

src/ViewModels/FetchInto.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Threading.Tasks;
2+
3+
namespace SourceGit.ViewModels
4+
{
5+
public class FetchInto : Popup
6+
{
7+
public Models.Branch Local
8+
{
9+
get;
10+
private set;
11+
}
12+
13+
public Models.Branch Upstream
14+
{
15+
get;
16+
private set;
17+
}
18+
19+
public FetchInto(Repository repo, Models.Branch local, Models.Branch upstream)
20+
{
21+
_repo = repo;
22+
Local = local;
23+
Upstream = upstream;
24+
View = new Views.FetchInto() { DataContext = this };
25+
}
26+
27+
public override Task<bool> Sure()
28+
{
29+
_repo.SetWatcherEnabled(false);
30+
ProgressDescription = "Fast-Forward ...";
31+
32+
return Task.Run(() =>
33+
{
34+
new Commands.Fetch(_repo.FullPath, Local, Upstream, SetProgressDescription).Exec();
35+
CallUIThread(() => _repo.SetWatcherEnabled(true));
36+
return true;
37+
});
38+
}
39+
40+
private readonly Repository _repo = null;
41+
}
42+
}

src/ViewModels/Repository.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,7 @@ public ContextMenu CreateContextMenuForLocalBranch(Models.Branch branch)
13541354
e.Handled = true;
13551355
};
13561356
menu.Items.Add(checkout);
1357+
menu.Items.Add(new MenuItem() { Header = "-" });
13571358

13581359
var worktree = _worktrees.Find(x => x.Branch == branch.FullName);
13591360
var upstream = _branches.Find(x => x.FullName == branch.Upstream);
@@ -1370,11 +1371,22 @@ public ContextMenu CreateContextMenuForLocalBranch(Models.Branch branch)
13701371
e.Handled = true;
13711372
};
13721373

1373-
menu.Items.Add(new MenuItem() { Header = "-" });
1374+
var fetchInto = new MenuItem();
1375+
fetchInto.Header = new Views.NameHighlightedTextBlock("BranchCM.FetchInto", upstream.FriendlyName, branch.Name);
1376+
fetchInto.Icon = App.CreateMenuIcon("Icons.Fetch");
1377+
fetchInto.IsEnabled = branch.TrackStatus.Ahead.Count == 0;
1378+
fetchInto.Click += (_, e) =>
1379+
{
1380+
if (PopupHost.CanCreatePopup())
1381+
PopupHost.ShowAndStartPopup(new FetchInto(this, branch, upstream));
1382+
e.Handled = true;
1383+
};
1384+
13741385
menu.Items.Add(fastForward);
1386+
menu.Items.Add(new MenuItem() { Header = "-" });
1387+
menu.Items.Add(fetchInto);
13751388
}
13761389

1377-
menu.Items.Add(new MenuItem() { Header = "-" });
13781390
menu.Items.Add(push);
13791391

13801392
var merge = new MenuItem();

src/Views/FetchInto.axaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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="500" d:DesignHeight="450"
7+
x:Class="SourceGit.Views.FetchInto"
8+
x:DataType="vm:FetchInto">
9+
<StackPanel Orientation="Vertical" Margin="8,0">
10+
<TextBlock FontSize="18"
11+
Classes="bold"
12+
Text="{DynamicResource Text.Fetch.Title}"/>
13+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,16,0,0">
14+
<Path Width="14" Height="14" Data="{StaticResource Icons.Branch}"/>
15+
<TextBlock Text="{Binding Upstream.FriendlyName}" Margin="8,0,0,0"/>
16+
<TextBlock Text="" Margin="8,0"/>
17+
<Path Width="14" Height="14" Data="{StaticResource Icons.Branch}"/>
18+
<TextBlock Text="{Binding Local.Name}" Margin="8,0,0,0"/>
19+
</StackPanel>
20+
</StackPanel>
21+
</UserControl>

src/Views/FetchInto.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 FetchInto : UserControl
6+
{
7+
public FetchInto()
8+
{
9+
InitializeComponent();
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)