Exemplo n.º 1
0
        /// <summary>
        /// Loads the view model from octokit models.
        /// </summary>
        /// <param name="pullRequest">The pull request model.</param>
        /// <param name="files">The pull request's changed files.</param>
        public async Task Load(IPullRequestModel pullRequest)
        {
            Model = pullRequest;
            Title = Resources.PullRequestNavigationItemText + " #" + pullRequest.Number;
            SourceBranchDisplayName = GetBranchDisplayName(pullRequest.Head?.Label);
            TargetBranchDisplayName = GetBranchDisplayName(pullRequest.Base.Label);
            Body = !string.IsNullOrWhiteSpace(pullRequest.Body) ? pullRequest.Body : "*No description provided.*";

            ChangedFilesTree.Clear();
            ChangedFilesList.Clear();

            // WPF doesn't support AddRange here so iterate through the changes.
            foreach (var change in CreateChangedFilesList(pullRequest.ChangedFiles))
            {
                ChangedFilesList.Add(change);
            }

            foreach (var change in CreateChangedFilesTree(ChangedFilesList).Children)
            {
                ChangedFilesTree.Add(change);
            }

            var localBranches = await pullRequestsService.GetLocalBranches(repository, pullRequest).ToList();

            var isCheckedOut = localBranches.Contains(repository.CurrentBranch);

            if (isCheckedOut)
            {
                var divergence = await pullRequestsService.CalculateHistoryDivergence(repository, Model.Number);

                var pullDisabled = divergence.BehindBy == 0 ? "No commits to pull" : null;
                var pushDisabled = divergence.AheadBy == 0 ?
                                   "No commits to push" :
                                   divergence.BehindBy > 0 ? "You must pull before you can push" : null;

                UpdateState   = new UpdateCommandState(divergence, pullDisabled, pushDisabled);
                CheckoutState = null;
            }
            else
            {
                var caption = localBranches.Count > 0 ?
                              "Checkout " + localBranches.First().DisplayName :
                              "Checkout to " + (await pullRequestsService.GetDefaultLocalBranchName(repository, Model.Number, Model.Title));
                var disabled = await pullRequestsService.IsWorkingDirectoryClean(repository) ?
                               null :
                               "Cannot checkout as your working directory has uncommitted changes.";

                CheckoutState = new CheckoutCommandState(caption, disabled);
                UpdateState   = null;
            }

            IsBusy = false;
        }
        /// <summary>
        /// Loads the view model from octokit models.
        /// </summary>
        /// <param name="pullRequest">The pull request model.</param>
        /// <param name="files">The pull request's changed files.</param>
        public async Task Load(IPullRequestModel pullRequest)
        {
            Model = pullRequest;
            Title = Resources.PullRequestNavigationItemText + " #" + pullRequest.Number;

            var prFromFork = pullRequestsService.IsPullRequestFromFork(repository, Model);

            SourceBranchDisplayName = GetBranchDisplayName(prFromFork, pullRequest.Head?.Label);
            TargetBranchDisplayName = GetBranchDisplayName(prFromFork, pullRequest.Base.Label);
            Body = !string.IsNullOrWhiteSpace(pullRequest.Body) ? pullRequest.Body : Resources.NoDescriptionProvidedMarkdown;

            ChangedFilesTree.Clear();
            ChangedFilesList.Clear();

            var treeChanges = await pullRequestsService.GetTreeChanges(repository, pullRequest);

            // WPF doesn't support AddRange here so iterate through the changes.
            foreach (var change in CreateChangedFilesList(pullRequest, treeChanges))
            {
                ChangedFilesList.Add(change);
            }

            foreach (var change in CreateChangedFilesTree(ChangedFilesList).Children)
            {
                ChangedFilesTree.Add(change);
            }

            var localBranches = await pullRequestsService.GetLocalBranches(repository, pullRequest).ToList();

            var isCheckedOut = localBranches.Contains(repository.CurrentBranch);

            if (isCheckedOut)
            {
                var divergence = await pullRequestsService.CalculateHistoryDivergence(repository, Model.Number);

                var    pullEnabled = divergence.BehindBy > 0;
                var    pushEnabled = divergence.AheadBy > 0 && !pullEnabled;
                string pullToolTip;
                string pushToolTip;

                if (pullEnabled)
                {
                    pullToolTip = string.Format(
                        Resources.PullRequestDetailsPullToolTip,
                        prFromFork ? Resources.Fork : Resources.Remote,
                        SourceBranchDisplayName);
                }
                else
                {
                    pullToolTip = Resources.NoCommitsToPull;
                }

                if (pushEnabled)
                {
                    pushToolTip = string.Format(
                        Resources.PullRequestDetailsPushToolTip,
                        prFromFork ? Resources.Fork : Resources.Remote,
                        SourceBranchDisplayName);
                }
                else if (divergence.AheadBy == 0)
                {
                    pushToolTip = Resources.NoCommitsToPush;
                }
                else
                {
                    pushToolTip = Resources.MustPullBeforePush;
                }

                UpdateState   = new UpdateCommandState(divergence, pullEnabled, pushEnabled, pullToolTip, pushToolTip);
                CheckoutState = null;
            }
            else
            {
                var caption = localBranches.Count > 0 ?
                              string.Format(Resources.PullRequestDetailsCheckout, localBranches.First().DisplayName) :
                              string.Format(Resources.PullRequestDetailsCheckoutTo, await pullRequestsService.GetDefaultLocalBranchName(repository, Model.Number, Model.Title));
                var clean = await pullRequestsService.IsWorkingDirectoryClean(repository);

                string disabled = null;

                if (pullRequest.Head == null || !pullRequest.Head.RepositoryCloneUrl.IsValidUri)
                {
                    disabled = Resources.SourceRepositoryNoLongerAvailable;
                }
                else if (!clean)
                {
                    disabled = Resources.WorkingDirectoryHasUncommittedCHanges;
                }

                CheckoutState = new CheckoutCommandState(caption, disabled);
                UpdateState   = null;
            }

            IsBusy = false;

            if (!isInCheckout)
            {
                pullRequestsService.RemoveUnusedRemotes(repository).Subscribe(_ => { });
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Loads the view model from octokit models.
        /// </summary>
        /// <param name="pullRequest">The pull request model.</param>
        /// <param name="files">The pull request's changed files.</param>
        public async Task Load(IPullRequestModel pullRequest)
        {
            Model = pullRequest;
            SourceBranchDisplayName = GetBranchDisplayName(pullRequest.Head?.Label);
            TargetBranchDisplayName = GetBranchDisplayName(pullRequest.Base.Label);
            Body = !string.IsNullOrWhiteSpace(pullRequest.Body) ? pullRequest.Body : "*No description provided.*";

            ChangedFilesTree.Clear();
            ChangedFilesList.Clear();

            // WPF doesn't support AddRange here so iterate through the changes.
            foreach (var change in CreateChangedFilesList(pullRequest.ChangedFiles))
            {
                ChangedFilesList.Add(change);
            }

            foreach (var change in CreateChangedFilesTree(ChangedFilesList).Children)
            {
                ChangedFilesTree.Add(change);
            }

            var localBranches = await pullRequestsService.GetLocalBranches(repository, pullRequest).ToList();

            if (localBranches.Contains(repository.CurrentBranch))
            {
                var divergence = await pullRequestsService.CalculateHistoryDivergence(repository, Model.Number);

                if (divergence.BehindBy == null)
                {
                    CheckoutMode = CheckoutMode.InvalidState;
                }
                else if (divergence.AheadBy > 0)
                {
                    CheckoutMode = pullRequestsService.IsPullRequestFromFork(repository, pullRequest) ?
                                   CheckoutMode.InvalidState : CheckoutMode.Push;
                }
                else if (divergence.BehindBy == 0)
                {
                    CheckoutMode = CheckoutMode.UpToDate;
                }
                else
                {
                    CheckoutMode  = CheckoutMode.NeedsPull;
                    CommitsBehind = divergence.BehindBy.Value;
                }
            }
            else if (localBranches.Count > 0)
            {
                CheckoutMode = CheckoutMode.Switch;
            }
            else
            {
                CheckoutMode = CheckoutMode.Fetch;
            }

            var clean = await pullRequestsService.IsCleanForCheckout(repository);

            CheckoutDisabledMessage = (!clean && CheckoutMode != CheckoutMode.UpToDate && CheckoutMode != CheckoutMode.Push) ?
                                      $"Cannot {GetCheckoutModeDescription(CheckoutMode)} as your working directory has uncommitted changes." :
                                      null;

            IsBusy = false;
        }