Beispiel #1
0
        /// <summary>
        ///     Get a specific build of a repository
        /// </summary>
        /// <returns>Process exit code.</returns>
        public override async Task <int> ExecuteAsync()
        {
            try
            {
                IRemote remote = RemoteFactory.GetBarOnlyRemote(_options, Logger);

                List <Build> matchingBuilds = null;
                if (_options.Id != 0)
                {
                    if (!string.IsNullOrEmpty(_options.BuildUri) ||
                        !string.IsNullOrEmpty(_options.Repo) ||
                        !string.IsNullOrEmpty(_options.Commit))
                    {
                        Console.WriteLine("--id should not be used with other options.");
                        return(Constants.ErrorCode);
                    }

                    matchingBuilds = new List <Build>()
                    {
                        await remote.GetBuildAsync(_options.Id)
                    };
                }
                else if (!string.IsNullOrEmpty(_options.BuildUri))
                {
                    if (_options.Id != 0 || !string.IsNullOrEmpty(_options.Repo) || !string.IsNullOrEmpty(_options.Commit))
                    {
                        Console.WriteLine("--uri should not be used with other options.");

                        return(Constants.ErrorCode);
                    }

                    Console.WriteLine("This option is not yet supported.");
                    return(Constants.ErrorCode);
                }
                else if (!string.IsNullOrEmpty(_options.Repo) || !string.IsNullOrEmpty(_options.Commit))
                {
                    if (string.IsNullOrEmpty(_options.Repo) != string.IsNullOrEmpty(_options.Commit))
                    {
                        Console.WriteLine("--repo and --commit should be used together.");
                        return(Constants.ErrorCode);
                    }
                    else if (_options.Id != 0 || !string.IsNullOrEmpty(_options.BuildUri))
                    {
                        Console.WriteLine("--repo and --commit should not be used with other options.");
                        return(Constants.ErrorCode);
                    }

                    matchingBuilds = (await remote.GetBuildsAsync(_options.Repo, _options.Commit)).ToList();
                }
                else
                {
                    Console.WriteLine("Please specify --id, --uri, or --repo and --commit to lookup a build.");
                    return(Constants.ErrorCode);
                }

                // Print the build info.
                if (!matchingBuilds.Any())
                {
                    Console.WriteLine($"Could not any builds matching the given criteria.");
                    return(Constants.ErrorCode);
                }

                switch (_options.OutputFormat)
                {
                case DarcOutputType.text:
                    foreach (Build build in matchingBuilds)
                    {
                        Console.Write(UxHelpers.GetTextBuildDescription(build));
                    }
                    break;

                case DarcOutputType.json:
                    Console.WriteLine(JsonConvert.SerializeObject(
                                          matchingBuilds.Select(build => UxHelpers.GetJsonBuildDescription(build)), Formatting.Indented));
                    break;

                default:
                    throw new NotImplementedException($"Output format type {_options.OutputFormat} not yet supported for get-build.");
                }

                return(Constants.SuccessCode);
            }
            catch (AuthenticationException e)
            {
                Console.WriteLine(e.Message);
                return(Constants.ErrorCode);
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Error: Failed to retrieve build information.");
                return(Constants.ErrorCode);
            }
        }
        /// <summary>
        ///     Obtain the root build.
        /// </summary>
        /// <returns>Root build to start with.</returns>
        private async Task <Build> GetRootBuildAsync()
        {
            if (!ValidateRootBuildOptions())
            {
                return(null);
            }

            IRemote remote = RemoteFactory.GetBarOnlyRemote(_options, Logger);

            string repoUri = GetRepoUri();

            if (_options.RootBuildId != 0)
            {
                Console.WriteLine($"Looking up build by id {_options.RootBuildId}");
                Build rootBuild = await remote.GetBuildAsync(_options.RootBuildId);

                if (rootBuild == null)
                {
                    Console.WriteLine($"No build found with id {_options.RootBuildId}");
                    return(null);
                }
                return(rootBuild);
            }
            else if (!string.IsNullOrEmpty(repoUri))
            {
                if (!string.IsNullOrEmpty(_options.Channel))
                {
                    IEnumerable <Channel> channels = await remote.GetChannelsAsync();

                    IEnumerable <Channel> desiredChannels = channels.Where(channel => channel.Name.Contains(_options.Channel, StringComparison.OrdinalIgnoreCase));
                    if (desiredChannels.Count() != 1)
                    {
                        Console.WriteLine($"Channel name {_options.Channel} did not match a unique channel. Available channels:");
                        foreach (var channel in channels)
                        {
                            Console.WriteLine($"  {channel.Name}");
                        }
                        return(null);
                    }
                    Channel targetChannel = desiredChannels.First();
                    Console.WriteLine($"Looking up latest build of '{repoUri}' on channel '{targetChannel.Name}'");
                    Build rootBuild = await remote.GetLatestBuildAsync(repoUri, targetChannel.Id);

                    if (rootBuild == null)
                    {
                        Console.WriteLine($"No build of '{repoUri}' found on channel '{targetChannel.Name}'");
                        return(null);
                    }
                    return(rootBuild);
                }
                else if (!string.IsNullOrEmpty(_options.Commit))
                {
                    Console.WriteLine($"Looking up builds of {_options.RepoUri}@{_options.Commit}");
                    IEnumerable <Build> builds = await remote.GetBuildsAsync(_options.RepoUri, _options.Commit);

                    // If more than one is available, print them with their IDs.
                    if (builds.Count() > 1)
                    {
                        Console.WriteLine($"There were {builds.Count()} potential root builds.  Please select one and pass it with --id");
                        foreach (var build in builds)
                        {
                            Console.WriteLine($"  {build.Id}: {build.AzureDevOpsBuildNumber} @ {build.DateProduced.ToLocalTime()}");
                        }
                        return(null);
                    }
                    Build rootBuild = builds.SingleOrDefault();
                    if (rootBuild == null)
                    {
                        Console.WriteLine($"No builds were found of {_options.RepoUri}@{_options.Commit}");
                    }
                    return(rootBuild);
                }
            }
            // Shouldn't get here if ValidateRootBuildOptions is correct.
            throw new DarcException("Options for root builds were not validated properly. Please contact @dnceng");
        }
