PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Mercurial.Net/PushCommand.cs

#
C# | 348 lines | 154 code | 30 blank | 164 comment | 3 complexity | ddae020c6131d94544a14026280d0cbd MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using Mercurial.Attributes;
  6. namespace Mercurial
  7. {
  8. /// <summary>
  9. /// This class implements the "hg push" command (<see href="http://www.selenic.com/mercurial/hg.1.html#push"/>):
  10. /// push changes to the specified destination.
  11. /// </summary>
  12. public sealed class PushCommand : MercurialCommandBase<PushCommand>
  13. {
  14. /// <summary>
  15. /// This is the backing field for the <see cref="Revisions"/> property.
  16. /// </summary>
  17. private readonly List<RevSpec> _Revisions = new List<RevSpec>();
  18. /// <summary>
  19. /// This is the backing field for the <see cref="Bookmarks"/> property.
  20. /// </summary>
  21. private readonly List<string> _Bookmarks = new List<string>();
  22. /// <summary>
  23. /// This is the backing field for the <see cref="Destination"/> property.
  24. /// </summary>
  25. private string _Destination = string.Empty;
  26. /// <summary>
  27. /// This is the backing field for the <see cref="SshCommand"/> property.
  28. /// </summary>
  29. private string _SshCommand = string.Empty;
  30. /// <summary>
  31. /// This is the backing field for the <see cref="RemoteCommand"/> property.
  32. /// </summary>
  33. private string _RemoteCommand = string.Empty;
  34. /// <summary>
  35. /// This is the backing field for the <see cref="VerifyServerCertificate"/> property.
  36. /// </summary>
  37. private bool _VerifyServerCertificate = true;
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="PushCommand"/> class.
  40. /// </summary>
  41. public PushCommand()
  42. : base("push")
  43. {
  44. // Do nothing here
  45. }
  46. /// <summary>
  47. /// Gets the collection of bookmarks to push.
  48. /// Default is empty.
  49. /// </summary>
  50. [RepeatableArgument(Option = "--bookmark")]
  51. public Collection<string> Bookmarks
  52. {
  53. get
  54. {
  55. return new Collection<string>(_Bookmarks);
  56. }
  57. }
  58. /// <summary>
  59. /// Gets or sets the ssh command to use when pushing.
  60. /// Default is <see cref="string.Empty"/>.
  61. /// </summary>
  62. [NullableArgument(NonNullOption = "--ssh")]
  63. [DefaultValue("")]
  64. public string SshCommand
  65. {
  66. get
  67. {
  68. return _SshCommand;
  69. }
  70. set
  71. {
  72. _SshCommand = (value ?? string.Empty).Trim();
  73. }
  74. }
  75. /// <summary>
  76. /// Gets or sets the hg command to run on the remote side.
  77. /// Default is <see cref="string.Empty"/>.
  78. /// </summary>
  79. [NullableArgument(NonNullOption = "--remotecmd")]
  80. [DefaultValue("")]
  81. public string RemoteCommand
  82. {
  83. get
  84. {
  85. return _RemoteCommand;
  86. }
  87. set
  88. {
  89. _RemoteCommand = (value ?? string.Empty).Trim();
  90. }
  91. }
  92. /// <summary>
  93. /// Gets or sets a value indicating whether to verify the server certificate. If set to <c>false</c>, will ignore web.cacerts configuration.
  94. /// Default value is <c>true</c>.
  95. /// </summary>
  96. [BooleanArgument(FalseOption = "--insecure")]
  97. [DefaultValue(true)]
  98. public bool VerifyServerCertificate
  99. {
  100. get
  101. {
  102. return _VerifyServerCertificate;
  103. }
  104. set
  105. {
  106. RequiresVersion(new Version(1, 7, 5), "VerifyServerCertificate property of the PushCommand class");
  107. _VerifyServerCertificate = value;
  108. }
  109. }
  110. /// <summary>
  111. /// Sets the <see cref="VerifyServerCertificate"/> property to the specified value and
  112. /// returns this <see cref="PushCommand"/> instance.
  113. /// </summary>
  114. /// <param name="value">
  115. /// The new value for the <see cref="VerifyServerCertificate"/> property.
  116. /// </param>
  117. /// <returns>
  118. /// This <see cref="PushCommand"/> instance.
  119. /// </returns>
  120. /// <remarks>
  121. /// This method is part of the fluent interface.
  122. /// </remarks>
  123. public PushCommand WithVerifyServerCertificate(bool value)
  124. {
  125. VerifyServerCertificate = value;
  126. return this;
  127. }
  128. /// <summary>
  129. /// Gets or sets the destination to pull from. If <see cref="string.Empty"/>, push to the
  130. /// default destination. Default is <see cref="string.Empty"/>.
  131. /// </summary>
  132. [NullableArgument]
  133. [DefaultValue("")]
  134. public string Destination
  135. {
  136. get
  137. {
  138. return _Destination;
  139. }
  140. set
  141. {
  142. _Destination = (value ?? string.Empty).Trim();
  143. }
  144. }
  145. /// <summary>
  146. /// Gets or sets a value indicating whether to force push to the destination, even if
  147. /// the repositories are unrelated, or pushing would create new heads in the
  148. /// destination repository. Default is <c>false</c>.
  149. /// </summary>
  150. [BooleanArgument(TrueOption = "--force")]
  151. [DefaultValue(false)]
  152. public bool Force
  153. {
  154. get;
  155. set;
  156. }
  157. /// <summary>
  158. /// Gets or sets a value indicating whether to allow creating a new branch in the destination
  159. /// repository. Default is <c>false</c>.
  160. /// </summary>
  161. [BooleanArgument(TrueOption = "--new-branch")]
  162. [DefaultValue(false)]
  163. public bool AllowCreatingNewBranch
  164. {
  165. get;
  166. set;
  167. }
  168. /// <summary>
  169. /// Gets or sets a value indicating whether to push large repositories in chunks.
  170. /// Default is <c>false</c>.
  171. /// </summary>
  172. [BooleanArgument(TrueOption = "--chunked")]
  173. [DefaultValue(false)]
  174. public bool ChunkedTransfer
  175. {
  176. get;
  177. set;
  178. }
  179. /// <summary>
  180. /// Gets the collection of revisions to include when pushing.
  181. /// If empty, push all changes. Default is empty.
  182. /// </summary>
  183. [RepeatableArgument(Option = "--rev")]
  184. public Collection<RevSpec> Revisions
  185. {
  186. get
  187. {
  188. return new Collection<RevSpec>(_Revisions);
  189. }
  190. }
  191. /// <summary>
  192. /// Sets the <see cref="Destination"/> property to the specified value and
  193. /// returns this <see cref="PushCommand"/> instance.
  194. /// </summary>
  195. /// <param name="value">
  196. /// The new value for the <see cref="Destination"/> property.
  197. /// </param>
  198. /// <returns>
  199. /// This <see cref="PushCommand"/> instance.
  200. /// </returns>
  201. /// <remarks>
  202. /// This method is part of the fluent interface.
  203. /// </remarks>
  204. public PushCommand WithDestination(string value)
  205. {
  206. Destination = value;
  207. return this;
  208. }
  209. /// <summary>
  210. /// Sets the <see cref="Force"/> property to the specified value and
  211. /// returns this <see cref="PushCommand"/> instance.
  212. /// </summary>
  213. /// <param name="value">
  214. /// The new value for the <see cref="Force"/> property,
  215. /// defaults to <c>true</c>.
  216. /// </param>
  217. /// <returns>
  218. /// This <see cref="PushCommand"/> instance.
  219. /// </returns>
  220. /// <remarks>
  221. /// This method is part of the fluent interface.
  222. /// </remarks>
  223. public PushCommand WithForce(bool value = true)
  224. {
  225. Force = value;
  226. return this;
  227. }
  228. /// <summary>
  229. /// Sets the <see cref="AllowCreatingNewBranch"/> property to the specified value and
  230. /// returns this <see cref="PushCommand"/> instance.
  231. /// </summary>
  232. /// <param name="value">
  233. /// The new value for the <see cref="AllowCreatingNewBranch"/> property,
  234. /// defaults to <c>true</c>.
  235. /// </param>
  236. /// <returns>
  237. /// This <see cref="PushCommand"/> instance.
  238. /// </returns>
  239. /// <remarks>
  240. /// This method is part of the fluent interface.
  241. /// </remarks>
  242. public PushCommand WithAllowCreatingNewBranch(bool value = true)
  243. {
  244. AllowCreatingNewBranch = value;
  245. return this;
  246. }
  247. /// <summary>
  248. /// Sets the <see cref="ChunkedTransfer"/> property to the specified value and
  249. /// returns this <see cref="PushCommand"/> instance.
  250. /// </summary>
  251. /// <param name="value">
  252. /// The new value for the <see cref="ChunkedTransfer"/> property.
  253. /// </param>
  254. /// <returns>
  255. /// This <see cref="PushCommand"/> instance.
  256. /// </returns>
  257. /// <remarks>
  258. /// This method is part of the fluent interface.
  259. /// </remarks>
  260. public PushCommand WithChunkedTransfer(bool value)
  261. {
  262. ChunkedTransfer = value;
  263. return this;
  264. }
  265. /// <summary>
  266. /// Adds the value to the <see cref="Bookmarks"/> collection property and
  267. /// returns this <see cref="PushCommand"/> instance.
  268. /// </summary>
  269. /// <param name="value">
  270. /// The value to add to the <see cref="Bookmarks"/> collection property.
  271. /// </param>
  272. /// <returns>
  273. /// This <see cref="PushCommand"/> instance.
  274. /// </returns>
  275. /// <remarks>
  276. /// This method is part of the fluent interface.
  277. /// </remarks>
  278. public PushCommand WithBookmark(string value)
  279. {
  280. Bookmarks.Add(value);
  281. return this;
  282. }
  283. /// <summary>
  284. /// Adds the value to the <see cref="Revisions"/> collection property and
  285. /// returns this <see cref="PushCommand"/> instance.
  286. /// </summary>
  287. /// <param name="value">
  288. /// The value to add to the <see cref="Revisions"/> collection property.
  289. /// </param>
  290. /// <returns>
  291. /// This <see cref="PushCommand"/> instance.
  292. /// </returns>
  293. /// <remarks>
  294. /// This method is part of the fluent interface.
  295. /// </remarks>
  296. public PushCommand WithRevision(RevSpec value)
  297. {
  298. Revisions.Add(value);
  299. return this;
  300. }
  301. /// <summary>
  302. /// Validates the command configuration. This method should throw the necessary
  303. /// exceptions to signal missing or incorrect configuration (like attempting to
  304. /// add files to the repository without specifying which files to add.)
  305. /// </summary>
  306. /// <exception cref="InvalidOperationException">
  307. /// <para>The <see cref="VerifyServerCertificate"/> command was used with Mercurial 1.7.4 or older.</para>
  308. /// </exception>
  309. public override void Validate()
  310. {
  311. base.Validate();
  312. if (Bookmarks.Count > 0)
  313. RequiresVersion(new Version(1, 8), "Bookmarks property of the PushCommand class");
  314. if (!VerifyServerCertificate && ClientExecutable.CurrentVersion < new Version(1, 7, 5))
  315. throw new InvalidOperationException("The 'VerifyServerCertificate' property is only available in Mercurial 1.7.5 and newer");
  316. }
  317. }
  318. }