Skip to content

Instantly share code, notes, and snippets.

@MichalStrehovsky
Last active October 27, 2019 21:50
Show Gist options
  • Save MichalStrehovsky/f72dfc680a1fc53e7003c3594b2beb8b to your computer and use it in GitHub Desktop.
Save MichalStrehovsky/f72dfc680a1fc53e7003c3594b2beb8b to your computer and use it in GitHub Desktop.
// On CoreCLR side: git format-patch --stdout -1 {SHA-HASH} > d:\git\in.patch
// run tool
// On CoreRT side: git am d:\git\out.patch
/*
mapping.txt:
src/tools/crossgen2/Common/CommandLine src/Common/src/CommandLine
src/tools/crossgen2/Common/Compiler src/ILCompiler.Compiler/src/Compiler
src/tools/crossgen2/Common/JitInterface src/JitInterface/src
src/tools/crossgen2/Common/Internal src/Common/src/Internal
src/tools/crossgen2/Common/System src/Common/src/System
src/tools/crossgen2/Common/TypeSystem src/Common/src/TypeSystem
src/tools/crossgen2/jitinterface src/Native/jitinterface
*/
using System;
using System.Collections.Generic;
using System.IO;
namespace PortCommit
{
class Program
{
static void Main(string[] args)
{
var map = new List<KeyValuePair<string, string>>();
foreach (var line in File.ReadLines(@"d:\git\mapping.txt"))
{
string[] components = line.Split(' ');
map.Add(new KeyValuePair<string, string>(components[0], components[1]));
}
using (var output = new StreamWriter(@"d:\git\out.patch"))
{
string[] lines = File.ReadAllLines(@"d:\git\in.patch");
int index = 0;
while (index < lines.Length)
{
if (index != 0)
output.WriteLine();
string line = lines[index];
if (line.StartsWith("diff --git "))
{
while (!lines[++index].StartsWith("---")) ;
string fileName = lines[index].Substring(6);
if (fileName == "ev/null")
{
fileName = lines[index + 1].Substring(6);
}
string replacement = null;
foreach (var m in map)
{
if (fileName.StartsWith(m.Key))
{
replacement = m.Value + fileName.Substring(m.Key.Length);
break;
}
}
if (replacement != null)
{
Console.WriteLine($"Porting file {fileName} to {replacement}");
output.WriteLine(lines[index - 2]); // diff --git
output.WriteLine(lines[index - 1]); // index
output.WriteLine("--- a/" + replacement);
index++;
if (lines[index] == "+++ /dev/null")
output.Write("+++ /dev/null");
else if (lines[index].Substring(6) != fileName)
throw new Exception("A and B mismatch");
else
output.Write("+++ b/" + replacement);
index++;
}
else
{
Console.WriteLine($"Skipping file {fileName}");
while (index < lines.Length && !lines[index].StartsWith("diff --git "))
index++;
}
continue;
}
output.Write(lines[index]);
index++;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment