Пример #1
0
        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);
        }
Пример #2
0
        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());
            }
        }
Пример #3
0
        private static void EnsureLocalBranchExistsForCurrentBranch(IGitRepository repo, ILog log, IRemote remote, string currentBranch)
        {
            if (log is null)
            {
                throw new ArgumentNullException(nameof(log));
            }

            if (remote is null)
            {
                throw new ArgumentNullException(nameof(remote));
            }

            if (string.IsNullOrEmpty(currentBranch))
            {
                return;
            }

            var isRef              = currentBranch.Contains("refs");
            var isBranch           = currentBranch.Contains("refs/heads");
            var localCanonicalName = !isRef
                ? "refs/heads/" + currentBranch
                : isBranch
                    ? currentBranch
                    : currentBranch.Replace("refs/", "refs/heads/");

            var repoTip = repo.Head.Tip;

            // We currently have the rep.Head of the *default* branch, now we need to look up the right one
            var originCanonicalName = $"{remote.Name}/{currentBranch}";
            var originBranch        = repo.Branches[originCanonicalName];

            if (originBranch != null)
            {
                repoTip = originBranch.Tip;
            }

            var repoTipId = repoTip.Id;

            var referenceName = ReferenceName.Parse(localCanonicalName);

            if (repo.Branches.All(b => !b.Name.Equals(referenceName)))
            {
                log.Info(isBranch ? $"Creating local branch {referenceName}"
                    : $"Creating local branch {referenceName} pointing at {repoTipId}");
                repo.Refs.Add(localCanonicalName, repoTipId.Sha);
            }
            else
            {
                log.Info(isBranch ? $"Updating local branch {referenceName} to point at {repoTip}"
                    : $"Updating local branch {referenceName} to match ref {currentBranch}");
                var localRef = repo.Refs[localCanonicalName];
                repo.Refs.UpdateTarget(localRef, repoTipId);
            }

            repo.Checkout(localCanonicalName);
        }
Пример #4
0
        public void Checkout(IGitRepository repo)
        {
            var runnerBranch = repo.TryCreateBranch(RunnerBranch);

            if (!repo.CurrentBranch.Equals(runnerBranch))
            {
                repo.ResetHard();
                repo.Checkout(runnerBranch);
            }
        }
Пример #5
0
        public GetResponse <IBranch> TryReset(IGitRepository repo)
        {
            var master = repo.MainBranch;

            if (master == null)
            {
                return(GetResponse <IBranch> .Fail("Could not find main branch"));
            }

            repo.ResetHard();
            repo.Checkout(master);
            repo.Pull();
            return(GetResponse <IBranch> .Succeed(master));
        }
Пример #6
0
        public void GeneralPipelineOrder(
            IGitRepository repo,
            ResetToLatestMain sut)
        {
            var branch = Substitute.For <IBranch>();

            repo.MainBranch.Returns(branch);
            sut.TryReset(repo);
            Received.InOrder(() =>
            {
                repo.ResetHard();
                repo.Checkout(Arg.Any <IBranch>());
                repo.Pull();
            });
        }
Пример #7
0
        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);
        }