private static void GroupSubBranchesIntoOneMainBranch( IGrouping <CommitId, KeyValuePair <string, MSubBranch> > groupByBranch) { // All sub branches in the groupByBranch have same name and parent commit id, lets take // the first sub branch and base the branch corresponding to the group on that sub branch MSubBranch subBranch = groupByBranch.First().Value; string branchId = subBranch.Name + "-" + subBranch.ParentCommitId; MBranch branch = GetBranch(branchId, subBranch); // Get active sub branches in group (1 if either local or remote, 2 if both) var activeSubBranches = groupByBranch .Where(b => b.Value.IsActive).Select(g => g.Value) .ToList(); branch.IsActive = activeSubBranches.Any(); MSubBranch localSubBranch = activeSubBranches.FirstOrDefault(b => b.IsLocal); MSubBranch remoteSubBranch = activeSubBranches.FirstOrDefault(b => b.IsRemote); branch.IsLocal = localSubBranch != null; branch.LocalTipCommitId = localSubBranch?.TipCommitId ?? CommitId.None; branch.IsRemote = remoteSubBranch != null; branch.RemoteTipCommitId = remoteSubBranch?.TipCommitId ?? CommitId.None; branch.IsCurrent = activeSubBranches.Any(b => b.IsCurrent); branch.IsDetached = activeSubBranches.Any(b => b.IsDetached); branch.LocalAheadCount = 0; branch.RemoteAheadCount = 0; // Set branch if of each sub branch groupByBranch.ForEach(b => b.Value.BranchId = branch.Id); }
public static Branch ToBranch(Repository repository, MBranch branch) { return(new Branch( repository, branch.Id, branch.Name, branch.TipCommitId, branch.FirstCommitId, branch.ParentCommitId, branch.LocalTipCommitId, branch.RemoteTipCommitId, branch.Commits.Select(c => c.Id).ToList(), branch.ParentBranchId, branch.ChildBranchNames.ToList(), branch.MainBranchId, branch.LocalSubBranchId, branch.IsActive, branch.IsLocal, branch.IsRemote, branch.IsMainPart, branch.IsLocalPart, branch.IsMultiBranch, branch.IsDetached, branch.LocalAheadCount, branch.RemoteAheadCount)); }
private void CombineMainWithLocalSubBranches(MRepository repository) { foreach (MBranch branch in repository.Branches.Values.Where(b => b.IsMainPart).ToList()) { MBranch localPart = repository.Branches[branch.LocalSubBranchId]; foreach (CommitId id in localPart.CommitIds) { branch.CommitIds.Add(id); repository.Commits[id].BranchId = branch.Id; } repository.Branches.Remove(localPart.Id); branch.IsMainPart = false; branch.LocalSubBranchId = null; } }
private static void CopyToVirtualCommit (MRepository repository, MBranch branch, MCommit commit, CommitSha virtualSha) { GitCommit gitCommit = new GitCommit( virtualSha, branch.ParentCommit.Subject, branch.ParentCommit.Message, branch.ParentCommit.Author, branch.ParentCommit.AuthorDate, branch.ParentCommit.CommitDate + TimeSpan.FromSeconds(1), new List <CommitId> { branch.ParentCommitId }); repository.GitCommits[commit.Id] = gitCommit; // commit.Id = GetId(); }
private static void SetCurrentBranchAndCommit(MRepository repository, IReadOnlyList <GitBranch> branches) { GitStatus status = repository.Status; MBranch currentBranch = repository.Branches.Values.First(b => b.IsActive && b.IsCurrent); repository.CurrentBranchId = currentBranch.Id; branches.TryGetCurrent(out GitBranch current); repository.CurrentCommitId = status.OK ? current != null ? repository.Commit(new CommitId(current.TipSha.Sha)).Id : CommitId.NoCommits : CommitId.Uncommitted; if (currentBranch.TipCommit.IsVirtual && currentBranch.TipCommit.FirstParentId == repository.CurrentCommitId) { repository.CurrentCommitId = currentBranch.TipCommit.Id; } }
private static void MakeLocalBranch( MRepository repository, MBranch branch, CommitId localTip, CommitId commonTip) { string name = $"{branch.Name}"; string branchId = name + "(local)-" + commonTip; MBranch localBranch; if (!repository.Branches.TryGetValue(branchId, out localBranch)) { localBranch = new MBranch { IsLocalPart = true, Repository = branch.Repository, Name = name, IsMultiBranch = false, IsActive = true, IsLocal = true, ParentCommitId = commonTip, }; localBranch.Id = branchId; repository.Branches[localBranch.Id] = localBranch; } localBranch.TipCommitId = localTip; localBranch.LocalTipCommitId = localTip; localBranch.IsCurrent = branch.IsCurrent; localBranch.IsActive = branch.IsActive; Stack <MCommit> commits = new Stack <MCommit>(); commits.Push(repository.Commits[localTip]); while (commits.Any()) { MCommit commit = commits.Pop(); if (!commit.IsCommon && commit.Branch == branch) { commit.IsLocalAhead = true; localBranch.CommitIds.Add(commit.Id); branch.CommitIds.Remove(commit.Id); commit.BranchId = localBranch.Id; commit.Parents .Where(p => p.Branch == branch) .ForEach(p => commits.Push(p)); } } localBranch.LocalAheadCount = localBranch.Commits.Count(); localBranch.RemoteAheadCount = 0; if (repository.Commits.TryGetValue(CommitId.Uncommitted, out MCommit uncommitted) && branch.TipCommitId == CommitId.Uncommitted) { localBranch.TipCommitId = uncommitted.Id; localBranch.LocalTipCommitId = uncommitted.Id; localBranch.CommitIds.Insert(0, uncommitted.Id); branch.CommitIds.Remove(uncommitted.Id); branch.TipCommitId = uncommitted.FirstParentId; uncommitted.BranchId = localBranch.Id; } localBranch.FirstCommitId = localBranch.Commits.Last().Id; branch.IsMainPart = true; branch.LocalSubBranchId = localBranch.Id; localBranch.MainBranchId = branch.Id; if (branch.IsCurrent) { branch.IsCurrent = false; } branch.IsLocal = false; branch.TipCommitId = branch.RemoteTipCommitId; if (!branch.CommitIds.Any()) { branch.FirstCommitId = branch.TipCommitId; } }