Beispiel #3
0
        /// <summary>
        ///     Get a specific build of a repository
        /// </summary>
        /// <returns>Process exit code.</returns>
        public override async Task <int> ExecuteAsync()
        {
            try
            {
                IRemote remote = RemoteFactory.GetBarOnlyRemote(_options, Logger);

                List <Build> matchingBuilds = null;
                if (_options.Id != 0)
                {
                    if (!string.IsNullOrEmpty(_options.Repo) ||
                        !string.IsNullOrEmpty(_options.Commit))
                    {
                        Console.WriteLine("--id should not be used with other options.");
                        return(Constants.ErrorCode);
                    }

                    matchingBuilds = new List <Build>()
                    {
                        await remote.GetBuildAsync(_options.Id)
                    };
                }
                else if (!string.IsNullOrEmpty(_options.Repo) || !string.IsNullOrEmpty(_options.Commit))
                {
                    if (string.IsNullOrEmpty(_options.Repo) != string.IsNullOrEmpty(_options.Commit))
                    {
                        Console.WriteLine("--repo and --commit should be used together.");

                        return(Constants.ErrorCode);
                    }
                    var subscriptions = await remote.GetSubscriptionsAsync();

                    var possibleRepos = subscriptions
                                        .SelectMany(subscription => new List <string> {
                        subscription.SourceRepository, subscription.TargetRepository
                    })
                                        .Where(r => r.Contains(_options.Repo, StringComparison.OrdinalIgnoreCase))
                                        .ToHashSet(StringComparer.OrdinalIgnoreCase);

                    matchingBuilds = new List <Build>();
                    foreach (string repo in possibleRepos)
                    {
                        matchingBuilds.AddRange(await remote.GetBuildsAsync(repo, _options.Commit));
                    }
                }
                else
                {
                    Console.WriteLine("Please specify --id, --uri, or --repo and --commit to lookup a build.");
                    return(Constants.ErrorCode);
                }

                // Print the build info.
                if (!matchingBuilds.Any())
                {
                    Console.WriteLine($"Could not any builds matching the given criteria.");
                    return(Constants.ErrorCode);
                }

                switch (_options.OutputFormat)
                {
                case DarcOutputType.text:
                    foreach (Build build in matchingBuilds)
                    {
                        Console.Write(UxHelpers.GetTextBuildDescription(build));
                    }
                    break;

                case DarcOutputType.json:
                    Console.WriteLine(JsonConvert.SerializeObject(
                                          matchingBuilds.Select(build => UxHelpers.GetJsonBuildDescription(build)), Formatting.Indented));
                    break;

                default:
                    throw new NotImplementedException($"Output format type {_options.OutputFormat} not yet supported for get-build.");
                }

                return(Constants.SuccessCode);
            }
            catch (AuthenticationException e)
            {
                Console.WriteLine(e.Message);
                return(Constants.ErrorCode);
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Error: Failed to retrieve build information.");
                return(Constants.ErrorCode);
            }
        }
        /// <summary>
        ///     Creates a new dependency graph
        /// </summary>
        /// <param name="remoteFactory">Remote for factory for obtaining remotes to</param>
        /// <param name="rootDependencies">Root set of dependencies.  If null, then repoUri and commit should be set</param>
        /// <param name="repoUri">Root repository uri.  Must be valid if no root dependencies are passed.</param>
        /// <param name="commit">Root commit.  Must be valid if no root dependencies were passed.</param>
        /// <param name="includeToolset">If true, toolset dependencies are included.</param>
        /// <param name="lookupBuilds">If true, the builds contributing to each node are looked up. Must be a remote build.</param>
        /// <param name="remote">If true, remote graph build is used.</param>
        /// <param name="logger">Logger</param>
        /// <param name="reposFolder">Path to repos</param>
        /// <param name="remotesMap">Map of remotes (e.g. https://github.com/dotnet/corefx) to folders</param>
        /// <param name="testPath">If running unit tests, commits will be looked up as folders under this path</param>
        /// <returns>New dependency graph</returns>
        private static async Task <DependencyGraph> BuildDependencyGraphImplAsync(
            IRemoteFactory remoteFactory,
            IEnumerable <DependencyDetail> rootDependencies,
            string repoUri,
            string commit,
            DependencyGraphBuildOptions options,
            bool remote,
            ILogger logger,
            string reposFolder,
            IEnumerable <string> remotesMap,
            string testPath)
        {
            ValidateBuildOptions(remoteFactory, rootDependencies, repoUri, commit,
                                 options, remote, logger, reposFolder, remotesMap, testPath);

            if (rootDependencies != null)
            {
                logger.LogInformation($"Starting build of graph from {rootDependencies.Count()} root dependencies " +
                                      $"({repoUri}@{commit})");
                foreach (DependencyDetail dependency in rootDependencies)
                {
                    logger.LogInformation($"  {dependency.Name}@{dependency.Version}");
                }
            }
            else
            {
                logger.LogInformation($"Starting build of graph from ({repoUri}@{commit})");
            }

            AssetComparer                        assetEqualityComparer     = new AssetComparer();
            HashSet <Build>                      allContributingBuilds     = null;
            HashSet <DependencyDetail>           dependenciesMissingBuilds = null;
            HashSet <Build>                      rootNodeBuilds            = null;
            Dictionary <DependencyDetail, Build> dependencyCache           =
                new Dictionary <DependencyDetail, Build>(new DependencyDetailComparer());
            List <LinkedList <DependencyGraphNode> > cycles = new List <LinkedList <DependencyGraphNode> >();

            EarlyBreakOnType breakOnType = options.EarlyBuildBreak.Type;
            HashSet <string> breakOn     = null;

            if (breakOnType != EarlyBreakOnType.None)
            {
                breakOn = new HashSet <string>(options.EarlyBuildBreak.BreakOn, StringComparer.OrdinalIgnoreCase);
            }

            if (options.LookupBuilds)
            {
                allContributingBuilds     = new HashSet <Build>(new BuildComparer());
                dependenciesMissingBuilds = new HashSet <DependencyDetail>(new DependencyDetailComparer());
                rootNodeBuilds            = new HashSet <Build>(new BuildComparer());

                // Look up the dependency and get the creating build.
                IRemote barOnlyRemote = await remoteFactory.GetBarOnlyRemoteAsync(logger);

                IEnumerable <Build> potentialRootNodeBuilds = await barOnlyRemote.GetBuildsAsync(repoUri, commit);

                // Filter by those actually producing the root dependencies, if they were supplied
                if (rootDependencies != null)
                {
                    potentialRootNodeBuilds = potentialRootNodeBuilds.Where(b =>
                                                                            b.Assets.Any(a => rootDependencies.Any(d => assetEqualityComparer.Equals(a, d))));
                }
                // It's entirely possible that the root has no builds (e.g. change just checked in).
                // Don't record those. Instead, users of the graph should just look at the
                // root node's contributing builds and determine whether it's empty or not.
                foreach (Build build in potentialRootNodeBuilds)
                {
                    allContributingBuilds.Add(build);
                    rootNodeBuilds.Add(build);
                    AddAssetsToBuildCache(build, dependencyCache, breakOnType, breakOn);
                }
            }

            // Create the root node and add the repo to the visited bit vector.
            DependencyGraphNode rootGraphNode = new DependencyGraphNode(repoUri, commit, rootDependencies, rootNodeBuilds);

            rootGraphNode.VisitedNodes.Add(repoUri);
            // Nodes to visit is a queue, so that the evaluation order
            // of the graph is breadth first.
            Queue <DependencyGraphNode> nodesToVisit = new Queue <DependencyGraphNode>();

            nodesToVisit.Enqueue(rootGraphNode);
            HashSet <DependencyDetail> uniqueDependencyDetails;

            if (rootGraphNode.Dependencies != null)
            {
                uniqueDependencyDetails = new HashSet <DependencyDetail>(
                    rootGraphNode.Dependencies,
                    new DependencyDetailComparer());
                // Remove the dependencies details from the
                // break on if break on type is Dependencies
                if (breakOnType == EarlyBreakOnType.Dependencies)
                {
                    rootGraphNode.Dependencies.Select(d => breakOn.Remove(d.Name));
                }
            }
            else
            {
                uniqueDependencyDetails = new HashSet <DependencyDetail>(
                    new DependencyDetailComparer());
            }

            // If we already found the assets/dependencies we wanted, clear the
            // visit list and we'll drop through.
            if (breakOnType != EarlyBreakOnType.None && breakOn.Count == 0)
            {
                logger.LogInformation($"Stopping graph build after finding all assets/dependencies.");
                nodesToVisit.Clear();
            }

            // Cache of nodes we've visited. If we reach a repo/commit combo already in the cache,
            // we can just add these nodes as a child. The cache key is '{repoUri}@{commit}'
            Dictionary <string, DependencyGraphNode> nodeCache = new Dictionary <string, DependencyGraphNode>();

            nodeCache.Add($"{rootGraphNode.Repository}@{rootGraphNode.Commit}", rootGraphNode);

            // Cache of incoherent nodes, looked up by repo URI.
            Dictionary <string, DependencyGraphNode> visitedRepoUriNodes = new Dictionary <string, DependencyGraphNode>();
            HashSet <DependencyGraphNode>            incoherentNodes     = new HashSet <DependencyGraphNode>();
            // Cache of incoherent dependencies, looked up by name
            Dictionary <string, DependencyDetail> incoherentDependenciesCache = new Dictionary <string, DependencyDetail>();
            HashSet <DependencyDetail>            incoherentDependencies      = new HashSet <DependencyDetail>();

            while (nodesToVisit.Count > 0)
            {
                DependencyGraphNode node = nodesToVisit.Dequeue();

                logger.LogInformation($"Visiting {node.Repository}@{node.Commit}");

                IEnumerable <DependencyDetail> dependencies;
                // In case of the root node which is initially put on the stack,
                // we already have the set of dependencies to start at (this may have been
                // filtered by the caller). So no need to get the dependencies again.
                if (node.Dependencies != null)
                {
                    dependencies = node.Dependencies;
                }
                else
                {
                    logger.LogInformation($"Getting dependencies at {node.Repository}@{node.Commit}");

                    dependencies = await GetDependenciesAsync(
                        remoteFactory,
                        remote,
                        logger,
                        node.Repository,
                        node.Commit,
                        options.IncludeToolset,
                        remotesMap,
                        reposFolder,
                        testPath);

                    // Set the dependencies on the current node.
                    node.Dependencies = dependencies;
                }

                if (dependencies != null)
                {
                    foreach (DependencyDetail dependency in dependencies)
                    {
                        // If this dependency is missing information, then skip it.
                        if (string.IsNullOrEmpty(dependency.RepoUri) ||
                            string.IsNullOrEmpty(dependency.Commit))
                        {
                            logger.LogInformation($"Dependency {dependency.Name}@{dependency.Version} in " +
                                                  $"{node.Repository}@{node.Commit} " +
                                                  $"is missing repository uri or commit information, skipping");
                            continue;
                        }

                        // If the dependency's repo uri has been traversed, we've reached a cycle in this subgraph
                        // and should break.
                        if (node.VisitedNodes.Contains(dependency.RepoUri))
                        {
                            logger.LogInformation($"Node {node.Repository}@{node.Commit} " +
                                                  $"introduces a cycle to {dependency.RepoUri}, skipping");

                            if (options.ComputeCyclePaths)
                            {
                                var newCycles = ComputeCyclePaths(node, dependency.RepoUri);
                                cycles.AddRange(newCycles);
                            }
                            continue;
                        }

                        // Add the individual dependency to the set of unique dependencies seen
                        // in the whole graph.
                        uniqueDependencyDetails.Add(dependency);
                        if (incoherentDependenciesCache.TryGetValue(dependency.Name, out DependencyDetail existingDependency))
                        {
                            incoherentDependencies.Add(existingDependency);
                            incoherentDependencies.Add(dependency);
                        }
                        else
                        {
                            incoherentDependenciesCache.Add(dependency.Name, dependency);
                        }

                        HashSet <Build> nodeContributingBuilds = null;
                        if (options.LookupBuilds)
                        {
                            nodeContributingBuilds = new HashSet <Build>(new BuildComparer());
                            // Look up dependency in cache first
                            if (dependencyCache.TryGetValue(dependency, out Build existingBuild))
                            {
                                nodeContributingBuilds.Add(existingBuild);
                                allContributingBuilds.Add(existingBuild);
                            }
                            else
                            {
                                // Look up the dependency and get the creating build.
                                IRemote barOnlyRemote = await remoteFactory.GetBarOnlyRemoteAsync(logger);

                                IEnumerable <Build> potentiallyContributingBuilds = await barOnlyRemote.GetBuildsAsync(dependency.RepoUri, dependency.Commit);

                                // Filter by those actually producing the dependency. Most of the time this won't
                                // actually result in a different set of contributing builds, but should avoid any subtle bugs where
                                // there might be overlap between repos, or cases where there were multiple builds at the same sha.
                                potentiallyContributingBuilds = potentiallyContributingBuilds.Where(b =>
                                                                                                    b.Assets.Any(a => assetEqualityComparer.Equals(a, dependency)));
                                if (!potentiallyContributingBuilds.Any())
                                {
                                    // Couldn't find a build that produced the dependency.
                                    dependenciesMissingBuilds.Add(dependency);
                                }
                                else
                                {
                                    foreach (Build build in potentiallyContributingBuilds)
                                    {
                                        allContributingBuilds.Add(build);
                                        nodeContributingBuilds.Add(build);
                                        AddAssetsToBuildCache(build, dependencyCache, breakOnType, breakOn);
                                    }
                                }
                            }
                        }

                        // We may have visited this node before.  If so, add it as a child and avoid additional walks.
                        // Update the list of contributing builds.
                        if (nodeCache.TryGetValue($"{dependency.RepoUri}@{dependency.Commit}", out DependencyGraphNode existingNode))
                        {
                            if (options.LookupBuilds)
                            {
                                // Add the contributing builds. It's possible that
                                // different dependencies on a single node (repo/sha) were produced
                                // from multiple builds
                                foreach (Build build in nodeContributingBuilds)
                                {
                                    existingNode.ContributingBuilds.Add(build);
                                }
                            }
                            logger.LogInformation($"Node {dependency.RepoUri}@{dependency.Commit} has already been created, adding as child");
                            node.AddChild(existingNode, dependency);
                            continue;
                        }

                        // Otherwise, create a new node for this dependency.
                        DependencyGraphNode newNode = new DependencyGraphNode(
                            dependency.RepoUri,
                            dependency.Commit,
                            null,
                            node.VisitedNodes,
                            nodeContributingBuilds);

                        // Cache the dependency and add it to the visitation stack.
                        nodeCache.Add($"{dependency.RepoUri}@{dependency.Commit}", newNode);
                        nodesToVisit.Enqueue(newNode);
                        newNode.VisitedNodes.Add(dependency.RepoUri);
                        node.AddChild(newNode, dependency);

                        // Calculate incoherencies. If we've not yet visited the repo uri, add the
                        // new node based on its repo uri. Otherwise, add both the new node and the visited
                        // node to the incoherent nodes.
                        if (visitedRepoUriNodes.TryGetValue(dependency.RepoUri, out DependencyGraphNode visitedNode))
                        {
                            incoherentNodes.Add(visitedNode);
                            incoherentNodes.Add(newNode);
                        }
                        else
                        {
                            visitedRepoUriNodes.Add(newNode.Repository, newNode);
                        }

                        // If breaking on dependencies, then decide whether we need to break
                        // here.
                        if (breakOnType == EarlyBreakOnType.Dependencies)
                        {
                            breakOn.Remove(dependency.Name);
                        }

                        if (breakOnType != EarlyBreakOnType.None && breakOn.Count == 0)
                        {
                            logger.LogInformation($"Stopping graph build after finding all assets/dependencies.");
                            nodesToVisit.Clear();
                            break;
                        }
                    }
                }
            }

            switch (options.NodeDiff)
            {
            case NodeDiff.None:
                // Nothing
                break;

            case NodeDiff.LatestInGraph:
                await DoLatestInGraphNodeDiffAsync(remoteFactory, logger, nodeCache, visitedRepoUriNodes);

                break;

            case NodeDiff.LatestInChannel:
                await DoLatestInChannelGraphNodeDiffAsync(remoteFactory, logger, nodeCache, visitedRepoUriNodes);

                break;
            }

            return(new DependencyGraph(rootGraphNode,
                                       uniqueDependencyDetails,
                                       incoherentDependencies,
                                       nodeCache.Values,
                                       incoherentNodes,
                                       allContributingBuilds,
                                       dependenciesMissingBuilds,
                                       cycles));
        }
