using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Animation; 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.Content = branch.Name; if (!branch.IsLocal) dialog.txtName.Text = branch.Name.Substring(branch.Remote.Length + 1); PopupManager.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.Content = tag.Name; PopupManager.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.Content = $"{commit.ShortSHA} {commit.Subject}"; PopupManager.Show(dialog); } /// /// Start create branch. /// /// /// private async void Start(object sender, RoutedEventArgs e) { txtName.GetBindingExpression(TextBox.TextProperty).UpdateSource(); if (Validation.GetHasError(txtName)) return; PopupManager.Lock(); status.Visibility = Visibility.Visible; DoubleAnimation anim = new DoubleAnimation(0, 360, TimeSpan.FromSeconds(1)); anim.RepeatBehavior = RepeatBehavior.Forever; statusIcon.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, anim); 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); } }); status.Visibility = Visibility.Collapsed; statusIcon.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, null); PopupManager.Close(true); } /// /// Cancel. /// /// /// private void Cancel(object sender, RoutedEventArgs e) { PopupManager.Close(); } } }