PageRenderTime 24ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/GitUI/CommandsDialogs/SettingsDialog/CheckSettingsLogic.cs

https://github.com/fraga/gitextensions
C# | 295 lines | 271 code | 23 blank | 1 comment | 35 complexity | 834d81eb6ce2176fd9a37c89f534010b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using GitCommands;
  6. using GitCommands.Settings;
  7. using GitCommands.Utils;
  8. using Microsoft.Win32;
  9. namespace GitUI.CommandsDialogs.SettingsDialog
  10. {
  11. public class CheckSettingsLogic
  12. {
  13. public readonly CommonLogic CommonLogic;
  14. private GitModule Module { get { return CommonLogic.Module; } }
  15. private ConfigFileSettings GlobalConfigFileSettings { get { return CommonLogic.ConfigFileSettingsSet.GlobalSettings; } }
  16. public CheckSettingsLogic(CommonLogic commonLogic)
  17. {
  18. CommonLogic = commonLogic;
  19. }
  20. public bool AutoSolveAllSettings()
  21. {
  22. if (!EnvUtils.RunningOnWindows())
  23. return SolveGitCommand();
  24. bool valid = SolveGitCommand();
  25. valid = SolveLinuxToolsDir() && valid;
  26. valid = SolveMergeToolForKDiff() && valid;
  27. valid = SolveDiffToolForKDiff() && valid;
  28. valid = SolveGitExtensionsDir() && valid;
  29. valid = SolveEditor() && valid;
  30. CommonLogic.ConfigFileSettingsSet.EffectiveSettings.Save();
  31. CommonLogic.RepoDistSettingsSet.EffectiveSettings.Save();
  32. return valid;
  33. }
  34. private bool SolveEditor()
  35. {
  36. string editor = CommonLogic.GetGlobalEditor();
  37. if (string.IsNullOrEmpty(editor))
  38. {
  39. GlobalConfigFileSettings.SetPathValue("core.editor", "\"" + AppSettings.GetGitExtensionsFullPath() + "\" fileeditor");
  40. }
  41. return true;
  42. }
  43. public bool SolveLinuxToolsDir(string possibleNewPath = null)
  44. {
  45. if (!EnvUtils.RunningOnWindows())
  46. {
  47. AppSettings.GitBinDir = "";
  48. return true;
  49. }
  50. string gitpath = AppSettings.GitCommandValue;
  51. if (!String.IsNullOrWhiteSpace(possibleNewPath))
  52. {
  53. gitpath = possibleNewPath.Trim();
  54. }
  55. foreach (var toolsPath in new[] { @"usr\bin\", @"bin\" })
  56. {
  57. gitpath = gitpath.Replace(@"\cmd\git.exe", @"\" + toolsPath)
  58. .Replace(@"\cmd\git.cmd", @"\" + toolsPath)
  59. .Replace(@"\bin\git.exe", @"\" + toolsPath);
  60. if (Directory.Exists(gitpath))
  61. {
  62. if (File.Exists(gitpath + "sh.exe") || File.Exists(gitpath + "sh"))
  63. {
  64. AppSettings.GitBinDir = gitpath;
  65. return true;
  66. }
  67. }
  68. if (CheckIfFileIsInPath("sh.exe") || CheckIfFileIsInPath("sh"))
  69. {
  70. AppSettings.GitBinDir = "";
  71. return true;
  72. }
  73. foreach (var path in GetGitLocations())
  74. {
  75. if (Directory.Exists(path + toolsPath))
  76. {
  77. if (File.Exists(path + toolsPath + "sh.exe") || File.Exists(path + toolsPath + "sh"))
  78. {
  79. AppSettings.GitBinDir = path + toolsPath;
  80. return true;
  81. }
  82. }
  83. }
  84. }
  85. return false;
  86. }
  87. private IEnumerable<string> GetGitLocations()
  88. {
  89. string envVariable = Environment.GetEnvironmentVariable("GITEXT_GIT");
  90. if (!String.IsNullOrEmpty(envVariable)) yield return envVariable;
  91. yield return
  92. CommonLogic.GetRegistryValue(Registry.LocalMachine,
  93. "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1", "InstallLocation");
  94. string programFiles = Environment.GetEnvironmentVariable("ProgramFiles");
  95. string programFilesX86 = null;
  96. if (8 == IntPtr.Size
  97. || !String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432")))
  98. programFilesX86 = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
  99. if (programFilesX86 != null)
  100. yield return programFilesX86 + @"\Git\";
  101. yield return programFiles + @"\Git\";
  102. if (programFilesX86 != null)
  103. yield return programFilesX86 + @"\msysgit\";
  104. yield return programFiles + @"\msysgit\";
  105. yield return @"C:\msysgit\";
  106. // cygwin has old git version on windows and bash has a lot of bugs
  107. yield return @"C:\cygwin\";
  108. }
  109. private IEnumerable<string> GetWindowsCommandLocations(string possibleNewPath = null)
  110. {
  111. if (!string.IsNullOrEmpty(possibleNewPath) && File.Exists(possibleNewPath))
  112. yield return possibleNewPath;
  113. if (!string.IsNullOrEmpty(AppSettings.GitCommandValue) && File.Exists(AppSettings.GitCommandValue))
  114. yield return AppSettings.GitCommandValue;
  115. foreach (var path in GetGitLocations())
  116. {
  117. if (Directory.Exists(path + @"bin\"))
  118. yield return path + @"bin\git.exe";
  119. }
  120. foreach (var path in GetGitLocations())
  121. {
  122. if (Directory.Exists(path + @"cmd\"))
  123. {
  124. yield return path + @"cmd\git.exe";
  125. yield return path + @"cmd\git.cmd";
  126. }
  127. }
  128. yield return "git";
  129. yield return "git.cmd";
  130. }
  131. public bool SolveGitExtensionsDir()
  132. {
  133. string fileName = AppSettings.GetGitExtensionsDirectory();
  134. if (Directory.Exists(fileName))
  135. {
  136. AppSettings.SetInstallDir(fileName);
  137. return true;
  138. }
  139. return false;
  140. }
  141. public bool SolveGitCommand(string possibleNewPath = null)
  142. {
  143. if (EnvUtils.RunningOnWindows())
  144. {
  145. var command = (from cmd in GetWindowsCommandLocations(possibleNewPath)
  146. let output = Module.RunCmd(cmd, string.Empty)
  147. where !string.IsNullOrEmpty(output)
  148. select cmd).FirstOrDefault();
  149. if (command != null)
  150. {
  151. AppSettings.GitCommandValue = command;
  152. return true;
  153. }
  154. return false;
  155. }
  156. AppSettings.GitCommandValue = "git";
  157. return !string.IsNullOrEmpty(Module.RunGitCmd(""));
  158. }
  159. public static bool CheckIfFileIsInPath(string fileName)
  160. {
  161. string path = Environment.GetEnvironmentVariable("PATH");
  162. return path.Split(';').Any(dir => File.Exists(dir + " \\" + fileName) || File.Exists(Path.Combine(dir, fileName)));
  163. }
  164. public bool SolveMergeToolForKDiff()
  165. {
  166. string mergeTool = CommonLogic.GetGlobalMergeTool();
  167. if (string.IsNullOrEmpty(mergeTool))
  168. {
  169. mergeTool = "kdiff3";
  170. GlobalConfigFileSettings.SetValue("merge.tool", mergeTool);
  171. }
  172. if (mergeTool.Equals("kdiff3", StringComparison.CurrentCultureIgnoreCase))
  173. return SolveMergeToolPathForKDiff();
  174. return true;
  175. }
  176. public bool SolveDiffToolForKDiff()
  177. {
  178. string diffTool = GetDiffToolFromConfig(GlobalConfigFileSettings);
  179. if (string.IsNullOrEmpty(diffTool))
  180. {
  181. diffTool = "kdiff3";
  182. SetDiffToolToConfig(GlobalConfigFileSettings, diffTool);
  183. }
  184. if (diffTool.Equals("kdiff3", StringComparison.CurrentCultureIgnoreCase))
  185. return SolveDiffToolPathForKDiff();
  186. return true;
  187. }
  188. public static string GetDiffToolFromConfig(ConfigFileSettings settings)
  189. {
  190. return settings.GetValue("diff.guitool");
  191. }
  192. public static void SetDiffToolToConfig(ConfigFileSettings settings, string diffTool)
  193. {
  194. settings.SetValue("diff.guitool", diffTool);
  195. }
  196. public bool SolveDiffToolPathForKDiff()
  197. {
  198. string kdiff3path = MergeToolsHelper.FindPathForKDiff(GlobalConfigFileSettings.GetValue("difftool.kdiff3.path"));
  199. if (string.IsNullOrEmpty(kdiff3path))
  200. return false;
  201. GlobalConfigFileSettings.SetPathValue("difftool.kdiff3.path", kdiff3path);
  202. return true;
  203. }
  204. public bool SolveMergeToolPathForKDiff()
  205. {
  206. string kdiff3path = MergeToolsHelper.FindPathForKDiff(GlobalConfigFileSettings.GetValue("mergetool.kdiff3.path"));
  207. if (string.IsNullOrEmpty(kdiff3path))
  208. return false;
  209. GlobalConfigFileSettings.SetPathValue("mergetool.kdiff3.path", kdiff3path);
  210. return true;
  211. }
  212. public bool CanFindGitCmd()
  213. {
  214. return !string.IsNullOrEmpty(Module.RunGitCmd(""));
  215. }
  216. public void AutoConfigMergeToolCmd()
  217. {
  218. string exeName;
  219. string exeFile = MergeToolsHelper.FindMergeToolFullPath(CommonLogic.ConfigFileSettingsSet, GetGlobalMergeToolText(), out exeName);
  220. if (String.IsNullOrEmpty(exeFile))
  221. {
  222. SetMergetoolPathText("");
  223. SetMergeToolCmdText("");
  224. }
  225. SetMergetoolPathText(exeFile);
  226. SetMergeToolCmdText(MergeToolsHelper.AutoConfigMergeToolCmd(GetGlobalMergeToolText(), exeFile));
  227. }
  228. private void SetMergetoolPathText(string text)
  229. {
  230. GlobalConfigFileSettings.SetPathValue(string.Format("mergetool.{0}.path", GetGlobalMergeToolText()), text);
  231. // orig (TODO: remove comment and rename method):
  232. //// MergetoolPath.Text = ...
  233. }
  234. private void SetMergeToolCmdText(string text)
  235. {
  236. GlobalConfigFileSettings.SetPathValue(string.Format("mergetool.{0}.cmd", GetGlobalMergeToolText()), text);
  237. // orig (TODO: remove comment and rename method):
  238. //// MergeToolCmd.Text = ...
  239. }
  240. private string GetGlobalMergeToolText()
  241. {
  242. return GlobalConfigFileSettings.GetValue("merge.tool");
  243. // orig (TODO: remove comment and rename method):
  244. //// GlobalMergeTool.Text;
  245. }
  246. public string GetMergeToolCmdText()
  247. {
  248. return GlobalConfigFileSettings.GetValue(string.Format("mergetool.{0}.cmd", GetGlobalMergeToolText()));
  249. // orig (TODO: remove comment and rename method):
  250. //// MergeToolCmd.Text
  251. }
  252. }
  253. }