暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UnityDiffTree.cs 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System.Collections.Generic;
  2. using Codice.Client.Commands;
  3. using Codice.Client.Common;
  4. using Codice.CM.Common;
  5. using Codice.Utils;
  6. using PlasticGui;
  7. using PlasticGui.Diff;
  8. using PlasticGui.WorkspaceWindow.Diff;
  9. namespace Unity.PlasticSCM.Editor.Views.Diff
  10. {
  11. internal class UnityDiffTree
  12. {
  13. internal UnityDiffTree()
  14. {
  15. mInnerTree = new DiffTree();
  16. mMetaCache = new MetaCache();
  17. }
  18. internal void BuildCategories(
  19. WorkspaceInfo wkInfo,
  20. List<ClientDiff> diffs,
  21. BranchResolver brResolver,
  22. bool skipMergeTracking)
  23. {
  24. mInnerTree.BuildCategories(
  25. RevisionInfoCodeReviewAdapter.CalculateCodeReviewEntries(
  26. wkInfo,
  27. diffs,
  28. brResolver,
  29. skipMergeTracking),
  30. brResolver);
  31. mMetaCache.Build(mInnerTree.GetNodes());
  32. }
  33. internal List<IDiffCategory> GetNodes()
  34. {
  35. return mInnerTree.GetNodes();
  36. }
  37. internal bool HasMeta(ClientDiffInfo difference)
  38. {
  39. return mMetaCache.ContainsMeta(difference);
  40. }
  41. internal ClientDiffInfo GetMetaDiff(ClientDiffInfo diff)
  42. {
  43. return mMetaCache.GetExistingMeta(diff);
  44. }
  45. internal void FillWithMeta(List<ClientDiffInfo> diffs)
  46. {
  47. diffs.AddRange(
  48. mMetaCache.GetExistingMeta(diffs));
  49. }
  50. internal void Sort(string key, bool sortAscending)
  51. {
  52. mInnerTree.Sort(key, sortAscending);
  53. }
  54. internal void Filter(Filter filter, List<string> columnNames)
  55. {
  56. mInnerTree.Filter(filter, columnNames);
  57. }
  58. MetaCache mMetaCache = new MetaCache();
  59. DiffTree mInnerTree;
  60. class MetaCache
  61. {
  62. internal void Build(List<IDiffCategory> categories)
  63. {
  64. mCache.Clear();
  65. HashSet<string> indexedKeys = BuildIndexedKeys(
  66. GetClientDiffInfos.FromCategories(categories));
  67. for (int i = 0; i < categories.Count; i++)
  68. {
  69. ExtractToMetaCache(
  70. (ITreeViewNode)categories[i],
  71. i,
  72. mCache,
  73. indexedKeys);
  74. }
  75. }
  76. internal bool ContainsMeta(ClientDiffInfo diff)
  77. {
  78. return mCache.ContainsKey(
  79. BuildKey.ForMetaDiff(diff));
  80. }
  81. internal ClientDiffInfo GetExistingMeta(ClientDiffInfo diff)
  82. {
  83. ClientDiffInfo result;
  84. if (!mCache.TryGetValue(BuildKey.ForMetaDiff(diff), out result))
  85. return null;
  86. return result;
  87. }
  88. internal List<ClientDiffInfo> GetExistingMeta(List<ClientDiffInfo> diffs)
  89. {
  90. List<ClientDiffInfo> result = new List<ClientDiffInfo>();
  91. foreach (ClientDiffInfo diff in diffs)
  92. {
  93. string key = BuildKey.ForMetaDiff(diff);
  94. ClientDiffInfo metaDiff;
  95. if (!mCache.TryGetValue(key, out metaDiff))
  96. continue;
  97. result.Add(metaDiff);
  98. }
  99. return result;
  100. }
  101. static void ExtractToMetaCache(
  102. ITreeViewNode node,
  103. int nodeIndex,
  104. Dictionary<string, ClientDiffInfo> cache,
  105. HashSet<string> indexedKeys)
  106. {
  107. if (node is ClientDiffInfo)
  108. {
  109. ClientDiffInfo diff = (ClientDiffInfo)node;
  110. string path = diff.DiffWithMount.Difference.Path;
  111. if (!MetaPath.IsMetaPath(path))
  112. return;
  113. string realPath = MetaPath.GetPathFromMetaPath(path);
  114. if (!indexedKeys.Contains(BuildKey.BuildCacheKey(
  115. BuildKey.GetCategoryGroup(diff),
  116. BuildKey.GetChangeCategory(diff),
  117. realPath)))
  118. return;
  119. // found foo.c and foo.c.meta
  120. // with the same chage types - move .meta to cache
  121. cache.Add(BuildKey.ForDiff(diff), diff);
  122. ((ChangeCategory)node.GetParent()).RemoveDiffAt(nodeIndex);
  123. }
  124. for (int i = node.GetChildrenCount() - 1; i >= 0; i--)
  125. {
  126. ExtractToMetaCache(
  127. node.GetChild(i),
  128. i,
  129. cache,
  130. indexedKeys);
  131. }
  132. }
  133. HashSet<string> BuildIndexedKeys(List<ClientDiffInfo> diffs)
  134. {
  135. HashSet<string> result = new HashSet<string>();
  136. foreach (ClientDiffInfo diff in diffs)
  137. {
  138. if (MetaPath.IsMetaPath(diff.DiffWithMount.Difference.Path))
  139. continue;
  140. result.Add(BuildKey.ForDiff(diff));
  141. }
  142. return result;
  143. }
  144. Dictionary<string, ClientDiffInfo> mCache =
  145. new Dictionary<string, ClientDiffInfo>();
  146. static class BuildKey
  147. {
  148. internal static string ForDiff(
  149. ClientDiffInfo diff)
  150. {
  151. return BuildCacheKey(
  152. GetCategoryGroup(diff),
  153. GetChangeCategory(diff),
  154. diff.DiffWithMount.Difference.Path);
  155. }
  156. internal static string ForMetaDiff(
  157. ClientDiffInfo diff)
  158. {
  159. return BuildCacheKey(
  160. GetCategoryGroup(diff),
  161. GetChangeCategory(diff),
  162. MetaPath.GetMetaPath(diff.DiffWithMount.Difference.Path));
  163. }
  164. internal static string BuildCacheKey(
  165. CategoryGroup categoryGroup,
  166. ChangeCategory changeCategory,
  167. string path)
  168. {
  169. string result = string.Concat(changeCategory.Type, ":", path);
  170. if (categoryGroup == null)
  171. return result;
  172. return string.Concat(categoryGroup.GetHeaderText(), ":", result);
  173. }
  174. internal static ChangeCategory GetChangeCategory(ClientDiffInfo diff)
  175. {
  176. return (ChangeCategory)diff.GetParent();
  177. }
  178. internal static CategoryGroup GetCategoryGroup(ClientDiffInfo diff)
  179. {
  180. ChangeCategory changeCategory = GetChangeCategory(diff);
  181. ITreeViewNode categoryGroup = changeCategory.GetParent();
  182. if (categoryGroup == null)
  183. return null;
  184. return (CategoryGroup)categoryGroup;
  185. }
  186. }
  187. }
  188. }
  189. }