Example #1
0
        public Task <RebaseInfo> RebaseBranch(string source, string target, Author author, string message)
        {
            var signature    = getSignature(author);
            var targetBranch = _repo.Branches[target];
            var sourceBranch = _repo.Branches[source];

            if (isTransactionInProgress(source))
            {
                var exceptionMessage = $"There is a transaction in progress for branch {source}. Complete the transaction first.";
                _logger.Warn(exceptionMessage);
                throw new ArgumentException(exceptionMessage);
            }

            lock (getLock(source))
            {
                var mergeRes = _repo.ObjectDatabase.MergeCommits(sourceBranch.Tip, targetBranch.Tip, new MergeTreeOptions());
                if (mergeRes.Status != MergeTreeStatus.Succeeded)
                {
                    var logMessage = $"Could not rebase {source} onto {target} because of conflicts. Please merge manually";
                    _logger.Trace(logMessage);

                    return(Task.FromResult(new RebaseInfo
                    {
                        Message = logMessage,
                        SourceBranch = source,
                        TargetBranch = target,
                        Status = RebaseResult.Conflicts,
                        Conflicts = mergeRes.Conflicts.Select(c => new ConflictInfo
                        {
                            SourceSha = c.Ours?.Id.Sha,
                            TargetSha = c.Theirs?.Id.Sha,
                            Path = c.Ours?.Path ?? c.Theirs.Path,
                            Type = object.ReferenceEquals(c.Ours, null) || object.ReferenceEquals(c.Theirs, null) ? ConflictType.Remove : ConflictType.Change
                        }).ToList()
                    }));
                }

                var previousCommit = targetBranch.Tip;
                var tree           = mergeRes.Tree;

                if (previousCommit != null && previousCommit.Tree.Id == tree.Id)
                {
                    return(Task.FromResult(RebaseInfo.Succeeded(source, target, string.Empty)));
                }

                var ancestors = previousCommit != null ? new List <Commit> {
                    previousCommit
                } : new List <Commit>();
                var commit = _repo.ObjectDatabase.CreateCommit(signature, signature, message, tree, ancestors, false);

                _repo.Refs.UpdateTarget(_repo.Refs[sourceBranch.CanonicalName], commit.Id);

                _logger.Trace($"Squashed and rebased {source} onto {target} with message {message}");

                push(source);

                return(Task.FromResult(RebaseInfo.Succeeded(source, target, commit.Sha)));
            }
        }
Example #2
0
 public void RebaseShouldSuccedWithValidInfo() =>
 _rebaseResult.ShouldBeEquivalentTo(RebaseInfo.Succeeded("test", "master", Repo.Branches["test"].Tip.Sha));
Example #3
0
 public void RebaseShouldSuccedWithValidInfo() =>
 _rebaseResult.ShouldBeEquivalentTo(RebaseInfo.Succeeded("test", "master", string.Empty));