Created
August 18, 2021 01:13
-
-
Save thoys/9c2aeec8cb0f86759b05094c147aacf0 to your computer and use it in GitHub Desktop.
Fixup weird float changes in Core GIT Project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp3.1</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="DiffPlex" Version="1.7.0" /> | |
<PackageReference Include="LibGit2Sharp" Version="0.26.2" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Text; | |
using DiffPlex.DiffBuilder.Model; | |
using LibGit2Sharp; | |
namespace DiffFixer | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var repo = new Repository(@"C:\YourPathToYourRepoHere"); | |
foreach (var item in repo.RetrieveStatus()) | |
{ | |
if (item.State != FileStatus.ModifiedInIndex && item.State != FileStatus.ModifiedInWorkdir) | |
{ | |
continue; | |
} | |
var targetFilePath = Path.Combine(repo.Info.WorkingDirectory, item.FilePath); | |
if (Path.GetExtension(targetFilePath).ToLower() != ".pbt") | |
{ | |
Console.WriteLine("Skipping " + targetFilePath + " (not a PBT)"); | |
continue; | |
} | |
var blob = repo.Head.Tip[item.FilePath].Target as Blob; | |
string commitContent; | |
Debug.Assert(blob != null, nameof(blob) + " != null"); | |
using (var content = new StreamReader(blob.GetContentStream(), Encoding.UTF8)) | |
{ | |
commitContent = content.ReadToEnd(); | |
} | |
string workingContent; | |
using (var content = new StreamReader(targetFilePath, Encoding.UTF8)) | |
{ | |
workingContent = content.ReadToEnd(); | |
} | |
var diffResult = DiffPlex.DiffBuilder.SideBySideDiffBuilder.Diff(commitContent, workingContent); | |
var lines = System.IO.File.ReadAllLines(targetFilePath); | |
for (int i = 0; i < diffResult.NewText.Lines.Count; i++) | |
{ | |
var line = diffResult.NewText.Lines[i]; | |
if (line.Type != ChangeType.Modified) | |
{ | |
continue; | |
} | |
var stringParts = line.Text.Trim().Split(':', StringSplitOptions.RemoveEmptyEntries); | |
var oldStringParts = diffResult.OldText.Lines[i].Text.Trim().Split(':', StringSplitOptions.RemoveEmptyEntries); | |
if (stringParts.Length != 2 || oldStringParts.Length != 2) | |
{ | |
continue; | |
} | |
if (stringParts[0] != oldStringParts[0]) | |
{ | |
continue; | |
} | |
if (!double.TryParse(stringParts[1], out var newNumber) || | |
!double.TryParse(oldStringParts[1], out var oldNumber)) | |
{ | |
continue; | |
} | |
var numberDiff = Math.Abs(oldNumber - newNumber); | |
bool isLastDigitDifferent = stringParts[1].Length == oldStringParts[1].Length | |
&& stringParts[1][..^1] == oldStringParts[1][..^1] | |
&& oldStringParts[1][^1] != stringParts[1][^1]; | |
double skipDiff = 0.00001; | |
if (isLastDigitDifferent || numberDiff < skipDiff) | |
{ | |
lines[i] = diffResult.OldText.Lines[i].Text; | |
} | |
} | |
File.WriteAllText(targetFilePath, string.Join("\n", lines) + "\n"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment