public static List <GitFileInfo> GetFileInfo(string file) { List <GitFileInfo> list = new List <GitFileInfo>(); if (File.Exists(file) == false) { return(list); } Console.WriteLine("解析文件:" + file); var repoPath = GetRepositoryPath(file); if (repoPath == null) { return(list); } if (repoPath.StartsWith("\\") == false) { repoPath = repoPath + "\\"; } var relativePath = file.Replace(repoPath, "").Replace("\\", "/"); var git = GetRepository(repoPath); var blameHunks = git.Blame().SetFilePath(relativePath).SetTextComparator(RawTextComparator.WS_IGNORE_ALL).Call(); blameHunks.ComputeAll(); try { for (int i = 0; i < int.MaxValue; i++) { var author = blameHunks.GetSourceAuthor(i); //var author = blameHunks.GetSourceCommit(0); GitFileInfo info = new GitFileInfo() { File = file, Line = i, Author = author.GetName(), CommitTime = author.GetWhen() }; list.Add(info); } } catch (Exception) { } return(list); }
public void GetContentStream_returns_expected_content() { // ARRANGE var path = CreateFile("file1.txt", "Line1", "Line2", "", "Line3"); var expectedContent = File.ReadAllText(path).Replace("\r\n", "\n"); GitAdd(); GitCommit(); using var repository = new Repository(m_WorkingDirectory); var blob = (Blob)repository.Head.Tip.Tree.First(x => x.Name == "file1.txt").Target; var sut = new GitFileInfo("file1.txt", new GitDirectoryInfo(repository.Head.Tip), blob); // ACT using var contentStream = sut.GetContentStream(); var content = contentStream.ReadAsString(); // ASSERT content.Should().Be(expectedContent); }
private IDocument ReadFile(IExecutionContext context, IGitRepository repository, GitFileInfo file, string?branchName, string?tagName) { if (branchName is null && tagName is null) { throw new InvalidOperationException(); } var repositoryUrlHash = repository.Url.ComputeHashString(); var placeholderDestinationPath = new NormalizedPath(repositoryUrlHash).Combine(branchName ?? tagName).Combine(file.FullName); var placeholderSourcePath = context.FileSystem.RootPath .Combine("git") .Combine(placeholderDestinationPath); var metadata = new Dictionary <string, object>() { { GitKeys.GitRepositoryUrl, repository.Url }, { GitKeys.GitCommit, file.Commit.ToString() }, { GitKeys.GitRelativePath, new NormalizedPath(file.FullName) }, }; if (branchName is object) { metadata.Add(GitKeys.GitBranch, branchName); } if (tagName is object) { metadata.Add(GitKeys.GitTag, tagName); } using var stream = file.GetContentStream(); var memoryStream = new MemoryStream(); stream.CopyTo(memoryStream); return(context.CreateDocument( source: placeholderSourcePath, destination: placeholderDestinationPath, stream: memoryStream) .Clone(metadata)); }