Esempio n. 1
0
        private static void AddEmptyBranchesVirtualTipCommits(MRepository repository)
        {
            IEnumerable <MBranch> emptyBranches = repository.Branches.Values
                                                  .Where(b => !b.Commits.Any() && !b.IsLocalPart);

            foreach (MBranch branch in emptyBranches)
            {
                string virtualShaText = (Guid.NewGuid() + Guid.NewGuid().ToString()).Replace("-", "")
                                        .Substring(0, 40);
                CommitSha virtualSha = new CommitSha(virtualShaText);
                CommitId  virtualId  = new CommitId(virtualShaText);

                MCommit commit = AddVirtualCommit(repository, virtualId);


                commit.IsVirtual = true;
                commit.BranchId  = branch.Id;
                commit.SetBranchName(branch.Name);
                CopyToVirtualCommit(repository, branch, commit, virtualSha);
                SetChildOfParents(commit);

                //repository.Commits[commit.Id] = commit;

                branch.CommitIds.Add(commit.Id);
                branch.TipCommitId   = commit.Id;
                branch.FirstCommitId = commit.Id;

                branch.TipCommit.BranchTips = $"{branch.Name} branch tip";
            }
        }
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
        public void SetBranchTipCommitsNames(MRepository repository)
        {
            IEnumerable <MSubBranch> branches = repository.SubBranches.Values
                                                .Where(b =>
                                                       b.TipCommit.BranchId == null &&
                                                       b.TipCommit.SubBranchId == null);

            foreach (MSubBranch branch in branches)
            {
                MCommit branchTip = branch.TipCommit;

                if (!branchTip.HasFirstChild &&
                    !branches.Any(b => b.Name != branch.Name && b.TipCommitId == branch.TipCommitId))
                {
                    branchTip.SetBranchName(branch.Name);
                    branchTip.SubBranchId = branch.SubBranchId;
                }
            }
        }
Esempio n. 4
0
        private static void CopyToUncommitedCommit(
            GitBranch currentBranch,
            MRepository repository,
            GitStatus status,
            MCommit commit,
            CommitId parentId)
        {
            int modifiedCount = status.AllChanges;
            int conflictCount = status.Conflicted;

            string subject = $"{modifiedCount} uncommitted changes in working folder";

            if (conflictCount > 0)
            {
                subject =
                    $"{conflictCount} conflicts and {modifiedCount} changes, {ShortSubject(status)}";
                commit.HasConflicts = true;
            }
            else if (status.IsMerging)
            {
                subject          = $"{modifiedCount} changes, {ShortSubject(status)}";
                commit.IsMerging = true;
            }

            GitCommit gitCommit = new GitCommit(
                CommitSha.Uncommitted,
                subject,
                subject,
                "",
                DateTime.Now,
                DateTime.Now,
                new List <CommitId> {
                parentId
            });

            repository.GitCommits[CommitId.Uncommitted] = gitCommit;

            commit.SetBranchName(currentBranch?.Name ?? "master");

            commit.Tickets  = "";
            commit.BranchId = null;
        }
Esempio n. 5
0
        public void AddBranchCommits(IReadOnlyList <GitBranch> branches, MRepository repository)
        {
            GitStatus status = repository.Status;

            Timing t = new Timing();
            IEnumerable <CommitSha> rootCommits = branches.Select(b => b.TipSha);

            if (branches.TryGetCurrent(out GitBranch current) && current.IsDetached)
            {
                rootCommits = rootCommits.Concat(new[] { current.TipSha });
            }

            if (!rootCommits.Any())
            {
                AddVirtualEmptyCommit(repository);
                rootCommits = new[] { CommitSha.NoCommits };
            }

            rootCommits = rootCommits.ToList();
            t.Log("Root commit ids");


            Dictionary <CommitSha, object> added = new Dictionary <CommitSha, object>();

            Dictionary <CommitId, BranchName> branchNameByCommitId        = new Dictionary <CommitId, BranchName>();
            Dictionary <CommitId, BranchName> subjectBranchNameByCommitId = new Dictionary <CommitId, BranchName>();

            Stack <CommitSha> commitShas = new Stack <CommitSha>();

            rootCommits.ForEach(sha => commitShas.Push(sha));
            rootCommits.ForEach(sha => added[sha] = null);
            t.Log("Pushed roots on stack");

            while (commitShas.Any())
            {
                CommitSha commitSha = commitShas.Pop();
                CommitId  commitId  = new CommitId(commitSha.Sha);

                GitCommit gitCommit;
                IEnumerable <CommitSha> parentIds = null;
                if (!repository.GitCommits.TryGetValue(commitId, out gitCommit))
                {
                    Log.Warn($"Unknown commit {commitSha}");
                    continue;
                }

                if (IsMergeCommit(gitCommit))
                {
                    TrySetBranchNameFromSubject(commitId, gitCommit, branchNameByCommitId, subjectBranchNameByCommitId);
                }

                MCommit commit = repository.Commit(commitId);
                if (!commit.IsSet)
                {
                    if (commit.Id == CommitId.NoCommits)
                    {
                        commit.IsVirtual = true;
                        commit.SetBranchName("master");
                    }

                    AddCommit(commit, gitCommit);

                    if (parentIds == null)
                    {
                        parentIds = gitCommit.ParentIds.Select(id => repository.GitCommits[id].Sha);
                    }

                    AddParents(parentIds, commitShas, added);
                }

                BranchName branchName;
                if (branchNameByCommitId.TryGetValue(commitId, out branchName))
                {
                    // Branch name set by a child commit (pull merge commit)
                    commit.SetBranchName(branchName);
                }

                BranchName subjectBranchName;
                if (subjectBranchNameByCommitId.TryGetValue(commitId, out subjectBranchName))
                {
                    // Subject branch name set by a child commit (merge commit)
                    gitCommit.SetBranchNameFromSubject(subjectBranchName);
                }
            }

            if (!status.OK)
            {
                // Adding a virtual "uncommitted" commit since current working folder status has changes
                AddVirtualUncommitted(current, status, repository);
            }
        }