MergeManagerBase.cs 4.6 KB

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