PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/GitUI/FormClone.cs

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