MergeManagerBase.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 
  2. namespace GitMerge
  3. {
  4. using UnityEngine;
  5. using System.IO;
  6. using System.Collections.Generic;
  7. public abstract class MergeManagerBase
  8. {
  9. internal const string OUR_FILE_POSTFIX = "--OURS";
  10. internal const string THEIR_FILE_POSTFIX = "--THEIRS";
  11. protected VCS vcs { private set; get; }
  12. protected GitMergeWindow window { private set; get; }
  13. internal List<GameObjectMergeActions> allMergeActions;
  14. protected static string fileName;
  15. protected static string theirFilename;
  16. public static bool isMergingScene { protected set; get; }
  17. public static bool isMergingPrefab { get { return !isMergingScene; } }
  18. public MergeManagerBase(GitMergeWindow window, VCS vcs)
  19. {
  20. this.window = window;
  21. this.vcs = vcs;
  22. allMergeActions = new List<GameObjectMergeActions>();
  23. }
  24. /// <summary>
  25. /// Creates "their" version of the file at the given path,
  26. /// named filename--THEIRS.unity.
  27. /// </summary>
  28. /// <param name="path">The path of the file, relative to the project folder.</param>
  29. protected void CheckoutTheirVersionOf(string path)
  30. {
  31. fileName = path;
  32. string basepath = Path.GetDirectoryName(path);
  33. string sceneName = Path.GetFileNameWithoutExtension(path);
  34. string extension = Path.GetExtension(path);
  35. string ourFilename = Path.Combine(basepath, sceneName + OUR_FILE_POSTFIX + extension);
  36. theirFilename = Path.Combine(basepath, sceneName + THEIR_FILE_POSTFIX + extension);
  37. File.Copy(path, ourFilename);
  38. try
  39. {
  40. vcs.CheckoutTheirs(path);
  41. }
  42. catch (VCSException e)
  43. {
  44. File.Delete(ourFilename);
  45. throw e;
  46. }
  47. File.Move(path, theirFilename);
  48. File.Move(ourFilename, path);
  49. }
  50. /// <summary>
  51. /// Finds all specific merge conflicts between two sets of GameObjects,
  52. /// representing "our" scene and "their" scene.
  53. /// </summary>
  54. /// <param name="ourObjects">The GameObjects of "our" version of the scene.</param>
  55. /// <param name="theirObjects">The GameObjects of "their" version of the scene.</param>
  56. protected void BuildAllMergeActions(List<GameObject> ourObjects, List<GameObject> theirObjects)
  57. {
  58. allMergeActions = new List<GameObjectMergeActions>();
  59. // Map "their" GameObjects to their respective ids
  60. var theirObjectsDict = new Dictionary<ObjectID, GameObject>();
  61. foreach (var theirs in theirObjects)
  62. {
  63. theirObjectsDict.Add(ObjectID.GetFor(theirs), theirs);
  64. }
  65. foreach (var ours in ourObjects)
  66. {
  67. // Try to find "their" equivalent to "our" GameObjects
  68. var id = ObjectID.GetFor(ours);
  69. GameObject theirs;
  70. theirObjectsDict.TryGetValue(id, out theirs);
  71. // If theirs is null, mergeActions.hasActions will be false
  72. var mergeActions = new GameObjectMergeActions(ours, theirs);
  73. if (mergeActions.hasActions)
  74. {
  75. allMergeActions.Add(mergeActions);
  76. }
  77. // Remove "their" GameObject from the dict to only keep those new to us
  78. theirObjectsDict.Remove(id);
  79. }
  80. // Every GameObject left in the dict is a...
  81. foreach (var theirs in theirObjectsDict.Values)
  82. {
  83. // ...new GameObject from them
  84. var mergeActions = new GameObjectMergeActions(null, theirs);
  85. if (mergeActions.hasActions)
  86. {
  87. allMergeActions.Add(mergeActions);
  88. }
  89. }
  90. }
  91. public abstract void CompleteMerge();
  92. public virtual void AbortMerge(bool showNotification = true)
  93. {
  94. MergeAction.inMergePhase = false;
  95. foreach (var actions in allMergeActions)
  96. {
  97. actions.UseOurs();
  98. }
  99. ObjectDictionaries.DestroyTheirObjects();
  100. ObjectDictionaries.Clear();
  101. allMergeActions = null;
  102. if (showNotification)
  103. {
  104. window.ShowNotification(new GUIContent("Merge aborted."));
  105. }
  106. }
  107. }
  108. }