/Mercurial.Net/RemoteInitCommand.cs

# · C# · 215 lines · 98 code · 19 blank · 98 comment · 3 complexity · ac103afcf361a7303e973f64f06c0cf0 MD5 · raw file

  1. using System;
  2. using System.ComponentModel;
  3. using Mercurial.Attributes;
  4. namespace Mercurial
  5. {
  6. /// <summary>
  7. /// This class implements the "hg init" command (<see href="http://www.selenic.com/mercurial/hg.1.html#init"/>):
  8. /// create a new repository at the given remote location.
  9. /// </summary>
  10. public sealed class RemoteInitCommand : MercurialCommandBase<RemoteInitCommand>
  11. {
  12. /// <summary>
  13. /// This is the backing field for the <see cref="SshCommand"/> property.
  14. /// </summary>
  15. private string _SshCommand = string.Empty;
  16. /// <summary>
  17. /// This is the backing field for the <see cref="RemoteCommand"/> property.
  18. /// </summary>
  19. private string _RemoteCommand = string.Empty;
  20. /// <summary>
  21. /// This is the backing field for the <see cref="VerifyServerCertificate"/> property.
  22. /// </summary>
  23. private bool _VerifyServerCertificate = true;
  24. /// <summary>
  25. /// This is the backing field for the <see cref="Location"/> property.
  26. /// </summary>
  27. private string _Location = string.Empty;
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="RemoteInitCommand"/> class.
  30. /// </summary>
  31. public RemoteInitCommand()
  32. : base("init")
  33. {
  34. // Do nothing here
  35. }
  36. /// <summary>
  37. /// Gets or sets the ssh command to use when cloning.
  38. /// Default is <see cref="string.Empty"/>.
  39. /// </summary>
  40. [NullableArgument(NonNullOption = "--ssh")]
  41. [DefaultValue("")]
  42. public string SshCommand
  43. {
  44. get
  45. {
  46. return _SshCommand;
  47. }
  48. set
  49. {
  50. _SshCommand = (value ?? string.Empty).Trim();
  51. }
  52. }
  53. /// <summary>
  54. /// Gets or sets the hg command to run on the remote side.
  55. /// Default is <see cref="string.Empty"/>.
  56. /// </summary>
  57. [NullableArgument(NonNullOption = "--remotecmd")]
  58. [DefaultValue("")]
  59. public string RemoteCommand
  60. {
  61. get
  62. {
  63. return _RemoteCommand;
  64. }
  65. set
  66. {
  67. _RemoteCommand = (value ?? string.Empty).Trim();
  68. }
  69. }
  70. /// <summary>
  71. /// Gets or sets a value indicating whether to verify the server certificate. If set to <c>false</c>, will ignore web.cacerts configuration.
  72. /// Default value is <c>true</c>.
  73. /// </summary>
  74. [BooleanArgument(FalseOption = "--insecure")]
  75. [DefaultValue(true)]
  76. public bool VerifyServerCertificate
  77. {
  78. get
  79. {
  80. return _VerifyServerCertificate;
  81. }
  82. set
  83. {
  84. RequiresVersion(new Version(1, 7, 5), "VerifyServerCertificate property of the PullCommand class");
  85. _VerifyServerCertificate = value;
  86. }
  87. }
  88. /// <summary>
  89. /// Gets or sets the Url of the remote location to initialize a repository.
  90. /// Default value is <see cref="string.Empty"/>.
  91. /// </summary>
  92. [NullableArgument]
  93. [DefaultValue("")]
  94. public string Location
  95. {
  96. get
  97. {
  98. return _Location;
  99. }
  100. set
  101. {
  102. _Location = (value ?? string.Empty).Trim();
  103. }
  104. }
  105. /// <summary>
  106. /// Sets the <see cref="SshCommand"/> property to the specified value and
  107. /// returns this <see cref="RemoteInitCommand"/> instance.
  108. /// </summary>
  109. /// <param name="value">
  110. /// The new value for the <see cref="SshCommand"/> property.
  111. /// </param>
  112. /// <returns>
  113. /// This <see cref="RemoteInitCommand"/> instance.
  114. /// </returns>
  115. /// <remarks>
  116. /// This method is part of the fluent interface.
  117. /// </remarks>
  118. public RemoteInitCommand WithSshCommand(string value)
  119. {
  120. SshCommand = value;
  121. return this;
  122. }
  123. /// <summary>
  124. /// Sets the <see cref="RemoteCommand"/> property to the specified value and
  125. /// returns this <see cref="RemoteInitCommand"/> instance.
  126. /// </summary>
  127. /// <param name="value">
  128. /// The new value for the <see cref="RemoteCommand"/> property.
  129. /// </param>
  130. /// <returns>
  131. /// This <see cref="RemoteInitCommand"/> instance.
  132. /// </returns>
  133. /// <remarks>
  134. /// This method is part of the fluent interface.
  135. /// </remarks>
  136. public RemoteInitCommand WithRemoteCommand(string value)
  137. {
  138. RemoteCommand = value;
  139. return this;
  140. }
  141. /// <summary>
  142. /// Sets the <see cref="VerifyServerCertificate"/> property to the specified value and
  143. /// returns this <see cref="RemoteInitCommand"/> instance.
  144. /// </summary>
  145. /// <param name="value">
  146. /// The new value for the <see cref="VerifyServerCertificate"/> property.
  147. /// </param>
  148. /// <returns>
  149. /// This <see cref="RemoteInitCommand"/> instance.
  150. /// </returns>
  151. /// <remarks>
  152. /// This method is part of the fluent interface.
  153. /// </remarks>
  154. public RemoteInitCommand WithVerifyServerCertificate(bool value)
  155. {
  156. VerifyServerCertificate = value;
  157. return this;
  158. }
  159. /// <summary>
  160. /// Sets the <see cref="VerifyServerCertificate"/> property to the specified value and
  161. /// returns this <see cref="RemoteInitCommand"/> instance.
  162. /// </summary>
  163. /// <param name="value">
  164. /// The new value for the <see cref="VerifyServerCertificate"/> property.
  165. /// </param>
  166. /// <returns>
  167. /// This <see cref="RemoteInitCommand"/> instance.
  168. /// </returns>
  169. /// <remarks>
  170. /// This method is part of the fluent interface.
  171. /// </remarks>
  172. public RemoteInitCommand WithLocation(string value)
  173. {
  174. Location = value;
  175. return this;
  176. }
  177. /// <summary>
  178. /// Validates the command configuration. This method should throw the necessary
  179. /// exceptions to signal missing or incorrect configuration (like attempting to
  180. /// add files to the repository without specifying which files to add.)
  181. /// </summary>
  182. /// <exception cref="InvalidOperationException">
  183. /// <para>The <see cref="VerifyServerCertificate"/> command was used with Mercurial 1.7.4 or older.</para>
  184. /// <para>- or -</para>
  185. /// <para>The <see cref="Location"/> property was blank.</para>
  186. /// </exception>
  187. public override void Validate()
  188. {
  189. base.Validate();
  190. if (StringEx.IsNullOrWhiteSpace(Location))
  191. throw new InvalidOperationException("The Location property must be set before executing a RemoteInitCommand");
  192. if (!VerifyServerCertificate && ClientExecutable.CurrentVersion < new Version(1, 7, 5))
  193. throw new InvalidOperationException("The 'VerifyServerCertificate' property is only available in Mercurial 1.7.5 and newer");
  194. }
  195. }
  196. }