2
This code is derived from jgit (http://eclipse.org/jgit).
3
Copyright owners are documented in jgit's IP log.
5
This program and the accompanying materials are made available
6
under the terms of the Eclipse Distribution License v1.0 which
7
accompanies this distribution, is reproduced below, and is
8
available at http://www.eclipse.org/org/documents/edl-v10.php
12
Redistribution and use in source and binary forms, with or
13
without modification, are permitted provided that the following
16
- Redistributions of source code must retain the above copyright
17
notice, this list of conditions and the following disclaimer.
19
- Redistributions in binary form must reproduce the above
20
copyright notice, this list of conditions and the following
21
disclaimer in the documentation and/or other materials provided
22
with the distribution.
24
- Neither the name of the Eclipse Foundation, Inc. nor the
25
names of its contributors may be used to endorse or promote
26
products derived from this software without specific prior
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
using System.Collections.Generic;
49
using NGit.Api.Errors;
57
/// A class used to execute a
59
/// command. It has setters for all
60
/// supported options and arguments of this command and a
61
/// <see cref="Call()">Call()</see>
63
/// to finally execute the command.
65
/// <seealso><a href="http://www.kernel.org/pub/software/scm/git/docs/git-push.html"
66
/// * >Git documentation about Push</a></seealso>
67
public class PushCommand : GitCommand<Iterable<PushResult>>
69
private string remote = Constants.DEFAULT_REMOTE_NAME;
71
private readonly IList<RefSpec> refSpecs;
73
private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
75
private string receivePack = RemoteConfig.DEFAULT_RECEIVE_PACK;
81
private bool thin = NGit.Transport.Transport.DEFAULT_PUSH_THIN;
85
private CredentialsProvider credentialsProvider;
87
/// <param name="repo"></param>
88
protected internal PushCommand(Repository repo) : base(repo)
90
refSpecs = new AList<RefSpec>(3);
96
/// command with all the options and parameters
97
/// collected by the setter methods of this class. Each instance of this
98
/// class should only be used for one invocation of the command (means: one
100
/// <see cref="Call()">Call()</see>
104
/// an iteration over
105
/// <see cref="NGit.Transport.PushResult">NGit.Transport.PushResult</see>
108
/// <exception cref="NGit.Api.Errors.InvalidRemoteException">when called with an invalid remote uri
110
/// <exception cref="NGit.Api.Errors.JGitInternalException">
111
/// a low-level exception of JGit has occurred. The original
112
/// exception can be retrieved by calling
113
/// <see cref="System.Exception.InnerException()">System.Exception.InnerException()</see>
116
public override Iterable<PushResult> Call()
119
AList<PushResult> pushResults = new AList<PushResult>(3);
122
if (refSpecs.IsEmpty())
124
Ref head = repo.GetRef(Constants.HEAD);
125
if (head != null && head.IsSymbolic())
127
refSpecs.AddItem(new RefSpec(head.GetLeaf().GetName()));
132
for (int i = 0; i < refSpecs.Count; i++)
134
refSpecs.Set(i, refSpecs[i].SetForceUpdate(true));
137
IList<NGit.Transport.Transport> transports;
138
transports = NGit.Transport.Transport.OpenAll(repo, remote, NGit.Transport.Transport.Operation.PUSH
140
foreach (NGit.Transport.Transport transport in transports)
144
transport.SetTimeout(timeout);
146
transport.SetPushThin(thin);
147
if (receivePack != null)
149
transport.SetOptionReceivePack(receivePack);
151
transport.SetDryRun(dryRun);
152
if (credentialsProvider != null)
154
transport.SetCredentialsProvider(credentialsProvider);
156
ICollection<RemoteRefUpdate> toPush = transport.FindRemoteRefUpdatesFor(refSpecs);
159
PushResult result = transport.Push(monitor, toPush);
160
pushResults.AddItem(result);
162
catch (TransportException e)
164
throw new JGitInternalException(JGitText.Get().exceptionCaughtDuringExecutionOfPushCommand
173
catch (URISyntaxException)
175
throw new InvalidRemoteException(MessageFormat.Format(JGitText.Get().invalidRemote
178
catch (NotSupportedException e)
180
throw new JGitInternalException(JGitText.Get().exceptionCaughtDuringExecutionOfPushCommand
183
catch (IOException e)
185
throw new JGitInternalException(JGitText.Get().exceptionCaughtDuringExecutionOfPushCommand
188
return pushResults.AsIterable ();
191
/// <summary>The remote (uri or name) used for the push operation.</summary>
193
/// The remote (uri or name) used for the push operation. If no remote is
194
/// set, the default value of <code>Constants.DEFAULT_REMOTE_NAME</code> will
197
/// <seealso cref="NGit.Constants.DEFAULT_REMOTE_NAME">NGit.Constants.DEFAULT_REMOTE_NAME
199
/// <param name="remote"></param>
202
/// <code>this</code>
204
public virtual NGit.Api.PushCommand SetRemote(string remote)
207
this.remote = remote;
211
/// <returns>the remote used for the remote operation</returns>
212
public virtual string GetRemote()
217
/// <summary>The remote executable providing receive-pack service for pack transports.
220
/// The remote executable providing receive-pack service for pack transports.
221
/// If no receive-pack is set, the default value of
222
/// <code>RemoteConfig.DEFAULT_RECEIVE_PACK</code> will be used.
224
/// <seealso cref="NGit.Transport.RemoteConfig.DEFAULT_RECEIVE_PACK">NGit.Transport.RemoteConfig.DEFAULT_RECEIVE_PACK
226
/// <param name="receivePack"></param>
229
/// <code>this</code>
231
public virtual NGit.Api.PushCommand SetReceivePack(string receivePack)
234
this.receivePack = receivePack;
238
/// <returns>the receive-pack used for the remote operation</returns>
239
public virtual string GetReceivePack()
244
/// <param name="timeout">the timeout used for the push operation</param>
247
/// <code>this</code>
249
public virtual NGit.Api.PushCommand SetTimeout(int timeout)
252
this.timeout = timeout;
256
/// <returns>the timeout used for the push operation</returns>
257
public virtual int GetTimeout()
262
/// <returns>the progress monitor for the push operation</returns>
263
public virtual ProgressMonitor GetProgressMonitor()
268
/// <summary>The progress monitor associated with the push operation.</summary>
270
/// The progress monitor associated with the push operation. By default, this
271
/// is set to <code>NullProgressMonitor</code>
273
/// <seealso cref="NGit.NullProgressMonitor">NGit.NullProgressMonitor</seealso>
274
/// <param name="monitor"></param>
277
/// <code>this</code>
279
public virtual NGit.Api.PushCommand SetProgressMonitor(ProgressMonitor monitor)
282
this.monitor = monitor;
286
/// <returns>the ref specs</returns>
287
public virtual IList<RefSpec> GetRefSpecs()
292
/// <summary>The ref specs to be used in the push operation</summary>
293
/// <param name="specs"></param>
296
/// <code>this</code>
298
public virtual NGit.Api.PushCommand SetRefSpecs(params RefSpec[] specs)
301
this.refSpecs.Clear();
302
Sharpen.Collections.AddAll(refSpecs, specs);
306
/// <summary>The ref specs to be used in the push operation</summary>
307
/// <param name="specs"></param>
310
/// <code>this</code>
312
public virtual NGit.Api.PushCommand SetRefSpecs(IList<RefSpec> specs)
315
this.refSpecs.Clear();
316
Sharpen.Collections.AddAll(this.refSpecs, specs);
320
/// <summary>Push all branches under refs/heads/*.</summary>
321
/// <remarks>Push all branches under refs/heads/*.</remarks>
322
/// <returns>{code this}</returns>
323
public virtual NGit.Api.PushCommand SetPushAll()
325
refSpecs.AddItem(NGit.Transport.Transport.REFSPEC_PUSH_ALL);
329
/// <summary>Push all tags under refs/tags/*.</summary>
330
/// <remarks>Push all tags under refs/tags/*.</remarks>
331
/// <returns>{code this}</returns>
332
public virtual NGit.Api.PushCommand SetPushTags()
334
refSpecs.AddItem(NGit.Transport.Transport.REFSPEC_TAGS);
338
/// <summary>Add a reference to push.</summary>
339
/// <remarks>Add a reference to push.</remarks>
340
/// <param name="ref">the source reference. The remote name will match.</param>
343
/// <code>this</code>
346
public virtual NGit.Api.PushCommand Add(Ref @ref)
348
refSpecs.AddItem(new RefSpec(@ref.GetLeaf().GetName()));
352
/// <summary>Add a reference to push.</summary>
353
/// <remarks>Add a reference to push.</remarks>
354
/// <param name="nameOrSpec">any reference name, or a reference specification.</param>
357
/// <code>this</code>
360
/// <exception cref="NGit.Api.Errors.JGitInternalException">the reference name cannot be resolved.
362
public virtual NGit.Api.PushCommand Add(string nameOrSpec)
364
if (0 <= nameOrSpec.IndexOf(':'))
366
refSpecs.AddItem(new RefSpec(nameOrSpec));
373
src = repo.GetRef(nameOrSpec);
375
catch (IOException e)
377
throw new JGitInternalException(JGitText.Get().exceptionCaughtDuringExecutionOfPushCommand
388
/// <returns>the dry run preference for the push operation</returns>
389
public virtual bool IsDryRun()
394
/// <summary>Sets whether the push operation should be a dry run</summary>
395
/// <param name="dryRun"></param>
398
/// <code>this</code>
400
public virtual NGit.Api.PushCommand SetDryRun(bool dryRun)
403
this.dryRun = dryRun;
407
/// <returns>the thin-pack preference for push operation</returns>
408
public virtual bool IsThin()
413
/// <summary>Sets the thin-pack preference for push operation.</summary>
415
/// Sets the thin-pack preference for push operation.
416
/// Default setting is Transport.DEFAULT_PUSH_THIN
418
/// <param name="thin"></param>
421
/// <code>this</code>
423
public virtual NGit.Api.PushCommand SetThin(bool thin)
430
/// <returns>the force preference for push operation</returns>
431
public virtual bool IsForce()
436
/// <summary>Sets the force preference for push operation.</summary>
437
/// <remarks>Sets the force preference for push operation.</remarks>
438
/// <param name="force"></param>
441
/// <code>this</code>
443
public virtual NGit.Api.PushCommand SetForce(bool force)
450
/// <param name="credentialsProvider">
452
/// <see cref="NGit.Transport.CredentialsProvider">NGit.Transport.CredentialsProvider
458
/// <code>this</code>
460
public virtual NGit.Api.PushCommand SetCredentialsProvider(CredentialsProvider credentialsProvider
464
this.credentialsProvider = credentialsProvider;