private async Task <R> MergeAsync(BranchName branchName) { Log.Debug($"Merge branch {branchName} into current branch ..."); R <IReadOnlyList <GitBranch> > branches = await gitBranchService.GetBranchesAsync(CancellationToken.None); if (branches.IsFaulted) { return(R.Error("Failed to merge", branches.Exception)); } // Trying to get both local and remote branch branches.Value.TryGet(branchName, out GitBranch localbranch); branches.Value.TryGet($"origin/{branchName}", out GitBranch remoteBranch); GitBranch branch = localbranch ?? remoteBranch; if (localbranch != null && remoteBranch != null) { // Both local and remote tip exists, use the branch with the most resent tip R <GitCommit> localTipCommit = await gitCommitService.GetCommitAsync(localbranch.TipSha.Sha, CancellationToken.None); R <GitCommit> remoteTipCommit = await gitCommitService.GetCommitAsync(remoteBranch.TipSha.Sha, CancellationToken.None); if (localTipCommit.IsFaulted || remoteTipCommit.IsFaulted) { return(R.Error("Failed to merge", remoteTipCommit.Exception)); } if (remoteTipCommit.Value.CommitDate > localTipCommit.Value.CommitDate) { branch = remoteBranch; } } if (branch == null) { return(R.Error($"Failed to Merge, not valid branch {branchName}")); } return(await gitMergeService.MergeAsync(branch.Name, CancellationToken.None)); }