public void Parse() { string summaryText = @" 1 1 NGitHub.nuspec 1 1 src/NGitHub/IRepositoryService.cs 5 5 src/NGitHub/RepositoryService.cs 1 1 src/NGitHub/SharedAssemblyInfo.cs - - Test.dll 4 files changed, 8 insertions(+), 8 deletions(-) "; var detail = new ChangeSetDetail(); GitExeRepository.ParseSummary(summaryText.AsReader(), detail); Assert.Equal(5, detail.Files.Count); Assert.Equal(4, detail.FilesChanged); Assert.Equal(8, detail.Insertions); Assert.Equal(8, detail.Deletions); AssertFile(detail, "NGitHub.nuspec", insertions: 1, deletions: 1, binary: false); AssertFile(detail, "src/NGitHub/IRepositoryService.cs", insertions: 1, deletions: 1, binary: false); AssertFile(detail, "src/NGitHub/RepositoryService.cs", insertions: 5, deletions: 5, binary: false); AssertFile(detail, "src/NGitHub/SharedAssemblyInfo.cs", insertions: 1, deletions: 1, binary: false); AssertFile(detail, "Test.dll", binary: true); }
internal static void ParseSummary(IStringReader reader, ChangeSetDetail detail) { reader.SkipWhitespace(); while (!reader.Done) { string line = reader.ReadLine(); if (ParserHelpers.IsSingleNewLine(line)) { break; } else if (line.Contains('\t')) { // n n path string[] parts = line.Split('\t'); int insertions; Int32.TryParse(parts[0], out insertions); int deletions; Int32.TryParse(parts[1], out deletions); string path = parts[2].TrimEnd(); detail.Files[path] = new FileInfo { Insertions = insertions, Deletions = deletions, Binary = parts[0] == "-" && parts[1] == "-" }; } else { // n files changed, n insertions(+), n deletions(-) ParserHelpers.ParseSummaryFooter(line, detail); } } }
private ChangeSetDetail PopulateDetails(string id, ChangeSetDetail detail) { var summaryCommand = new DiffCommand { SummaryOnly = true }; if (!String.IsNullOrEmpty(id)) { summaryCommand.ChangeIntroducedByRevision = id; } IStringReader summaryReader = _repository.Diff(summaryCommand).AsReader(); ParseSummary(summaryReader, detail); var diffCommand = new DiffCommand { UseGitDiffFormat = true, }; if (!String.IsNullOrEmpty(id)) { diffCommand.ChangeIntroducedByRevision = id; } var diffReader = _repository.Diff(diffCommand).AsReader(); GitExeRepository.ParseDiffAndPopulate(diffReader, detail); return(detail); }
public ChangeSetDetail GetDetails(string id) { var changeSet = GetChangeSet(id); var detail = new ChangeSetDetail(changeSet); return(PopulateDetails(id, detail)); }
public ChangeSetDetailViewModel GetWorking() { ChangeSetDetail workingChanges = _repository.GetWorkingChanges(); if (workingChanges != null) { return(new ChangeSetDetailViewModel(workingChanges)); } return(null); }
public ChangeSetDetailViewModel(ChangeSetDetail detail) { if (detail.ChangeSet != null) { ChangeSet = new ChangeSetViewModel(detail.ChangeSet); } Deletions = detail.Deletions; FilesChanged = detail.FilesChanged; Insertions = detail.Insertions; Files = detail.Files; }
internal static ChangeSetDetail ParseCommitAndSummary(IStringReader reader) { // Parse the changeset ChangeSet changeSet = ParseCommit(reader); var detail = new ChangeSetDetail(changeSet); ParseSummary(reader, detail); return(detail); }
public void PopulateStatusHandlesFilesWithSpaces() { string status = @" A New File "; ChangeSetDetail detail = new ChangeSetDetail(); detail.Files["New File"] = new FileInfo(); GitExeRepository.PopulateStatus(status.AsReader(), detail); Assert.Equal(ChangeType.Added, detail.Files["New File"].Status); }
private void AssertFile(ChangeSetDetail detail, string path, int?insertions = null, int?deletions = null, bool binary = false) { FileInfo fi; Assert.True(detail.Files.TryGetValue(path, out fi)); Assert.Equal(binary, fi.Binary); if (insertions != null) { Assert.Equal(insertions, fi.Insertions); } if (deletions != null) { Assert.Equal(deletions, fi.Deletions); } }
internal static void ParseSummaryFooter(string line, ChangeSetDetail detail) { // n files changed, n insertions(+), n deletions(-) var subReader = line.AsReader(); subReader.SkipWhitespace(); detail.FilesChanged = subReader.ReadInt(); subReader.ReadUntil(','); subReader.Skip(1); subReader.SkipWhitespace(); detail.Insertions = subReader.ReadInt(); subReader.ReadUntil(','); subReader.Skip(1); subReader.SkipWhitespace(); detail.Deletions = subReader.ReadInt(); }
public void ParseSummaryReadsSummaryLineFromDiff() { // Arrange string input = @"Bar.txt | 1 - Baz.txt | 1 + Hello.txt | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-)"; var changeSetDetail = new ChangeSetDetail(); // Act HgRepository.ParseSummary(input.AsReader(), changeSetDetail); // Assert Assert.Equal(3, changeSetDetail.FilesChanged); Assert.Equal(2, changeSetDetail.Insertions); Assert.Equal(2, changeSetDetail.Deletions); }
private static ChangeSetDetail ParseShow(IStringReader reader, bool includeChangeSet = true) { ChangeSetDetail detail = null; if (includeChangeSet) { detail = ParseCommitAndSummary(reader); } else { detail = new ChangeSetDetail(); ParseSummary(reader, detail); } ParseDiffAndPopulate(reader, detail); return(detail); }
public void ParseDiffChunkHandlesFilesWithSpacesInName() { string diff = @"diff --git a/New File b/New File new file mode 100644 index 0000000..261a6bf --- /dev/null +++ b/New File @@ -0,0 +1 @@ +Ayayayya \ No newline at end of file"; ChangeSetDetail detail = null; var diffChunk = GitExeRepository.ParseDiffChunk(diff.AsReader(), ref detail); Assert.False(diffChunk.Binary); Assert.Equal("New File", diffChunk.FileName); Assert.Equal(2, diffChunk.Lines.Count); Assert.Equal("+Ayayayya", diffChunk.Lines[1].Text.TrimEnd()); }
private ChangeSetDetail MakeNewFileDiff(IEnumerable <FileStatus> statuses) { var changeSetDetail = new ChangeSetDetail(); foreach (var fileStatus in statuses) { var fileInfo = new FileInfo { Status = fileStatus.Status }; foreach (var diff in CreateDiffLines(fileStatus.Path)) { fileInfo.DiffLines.Add(diff); } changeSetDetail.Files[fileStatus.Path] = fileInfo; changeSetDetail.FilesChanged++; changeSetDetail.Insertions += fileInfo.DiffLines.Count; } return(changeSetDetail); }
private void ParseSummary(IStringReader reader, ChangeSetDetail detail) { while (!reader.Done) { string line = reader.ReadLine(); if (line.Contains("|")) { string[] parts = line.Split('|'); string path = parts[0].Trim(); // TODO: Figure out a way to get this information detail.Files[path] = new FileInfo(); } else { // n files changed, n insertions(+), n deletions(-) ParserHelpers.ParseSummaryFooter(line, detail); } } }
internal static void PopulateStatus(IStringReader reader, ChangeSetDetail detail) { while (!reader.Done) { string line = reader.ReadLine(); // Status lines contain tabs if (!line.Contains("\t")) { continue; } var lineReader = line.AsReader(); string status = lineReader.ReadUntilWhitespace(); lineReader.SkipWhitespace(); string name = lineReader.ReadToEnd().TrimEnd(); lineReader.SkipWhitespace(); FileInfo file; if (detail.Files.TryGetValue(name, out file)) { file.Status = ConvertStatus(status); } } }
internal static void ParseDiffAndPopulate(IStringReader reader, ChangeSetDetail detail) { foreach (var diff in ParseDiff(reader)) { FileInfo stats; if (!detail.Files.TryGetValue(diff.FileName, out stats)) { stats = new FileInfo(); detail.Files.Add(diff.FileName, stats); } // Set the binary flag if any of the files are binary bool binary = diff.Binary || stats.Binary; stats.Binary = binary; diff.Binary = binary; foreach (var line in diff.Lines) { stats.DiffLines.Add(line); } } }
private static FileDiff ParseDiffHeader(IStringReader reader, ChangeSetDetail merge) { string fileName = ParseFileName(reader.ReadLine()); bool binary = false; while (!reader.Done) { string line = reader.ReadLine(); if (line.StartsWith("@@", StringComparison.Ordinal)) { reader.PutBack(line.Length); break; } else if (line.StartsWith("GIT binary patch", StringComparison.Ordinal)) { binary = true; } } if (binary) { // Skip binary files reader.ReadToEnd(); } var diff = new FileDiff(fileName) { Binary = binary }; // Skip files from merged changesets if (merge != null && merge.Files.ContainsKey(fileName)) { return(null); } return(diff); }
internal static IEnumerable <FileDiff> ParseDiff(IStringReader reader) { var builder = new StringBuilder(); // If this was a merge change set then we'll parse the details out of the // first diff ChangeSetDetail merge = null; do { string line = reader.ReadLine(); // If we see a new diff header then process the previous diff if any if ((reader.Done || IsDiffHeader(line)) && builder.Length > 0) { if (reader.Done) { builder.Append(line); } string diffChunk = builder.ToString(); FileDiff diff = ParseDiffChunk(diffChunk.AsReader(), ref merge); if (diff != null) { yield return(diff); } builder.Clear(); } if (!reader.Done) { builder.Append(line); } } while (!reader.Done); }
private ChangeSetDetail MakeNewFileDiff(IEnumerable<FileStatus> statuses) { var changeSetDetail = new ChangeSetDetail(); foreach (var fileStatus in statuses) { var fileInfo = new FileInfo { Status = fileStatus.Status }; foreach (var diff in CreateDiffLines(fileStatus.Path)) { fileInfo.DiffLines.Add(diff); } changeSetDetail.Files[fileStatus.Path] = fileInfo; changeSetDetail.FilesChanged++; changeSetDetail.Insertions += fileInfo.DiffLines.Count; } return changeSetDetail; }
private static ChangeSetDetail ParseShow(IStringReader reader, bool includeChangeSet = true) { ChangeSetDetail detail = null; if (includeChangeSet) { detail = ParseCommitAndSummary(reader); } else { detail = new ChangeSetDetail(); ParseSummary(reader, detail); } ParseDiffAndPopulate(reader, detail); return detail; }
private static FileDiff ParseDiffHeader(IStringReader reader, ChangeSetDetail merge) { string fileName = ParseFileName(reader.ReadLine()); bool binary = false; while (!reader.Done) { string line = reader.ReadLine(); if (line.StartsWith("@@", StringComparison.Ordinal)) { reader.PutBack(line.Length); break; } else if (line.StartsWith("GIT binary patch", StringComparison.Ordinal)) { binary = true; } } if (binary) { // Skip binary files reader.ReadToEnd(); } var diff = new FileDiff(fileName) { Binary = binary }; // Skip files from merged changesets if (merge != null && merge.Files.ContainsKey(fileName)) { return null; } return diff; }
internal static void PopulateStatus(IStringReader reader, ChangeSetDetail detail) { while (!reader.Done) { string line = reader.ReadLine(); // Status lines contain tabs if (!line.Contains("\t")) { continue; } var lineReader = line.AsReader(); string status = lineReader.ReadUntilWhitespace(); lineReader.SkipWhitespace(); string name = lineReader.ReadToEnd().TrimEnd(); lineReader.SkipWhitespace(); FileInfo file; if (detail.Files.TryGetValue(name, out file)) { file.Status = ConvertStatus(status); } } }
internal static void ParseSummary(IStringReader reader, ChangeSetDetail detail) { reader.SkipWhitespace(); while (!reader.Done) { string line = reader.ReadLine(); if (ParserHelpers.IsSingleNewLine(line)) { break; } else if (line.Contains('\t')) { // n n path string[] parts = line.Split('\t'); int insertions; Int32.TryParse(parts[0], out insertions); int deletions; Int32.TryParse(parts[1], out deletions); string path = parts[2].TrimEnd(); detail.Files[path] = new FileInfo { Insertions = insertions, Deletions = deletions, Binary = parts[0] == "-" && parts[1] == "-" }; } else { // n files changed, n insertions(+), n deletions(-) ParserHelpers.ParseSummaryFooter(line, detail); } } }
internal static FileDiff ParseDiffChunk(IStringReader reader, ref ChangeSetDetail merge) { var diff = ParseDiffHeader(reader, merge); if (diff == null) { return null; } // Current diff range DiffRange currentRange = null; int? leftCounter = null; int? rightCounter = null; // Parse the file diff while (!reader.Done) { int? currentLeft = null; int? currentRight = null; string line = reader.ReadLine(); if (line.Equals(@"\ No newline at end of file", StringComparison.OrdinalIgnoreCase)) { continue; } bool isDiffRange = line.StartsWith("@@", StringComparison.Ordinal); ChangeType? changeType = null; if (line.StartsWith("+", StringComparison.Ordinal)) { changeType = ChangeType.Added; currentRight = ++rightCounter; currentLeft = null; } else if (line.StartsWith("-", StringComparison.Ordinal)) { changeType = ChangeType.Deleted; currentLeft = ++leftCounter; currentRight = null; } else if (IsCommitHeader(line)) { reader.PutBack(line.Length); merge = ParseCommitAndSummary(reader); } else { if (!isDiffRange) { currentLeft = ++leftCounter; currentRight = ++rightCounter; } changeType = ChangeType.None; } if (changeType != null) { var lineDiff = new LineDiff(changeType.Value, line); if (!isDiffRange) { lineDiff.LeftLine = currentLeft; lineDiff.RightLine = currentRight; } diff.Lines.Add(lineDiff); } if (isDiffRange) { // Parse the new diff range currentRange = DiffRange.Parse(line.AsReader()); leftCounter = currentRange.LeftFrom - 1; rightCounter = currentRange.RightFrom - 1; } } return diff; }
internal static void ParseDiffAndPopulate(IStringReader reader, ChangeSetDetail detail) { foreach (var diff in ParseDiff(reader)) { FileInfo stats; if (!detail.Files.TryGetValue(diff.FileName, out stats)) { stats = new FileInfo(); detail.Files.Add(diff.FileName, stats); } // Set the binary flag if any of the files are binary bool binary = diff.Binary || stats.Binary; stats.Binary = binary; diff.Binary = binary; foreach (var line in diff.Lines) { stats.DiffLines.Add(line); } } }
internal static ChangeSetDetail ParseCommitAndSummary(IStringReader reader) { // Parse the changeset ChangeSet changeSet = ParseCommit(reader); var detail = new ChangeSetDetail(changeSet); ParseSummary(reader, detail); return detail; }
public ChangeSetDetail GetDetails(string id) { var changeSet = GetChangeSet(id); var detail = new ChangeSetDetail(changeSet); return PopulateDetails(id, detail); }
private void ParseSummary(IStringReader reader, ChangeSetDetail detail) { while (!reader.Done) { string line = reader.ReadLine(); if (line.Contains("|")) { string[] parts = line.Split('|'); string path = parts[0].Trim(); // TODO: Figure out a way to get this information detail.Files[path] = new FileInfo(); } else { // n files changed, n insertions(+), n deletions(-) ParserHelpers.ParseSummaryFooter(line, detail); } } }
private ChangeSetDetail PopulateDetails(string id, ChangeSetDetail detail) { var summaryCommand = new DiffCommand { SummaryOnly = true }; if (!String.IsNullOrEmpty(id)) { summaryCommand.ChangeIntroducedByRevision = id; } IStringReader summaryReader = _repository.Diff(summaryCommand).AsReader(); ParseSummary(summaryReader, detail); var diffCommand = new DiffCommand { UseGitDiffFormat = true, }; if (!String.IsNullOrEmpty(id)) { diffCommand.ChangeIntroducedByRevision = id; } var diffReader = _repository.Diff(diffCommand).AsReader(); GitExeRepository.ParseDiffAndPopulate(diffReader, detail); return detail; }
internal static FileDiff ParseDiffChunk(IStringReader reader, ref ChangeSetDetail merge) { var diff = ParseDiffHeader(reader, merge); if (diff == null) { return(null); } // Current diff range DiffRange currentRange = null; int? leftCounter = null; int? rightCounter = null; // Parse the file diff while (!reader.Done) { int? currentLeft = null; int? currentRight = null; string line = reader.ReadLine(); if (line.Equals(@"\ No newline at end of file", StringComparison.OrdinalIgnoreCase)) { continue; } bool isDiffRange = line.StartsWith("@@", StringComparison.Ordinal); ChangeType?changeType = null; if (line.StartsWith("+", StringComparison.Ordinal)) { changeType = ChangeType.Added; currentRight = ++rightCounter; currentLeft = null; } else if (line.StartsWith("-", StringComparison.Ordinal)) { changeType = ChangeType.Deleted; currentLeft = ++leftCounter; currentRight = null; } else if (IsCommitHeader(line)) { reader.PutBack(line.Length); merge = ParseCommitAndSummary(reader); } else { if (!isDiffRange) { currentLeft = ++leftCounter; currentRight = ++rightCounter; } changeType = ChangeType.None; } if (changeType != null) { var lineDiff = new LineDiff(changeType.Value, line); if (!isDiffRange) { lineDiff.LeftLine = currentLeft; lineDiff.RightLine = currentRight; } diff.Lines.Add(lineDiff); } if (isDiffRange) { // Parse the new diff range currentRange = DiffRange.Parse(line.AsReader()); leftCounter = currentRange.LeftFrom - 1; rightCounter = currentRange.RightFrom - 1; } } return(diff); }