1 /*******************************************************************************
2 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * See LICENSE for the full license text, also available.
7 *******************************************************************************/
8 package org
.spearce
.egit
.core
.op
;
10 import java
.lang
.reflect
.InvocationTargetException
;
12 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
13 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
14 import org
.eclipse
.core
.runtime
.SubProgressMonitor
;
15 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
16 import org
.eclipse
.osgi
.util
.NLS
;
17 import org
.spearce
.egit
.core
.CoreText
;
18 import org
.spearce
.egit
.core
.EclipseGitProgressTransformer
;
19 import org
.spearce
.jgit
.errors
.NoRemoteRepositoryException
;
20 import org
.spearce
.jgit
.errors
.NotSupportedException
;
21 import org
.spearce
.jgit
.errors
.TransportException
;
22 import org
.spearce
.jgit
.lib
.Repository
;
23 import org
.spearce
.jgit
.transport
.PushResult
;
24 import org
.spearce
.jgit
.transport
.RemoteConfig
;
25 import org
.spearce
.jgit
.transport
.Transport
;
26 import org
.spearce
.jgit
.transport
.URIish
;
29 * Push operation: pushing from local repository to one or many remote ones.
31 public class PushOperation
implements IRunnableWithProgress
{
32 private static final int WORK_UNITS_PER_TRANSPORT
= 10;
34 private final Repository localDb
;
36 private final PushOperationSpecification specification
;
38 private final boolean dryRun
;
40 private final RemoteConfig rc
;
42 private final PushOperationResult operationResult
= new PushOperationResult();
45 * Create push operation for provided specification.
47 * Operation is not performed within constructor,
48 * {@link #run(IProgressMonitor)} method must be called for that.
52 * @param specification
53 * specification of ref updates for remote repositories.
55 * optional remote config to apply on used transports. May be
58 * true if push operation should just check for possible result
59 * and not really update remote refs, false otherwise - when push
60 * should act normally.
62 public PushOperation(final Repository localDb
,
63 final PushOperationSpecification specification
,
64 final boolean dryRun
, final RemoteConfig rc
) {
65 this.localDb
= localDb
;
66 this.specification
= specification
;
72 * @return push operation result.
74 public PushOperationResult
getOperationResult() {
75 return operationResult
;
79 * @return operation specification, as provided in constructor.
81 public PushOperationSpecification
getSpecification() {
86 * Execute operation and store result. Operation is executed independently
87 * on each remote repository.
90 * @throws InvocationTargetException
91 * Cause of this exceptions may include
92 * {@link TransportException}, {@link NotSupportedException} or
93 * some unexpected {@link RuntimeException}.
94 * @see IRunnableWithProgress#run(IProgressMonitor)
96 public void run(IProgressMonitor monitor
) throws InvocationTargetException
{
98 monitor
= new NullProgressMonitor();
100 final int totalWork
= specification
.getURIsNumber()
101 * WORK_UNITS_PER_TRANSPORT
;
103 monitor
.beginTask(CoreText
.PushOperation_taskNameDryRun
, totalWork
);
105 monitor
.beginTask(CoreText
.PushOperation_taskNameNormalRun
,
108 for (final URIish uri
: specification
.getURIs()) {
109 final SubProgressMonitor subMonitor
= new SubProgressMonitor(
110 monitor
, WORK_UNITS_PER_TRANSPORT
,
111 SubProgressMonitor
.PREPEND_MAIN_LABEL_TO_SUBTASK
);
112 Transport transport
= null;
114 if (monitor
.isCanceled()) {
115 operationResult
.addOperationResult(uri
,
116 CoreText
.PushOperation_resultCancelled
);
119 transport
= Transport
.open(localDb
, uri
);
122 transport
.applyConfig(rc
);
123 transport
.setDryRun(dryRun
);
124 final EclipseGitProgressTransformer gitSubMonitor
= new EclipseGitProgressTransformer(
126 final PushResult pr
= transport
.push(gitSubMonitor
,
127 specification
.getRefUpdates(uri
));
128 operationResult
.addOperationResult(uri
, pr
);
129 } catch (final NoRemoteRepositoryException e
) {
130 operationResult
.addOperationResult(uri
, NLS
.bind(
131 CoreText
.PushOperation_resultNoServiceError
, e
133 } catch (final TransportException e
) {
134 operationResult
.addOperationResult(uri
, NLS
.bind(
135 CoreText
.PushOperation_resultTransportError
, e
137 } catch (final NotSupportedException e
) {
138 operationResult
.addOperationResult(uri
, NLS
.bind(
139 CoreText
.PushOperation_resultNotSupported
, e
142 if (transport
!= null) {
145 // Dirty trick to get things always working.
146 subMonitor
.beginTask("", WORK_UNITS_PER_TRANSPORT
); //$NON-NLS-1$