using System.Collections.Generic; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace SourceGit.UI { /// /// Create branch dialog /// public partial class CreateBranch : UserControl { private Git.Repository repo = null; private string based = null; /// /// New branch name. /// public string BranchName { get; set; } /// /// Auto Stash /// public bool AutoStash { get; set; } = false; /// /// Constructor. /// /// Opened repository public CreateBranch(Git.Repository opened) { InitializeComponent(); repo = opened; nameValidator.Repo = opened; } /// /// Create branch based on current head. /// /// public static void Show(Git.Repository repo) { var current = repo.CurrentBranch(); if (current != null) Show(repo, current); } /// /// Create branch base on existed one. /// /// /// public static void Show(Git.Repository repo, Git.Branch branch) { var dialog = new CreateBranch(repo); dialog.based = branch.Name; dialog.basedOnType.Data = dialog.FindResource("Icon.Branch") as Geometry; dialog.basedOnDesc.Text = branch.Name; if (!branch.IsLocal) dialog.txtName.Text = branch.Name.Substring(branch.Remote.Length + 1); repo.GetPopupManager()?.Show(dialog); } /// /// Create branch based on tag. /// /// /// public static void Show(Git.Repository repo, Git.Tag tag) { var dialog = new CreateBranch(repo); dialog.based = tag.Name; dialog.basedOnType.Data = dialog.FindResource("Icon.Tag") as Geometry; dialog.basedOnDesc.Text = tag.Name; repo.GetPopupManager()?.Show(dialog); } /// /// Create branch based on commit. /// /// /// public static void Show(Git.Repository repo, Git.Commit commit) { var dialog = new CreateBranch(repo); dialog.based = commit.SHA; dialog.basedOnType.Data = dialog.FindResource("Icon.Commit") as Geometry; dialog.basedOnDesc.Text = $"{commit.ShortSHA} {commit.Subject}"; repo.GetPopupManager()?.Show(dialog); } /// /// Start create branch. /// /// /// private async void Start(object sender, RoutedEventArgs e) { txtName.GetBindingExpression(TextBox.TextProperty).UpdateSource(); if (Validation.GetHasError(txtName)) return; var popup = repo.GetPopupManager(); popup?.Lock(); bool checkout = chkCheckout.IsChecked == true; await Task.Run(() => { if (checkout) { bool stashed = false; if (repo.LocalChanges().Count > 0 && AutoStash) { Git.Stash.Push(repo, true, "CREATE BRANCH AUTO STASH", new List()); stashed = true; } repo.Checkout($"-b {BranchName} {based}"); if (stashed) { var stashes = repo.Stashes(); if (stashes.Count > 0) stashes[0].Pop(repo); } } else { Git.Branch.Create(repo, BranchName, based); } }); popup?.Close(true); } /// /// Cancel. /// /// /// private void Cancel(object sender, RoutedEventArgs e) { repo.GetPopupManager()?.Close(); } } }