VCS.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 
  2. namespace GitMerge
  3. {
  4. using UnityEngine;
  5. using UnityEditor;
  6. using System.Diagnostics;
  7. using System.ComponentModel;
  8. using System.IO;
  9. /// <summary>
  10. /// This abstract class represents a vcs interface.
  11. /// It manages saving and retrieving the exe path from/to the EditorPrefs
  12. /// and offers a small set of actions using the vcs.
  13. /// </summary>
  14. public abstract class VCS
  15. {
  16. protected abstract string GetDefaultPath();
  17. protected abstract string EditorPrefsKey();
  18. public abstract void CheckoutOurs(string path);
  19. public abstract void CheckoutTheirs(string path);
  20. public abstract void MarkAsMerged(string path);
  21. public string GetExePath()
  22. {
  23. if (EditorPrefs.HasKey(EditorPrefsKey()))
  24. {
  25. return EditorPrefs.GetString(EditorPrefsKey());
  26. }
  27. return GetDefaultPath();
  28. }
  29. public void SetPath(string path)
  30. {
  31. EditorPrefs.SetString(EditorPrefsKey(), path);
  32. }
  33. /// <summary>
  34. /// Executes the VCS as a subprocess.
  35. /// </summary>
  36. /// <param name="args">The parameters passed. Like "status" for "git status".</param>
  37. /// <returns>Whatever the call returns.</returns>
  38. protected string Execute(string args, string workingDirectoryPath)
  39. {
  40. var process = new Process();
  41. var startInfo = new ProcessStartInfo();
  42. startInfo.WindowStyle = ProcessWindowStyle.Hidden;
  43. startInfo.FileName = GetExePath();
  44. startInfo.Arguments = args;
  45. startInfo.UseShellExecute = false;
  46. startInfo.RedirectStandardOutput = true;
  47. startInfo.WorkingDirectory = workingDirectoryPath;
  48. process.StartInfo = startInfo;
  49. try
  50. {
  51. process.Start();
  52. }
  53. catch (Win32Exception)
  54. {
  55. throw new VCSException();
  56. }
  57. string output = process.StandardOutput.ReadToEnd();
  58. process.WaitForExit();
  59. return output;
  60. }
  61. private static string GetAboluteFolderPath(string relativeFilePath)
  62. {
  63. var projectPath = Application.dataPath;
  64. projectPath = Directory.GetParent(projectPath).FullName;
  65. var fullPath = Path.Combine(projectPath, relativeFilePath);
  66. return Path.GetDirectoryName(fullPath);
  67. }
  68. protected static void GetAbsoluteFolderPathAndFilename(string relativeFilePath, out string absoluteFolderPath, out string filename)
  69. {
  70. absoluteFolderPath = GetAboluteFolderPath(relativeFilePath);
  71. filename = Path.GetFileName(relativeFilePath);
  72. }
  73. }
  74. }