/GitCommands/Repository/Repositories.cs

https://github.com/fraga/gitextensions · C# · 270 lines · 246 code · 23 blank · 1 comment · 47 complexity · d0377ca39da5f6e70e7311114a3a0ed4 MD5 · raw file

  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using System.Xml;
  8. using System.Xml.Serialization;
  9. namespace GitCommands.Repository
  10. {
  11. public static class Repositories
  12. {
  13. private static Task<RepositoryHistory> _repositoryHistory;
  14. private static RepositoryHistory _remoteRepositoryHistory;
  15. private static BindingList<RepositoryCategory> _repositoryCategories;
  16. private const int DefaultRepositoriesCount = 30;
  17. public static Task<RepositoryHistory> LoadRepositoryHistoryAsync()
  18. {
  19. if (_repositoryHistory != null)
  20. return _repositoryHistory;
  21. _repositoryHistory = Task.Factory.StartNew(() => LoadRepositoryHistory());
  22. return _repositoryHistory;
  23. }
  24. private static RepositoryHistory LoadRepositoryHistory()
  25. {
  26. int size = AppSettings.GetInt("history size", DefaultRepositoriesCount);
  27. object setting = AppSettings.GetString("history", null);
  28. if (setting == null)
  29. {
  30. return new RepositoryHistory(size);
  31. }
  32. RepositoryHistory repositoryHistory = DeserializeHistoryFromXml(setting.ToString());
  33. if (repositoryHistory == null)
  34. return new RepositoryHistory(size);
  35. repositoryHistory.MaxCount = size;
  36. AssignRepositoryHistoryFromCategories(repositoryHistory, null);
  37. // migration from old version (move URL history to _remoteRepositoryHistory)
  38. if (AppSettings.GetString("history remote", null) == null)
  39. {
  40. _remoteRepositoryHistory = new RepositoryHistory(size);
  41. foreach (Repository repo in repositoryHistory.Repositories)
  42. {
  43. if (repo.IsRemote)
  44. {
  45. repo.Path = repo.Path.Replace('\\', '/');
  46. _remoteRepositoryHistory.AddRepository(repo);
  47. }
  48. }
  49. foreach (Repository repo in _remoteRepositoryHistory.Repositories)
  50. {
  51. repositoryHistory.RemoveRepository(repo);
  52. }
  53. }
  54. return repositoryHistory;
  55. }
  56. public static RepositoryHistory RepositoryHistory
  57. {
  58. get
  59. {
  60. if (_repositoryHistory == null)
  61. LoadRepositoryHistoryAsync();
  62. return _repositoryHistory.Result;
  63. }
  64. }
  65. public static RepositoryHistory RemoteRepositoryHistory
  66. {
  67. get
  68. {
  69. if (_repositoryHistory != null && _repositoryHistory.Status == TaskStatus.Running)
  70. _repositoryHistory.Wait();
  71. if (_remoteRepositoryHistory == null)
  72. {
  73. object setting = AppSettings.GetString("history remote", null);
  74. if (setting != null)
  75. {
  76. _remoteRepositoryHistory = DeserializeHistoryFromXml(setting.ToString());
  77. _remoteRepositoryHistory.MaxCount = DefaultRepositoriesCount;
  78. }
  79. }
  80. return _remoteRepositoryHistory ?? (_remoteRepositoryHistory = new RepositoryHistory(DefaultRepositoriesCount));
  81. }
  82. }
  83. private static void AssignRepositoryHistoryFromCategories(RepositoryHistory repositoryHistory, string path)
  84. {
  85. foreach (Repository repo in repositoryHistory.Repositories)
  86. {
  87. if (path == null || path.Equals(repo.Path, StringComparison.CurrentCultureIgnoreCase))
  88. {
  89. Repository catRepo = FindFirstCategoryRepository(repo.Path);
  90. if (catRepo != null)
  91. repo.Assign(catRepo);
  92. }
  93. }
  94. }
  95. private static void AssignRepositoryHistoryFromCategories(string path)
  96. {
  97. AssignRepositoryHistoryFromCategories(RepositoryHistory, path);
  98. }
  99. private static Repository FindFirstCategoryRepository(string path)
  100. {
  101. foreach (Repository repo in RepositoryCategories.SelectMany(category => category.Repositories))
  102. {
  103. if (repo.Path != null && repo.Path.Equals(path, StringComparison.CurrentCultureIgnoreCase))
  104. return repo;
  105. }
  106. return null;
  107. }
  108. public static BindingList<RepositoryCategory> RepositoryCategories
  109. {
  110. get
  111. {
  112. if (_repositoryCategories == null)
  113. {
  114. object setting = AppSettings.GetString("repositories", null);
  115. if (setting != null)
  116. {
  117. _repositoryCategories = DeserializeRepositories(setting.ToString());
  118. }
  119. }
  120. return _repositoryCategories ?? (_repositoryCategories = new BindingList<RepositoryCategory>());
  121. }
  122. }
  123. private static string SerializeRepositories(BindingList<RepositoryCategory> categories)
  124. {
  125. try
  126. {
  127. var sw = new StringWriter();
  128. var serializer = new XmlSerializer(typeof(BindingList<RepositoryCategory>));
  129. serializer.Serialize(sw, categories);
  130. return sw.ToString();
  131. }
  132. catch
  133. {
  134. return null;
  135. }
  136. }
  137. private static BindingList<RepositoryCategory> DeserializeRepositories(string xml)
  138. {
  139. BindingList<RepositoryCategory> repositories = null;
  140. try
  141. {
  142. var serializer = new XmlSerializer(typeof(BindingList<RepositoryCategory>));
  143. StringReader stringReader = null;
  144. try
  145. {
  146. stringReader = new StringReader(xml);
  147. using (var xmlReader = new XmlTextReader(stringReader))
  148. {
  149. stringReader = null;
  150. repositories = serializer.Deserialize(xmlReader) as BindingList<RepositoryCategory>;
  151. if (repositories != null)
  152. {
  153. foreach (var repositoryCategory in repositories)
  154. {
  155. repositoryCategory.SetIcon();
  156. }
  157. }
  158. }
  159. }
  160. finally
  161. {
  162. if (stringReader != null)
  163. stringReader.Dispose();
  164. }
  165. }
  166. catch (Exception ex)
  167. {
  168. Trace.WriteLine(ex.Message);
  169. }
  170. return repositories;
  171. }
  172. private static string SerializeHistoryIntoXml(RepositoryHistory history)
  173. {
  174. try
  175. {
  176. var sw = new StringWriter();
  177. var serializer = new XmlSerializer(typeof(RepositoryHistory));
  178. serializer.Serialize(sw, history);
  179. return sw.ToString();
  180. }
  181. catch
  182. {
  183. return null;
  184. }
  185. }
  186. private static RepositoryHistory DeserializeHistoryFromXml(string xml)
  187. {
  188. if (string.IsNullOrEmpty(xml))
  189. return null;
  190. RepositoryHistory history = null;
  191. try
  192. {
  193. var serializer = new XmlSerializer(typeof(RepositoryHistory));
  194. StringReader stringReader = null;
  195. try
  196. {
  197. stringReader = new StringReader(xml);
  198. using (var xmlReader = new XmlTextReader(stringReader))
  199. {
  200. stringReader = null;
  201. var obj = serializer.Deserialize(xmlReader) as RepositoryHistory;
  202. if (obj != null)
  203. {
  204. history = obj;
  205. history.SetIcon();
  206. }
  207. }
  208. }
  209. finally
  210. {
  211. if (stringReader != null)
  212. stringReader.Dispose();
  213. }
  214. }
  215. catch (Exception ex)
  216. {
  217. Trace.WriteLine(ex.Message);
  218. }
  219. return history;
  220. }
  221. public static void SaveSettings()
  222. {
  223. if (_repositoryHistory != null)
  224. AppSettings.SetString("history", SerializeHistoryIntoXml(_repositoryHistory.Result));
  225. if (_remoteRepositoryHistory != null)
  226. AppSettings.SetString("history remote", SerializeHistoryIntoXml(_remoteRepositoryHistory));
  227. if (_repositoryCategories != null)
  228. AppSettings.SetString("repositories", SerializeRepositories(_repositoryCategories));
  229. }
  230. public static void AddCategory(string title)
  231. {
  232. RepositoryCategories.Add(new RepositoryCategory { Description = title });
  233. }
  234. public static void AddMostRecentRepository(string repo)
  235. {
  236. if (Repository.PathIsUrl(repo))
  237. {
  238. RemoteRepositoryHistory.AddMostRecentRepository(repo);
  239. }
  240. else
  241. {
  242. RepositoryHistory.AddMostRecentRepository(repo);
  243. AssignRepositoryHistoryFromCategories(repo);
  244. }
  245. }
  246. }
  247. }