PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/GitUI/CommandsDialogs/FormClone.cs

https://github.com/vbjay/gitextensions
C# | 307 lines | 257 code | 49 blank | 1 comment | 41 complexity | d80f5347ae07d4b98fe61635cab6d149 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Windows.Forms;
  7. using GitCommands;
  8. using GitCommands.Repository;
  9. using ResourceManager;
  10. namespace GitUI.CommandsDialogs
  11. {
  12. public partial class FormClone : GitModuleForm
  13. {
  14. private readonly TranslationString _infoNewRepositoryLocation =
  15. new TranslationString("The repository will be cloned to a new directory located here:" + Environment.NewLine +
  16. "{0}");
  17. private readonly TranslationString _infoDirectoryExists =
  18. new TranslationString("(Directory already exists)");
  19. private readonly TranslationString _infoDirectoryNew =
  20. new TranslationString("(New directory)");
  21. private readonly TranslationString _questionOpenRepo =
  22. new TranslationString("The repository has been cloned successfully." + Environment.NewLine +
  23. "Do you want to open the new repository \"{0}\" now?");
  24. private readonly TranslationString _questionOpenRepoCaption =
  25. new TranslationString("Open");
  26. private bool openedFromProtocolHandler;
  27. private readonly string url;
  28. private GitModuleChangedEventHandler GitModuleChanged;
  29. // for translation only
  30. private FormClone()
  31. : this(null, null, false, null)
  32. {
  33. }
  34. public FormClone(GitUICommands aCommands, string url, bool openedFromProtocolHandler, GitModuleChangedEventHandler GitModuleChanged)
  35. : base(aCommands)
  36. {
  37. this.GitModuleChanged = GitModuleChanged;
  38. InitializeComponent();
  39. Translate();
  40. this.openedFromProtocolHandler = openedFromProtocolHandler;
  41. this.url = url;
  42. }
  43. protected override void OnRuntimeLoad(EventArgs e)
  44. {
  45. base.OnRuntimeLoad(e);
  46. FillFromDropDown();
  47. _NO_TRANSLATE_To.Text = AppSettings.DefaultCloneDestinationPath;
  48. if (url != null)
  49. {
  50. _NO_TRANSLATE_From.Text = url;
  51. if (!Module.IsValidGitWorkingDir())
  52. _NO_TRANSLATE_To.Text = Module.WorkingDir;
  53. }
  54. else
  55. {
  56. if (Module.IsValidGitWorkingDir())
  57. _NO_TRANSLATE_From.Text = Module.WorkingDir;
  58. else if (!string.IsNullOrEmpty(Module.WorkingDir))
  59. _NO_TRANSLATE_To.Text = Module.WorkingDir;
  60. }
  61. FromTextUpdate(null, null);
  62. }
  63. private void OkClick(object sender, EventArgs e)
  64. {
  65. try
  66. {
  67. Cursor = Cursors.Default;
  68. _branchListLoader.Cancel();
  69. var dirTo = Path.Combine(_NO_TRANSLATE_To.Text, _NO_TRANSLATE_NewDirectory.Text);
  70. Repositories.AddMostRecentRepository(_NO_TRANSLATE_From.Text);
  71. Repositories.AddMostRecentRepository(dirTo);
  72. if (!Directory.Exists(dirTo))
  73. Directory.CreateDirectory(dirTo);
  74. var cloneCmd = GitCommandHelpers.CloneCmd(_NO_TRANSLATE_From.Text, dirTo,
  75. CentralRepository.Checked, cbIntializeAllSubmodules.Checked, Branches.Text, null);
  76. using (var fromProcess = new FormRemoteProcess(Module, AppSettings.GitCommand, cloneCmd))
  77. {
  78. fromProcess.SetUrlTryingToConnect(_NO_TRANSLATE_From.Text);
  79. fromProcess.ShowDialog(this);
  80. if (fromProcess.ErrorOccurred() || Module.InTheMiddleOfPatch())
  81. return;
  82. }
  83. if (openedFromProtocolHandler && AskIfNewRepositoryShouldBeOpened(dirTo))
  84. {
  85. Hide();
  86. GitUICommands uiCommands = new GitUICommands(dirTo);
  87. uiCommands.StartBrowseDialog();
  88. }
  89. else if (ShowInTaskbar == false && GitModuleChanged != null &&
  90. AskIfNewRepositoryShouldBeOpened(dirTo))
  91. GitModuleChanged(new GitModule(dirTo));
  92. Close();
  93. }
  94. catch (Exception ex)
  95. {
  96. MessageBox.Show(this, "Exception: " + ex.Message, "Clone failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
  97. }
  98. }
  99. private bool AskIfNewRepositoryShouldBeOpened(string dirTo)
  100. {
  101. return MessageBox.Show(this, string.Format(_questionOpenRepo.Text, dirTo), _questionOpenRepoCaption.Text,
  102. MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
  103. }
  104. private void FromBrowseClick(object sender, EventArgs e)
  105. {
  106. using (var dialog = new FolderBrowserDialog { SelectedPath = _NO_TRANSLATE_From.Text })
  107. {
  108. if (dialog.ShowDialog(this) == DialogResult.OK)
  109. _NO_TRANSLATE_From.Text = dialog.SelectedPath;
  110. }
  111. FromTextUpdate(sender, e);
  112. }
  113. private void ToBrowseClick(object sender, EventArgs e)
  114. {
  115. using (var dialog = new FolderBrowserDialog { SelectedPath = _NO_TRANSLATE_To.Text })
  116. {
  117. if (dialog.ShowDialog(this) == DialogResult.OK)
  118. _NO_TRANSLATE_To.Text = dialog.SelectedPath;
  119. }
  120. ToTextUpdate(sender, e);
  121. }
  122. private void FillFromDropDown()
  123. {
  124. System.ComponentModel.BindingList<Repository> repos = Repositories.RemoteRepositoryHistory.Repositories;
  125. if (_NO_TRANSLATE_From.Items.Count != repos.Count)
  126. {
  127. _NO_TRANSLATE_To.Items.Clear();
  128. foreach (Repository repo in repos)
  129. _NO_TRANSLATE_From.Items.Add(repo.Path);
  130. }
  131. }
  132. private void ToDropDown(object sender, EventArgs e)
  133. {
  134. System.ComponentModel.BindingList<Repository> repos = Repositories.RepositoryHistory.Repositories;
  135. if (_NO_TRANSLATE_To.Items.Count != repos.Count)
  136. {
  137. _NO_TRANSLATE_To.Items.Clear();
  138. foreach (Repository repo in repos)
  139. _NO_TRANSLATE_To.Items.Add(repo.Path);
  140. }
  141. }
  142. private void LoadSshKeyClick(object sender, EventArgs e)
  143. {
  144. BrowseForPrivateKey.BrowseAndLoad(this);
  145. }
  146. private void FormCloneLoad(object sender, EventArgs e)
  147. {
  148. if (!GitCommandHelpers.Plink())
  149. LoadSSHKey.Visible = false;
  150. }
  151. private void FromSelectedIndexChanged(object sender, EventArgs e)
  152. {
  153. FromTextUpdate(sender, e);
  154. }
  155. private void FromTextUpdate(object sender, EventArgs e)
  156. {
  157. var path = _NO_TRANSLATE_From.Text;
  158. path = path.TrimEnd(new[] { '\\', '/' });
  159. const string standardRepositorySuffix = ".git";
  160. if (path.EndsWith(standardRepositorySuffix))
  161. path = path.Substring(0, path.Length - standardRepositorySuffix.Length);
  162. if (path.Contains("\\") || path.Contains("/"))
  163. _NO_TRANSLATE_NewDirectory.Text = path.Substring(path.LastIndexOfAny(new[] { '\\', '/' }) + 1);
  164. Branches.DataSource = null;
  165. ToTextUpdate(sender, e);
  166. }
  167. private void ToTextUpdate(object sender, EventArgs e)
  168. {
  169. string destinationPath = string.Empty;
  170. if (string.IsNullOrEmpty(_NO_TRANSLATE_To.Text))
  171. destinationPath += "[" + label2.Text + "]";
  172. else
  173. destinationPath += _NO_TRANSLATE_To.Text.TrimEnd(new[] { '\\', '/' });
  174. destinationPath += "\\";
  175. if (string.IsNullOrEmpty(_NO_TRANSLATE_NewDirectory.Text))
  176. destinationPath += "[" + label3.Text + "]";
  177. else
  178. destinationPath += _NO_TRANSLATE_NewDirectory.Text;
  179. Info.Text = string.Format(_infoNewRepositoryLocation.Text, destinationPath);
  180. if (destinationPath.Contains("[") || destinationPath.Contains("]"))
  181. {
  182. Info.ForeColor = Color.Red;
  183. return;
  184. }
  185. if (Directory.Exists(destinationPath))
  186. {
  187. if (Directory.GetDirectories(destinationPath).Length > 0 || Directory.GetFiles(destinationPath).Length > 0)
  188. {
  189. Info.Text += " " + _infoDirectoryExists.Text;
  190. Info.ForeColor = Color.Red;
  191. }
  192. else
  193. {
  194. Info.ForeColor = Color.Black;
  195. }
  196. }
  197. else
  198. {
  199. Info.Text += " " + _infoDirectoryNew.Text;
  200. Info.ForeColor = Color.Black;
  201. }
  202. }
  203. private void NewDirectoryTextChanged(object sender, EventArgs e)
  204. {
  205. ToTextUpdate(sender, e);
  206. }
  207. private void ToSelectedIndexChanged(object sender, EventArgs e)
  208. {
  209. ToTextUpdate(sender, e);
  210. }
  211. private readonly AsyncLoader _branchListLoader = new AsyncLoader();
  212. private void UpdateBranches(RemoteActionResult<IList<GitRef>> branchList)
  213. {
  214. Cursor = Cursors.Default;
  215. if (branchList.HostKeyFail)
  216. {
  217. string remoteUrl = _NO_TRANSLATE_From.Text;
  218. if (FormRemoteProcess.AskForCacheHostkey(this, Module, remoteUrl))
  219. {
  220. LoadBranches();
  221. }
  222. }
  223. else if (branchList.AuthenticationFail)
  224. {
  225. string loadedKey;
  226. if (FormPuttyError.AskForKey(this, out loadedKey))
  227. {
  228. LoadBranches();
  229. }
  230. }
  231. else
  232. {
  233. string text = Branches.Text;
  234. Branches.DataSource = branchList.Result;
  235. if (branchList.Result.Any(a => a.LocalName == text))
  236. {
  237. Branches.Text = text;
  238. }
  239. }
  240. }
  241. private void LoadBranches()
  242. {
  243. Branches.DisplayMember = "LocalName";
  244. string from = _NO_TRANSLATE_From.Text;
  245. Cursor = Cursors.AppStarting;
  246. _branchListLoader.Load(() => Module.GetRemoteRefs(from, false, true), UpdateBranches);
  247. }
  248. private void Branches_DropDown(object sender, EventArgs e)
  249. {
  250. LoadBranches();
  251. }
  252. }
  253. }