Пример #1
0
        protected override async Task <Dictionary <string, SlnFile> > ProcessImpl(
            Task <Dictionary <string, SlnFile> > previousTask)
        {
            var fileDictObj = await previousTask;

            var fileDict = (Dictionary <string, SlnFile>)fileDictObj;

            var repoResult = GitRunner.FindRepository(_workingDirectory);

            if (!repoResult.foundRepo)
            {
                Logger.LogError("Unable to find Git repository located in {0}. Shutting down.", _workingDirectory);
                return(new Dictionary <string, SlnFile>());
            }

            // validate the target branch
            if (!DiffHelper.HasBranch(repoResult.repo, _targetGitBranch))
            {
                Logger.LogError("Current git repository doesn't have any branch named [{0}]. Shutting down.", _targetGitBranch);
                return(new Dictionary <string, SlnFile>());
            }

            var repo          = repoResult.repo;
            var affectedFiles = DiffHelper.ChangedFiles(repo, _targetGitBranch).ToList();

            var projectFolders = fileDict.Where(x => x.Value.FileType == FileType.Project).ToDictionary(x => Path.GetDirectoryName(x.Key), v => Tuple.Create(v.Key, v.Value));

            // filter out any files that aren't affected by the diff
            var newDict = new Dictionary <string, SlnFile>();

            foreach (var file in affectedFiles)
            {
                Logger.LogDebug("Affected file: {0}", file);
                // this file is in the solution
                if (fileDict.ContainsKey(file))
                {
                    newDict[file] = fileDict[file];
                }
                else
                {
                    // special case - not all of the affected files were in the solution.
                    // Check to see if these affected files are in the same folder as any of the projects
                    var directoryName = Path.GetDirectoryName(file);

                    if (TryFindSubFolder(projectFolders.Keys, directoryName, out var projectFolder))
                    {
                        var project     = projectFolders[projectFolder].Item2;
                        var projectPath = projectFolders[projectFolder].Item1;
                        Logger.LogInformation("Adding project {0} to the set of affected files because non-code file {1}, " +
                                              "found inside same directory [{2}], was modified.", projectPath, file, directoryName);
                        newDict[projectPath] = project;
                    }
                }
            }

            // special case - not all of the affected files were in the solution.
            // Check to see if these affected files are in the same folder as any of the projects

            return(newDict);
        }
Пример #2
0
        public async Task <Dictionary <string, ICollection <string> > > Run()
        {
            // load the git repository
            var repoResult = GitRunner.FindRepository(Settings.WorkingDirectory);

            if (!repoResult.foundRepo)
            {
                Logger.LogError("Unable to find Git repository located in {0}. Shutting down.", Settings.WorkingDirectory);
                return(new Dictionary <string, ICollection <string> >());
            }

            // validate the target branch
            if (!DiffHelper.HasBranch(repoResult.repo, Settings.TargetBranch))
            {
                Logger.LogError("Current git repository doesn't have any branch named [{0}]. Shutting down.", Settings.TargetBranch);
                return(new Dictionary <string, ICollection <string> >());
            }

            // start the cancellation timer.
            _cts.CancelAfter(Settings.TimeoutDuration);
            var listAllFilesCmd  = new ListAffectedFilesCmd(Logger, _cts.Token, Settings.TargetBranch);
            var filterAllFolders = new FilterAffectedFoldersCmd(Logger, _cts.Token);

            return(await filterAllFolders.Process(listAllFilesCmd.Process(Task.FromResult(repoResult.repo))));
        }
Пример #3
0
        public void Should_detect_Repository()
        {
            var results = GitRunner.FindRepository(Repository.BasePath);

            results.foundRepo.Should().BeTrue();

            // note: due to what I believe is native interop here, the Repository.Info.WorkingDirectory
            // string appears to have an extra null terminator at the end
            results.repo.Info.WorkingDirectory.Should().Contain(Repository.BasePath.Trim());
        }
Пример #4
0
        protected override async Task <Dictionary <string, SlnFile> > ProcessImpl(
            Task <Dictionary <string, SlnFile> > previousTask)
        {
            var fileDictObj = await previousTask;

            var fileDict = (Dictionary <string, SlnFile>)fileDictObj;

            var repoResult = GitRunner.FindRepository(_workingDirectory);

            if (!repoResult.foundRepo)
            {
                Logger.LogError("Unable to find Git repository located in {0}. Shutting down.", _workingDirectory);
                return(new Dictionary <string, SlnFile>());
            }

            // validate the target branch
            if (!DiffHelper.HasBranch(repoResult.repo, _targetGitBranch))
            {
                Logger.LogError("Current git repository doesn't have any branch named [{0}]. Shutting down.", _targetGitBranch);
                return(new Dictionary <string, SlnFile>());
            }

            var repo          = repoResult.repo;
            var affectedFiles = DiffHelper.ChangedFiles(repo, _targetGitBranch);

            // filter out any files that aren't affected by the diff
            var newDict = new Dictionary <string, SlnFile>();

            foreach (var file in affectedFiles)
            {
                Logger.LogDebug("Affected file: {0}", file);
                // this file is in the solution
                if (fileDict.ContainsKey(file))
                {
                    newDict[file] = fileDict[file];
                }
            }

            return(newDict);
        }
Пример #5
0
        private static async Task <int> RunIncrementalist(SlnOptions options)
        {
            var logger = new ConsoleLogger("Incrementalist",
                                           (s, level) => level >= (options.Verbose ? LogLevel.Debug : LogLevel.Information), false);

            try
            {
                var pwd        = options.WorkingDirectory ?? Directory.GetCurrentDirectory();
                var insideRepo = Repository.IsValid(pwd);
                if (!insideRepo)
                {
                    logger.LogError("Current path {0} is not located inside any known Git repository.", pwd);
                    return(-2);
                }


                var repoFolder    = Repository.Discover(pwd);
                var workingFolder = Directory.GetParent(repoFolder).Parent;

                var repoResult = GitRunner.FindRepository(workingFolder.FullName);

                if (!repoResult.foundRepo)
                {
                    Console.WriteLine("Unable to find Git repository located in {0}. Shutting down.", workingFolder.FullName);
                    return(-3);
                }

                // validate the target branch
                if (!DiffHelper.HasBranch(repoResult.repo, options.GitBranch))
                {
                    // workaround common CI server issues and check to see if this same branch is located
                    // under "origin/{branchname}"
                    options.GitBranch = $"origin/{options.GitBranch}";
                    if (!DiffHelper.HasBranch(repoResult.repo, options.GitBranch))
                    {
                        Console.WriteLine("Current git repository doesn't have any branch named [{0}]. Shutting down.", options.GitBranch);
                        Console.WriteLine("[Debug] Here are all of the currently known branches in this repository");
                        foreach (var b in repoResult.repo.Branches)
                        {
                            Console.WriteLine(b.FriendlyName);
                        }
                        return(-4);
                    }
                }

                if (!string.IsNullOrEmpty(repoFolder))
                {
                    if (options.ListFolders)
                    {
                        await AnalyzeFolderDiff(options, workingFolder, logger);
                    }
                    else
                    {
                        await AnaylzeSolutionDIff(options, workingFolder, logger);
                    }
                }

                return(0);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Error encountered during execution of Incrementalist.");
                return(-1);
            }
        }