public void ShouldSetTheExtensionAsEmpty()
                {
                    var detector = new ExtensionDetector();
                    var result   = detector.Detect("./some/file/path/.gitignore");

                    Assert.Equal(null, result.Extension);
                }
                public void ShouldClassifyTheFileAsUnknown()
                {
                    var detector = new ExtensionDetector();
                    var result   = detector.Detect("./some/file/path/.gitignore");

                    Assert.Equal(null, result.Language);
                    Assert.Equal(DetectionResult.Unknown, result);
                }
                public void ShouldReturnTheLanguageAndNumberOfLines(string filepath, string resourceName,
                                                                    ProgrammingLanguage expectedLanguage, int expectedlines)
                {
                    var detector = new ExtensionDetector();
                    var assembly = Assembly.GetExecutingAssembly();

                    var names = Assembly.GetExecutingAssembly().GetManifestResourceNames();

                    using (var stream = assembly.GetManifestResourceStream($"Proggr.LangDetect.Tests.Fixtures.{resourceName}"))
                        using (var reader = new StreamReader(stream))
                        {
                            var contents = reader.ReadToEnd();
                            var result   = detector.Detect(filepath, contents);

                            Assert.Equal(expectedLanguage, result.Language);
                            Assert.Equal(expectedlines, result.Lines);
                        }
                }
示例#4
0
        public override async Task <JobResult> Run()
        {
            var args         = JobDescriptor.GetArgumentsJson <DetectAndHashJobArgs>();
            var codeLocation = _codeLocationRepository.GetCodeLocation(args.RepositoryId);

            return(await Task.Run(() =>
            {
                try
                {
                    var repoRoot = _codeLocationRepository.GetCodeLocationLocalPath(codeLocation.FullName);
                    var repo = new Repository(repoRoot, new RepositoryOptions()
                    {
                    });

                    Parallel.ForEach(args.Shas, async(sha) =>
                    {
                        var commit = await _repositoryController.GetCommit(repo, sha);
                        var patch = await CommitController.GetChangeSet(repo, commit);

                        Parallel.ForEach(patch, (change) =>
                        {
                            if (change.Mode == Mode.NonExecutableFile && change.Status != ChangeKind.Deleted &&
                                change.Status != ChangeKind.Ignored && change.Status != ChangeKind.Untracked)
                            {
                                var filePath = Path.Combine(repoRoot, change.Path);
                                string contents = null;
                                if (!change.IsBinaryComparison)
                                {
                                    var treeEntry = commit.Tree[change.Path];
                                    if (treeEntry.TargetType == TreeEntryTargetType.Blob)
                                    {
                                        // this is a file we can open and read
                                        // get the file contents
                                        var blob = (Blob)treeEntry.Target;
                                        var contentStream = blob.GetContentStream();
                                        using (var reader = new StreamReader(contentStream, Encoding.UTF8))
                                        {
                                            contents = reader.ReadToEnd();
                                        }
                                    }
                                }

                                var detection = _extDectector.Detect(filePath, contents);

                                // compute our hashes
                                byte[] raw, nowhitespace, nonewlines;
                                using (MD5 md5 = MD5.Create())
                                {
                                    raw = md5.ComputeHash(Encoding.UTF8.GetBytes(contents));
                                    nowhitespace = md5.ComputeHash(Encoding.UTF8.GetBytes(contents.Replace(" ", "")));
                                    nonewlines = md5.ComputeHash(Encoding.UTF8.GetBytes(contents.Replace("\n", "")));
                                }

                                // save the results
                                _fileRepository.AddFile(new FileData
                                {
                                    CommitId = sha,
                                    Ext = detection.Extension,
                                    FileName = filePath,
                                    RelativePath = change.Path,
                                    HashRaw = raw,
                                    HashNoWhiteSpace = nowhitespace,
                                    HashNoNewLines = nonewlines
                                });
                            }
                        });
                        // get the files that changed in this commit
                    });

                    return Task.FromResult <JobResult>(new JobSuccessResult(this));
                }
                catch (Exception e)
                {
                    return Task.FromResult <JobResult>(new JobFailureResult(e, this));
                }
            }));
        }