using System.Collections.Generic; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace SourceGit.UI { /// /// Git pull /// public partial class Pull : UserControl { private Git.Repository repo = null; private string preferRemote = null; private string preferBranch = null; /// /// Constructor /// /// Opened repository /// Prefered remote branch. public Pull(Git.Repository opened, string preferRemoteBranch) { repo = opened; InitializeComponent(); SetContent(preferRemoteBranch); } /// /// Display git pull dialog. /// /// Opened repository /// Prefered remote branch public static void Show(Git.Repository opened, string preferRemoteBranch = null) { PopupManager.Show(new Pull(opened, preferRemoteBranch)); } /// /// Set content. /// private void SetContent(string prefered) { var branches = repo.Branches(); var remotes = new List(); var current = null as Git.Branch; foreach (var b in branches) { if (b.IsLocal) { if (b.IsCurrent) current = b; } else { if (!remotes.Contains(b.Remote)) remotes.Add(b.Remote); } } if (!string.IsNullOrEmpty(prefered)) { preferRemote = prefered.Substring(0, prefered.IndexOf('/')); preferBranch = prefered; } else if (current != null && !string.IsNullOrEmpty(current.Upstream)) { var upstream = current.Upstream.Substring("refs/remotes/".Length); preferRemote = upstream.Substring(0, upstream.IndexOf('/')); preferBranch = upstream; } txtInto.Content = current.Name; combRemotes.ItemsSource = remotes; combRemotes.SelectedItem = preferRemote; } /// /// Start pull /// /// /// private async void Start(object sender, RoutedEventArgs e) { var remote = combRemotes.SelectedItem as string; var branch = combBranches.SelectedItem as string; var rebase = chkRebase.IsChecked == true; var autoStash = chkAutoStash.IsChecked == true; if (remote == null || branch == null) return; PopupManager.Lock(); await Task.Run(() => repo.Pull(remote, branch.Substring(branch.IndexOf('/')+1), PopupManager.UpdateStatus, rebase, autoStash)); PopupManager.Close(true); } /// /// Cancel. /// /// /// private void Cancel(object sender, RoutedEventArgs e) { PopupManager.Close(); } /// /// Remote selection changed event. /// /// /// private void RemotesSelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count != 1) return; var remote = e.AddedItems[0] as string; var allBranches = repo.Branches(); var branches = new List(); foreach (var b in allBranches) { if (!b.IsLocal && b.Remote == remote) { branches.Add(b.Name); } } combBranches.ItemsSource = branches; if (remote == preferRemote && preferBranch != null) { combBranches.SelectedItem = preferBranch; } else { combBranches.SelectedIndex = 0; } } } }