Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        private static void SetMasterBranchCommits(MRepository repository, MSubBranch subBranch)
        {
            CommitId commitId = subBranch.TipCommitId;

            while (commitId != CommitId.None)
            {
                MCommit commit = repository.Commits[commitId];

                if (commit.BranchName == subBranch.Name && commit.SubBranchId != null)
                {
                    // Do not break if commit is the tip
                    if (!(commit.Id == subBranch.TipCommitId && commit.SubBranchId == subBranch.SubBranchId))
                    {
                        break;
                    }
                }

                if (commit.HasBranchName && commit.BranchName != subBranch.Name)
                {
                    Log.Warn($"commit {commit} already has branch {commit.BranchName} != {subBranch.Name}");
                    break;
                }

                commit.SetBranchName(subBranch.Name);
                commit.SubBranchId = subBranch.SubBranchId;
                commitId           = commit.FirstParentId;
            }
        }
Esempio n. 3
0
 private static MBranch CreateBranchBasedOnSubBranch(MSubBranch subBranch)
 {
     return(new MBranch
     {
         Repository = subBranch.Repository,
         Name = subBranch.Name,
         IsMultiBranch = subBranch.IsMultiBranch,
         IsActive = subBranch.IsActive,
         ParentCommitId = subBranch.ParentCommitId
     });
 }
Esempio n. 4
0
        public void AddActiveBranches(IReadOnlyList <GitBranch> branches, MRepository repository)
        {
            GitStatus status = repository.Status;

            //GitBranch2 currentBranch = branches.GetCurrent();

            foreach (GitBranch gitBranch in branches)
            {
                BranchName branchName = gitBranch.Name;
                if (branchName == BranchName.OriginHead || branchName == BranchName.Head)
                {
                    continue;
                }

                MSubBranch subBranch = ToBranch(gitBranch, repository);
                repository.SubBranches[subBranch.SubBranchId] = subBranch;

                if (!status.OK && gitBranch.IsCurrent && !gitBranch.IsRemote)
                {
                    // Setting virtual uncommitted commit as tip of the current branch
                    subBranch.TipCommitId           = repository.Uncommitted.Id;
                    subBranch.TipCommit.SubBranchId = subBranch.SubBranchId;
                }
            }

            if (!branches.Any())
            {
                MSubBranch subBranch = ToEmptyMasterBranch(repository);
                repository.SubBranches[subBranch.SubBranchId] = subBranch;

                if (!status.OK)
                {
                    // Setting virtual uncommitted commit as tip of the current branch
                    subBranch.TipCommitId           = repository.Uncommitted.Id;
                    subBranch.TipCommit.SubBranchId = subBranch.SubBranchId;
                }
            }

            if (branches.TryGetCurrent(out GitBranch currentBranch) && currentBranch.IsDetached)
            {
                MSubBranch subBranch = ToBranch(currentBranch, repository);
                repository.SubBranches[subBranch.SubBranchId] = subBranch;

                if (!status.OK)
                {
                    // Setting virtual uncommitted commit as tip of the detached branch
                    subBranch.TipCommitId           = repository.Uncommitted.Id;
                    subBranch.TipCommit.SubBranchId = subBranch.SubBranchId;
                }
            }
        }
Esempio n. 5
0
 private void SetSubBranchCommits(MSubBranch subBranch)
 {
     foreach (MCommit commit in subBranch.TipCommit.FirstAncestors()
              .TakeWhile(c =>
                         c.BranchId == null &&
                         c.SubBranchId == null &&
                         (commitBranchNameService.GetBranchName(c) == null ||
                          commitBranchNameService.GetBranchName(c) == subBranch.Name) &&
                         !c.FirstChildren.Any(fc => fc.BranchName != subBranch.Name)))
     {
         commit.SetBranchName(subBranch.Name);
         commit.SubBranchId = subBranch.SubBranchId;
     }
 }
