Exemplo n.º 1
0
        /// <summary>
        /// Runs a blocking force checkout to <paramref name="committish"/>
        /// </summary>
        /// <param name="committish">The committish to checkout</param>
        /// <param name="progressReporter">Progress reporter <see cref="Action{T}"/></param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation</param>
        void RawCheckout(string committish, Action <int> progressReporter, CancellationToken cancellationToken)
        {
            logger.LogTrace("Checkout: {0}", committish);

            progressReporter(0);
            cancellationToken.ThrowIfCancellationRequested();

            var checkoutOptions = new CheckoutOptions
            {
                CheckoutModifiers  = CheckoutModifiers.Force,
                OnCheckoutProgress = CheckoutProgressHandler(progressReporter)
            };

            void RunCheckout() => commands.Checkout(
                libGitRepo,
                checkoutOptions,
                committish);

            try
            {
                RunCheckout();
            }
            catch (NotFoundException)
            {
                // Maybe (likely) a remote?
                var remoteName      = $"origin/{committish}";
                var potentialBranch = libGitRepo.Branches.FirstOrDefault(
                    branch => branch.FriendlyName.Equals(remoteName, StringComparison.Ordinal));
                cancellationToken.ThrowIfCancellationRequested();

                if (potentialBranch == default)
                {
                    throw;
                }

                logger.LogDebug("Creating local branch for {0}...", potentialBranch.FriendlyName);
                libGitRepo.CreateBranch(committish, potentialBranch.Tip);
                cancellationToken.ThrowIfCancellationRequested();

                RunCheckout();
            }

            cancellationToken.ThrowIfCancellationRequested();

            libGitRepo.RemoveUntrackedFiles();
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public Task <bool> ShaIsParent(string sha, CancellationToken cancellationToken) => Task.Factory.StartNew(
            () =>
        {
            var targetCommit = libGitRepo.Lookup <Commit>(sha);
            if (targetCommit == null)
            {
                logger.LogTrace("Commit {0} not found in repository", sha);
                return(false);
            }

            cancellationToken.ThrowIfCancellationRequested();
            var startSha    = Head;
            var mergeResult = libGitRepo.Merge(
                targetCommit,
                new Signature(
                    DefaultCommitterName,
                    DefaultCommitterEmail,
                    DateTimeOffset.UtcNow),
                new MergeOptions
            {
                FastForwardStrategy = FastForwardStrategy.FastForwardOnly,
                FailOnConflict      = true,
            });

            if (mergeResult.Status == MergeStatus.UpToDate)
            {
                return(true);
            }

            commands.Checkout(
                libGitRepo,
                new CheckoutOptions
            {
                CheckoutModifiers = CheckoutModifiers.Force,
            },
                startSha);

            return(false);
        },
            cancellationToken,
            DefaultIOManager.BlockingTaskCreationOptions,
            TaskScheduler.Current);