sourcegit/src/Views/Popups/CreateBranch.xaml.cs

92 lines
3.2 KiB
C#
Raw Normal View History

2021-04-29 05:05:55 -07:00
using System.Threading.Tasks;
using System.Windows;
2021-04-29 05:05:55 -07:00
using System.Windows.Controls;
using System.Windows.Media;
namespace SourceGit.Views.Popups {
/// <summary>
/// 新建分支面板
/// </summary>
public partial class CreateBranch : Controls.PopupWidget {
private string repo = null;
private string basedOn = null;
public string BranchName { get; set; } = "";
public bool AutoStash { get; set; } = true;
2021-04-29 05:05:55 -07:00
public CreateBranch(Models.Repository repo, Models.Branch branch) {
this.repo = repo.Path;
this.basedOn = branch.FullName;
if (!branch.IsLocal) BranchName = branch.Name;
2021-04-29 05:05:55 -07:00
InitializeComponent();
ruleBranch.Repo = repo;
iconBased.Data = FindResource("Icon.Branch") as Geometry;
txtBased.Text = !string.IsNullOrEmpty(branch.Remote) ? $"{branch.Remote}/{branch.Name}" : branch.Name;
}
public CreateBranch(Models.Repository repo, Models.Commit commit) {
this.repo = repo.Path;
this.basedOn = commit.SHA;
InitializeComponent();
ruleBranch.Repo = repo;
iconBased.Data = FindResource("Icon.Commit") as Geometry;
txtSHA.Text = commit.ShortSHA;
txtBased.Text = commit.Subject;
badgeSHA.Visibility = Visibility.Visible;
2021-04-29 05:05:55 -07:00
}
public CreateBranch(Models.Repository repo, Models.Tag tag) {
this.repo = repo.Path;
this.basedOn = tag.Name;
2021-04-29 05:05:55 -07:00
InitializeComponent();
ruleBranch.Repo = repo;
iconBased.Data = FindResource("Icon.Tag") as Geometry;
txtBased.Text = tag.Name;
}
public override string GetTitle() {
return App.Text("CreateBranch");
}
public override Task<bool> Start() {
txtBranchName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
if (Validation.GetHasError(txtBranchName)) return null;
var checkout = chkCheckout.IsChecked == true;
return Task.Run(() => {
Models.Watcher.SetEnabled(repo, false);
if (checkout) {
var changes = new Commands.LocalChanges(repo).Result();
if (changes.Count > 0) {
if (AutoStash) {
if (!new Commands.Stash(repo).Push(changes, "NEWBRANCH_AUTO_STASH")) {
2021-04-29 05:05:55 -07:00
return false;
}
} else {
new Commands.Discard(repo).Whole();
2021-04-29 05:05:55 -07:00
}
} else {
AutoStash = false;
2021-04-29 05:05:55 -07:00
}
UpdateProgress($"Create new branch '{BranchName}'");
new Commands.Checkout(repo).Branch(BranchName, basedOn, UpdateProgress);
2021-04-29 05:05:55 -07:00
if (AutoStash) new Commands.Stash(repo).Pop("stash@{0}");
} else {
new Commands.Branch(repo, BranchName).Create(basedOn);
}
Models.Watcher.SetEnabled(repo, true);
return true;
});
}
}
}