PageRenderTime 49ms CodeModel.GetById 21ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 1ms

/Mercurial.Net/PushCommand.cs

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