Exemplo n.º 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);
        }
Exemplo n.º 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))));
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
            }
        }
Exemplo n.º 5
0
 public void ShouldDetectExistingBranch()
 {
     Repository.CreateBranch("foo");
     DiffHelper.HasBranch(Repository.Repository, "foo").Should().BeTrue();
 }