Beispiel #1
0
        private static void ApplyChanges(string masterBranch, string fromVersion, string toVersion)
        {
            string gitRootDir = GitOperations.GetRoot();

            Debug.Assert(Directory.Exists(gitRootDir));

            string currentDir = Directory.GetCurrentDirectory();

            Debug.Assert(IOHelper.IsSubdirectory(gitRootDir, currentDir));

            string[] directoriesWithDifferences = GitOperations.GetDifferentFiles(masterBranch)
                                                  .Where(path => path.ToLower().Contains("/" + fromVersion.ToLower() + "/"))
                                                  .Select(path => path.Substring(0, path.ToLower().IndexOf(fromVersion.ToLower())))
                                                  .Distinct()
                                                  .ToArray();
            Dictionary <string, string> patchPaths = new Dictionary <string, string>();

            OldLogger.Start("Generating Patches", OldLogger.LevelValue.Normal);
            foreach (string directoryWithDifference in directoriesWithDifferences)
            {
                string patchPath = Path.Combine(gitRootDir, fromVersion + "." + directoryWithDifference.Replace('\\', '-').Replace('/', '-').Trim(new char[] { '-' }) + ".patch");

                // git format-patch --relative origin/master . --stdout
                ProcessHelper proc = new ProcessHelper(@"C:\Program Files\Git\cmd\git.exe", $"format-patch --relative {masterBranch} . --stdout");
                proc.WorkingDirectory = Path.Combine(gitRootDir, directoryWithDifference, fromVersion);
                proc.Go();
                File.WriteAllText(patchPath, string.Join("\n", proc.RawOutput));
                patchPaths.Add(directoryWithDifference, patchPath);
            }
            OldLogger.Stop("Generating Patches", OldLogger.LevelValue.Normal);


            for (int ii = 0; ii < directoriesWithDifferences.Length; ii++)
            {
                string directoryWithDifference = directoriesWithDifferences[ii];
                string destinationDirectory    = Path.Combine(gitRootDir, directoryWithDifference, toVersion).Replace('\\', '/');
                string patchPath = patchPaths[directoryWithDifference];

                OldLogger.Start("Applying changes to " + destinationDirectory, OldLogger.LevelValue.Normal);
                // git apply --reject --directory=<destination-dir> --unsafe-paths <patch-path>
                ProcessHelper proc = new ProcessHelper("git.exe", $"apply --reject --directory={destinationDirectory} --unsafe-paths {patchPath}");
                proc.WorkingDirectory = gitRootDir;
                proc.Go();
                foreach (string line in proc.RawOutput)
                {
                    Console.WriteLine(line);
                }
                OldLogger.Stop("Applying changes to " + destinationDirectory, OldLogger.LevelValue.Normal);

                if (proc.RawOutput.Length > 0 && ii < directoriesWithDifferences.Length - 1)
                {
                    Console.WriteLine("Press any key to continue");
                    Console.ReadKey();
                }
            }
        }