feature<Checkout>: add a status panel for checkout progress

This commit is contained in:
leo 2021-05-26 18:43:28 +08:00
parent 001453d6ff
commit 67f5eed9a0
5 changed files with 64 additions and 24 deletions

View file

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
@ -6,13 +7,16 @@ namespace SourceGit.Commands {
/// 检出 /// 检出
/// </summary> /// </summary>
public class Checkout : Command { public class Checkout : Command {
private Action<string> handler = null;
public Checkout(string repo) { public Checkout(string repo) {
Cwd = repo; Cwd = repo;
} }
public bool Branch(string branch) { public bool Branch(string branch, Action<string> onProgress) {
Args = $"checkout {branch}"; Args = $"checkout --progress {branch}";
TraitErrorAsOutput = true;
handler = onProgress;
return Exec(); return Exec();
} }
@ -42,5 +46,9 @@ namespace SourceGit.Commands {
Args = builder.ToString(); Args = builder.ToString();
return Exec(); return Exec();
} }
public override void OnReadline(string line) {
handler?.Invoke(line);
}
} }
} }

View file

@ -0,0 +1,10 @@
<controls:PopupWidget
x:Class="SourceGit.Views.Popups.Checkout"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:controls="clr-namespace:SourceGit.Views.Controls"
mc:Ignorable="d"
d:DesignWidth="500" Height="100">
</controls:PopupWidget>

View file

@ -0,0 +1,34 @@
using System.Threading.Tasks;
namespace SourceGit.Views.Popups {
/// <summary>
/// 切换分支
/// </summary>
public partial class Checkout : Controls.PopupWidget {
private string repo;
private string branch;
public Checkout(string repo, string branch) {
this.repo = repo;
this.branch = branch;
InitializeComponent();
}
public override string GetTitle() {
return App.Text("BranchCM.Checkout", branch);
}
public override Task<bool> Start() {
UpdateProgress(GetTitle());
return Task.Run(() => {
Models.Watcher.SetEnabled(repo, false);
new Commands.Checkout(repo).Branch(branch, UpdateProgress);
Models.Watcher.SetEnabled(repo, true);
return true;
});
}
}
}

View file

@ -424,7 +424,7 @@ namespace SourceGit.Views.Widgets {
if (node.Type == BranchNodeType.Branch) NavigateTo((node.Data as Models.Branch).Head); if (node.Type == BranchNodeType.Branch) NavigateTo((node.Data as Models.Branch).Head);
} }
private async void OnTreeDoubleClick(object sender, MouseButtonEventArgs e) { private void OnTreeDoubleClick(object sender, MouseButtonEventArgs e) {
var item = sender as Controls.TreeItem; var item = sender as Controls.TreeItem;
if (item == null) return; if (item == null) return;
@ -434,9 +434,7 @@ namespace SourceGit.Views.Widgets {
var branch = node.Data as Models.Branch; var branch = node.Data as Models.Branch;
if (!branch.IsLocal || branch.IsCurrent) return; if (!branch.IsLocal || branch.IsCurrent) return;
Models.Watcher.SetEnabled(repo.Path, false); new Popups.Checkout(repo.Path, branch.Name).ShowAndStart();
await Task.Run(() => new Commands.Checkout(repo.Path).Branch(branch.Name));
Models.Watcher.SetEnabled(repo.Path, true);
} }
private void OnTreeContextMenuOpening(object sender, ContextMenuEventArgs e) { private void OnTreeContextMenuOpening(object sender, ContextMenuEventArgs e) {
@ -510,10 +508,8 @@ namespace SourceGit.Views.Widgets {
var checkout = new MenuItem(); var checkout = new MenuItem();
checkout.Header = App.Text("BranchCM.Checkout", branch.Name); checkout.Header = App.Text("BranchCM.Checkout", branch.Name);
checkout.Click += async (o, e) => { checkout.Click += (o, e) => {
Models.Watcher.SetEnabled(repo.Path, false); new Popups.Checkout(repo.Path, branch.Name).ShowAndStart();
await Task.Run(() => new Commands.Checkout(repo.Path).Branch(branch.Name));
Models.Watcher.SetEnabled(repo.Path, true);
e.Handled = true; e.Handled = true;
}; };
menu.Items.Add(checkout); menu.Items.Add(checkout);
@ -676,14 +672,11 @@ namespace SourceGit.Views.Widgets {
var checkout = new MenuItem(); var checkout = new MenuItem();
checkout.Header = App.Text("BranchCM.Checkout", branch.Name); checkout.Header = App.Text("BranchCM.Checkout", branch.Name);
checkout.Click += async (o, e) => { checkout.Click += (o, e) => {
foreach (var b in repo.Branches) { foreach (var b in repo.Branches) {
if (b.IsLocal && b.Upstream == branch.FullName) { if (b.IsLocal && b.Upstream == branch.FullName) {
if (b.IsCurrent) return; if (b.IsCurrent) return;
new Popups.Checkout(repo.Path, b.Name).ShowAndStart();
Models.Watcher.SetEnabled(repo.Path, false);
await Task.Run(() => new Commands.Checkout(repo.Path).Branch(b.Name));
Models.Watcher.SetEnabled(repo.Path, true);
return; return;
} }
} }

View file

@ -457,10 +457,8 @@ namespace SourceGit.Views.Widgets {
var checkout = new MenuItem(); var checkout = new MenuItem();
checkout.Header = App.Text("BranchCM.Checkout", branch.Name); checkout.Header = App.Text("BranchCM.Checkout", branch.Name);
checkout.Click += async (o, e) => { checkout.Click += (o, e) => {
Models.Watcher.SetEnabled(repo.Path, false); new Popups.Checkout(repo.Path, branch.Name).ShowAndStart();
await Task.Run(() => new Commands.Checkout(repo.Path).Branch(branch.Name));
Models.Watcher.SetEnabled(repo.Path, true);
e.Handled = true; e.Handled = true;
}; };
submenu.Items.Add(checkout); submenu.Items.Add(checkout);
@ -528,14 +526,11 @@ namespace SourceGit.Views.Widgets {
var checkout = new MenuItem(); var checkout = new MenuItem();
checkout.Header = App.Text("BranchCM.Checkout", name); checkout.Header = App.Text("BranchCM.Checkout", name);
checkout.Click += async (o, e) => { checkout.Click += (o, e) => {
foreach (var b in repo.Branches) { foreach (var b in repo.Branches) {
if (b.IsLocal && b.Upstream == branch.FullName) { if (b.IsLocal && b.Upstream == branch.FullName) {
if (b.IsCurrent) return; if (b.IsCurrent) return;
new Popups.Checkout(repo.Path, b.Name).ShowAndStart();
Models.Watcher.SetEnabled(repo.Path, false);
await Task.Run(() => new Commands.Checkout(repo.Path).Branch(b.Name));
Models.Watcher.SetEnabled(repo.Path, true);
return; return;
} }
} }