/GitUI/Script/RunScript.cs

https://github.com/yiketudou/gitextensions · C# · 312 lines · 292 code · 19 blank · 1 comment · 83 complexity · 580dcbcc3ae182095e215e460fa7fb87 MD5 · raw file

  1. using System.Collections.Generic;
  2. using GitCommands;
  3. using GitUI.Script;
  4. using System.Windows.Forms;
  5. namespace GitUI.Script
  6. {
  7. public static class ScriptRunner
  8. {
  9. public static void RunScript(string script, RevisionGrid RevisionGrid)
  10. {
  11. if (string.IsNullOrEmpty(script))
  12. return;
  13. ScriptInfo scriptInfo = ScriptManager.GetScript(script);
  14. string command;
  15. string argument;
  16. if (scriptInfo == null)
  17. {
  18. MessageBox.Show("Cannot find script: " + script, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  19. return;
  20. }
  21. else
  22. {
  23. command = scriptInfo.Command;
  24. argument = scriptInfo.Arguments;
  25. }
  26. if (string.IsNullOrEmpty(command))
  27. return;
  28. //Make sure we are able to run git, even if git is not in the path
  29. if (command.Equals("git", System.StringComparison.CurrentCultureIgnoreCase) ||
  30. command.Equals("{git}", System.StringComparison.CurrentCultureIgnoreCase))
  31. command = Settings.GitCommand;
  32. if (command.Equals("gitextensions", System.StringComparison.CurrentCultureIgnoreCase) ||
  33. command.Equals("{gitextensions}", System.StringComparison.CurrentCultureIgnoreCase) ||
  34. command.Equals("gitex", System.StringComparison.CurrentCultureIgnoreCase) ||
  35. command.Equals("{gitex}", System.StringComparison.CurrentCultureIgnoreCase))
  36. command = Settings.GetGitExtensionsFullPath();
  37. string[] options =
  38. {
  39. "{sTag}",
  40. "{sBranch}",
  41. "{sLocalBranch}",
  42. "{sRemoteBranch}",
  43. "{sRemote}",
  44. "{sHash}",
  45. "{sMessage}",
  46. "{sAuthor}",
  47. "{sCommitter}",
  48. "{sAuthorDate}",
  49. "{sCommitDate}",
  50. "{cTag}",
  51. "{cBranch}",
  52. "{cLocalBranch}",
  53. "{cRemoteBranch}",
  54. "{cHash}",
  55. "{cMessage}",
  56. "{cAuthor}",
  57. "{cCommitter}",
  58. "{cAuthorDate}",
  59. "{cCommitDate}",
  60. "{cDefaultRemote}",
  61. "{UserInput}"
  62. };
  63. GitRevision selectedRevision = null;
  64. GitRevision currentRevision = null;
  65. var selectedLocalBranches = new List<GitHead>();
  66. var selectedRemoteBranches = new List<GitHead>();
  67. var selectedRemotes = new List<string>();
  68. var selectedBranches = new List<GitHead>();
  69. var selectedTags = new List<GitHead>();
  70. var currentLocalBranches = new List<GitHead>();
  71. var currentRemoteBranches = new List<GitHead>();
  72. var currentBranches = new List<GitHead>();
  73. var currentTags = new List<GitHead>();
  74. foreach (string option in options)
  75. {
  76. if (!string.IsNullOrEmpty(argument) && argument.Contains(option))
  77. {
  78. if (option.StartsWith("{s") && selectedRevision == null)
  79. {
  80. if (RevisionGrid == null)
  81. {
  82. MessageBox.Show("Option " + option + " is only supported when started from revision grid.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  83. return;
  84. }
  85. selectedRevision = RevisionGrid.GetRevision(RevisionGrid.LastRow);
  86. foreach (GitHead head in selectedRevision.Heads)
  87. {
  88. if (head.IsTag)
  89. selectedTags.Add(head);
  90. else if (head.IsHead || head.IsRemote)
  91. {
  92. selectedBranches.Add(head);
  93. if (head.IsRemote)
  94. {
  95. selectedRemoteBranches.Add(head);
  96. if (!selectedRemotes.Contains(head.Remote))
  97. selectedRemotes.Add(head.Remote);
  98. }
  99. else
  100. selectedLocalBranches.Add(head);
  101. }
  102. }
  103. }
  104. else if (option.StartsWith("{c") && currentRevision == null)
  105. {
  106. IList<GitHead> heads;
  107. if (RevisionGrid == null)
  108. {
  109. heads = new List<GitHead>();
  110. string currentRevisionGuid = GitCommandHelpers.GetCurrentCheckout();
  111. foreach (GitHead head in GitCommandHelpers.GetHeads(true, true))
  112. {
  113. if (head.Guid == currentRevisionGuid)
  114. heads.Add(head);
  115. }
  116. }
  117. else
  118. {
  119. currentRevision = RevisionGrid.GetCurrentRevision();
  120. heads = currentRevision.Heads;
  121. }
  122. foreach (GitHead head in heads)
  123. {
  124. if (head.IsTag)
  125. currentTags.Add(head);
  126. else if (head.IsHead || head.IsRemote)
  127. {
  128. currentBranches.Add(head);
  129. if (head.IsRemote)
  130. currentRemoteBranches.Add(head);
  131. else
  132. currentLocalBranches.Add(head);
  133. }
  134. }
  135. }
  136. switch (option)
  137. {
  138. case "{sTag}":
  139. if (selectedTags.Count == 1)
  140. argument = argument.Replace(option, selectedTags[0].Name);
  141. else if (selectedTags.Count != 0)
  142. argument = argument.Replace(option, askToSpecify(selectedTags, "Selected Revision Tag"));
  143. else
  144. argument = argument.Replace(option, "");
  145. break;
  146. case "{sBranch}":
  147. if (selectedBranches.Count == 1)
  148. argument = argument.Replace(option, selectedBranches[0].Name);
  149. else if (selectedBranches.Count != 0)
  150. argument = argument.Replace(option,
  151. askToSpecify(selectedBranches, "Selected Revision Branch"));
  152. else
  153. argument = argument.Replace(option, "");
  154. break;
  155. case "{sLocalBranch}":
  156. if (selectedLocalBranches.Count == 1)
  157. argument = argument.Replace(option, selectedLocalBranches[0].Name);
  158. else if (selectedLocalBranches.Count != 0)
  159. argument = argument.Replace(option,
  160. askToSpecify(selectedLocalBranches,
  161. "Selected Revision Local Branch"));
  162. else
  163. argument = argument.Replace(option, "");
  164. break;
  165. case "{sRemoteBranch}":
  166. if (selectedRemoteBranches.Count == 1)
  167. argument = argument.Replace(option, selectedRemoteBranches[0].Name);
  168. else if (selectedRemoteBranches.Count != 0)
  169. argument = argument.Replace(option,
  170. askToSpecify(selectedRemoteBranches,
  171. "Selected Revision Remote Branch"));
  172. else
  173. argument = argument.Replace(option, "");
  174. break;
  175. case "{sRemote}":
  176. if (selectedRemotes.Count == 1)
  177. argument = argument.Replace(option, selectedRemotes[0]);
  178. else if (selectedRemotes.Count != 0)
  179. {
  180. argument = argument.Replace(option,
  181. askToSpecify(selectedRemotes, "Selected Revision Remote"));
  182. }
  183. else
  184. argument = argument.Replace(option, "");
  185. break;
  186. case "{sHash}":
  187. argument = argument.Replace(option, selectedRevision.Guid);
  188. break;
  189. case "{sMessage}":
  190. argument = argument.Replace(option, selectedRevision.Message);
  191. break;
  192. case "{sAuthor}":
  193. argument = argument.Replace(option, selectedRevision.Author);
  194. break;
  195. case "{sCommitter}":
  196. argument = argument.Replace(option, selectedRevision.Committer);
  197. break;
  198. case "{sAuthorDate}":
  199. argument = argument.Replace(option, selectedRevision.AuthorDate.ToString());
  200. break;
  201. case "{sCommitDate}":
  202. argument = argument.Replace(option, selectedRevision.CommitDate.ToString());
  203. break;
  204. case "{cTag}":
  205. if (currentTags.Count == 1)
  206. argument = argument.Replace(option, currentTags[0].Name);
  207. else if (currentTags.Count != 0)
  208. argument = argument.Replace(option, askToSpecify(currentTags, "Current Revision Tag"));
  209. else
  210. argument = argument.Replace(option, "");
  211. break;
  212. case "{cBranch}":
  213. if (currentBranches.Count == 1)
  214. argument = argument.Replace(option, currentBranches[0].Name);
  215. else if (currentBranches.Count != 0)
  216. argument = argument.Replace(option,
  217. askToSpecify(currentBranches, "Current Revision Branch"));
  218. else
  219. argument = argument.Replace(option, "");
  220. break;
  221. case "{cLocalBranch}":
  222. if (currentLocalBranches.Count == 1)
  223. argument = argument.Replace(option, currentLocalBranches[0].Name);
  224. else if (currentLocalBranches.Count != 0)
  225. argument = argument.Replace(option,
  226. askToSpecify(currentLocalBranches,
  227. "Current Revision Local Branch"));
  228. else
  229. argument = argument.Replace(option, "");
  230. break;
  231. case "{cRemoteBranch}":
  232. if (currentRemoteBranches.Count == 1)
  233. argument = argument.Replace(option, currentRemoteBranches[0].Name);
  234. else if (currentRemoteBranches.Count != 0)
  235. argument = argument.Replace(option,
  236. askToSpecify(currentRemoteBranches,
  237. "Current Revision Remote Branch"));
  238. else
  239. argument = argument.Replace(option, "");
  240. break;
  241. case "{cHash}":
  242. argument = argument.Replace(option, currentRevision.Guid);
  243. break;
  244. case "{cMessage}":
  245. argument = argument.Replace(option, currentRevision.Message);
  246. break;
  247. case "{cAuthor}":
  248. argument = argument.Replace(option, currentRevision.Author);
  249. break;
  250. case "{cCommitter}":
  251. argument = argument.Replace(option, currentRevision.Committer);
  252. break;
  253. case "{cAuthorDate}":
  254. argument = argument.Replace(option, currentRevision.AuthorDate.ToString());
  255. break;
  256. case "{cCommitDate}":
  257. argument = argument.Replace(option, currentRevision.CommitDate.ToString());
  258. break;
  259. case "{cDefaultRemote}":
  260. if (currentBranches.Count == 1)
  261. argument = argument.Replace(option, GitCommandHelpers.GetSetting(string.Format("branch.{0}.remote", currentBranches[0].Name)));
  262. else if (currentBranches.Count != 0)
  263. argument = argument.Replace(option, GitCommandHelpers.GetSetting(string.Format("branch.{0}.remote",
  264. askToSpecify(currentBranches, "Current Revision Branch"))));
  265. else
  266. argument = argument.Replace(option, "");
  267. break;
  268. case "{UserInput}":
  269. SimplePrompt Prompt = new SimplePrompt();
  270. Prompt.ShowDialog();
  271. argument = argument.Replace(option, Prompt.UserInput);
  272. break;
  273. default:
  274. break;
  275. }
  276. }
  277. }
  278. new FormProcess(command, argument).ShowDialog();
  279. }
  280. private static string askToSpecify(IEnumerable<GitHead> options, string title)
  281. {
  282. var f = new FormRunScriptSpecify(options, title);
  283. f.ShowDialog();
  284. return f.ret;
  285. }
  286. private static string askToSpecify(IEnumerable<string> options, string title)
  287. {
  288. var f = new FormRunScriptSpecify(options, title);
  289. f.ShowDialog();
  290. return f.ret;
  291. }
  292. }
  293. }