Esempio n. 6
0
        public void AddMultiBranches(MRepository repository)
        {
            bool isFound;

            do
            {
                isFound = false;
                foreach (var commit in repository.Commits.Values)
                {
                    if (commit.BranchId == null && !commit.HasBranchName)
                    {
                        isFound = true;

                        BranchName branchName = AnonyousBranchPrefix + commit.ShortId;

                        if (commit.FirstChildren.Count() > 1)
                        {
                            branchName = MultibranchPrefix + commit.ShortId;
                        }
                        else
                        {
                            BranchName commitBranchName = commitBranchNameService.GetBranchName(commit);
                            if (commitBranchName != null)
                            {
                                branchName = commitBranchName;
                            }
                        }

                        MSubBranch subBranch = new MSubBranch
                        {
                            Repository  = repository,
                            SubBranchId = Guid.NewGuid().ToString(),
                            Name        = branchName,
                            TipCommitId = commit.Id,
                            IsActive    = false,
                        };

                        subBranch.IsAnonymous   = IsBranchNameAnonyous(branchName);
                        subBranch.IsMultiBranch = IsBranchNameMultiBranch(branchName);

                        repository.SubBranches[subBranch.SubBranchId] = subBranch;

                        commit.SetBranchName(branchName);
                        commit.SubBranchId = subBranch.SubBranchId;
                        SetSubBranchCommits(subBranch);
                    }
                }
            } while (isFound);
        }
        private static MCommit GetRootCommit(MRepository repository)
        {
            MSubBranch mSubBranch = repository.SubBranches
                                    .FirstOrDefault(b => b.Value.IsActive && b.Value.Name == BranchName.Master).Value;

            if (mSubBranch == null)
            {
                Asserter.FailFast($"Repository {repository.WorkingFolder} does not have a master branch");
            }

            IEnumerable <MCommit> firstAncestors = mSubBranch.TipCommit.FirstAncestorsAnSelf();
            MCommit rootCommit = firstAncestors.Last();

            return(rootCommit);
        }
Esempio n. 8
0
        private static MBranch GetBranch(string branchId, MSubBranch subBranch)
        {
            MRepository repository = subBranch.Repository;

            MBranch branch;

            if (!repository.Branches.TryGetValue(branchId, out branch))
            {
                branch    = CreateBranchBasedOnSubBranch(subBranch);
                branch.Id = branchId;
                branch.Repository.Branches[branch.Id] = branch;
            }

            return(branch);
        }
Esempio n. 9
0
        public void SetMasterBranchCommits(MRepository repository)
        {
            // Local master
            MSubBranch master = repository.SubBranches
                                .FirstOrDefault(b => b.Value.Name == BranchName.Master && !b.Value.IsRemote).Value;

            if (master != null)
            {
                SetMasterBranchCommits(repository, master);
            }

            // Remote master
            master = repository.SubBranches
                     .FirstOrDefault(b => b.Value.Name == BranchName.Master && b.Value.IsRemote).Value;
            if (master != null)
            {
                SetMasterBranchCommits(repository, master);
            }
        }
Esempio n. 10
0
        public void AddInactiveBranches(MRepository repository)
        {
            // Get the list of active branch tips
            List <CommitId> activeBranches = repository.SubBranches
                                             .Where(b => b.Value.IsActive)
                                             .Select(b => b.Value.TipCommitId)
                                             .ToList();

            // Commits which has no child, which has this commit as a first parent, i.e. it is the
            // top of a branch and there is no existing branch at this commit
            IEnumerable <MCommit> topCommits = repository.Commits.Values
                                               .Where(commit =>
                                                      commit.BranchId == null &&
                                                      commit.SubBranchId == null &&
                                                      !commit.HasFirstChild &&
                                                      !activeBranches.Contains(commit.Id))
                                               .ToList();

            foreach (MCommit commit in topCommits)
            {
                MSubBranch subBranch = new MSubBranch
                {
                    Repository  = repository,
                    SubBranchId = Guid.NewGuid().ToString(),
                    TipCommitId = commit.Id,
                };

                BranchName branchName = TryFindBranchName(commit);
                if (branchName == null)
                {
                    branchName = AnonyousBranchPrefix + commit.ShortId;
                }

                subBranch.IsAnonymous   = IsBranchNameAnonyous(branchName);
                subBranch.IsMultiBranch = IsBranchNameMultiBranch(branchName);
                subBranch.Name          = branchName;

                repository.SubBranches[subBranch.SubBranchId] = subBranch;
            }
        }
Esempio n. 11
0
        public void AddMissingInactiveBranches(MRepository repository)
        {
            bool isFound;

            do
            {
                isFound = false;
                foreach (var commit in repository.Commits.Values)
                {
                    if (commit.BranchId == null && commit.HasBranchName && commit.SubBranchId == null)
                    {
                        isFound = true;

                        BranchName branchName = commit.BranchName;

                        MSubBranch subBranch = new MSubBranch
                        {
                            Repository  = repository,
                            Name        = branchName,
                            SubBranchId = Guid.NewGuid().ToString(),
                            TipCommitId = commit.Id,
                        };

                        subBranch.IsAnonymous   = IsBranchNameAnonyous(branchName);
                        subBranch.IsMultiBranch = IsBranchNameMultiBranch(branchName);

                        repository.SubBranches[subBranch.SubBranchId] = subBranch;

                        commit.SetBranchName(subBranch.Name);
                        commit.SubBranchId = subBranch.SubBranchId;

                        SetSubBranchCommits(subBranch);
                    }
                }
            } while (isFound);
        }