public static Branch SwitchToTargetBranch(this IGitRepository repository, CherryPickConfig config) { GetLocalAndRemoteBranch(repository, config.TargetBranch, config.TargetBranchRemote, out var targetBranch, out var targetBranchRemote); if (targetBranch == null && targetBranchRemote != null) { targetBranch = repository.CreateBranch(config.TargetBranch, targetBranchRemote.Tip); } if (targetBranch is null) { throw new InvalidOperationException(string.Format("Branch {0} not found", config.TargetBranch)); } // Checkout target branch repository.Checkout(targetBranch); if (config.SyncTargetBranch && targetBranchRemote != null) { try { // And try pull with fast forward from remote repository.Merge( targetBranchRemote, repository.Config.BuildSignature(DateTimeOffset.Now), new MergeOptions { FastForwardStrategy = FastForwardStrategy.FastForwardOnly }); } catch (NonFastForwardException) { } } return(targetBranch); }
void CommitChanges(IGitRepository repository, IEnumerable <StatusEntry> entries, CommitDialog dialog) { if (mainThread.ShowDialog(dialog) == true) { if (!string.IsNullOrEmpty(dialog.NewBranchName)) { repository.Checkout(repository.CreateBranch(dialog.NewBranchName)); } foreach (var entry in entries) { repository.Stage(entry.FilePath); } eventStream.Push <Status>(0.5f); var signature = repository.Config.BuildSignature(DateTimeOffset.Now); repository.Commit( dialog.Message, signature, signature, CreateCommitOptions()); } }
public Task ExecuteAsync(CancellationToken cancellation = default) { var dialog = new SwitchDialog(branches: repository.Branches.Select(x => x.FriendlyName).OrderBy(x => x).ToArray()); if (mainThread.Invoke(() => dialog.ShowDialog()) == true && !string.IsNullOrEmpty(dialog.Branch)) { var branch = repository.Branches.FirstOrDefault(x => x.FriendlyName == dialog.Branch); var targetBranch = branch; var targetBranchName = dialog.Branch; var overwriteTargetBranch = false; // Check if the selected branch is remote if (branch?.IsRemote == true) { // Get the branch name from the remote targetBranchName = branch.GetName(); // Allow the user to create a branch for the remote var createBranchDialog = new InputBox("Create Branch", "Branch") { Text = targetBranchName }; if (mainThread.Invoke(() => createBranchDialog.ShowDialog()) == true) { // Check if the new branch already exists targetBranchName = createBranchDialog.Text; targetBranch = repository.Branches.FirstOrDefault(x => !x.IsRemote && x.FriendlyName == createBranchDialog.Text); if (targetBranch != null) { // The branch already exist => ask the user to overwrite it var forceDialog = new MessageBox( "Warning", DialogBoxButton.Ok | DialogBoxButton.Cancel, "A branch with this name already exists. Do you want to overwrite it?"); overwriteTargetBranch = mainThread.Invoke(() => forceDialog.ShowDialog() == true); if (!overwriteTargetBranch) { return(CancelCheckout()); } } } else { return(CancelCheckout()); } } // 1. Check the remote branch if remote was selected if (branch?.IsRemote == true) { repository.Checkout(branch); } // 2. Remove the existing branch if the user decided to overwrite it if (overwriteTargetBranch && targetBranch != null) { eventStream.Push(Status.Create(0.2f, "Removing branch {0}", targetBranch.FriendlyName)); repository.Branches.Remove(targetBranch); } // 3. Create the branch if it does not exist if (targetBranch == null) { eventStream.Push(Status.Create(0.4f, "Creating branch {0}", targetBranchName)); targetBranch = repository.CreateBranch(targetBranchName); } // 4. Checkout the branch eventStream.Push(Status.Create(0.6f, "Swithing to branch {0}", targetBranchName)); repository.Checkout(targetBranch); // 5. Update submodules if (dialog.UpdateSubmodules) { eventStream.Push(Status.Create(0.8f, "Updating submodules...")); repository.UpdateSubmodules(eventStream: eventStream); } eventStream.Push(new BranchChanged(targetBranchName)); eventStream.Push(Status.Succeeded()); } return(Task.CompletedTask); }