Exemple #1
0
 private async Task <ImmutableList <string> > GetUpstreamBranches(string branch, Dictionary <string, ImmutableList <string> > upstreamBranchListings)
 {
     if (!upstreamBranchListings.ContainsKey(branch))
     {
         var result = (
             from b in (await settings.GetUpstreamBranches(branch).FirstOrDefaultAsync())
             select b.GroupName
             ).ToImmutableList();
         var removed = new HashSet <string>();
         foreach (var entry in result.ToArray())
         {
             if (removed.Contains(entry))
             {
                 // already found this as an exception
                 continue;
             }
             foreach (var newEntry in (await settings.GetAllUpstreamBranches(entry).FirstOrDefaultAsync()).Select(b => b.GroupName))
             {
                 removed.Add(newEntry);
             }
         }
         upstreamBranchListings[branch] = result.Except(removed).ToImmutableList();
     }
     return(upstreamBranchListings[branch]);
 }
            private async Task <ImmutableList <BranchGroupCompleteData> > GetBranchesToRemove(BranchGroupCompleteData targetBranch)
            {
                if (targetBranch.DirectDownstreamBranchGroups.Contains(newBaseBranch))
                {
                    // We're deleting an old branch, and all of its stuff moves down. This is like an integration branch rolling up. Easy.
                    return(new[] { targetBranch }.ToImmutableList());
                }
                var allBranches = await repository.AllBranches().FirstOrDefaultAsync();

                var actualBranches = await repository.DetectUpstream(sourceBranch);

                var upstreamBranches = await branchSettings.GetAllUpstreamBranches(sourceBranch).FirstAsync();

                // Filter to only the actual upstream branches; don't just remove anything that is downstream and happens to match!
                actualBranches = actualBranches.Intersect(upstreamBranches.Select(b => b.GroupName)).ToImmutableList();

                // FIXME - this is the _branches_ not the _groups_ that are up-to-date. That should be okay for these purposes.
                var downstream = (await branchSettings.GetBranchDetails(newBaseBranch).FirstAsync()).DownstreamBranchGroups;

                var consolidatingBranches = (await(from branch in allBranches.ToObservable()
                                                   where actualBranches.Contains(branch.GroupName) || branch.Branches.Select(b => b.Name).Any(actualBranches.Contains)
                                                   where downstream.Contains(branch.GroupName)
                                                   from result in GetLatestBranchTuple(branch)
                                                   select result
                                                   ).ToArray()).ToImmutableHashSet();


                var branchesToRemove = await FindReadyToConsolidate(consolidatingBranches);

                branchesToRemove = branchesToRemove.Concat(allBranches.Where(g => g.Branches.Select(b => b.Name).Contains(sourceBranch))).ToImmutableList();
                return(branchesToRemove);
            }
        private async System.Threading.Tasks.Task <ImmutableList <string> > PruneUpstream(GitRef original, ImmutableList <GitRef> allUpstream, ImmutableList <BranchGroup> configured, ImmutableList <GitRef> allRemotes)
        {
            var configuredLatest = configured.ToDictionary(branch => branch.GroupName, branch => branchIteration.GetLatestBranchNameIteration(branch.GroupName, allRemotes.Select(b => b.Name)));

            allUpstream = allUpstream.Where(maybeHasNewer =>
                                            !configured.Any(c => branchIteration.IsBranchIteration(c.GroupName, maybeHasNewer.Name) && maybeHasNewer.Name != configuredLatest[c.GroupName])
                                            ).ToImmutableList();

            for (var i = 0; i < allUpstream.Count; i++)
            {
                var upstream        = allUpstream[i];
                var isConfigured    = configuredLatest.Values.Contains(upstream.Name);
                var furtherUpstream = (from branchGroup in (await branchSettings.GetAllUpstreamBranches(upstream.Name).FirstOrDefaultAsync())
                                       let latest = configuredLatest[branchGroup.GroupName]
                                                    where allRemotes.Find(b => b.Name == latest).Commit != upstream.Commit
                                                    select latest)
                                      .ToImmutableHashSet();
                var oldLength = allUpstream.Count;
                allUpstream = allUpstream.Where(maybeMatch => !furtherUpstream.Contains(maybeMatch.Name)).ToImmutableList();
                if (oldLength != allUpstream.Count)
                {
                    i = -1;
                }
            }

            // TODO - this could be much smarter
            for (var i = 0; i < allUpstream.Count; i++)
            {
                var upstream        = allUpstream[i];
                var furtherUpstream = await repositoryState.DetectUpstream(upstream.Name, false);

                if (allUpstream.Intersect(furtherUpstream).Any())
                {
                    allUpstream = allUpstream.Except(furtherUpstream).ToImmutableList();
                    i           = -1;
                }
            }

            return(allUpstream.Select(b => b.Name).ToImmutableList());
        }