Beispiel #5
0
        /// <summary>
        ///     Creates a new dependency graph
        /// </summary>
        /// <param name="remoteFactory">Remote for factory for obtaining remotes to</param>
        /// <param name="rootDependencies">Root set of dependencies.  If null, then repoUri and commit should be set</param>
        /// <param name="repoUri">Root repository uri.  Must be valid if no root dependencies are passed.</param>
        /// <param name="commit">Root commit.  Must be valid if no root dependencies were passed.</param>
        /// <param name="includeToolset">If true, toolset dependencies are included.</param>
        /// <param name="lookupBuilds">If true, the builds contributing to each node are looked up. Must be a remote build.</param>
        /// <param name="remote">If true, remote graph build is used.</param>
        /// <param name="logger">Logger</param>
        /// <param name="reposFolder">Path to repos</param>
        /// <param name="remotesMap">Map of remotes (e.g. https://github.com/dotnet/corefx) to folders</param>
        /// <param name="testPath">If running unit tests, commits will be looked up as folders under this path</param>
        /// <returns>New dependency graph</returns>
        private static async Task <DependencyGraph> BuildDependencyGraphImplAsync(
            IRemoteFactory remoteFactory,
            IEnumerable <DependencyDetail> rootDependencies,
            string repoUri,
            string commit,
            DependencyGraphBuildOptions options,
            bool remote,
            ILogger logger,
            string reposFolder,
            IEnumerable <string> remotesMap,
            string testPath)
        {
            ValidateBuildOptions(remoteFactory, rootDependencies, repoUri, commit,
                                 options, remote, logger, reposFolder, remotesMap, testPath);

            if (rootDependencies != null)
            {
                logger.LogInformation($"Starting build of graph from {rootDependencies.Count()} root dependencies " +
                                      $"({repoUri}@{commit})");
                foreach (DependencyDetail dependency in rootDependencies)
                {
                    logger.LogInformation($"  {dependency.Name}@{dependency.Version}");
                }
            }
            else
            {
                logger.LogInformation($"Starting build of graph from ({repoUri}@{commit})");
            }

            IRemote barOnlyRemote = null;

            if (remote)
            {
                // Look up the dependency and get the creating build.
                barOnlyRemote = await remoteFactory.GetBarOnlyRemoteAsync(logger);
            }

            List <LinkedList <DependencyGraphNode> >          cycles           = new List <LinkedList <DependencyGraphNode> >();
            Dictionary <string, Task <IEnumerable <Build> > > buildLookupTasks = null;

            if (options.LookupBuilds)
            {
                buildLookupTasks = new Dictionary <string, Task <IEnumerable <Build> > >();

                // Look up the dependency and get the creating build.
                buildLookupTasks.Add($"{repoUri}@{commit}", barOnlyRemote.GetBuildsAsync(repoUri, commit));
            }

            // Create the root node and add the repo to the visited bit vector.
            List <Build>        allContributingBuilds = null;
            DependencyGraphNode rootGraphNode         = new DependencyGraphNode(repoUri, commit, rootDependencies, null);

            rootGraphNode.VisitedNodes.Add(repoUri);
            // Nodes to visit is a queue, so that the evaluation order
            // of the graph is breadth first.
            Queue <DependencyGraphNode> nodesToVisit = new Queue <DependencyGraphNode>();

            nodesToVisit.Enqueue(rootGraphNode);
            HashSet <DependencyDetail> uniqueDependencyDetails;

            if (rootGraphNode.Dependencies != null)
            {
                uniqueDependencyDetails = new HashSet <DependencyDetail>(
                    rootGraphNode.Dependencies,
                    new DependencyDetailComparer());
            }
            else
            {
                uniqueDependencyDetails = new HashSet <DependencyDetail>(
                    new DependencyDetailComparer());
            }

            // Cache of nodes we've visited. If we reach a repo/commit combo already in the cache,
            // we can just add these nodes as a child. The cache key is '{repoUri}@{commit}'
            Dictionary <string, DependencyGraphNode> nodeCache = new Dictionary <string, DependencyGraphNode>();

            nodeCache.Add($"{rootGraphNode.Repository}@{rootGraphNode.Commit}", rootGraphNode);

            // Cache of incoherent nodes, looked up by repo URI.
            Dictionary <string, DependencyGraphNode> visitedRepoUriNodes = new Dictionary <string, DependencyGraphNode>();
            HashSet <DependencyGraphNode>            incoherentNodes     = new HashSet <DependencyGraphNode>();
            // Cache of incoherent dependencies, looked up by name
            Dictionary <string, DependencyDetail> incoherentDependenciesCache = new Dictionary <string, DependencyDetail>();
            HashSet <DependencyDetail>            incoherentDependencies      = new HashSet <DependencyDetail>();

            while (nodesToVisit.Count > 0)
            {
                DependencyGraphNode node = nodesToVisit.Dequeue();

                logger.LogInformation($"Visiting {node.Repository}@{node.Commit}");

                IEnumerable <DependencyDetail> dependencies;
                // In case of the root node which is initially put on the stack,
                // we already have the set of dependencies to start at (this may have been
                // filtered by the caller). So no need to get the dependencies again.
                if (node.Dependencies != null)
                {
                    dependencies = node.Dependencies;
                }
                else
                {
                    logger.LogInformation($"Getting dependencies at {node.Repository}@{node.Commit}");

                    dependencies = await GetDependenciesAsync(
                        remoteFactory,
                        remote,
                        logger,
                        options.GitExecutable,
                        node.Repository,
                        node.Commit,
                        options.IncludeToolset,
                        remotesMap,
                        reposFolder,
                        testPath);

                    // Set the dependencies on the current node.
                    node.Dependencies = dependencies;
                }

                if (dependencies != null)
                {
                    foreach (DependencyDetail dependency in dependencies)
                    {
                        // If this dependency is missing information, then skip it.
                        if (string.IsNullOrEmpty(dependency.RepoUri) ||
                            string.IsNullOrEmpty(dependency.Commit))
                        {
                            logger.LogInformation($"Dependency {dependency.Name}@{dependency.Version} in " +
                                                  $"{node.Repository}@{node.Commit} " +
                                                  $"is missing repository uri or commit information, skipping");
                            continue;
                        }

                        if (options.LookupBuilds)
                        {
                            if (!buildLookupTasks.ContainsKey($"{dependency.RepoUri}@{dependency.Commit}"))
                            {
                                buildLookupTasks.Add($"{dependency.RepoUri}@{dependency.Commit}", barOnlyRemote.GetBuildsAsync(dependency.RepoUri, dependency.Commit));
                            }
                        }

                        // If the dependency's repo uri has been traversed, we've reached a cycle in this subgraph
                        // and should break.
                        if (node.VisitedNodes.Contains(dependency.RepoUri))
                        {
                            logger.LogInformation($"Node {node.Repository}@{node.Commit} " +
                                                  $"introduces a cycle to {dependency.RepoUri}, skipping");

                            if (options.ComputeCyclePaths)
                            {
                                var newCycles = ComputeCyclePaths(node, dependency.RepoUri);
                                cycles.AddRange(newCycles);
                            }
                            continue;
                        }

                        // Add the individual dependency to the set of unique dependencies seen
                        // in the whole graph.
                        uniqueDependencyDetails.Add(dependency);
                        if (incoherentDependenciesCache.TryGetValue(dependency.Name, out DependencyDetail existingDependency))
                        {
                            incoherentDependencies.Add(existingDependency);
                            incoherentDependencies.Add(dependency);
                        }
                        else
                        {
                            incoherentDependenciesCache.Add(dependency.Name, dependency);
                        }

                        // We may have visited this node before.  If so, add it as a child and avoid additional walks.
                        // Update the list of contributing builds.
                        if (nodeCache.TryGetValue($"{dependency.RepoUri}@{dependency.Commit}", out DependencyGraphNode existingNode))
                        {
                            logger.LogInformation($"Node {dependency.RepoUri}@{dependency.Commit} has already been created, adding as child");
                            node.AddChild(existingNode, dependency);
                            continue;
                        }

                        // Otherwise, create a new node for this dependency.
                        DependencyGraphNode newNode = new DependencyGraphNode(
                            dependency.RepoUri,
                            dependency.Commit,
                            null,
                            node.VisitedNodes,
                            null);

                        // Cache the dependency and add it to the visitation stack.
                        nodeCache.Add($"{dependency.RepoUri}@{dependency.Commit}", newNode);
                        nodesToVisit.Enqueue(newNode);
                        newNode.VisitedNodes.Add(dependency.RepoUri);
                        node.AddChild(newNode, dependency);

                        // Calculate incoherencies. If we've not yet visited the repo uri, add the
                        // new node based on its repo uri. Otherwise, add both the new node and the visited
                        // node to the incoherent nodes.
                        if (visitedRepoUriNodes.TryGetValue(dependency.RepoUri, out DependencyGraphNode visitedNode))
                        {
                            incoherentNodes.Add(visitedNode);
                            incoherentNodes.Add(newNode);
                        }
                        else
                        {
                            visitedRepoUriNodes.Add(newNode.Repository, newNode);
                        }
                    }
                }
            }

            if (options.LookupBuilds)
            {
                allContributingBuilds = await ComputeContributingBuildsAsync(buildLookupTasks,
                                                                             nodeCache.Values,
                                                                             logger);
            }

            switch (options.NodeDiff)
            {
            case NodeDiff.None:
                // Nothing
                break;

            case NodeDiff.LatestInGraph:
                await DoLatestInGraphNodeDiffAsync(remoteFactory, logger, nodeCache, visitedRepoUriNodes);

                break;

            case NodeDiff.LatestInChannel:
                await DoLatestInChannelGraphNodeDiffAsync(remoteFactory, logger, nodeCache, visitedRepoUriNodes);

                break;
            }

            return(new DependencyGraph(rootGraphNode,
                                       uniqueDependencyDetails,
                                       incoherentDependencies,
                                       nodeCache.Values,
                                       incoherentNodes,
                                       allContributingBuilds,
                                       cycles));
        }