PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/GitCommands/Repository/Repositories.cs

https://github.com/vbjay/gitextensions
C# | 276 lines | 253 code | 22 blank | 1 comment | 47 complexity | 967a17c18abfb600f6cd95a1a443c248 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0
  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. public static Task<RepositoryHistory> LoadRepositoryHistoryAsync()
  17. {
  18. if (_repositoryHistory != null)
  19. return _repositoryHistory;
  20. _repositoryHistory = Task.Factory.StartNew(() => LoadRepositoryHistory());
  21. return _repositoryHistory;
  22. }
  23. private static RepositoryHistory LoadRepositoryHistory()
  24. {
  25. var repositoryHistory = new RepositoryHistory();
  26. object setting = AppSettings.GetString("history", null);
  27. if (setting == null)
  28. {
  29. repositoryHistory = new RepositoryHistory();
  30. return repositoryHistory;
  31. }
  32. repositoryHistory = DeserializeHistoryFromXml(setting.ToString());
  33. if (repositoryHistory != null)
  34. {
  35. AssignRepositoryHistoryFromCategories(repositoryHistory, null);
  36. // migration from old version (move URL history to _remoteRepositoryHistory)
  37. if (AppSettings.GetString("history remote", null) == null)
  38. {
  39. _remoteRepositoryHistory = new RepositoryHistory();
  40. foreach (Repository repo in repositoryHistory.Repositories)
  41. {
  42. if (repo.IsRemote)
  43. {
  44. repo.Path = repo.Path.Replace('\\', '/');
  45. _remoteRepositoryHistory.AddRepository(repo);
  46. }
  47. }
  48. foreach (Repository repo in _remoteRepositoryHistory.Repositories)
  49. {
  50. repositoryHistory.RemoveRepository(repo);
  51. }
  52. }
  53. }
  54. return repositoryHistory ?? new 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. }
  78. }
  79. return _remoteRepositoryHistory ?? (_remoteRepositoryHistory = new RepositoryHistory());
  80. }
  81. private set
  82. {
  83. _remoteRepositoryHistory = value;
  84. }
  85. }
  86. private static void AssignRepositoryHistoryFromCategories(RepositoryHistory repositoryHistory, string path)
  87. {
  88. foreach (Repository repo in repositoryHistory.Repositories)
  89. {
  90. if (path == null || path.Equals(repo.Path, StringComparison.CurrentCultureIgnoreCase))
  91. {
  92. Repository catRepo = FindFirstCategoryRepository(repo.Path);
  93. if (catRepo != null)
  94. repo.Assign(catRepo);
  95. }
  96. }
  97. }
  98. private static void AssignRepositoryHistoryFromCategories(string path)
  99. {
  100. AssignRepositoryHistoryFromCategories(RepositoryHistory, path);
  101. }
  102. private static Repository FindFirstCategoryRepository(string path)
  103. {
  104. foreach (Repository repo in RepositoryCategories.SelectMany(category => category.Repositories))
  105. {
  106. if (repo.Path != null && repo.Path.Equals(path, StringComparison.CurrentCultureIgnoreCase))
  107. return repo;
  108. }
  109. return null;
  110. }
  111. public static BindingList<RepositoryCategory> RepositoryCategories
  112. {
  113. get
  114. {
  115. if (_repositoryCategories == null)
  116. {
  117. object setting = AppSettings.GetString("repositories", null);
  118. if (setting != null)
  119. {
  120. _repositoryCategories = DeserializeRepositories(setting.ToString());
  121. }
  122. }
  123. return _repositoryCategories ?? (_repositoryCategories = new BindingList<RepositoryCategory>());
  124. }
  125. private set
  126. {
  127. _repositoryCategories = value;
  128. }
  129. }
  130. private static string SerializeRepositories(BindingList<RepositoryCategory> categories)
  131. {
  132. try
  133. {
  134. var sw = new StringWriter();
  135. var serializer = new XmlSerializer(typeof(BindingList<RepositoryCategory>));
  136. serializer.Serialize(sw, categories);
  137. return sw.ToString();
  138. }
  139. catch
  140. {
  141. return null;
  142. }
  143. }
  144. private static BindingList<RepositoryCategory> DeserializeRepositories(string xml)
  145. {
  146. BindingList<RepositoryCategory> repositories = null;
  147. try
  148. {
  149. var serializer = new XmlSerializer(typeof(BindingList<RepositoryCategory>));
  150. StringReader stringReader = null;
  151. try
  152. {
  153. stringReader = new StringReader(xml);
  154. using (var xmlReader = new XmlTextReader(stringReader))
  155. {
  156. stringReader = null;
  157. repositories = serializer.Deserialize(xmlReader) as BindingList<RepositoryCategory>;
  158. if (repositories != null)
  159. {
  160. foreach (var repositoryCategory in repositories)
  161. {
  162. repositoryCategory.SetIcon();
  163. }
  164. }
  165. }
  166. }
  167. finally
  168. {
  169. if (stringReader != null)
  170. stringReader.Dispose();
  171. }
  172. }
  173. catch (Exception ex)
  174. {
  175. Trace.WriteLine(ex.Message);
  176. }
  177. return repositories;
  178. }
  179. private static string SerializeHistoryIntoXml(RepositoryHistory history)
  180. {
  181. try
  182. {
  183. var sw = new StringWriter();
  184. var serializer = new XmlSerializer(typeof(RepositoryHistory));
  185. serializer.Serialize(sw, history);
  186. return sw.ToString();
  187. }
  188. catch
  189. {
  190. return null;
  191. }
  192. }
  193. private static RepositoryHistory DeserializeHistoryFromXml(string xml)
  194. {
  195. if (string.IsNullOrEmpty(xml))
  196. return null;
  197. RepositoryHistory history = null;
  198. try
  199. {
  200. var serializer = new XmlSerializer(typeof(RepositoryHistory));
  201. StringReader stringReader = null;
  202. try
  203. {
  204. stringReader = new StringReader(xml);
  205. using (var xmlReader = new XmlTextReader(stringReader))
  206. {
  207. stringReader = null;
  208. var obj = serializer.Deserialize(xmlReader) as RepositoryHistory;
  209. if (obj != null)
  210. {
  211. history = obj;
  212. history.SetIcon();
  213. }
  214. }
  215. }
  216. finally
  217. {
  218. if (stringReader != null)
  219. stringReader.Dispose();
  220. }
  221. }
  222. catch (Exception ex)
  223. {
  224. Trace.WriteLine(ex.Message);
  225. }
  226. return history;
  227. }
  228. public static void SaveSettings()
  229. {
  230. if (_repositoryHistory != null)
  231. AppSettings.SetString("history", Repositories.SerializeHistoryIntoXml(_repositoryHistory.Result));
  232. if (_remoteRepositoryHistory != null)
  233. AppSettings.SetString("history remote", Repositories.SerializeHistoryIntoXml(_remoteRepositoryHistory));
  234. if (_repositoryCategories != null)
  235. AppSettings.SetString("repositories", SerializeRepositories(_repositoryCategories));
  236. }
  237. public static void AddCategory(string title)
  238. {
  239. RepositoryCategories.Add(new RepositoryCategory { Description = title });
  240. }
  241. public static void AddMostRecentRepository(string repo)
  242. {
  243. if (Repository.PathIsUrl(repo))
  244. {
  245. RemoteRepositoryHistory.AddMostRecentRepository(repo);
  246. }
  247. else
  248. {
  249. RepositoryHistory.AddMostRecentRepository(repo);
  250. AssignRepositoryHistoryFromCategories(repo);
  251. }
  252. }
  253. }
  254. }