/// <summary> /// Force push the current repository HEAD to <see cref="RemoteTemporaryBranchName"/>; /// </summary> /// <param name="username">The username to fetch from the origin repository</param> /// <param name="password">The password to fetch from the origin repository</param> /// <param name="progressReporter"><see cref="Action{T1}"/> to report 0-100 <see cref="int"/> progress of the operation</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation</param> /// <returns>A <see cref="Task"/> representing the running operation</returns> Task PushHeadToTemporaryBranch(string username, string password, Action <int> progressReporter, CancellationToken cancellationToken) => Task.Factory.StartNew(() => { logger.LogInformation("Pushing changes to temporary remote branch..."); var branch = repository.CreateBranch(RemoteTemporaryBranchName); try { cancellationToken.ThrowIfCancellationRequested(); var remote = repository.Network.Remotes.First(); try { var forcePushString = String.Format(CultureInfo.InvariantCulture, "+{0}:{0}", branch.CanonicalName); repository.Network.Push(remote, forcePushString, GeneratePushOptions(progress => progressReporter((int)(0.9f * progress)), username, password, cancellationToken)); var removalString = String.Format(CultureInfo.InvariantCulture, ":{0}", branch.CanonicalName); repository.Network.Push(remote, removalString, GeneratePushOptions(progress => progressReporter(90 + (int)(0.1f * progress)), username, password, cancellationToken)); } catch (UserCancelledException) { cancellationToken.ThrowIfCancellationRequested(); } catch (LibGit2SharpException e) { logger.LogWarning("Unable to push to temporary branch! Exception: {0}", e); } } finally { repository.Branches.Remove(branch); } }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
/// <summary> /// Force push the current repository HEAD to <see cref="Repository.RemoteTemporaryBranchName"/>; /// </summary> /// <param name="username">The username to fetch from the origin repository</param> /// <param name="password">The password to fetch from the origin repository</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation</param> /// <returns>A <see cref="Task"/> representing the running operation</returns> Task PushHeadToTemporaryBranch(string username, string password, CancellationToken cancellationToken) => Task.Factory.StartNew(() => { var branch = repository.CreateBranch(RemoteTemporaryBranchName); try { cancellationToken.ThrowIfCancellationRequested(); var remote = repository.Network.Remotes.First(); try { repository.Network.Push(remote, String.Format(CultureInfo.InvariantCulture, "+{0}:{0}", branch.CanonicalName), new PushOptions { OnPackBuilderProgress = (a, b, c) => !cancellationToken.IsCancellationRequested, OnNegotiationCompletedBeforePush = (a) => !cancellationToken.IsCancellationRequested, OnPushTransferProgress = (a, b, c) => !cancellationToken.IsCancellationRequested, CredentialsProvider = (a, b, c) => username != null ? (Credentials) new UsernamePasswordCredentials { Username = username, Password = password } : new DefaultCredentials() }); } catch (UserCancelledException) { cancellationToken.ThrowIfCancellationRequested(); } } finally { repository.Branches.Remove(branch); } }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
/// <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(); }