Remove System.out.println from RevWalkFilterTest
[jgit.git] / org.spearce.egit.core / src / org / spearce / egit / core / op / PushOperation.java
bloba0f2e5cf35c388aa40cd134cf0291f81b04fb558
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;
28 /**
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();
44 /**
45 * Create push operation for provided specification.
46 * <p>
47 * Operation is not performed within constructor,
48 * {@link #run(IProgressMonitor)} method must be called for that.
50 * @param localDb
51 * local repository.
52 * @param specification
53 * specification of ref updates for remote repositories.
54 * @param rc
55 * optional remote config to apply on used transports. May be
56 * null.
57 * @param dryRun
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;
67 this.dryRun = dryRun;
68 this.rc = rc;
71 /**
72 * @return push operation result.
74 public PushOperationResult getOperationResult() {
75 return operationResult;
78 /**
79 * @return operation specification, as provided in constructor.
81 public PushOperationSpecification getSpecification() {
82 return specification;
85 /**
86 * Execute operation and store result. Operation is executed independently
87 * on each remote repository.
88 * <p>
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 {
97 if (monitor == null)
98 monitor = new NullProgressMonitor();
100 final int totalWork = specification.getURIsNumber()
101 * WORK_UNITS_PER_TRANSPORT;
102 if (dryRun)
103 monitor.beginTask(CoreText.PushOperation_taskNameDryRun, totalWork);
104 else
105 monitor.beginTask(CoreText.PushOperation_taskNameNormalRun,
106 totalWork);
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;
113 try {
114 if (monitor.isCanceled()) {
115 operationResult.addOperationResult(uri,
116 CoreText.PushOperation_resultCancelled);
117 continue;
119 transport = Transport.open(localDb, uri);
121 if (rc != null)
122 transport.applyConfig(rc);
123 transport.setDryRun(dryRun);
124 final EclipseGitProgressTransformer gitSubMonitor = new EclipseGitProgressTransformer(
125 subMonitor);
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
132 .getMessage()));
133 } catch (final TransportException e) {
134 operationResult.addOperationResult(uri, NLS.bind(
135 CoreText.PushOperation_resultTransportError, e
136 .getMessage()));
137 } catch (final NotSupportedException e) {
138 operationResult.addOperationResult(uri, NLS.bind(
139 CoreText.PushOperation_resultNotSupported, e
140 .getMessage()));
141 } finally {
142 if (transport != null) {
143 transport.close();
145 // Dirty trick to get things always working.
146 subMonitor.beginTask("", WORK_UNITS_PER_TRANSPORT); //$NON-NLS-1$
147 subMonitor.done();
148 subMonitor.done();
151 monitor.done();