View Javadoc
1   /*
2    * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
3    * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.pgm;
46  
47  import static java.lang.Character.valueOf;
48  
49  import java.io.IOException;
50  import java.text.MessageFormat;
51  import java.util.ArrayList;
52  import java.util.List;
53  
54  import org.eclipse.jgit.api.Git;
55  import org.eclipse.jgit.api.PushCommand;
56  import org.eclipse.jgit.lib.Constants;
57  import org.eclipse.jgit.lib.ObjectId;
58  import org.eclipse.jgit.lib.ObjectReader;
59  import org.eclipse.jgit.lib.Ref;
60  import org.eclipse.jgit.lib.TextProgressMonitor;
61  import org.eclipse.jgit.pgm.internal.CLIText;
62  import org.eclipse.jgit.transport.PushResult;
63  import org.eclipse.jgit.transport.RefSpec;
64  import org.eclipse.jgit.transport.RemoteRefUpdate;
65  import org.eclipse.jgit.transport.RemoteRefUpdate.Status;
66  import org.eclipse.jgit.transport.Transport;
67  import org.eclipse.jgit.transport.URIish;
68  import org.kohsuke.args4j.Argument;
69  import org.kohsuke.args4j.Option;
70  
71  @Command(common = true, usage = "usage_UpdateRemoteRepositoryFromLocalRefs")
72  class Push extends TextBuiltin {
73  	@Option(name = "--timeout", metaVar = "metaVar_seconds", usage = "usage_abortConnectionIfNoActivity")
74  	int timeout = -1;
75  
76  	@Argument(index = 0, metaVar = "metaVar_uriish")
77  	private String remote = Constants.DEFAULT_REMOTE_NAME;
78  
79  	@Argument(index = 1, metaVar = "metaVar_refspec")
80  	private final List<RefSpec> refSpecs = new ArrayList<RefSpec>();
81  
82  	@Option(name = "--all")
83  	private boolean all;
84  
85  	@Option(name = "--tags")
86  	private boolean tags;
87  
88  	@Option(name = "--verbose", aliases = { "-v" })
89  	private boolean verbose = false;
90  
91  	@Option(name = "--thin")
92  	private boolean thin = Transport.DEFAULT_PUSH_THIN;
93  
94  	@Option(name = "--no-thin")
95  	void nothin(@SuppressWarnings("unused") final boolean ignored) {
96  		thin = false;
97  	}
98  
99  	@Option(name = "--force", aliases = { "-f" })
100 	private boolean force;
101 
102 	@Option(name = "--receive-pack", metaVar = "metaVar_path")
103 	private String receivePack;
104 
105 	@Option(name = "--dry-run")
106 	private boolean dryRun;
107 
108 	private boolean shownURI;
109 
110 	@Override
111 	protected void run() throws Exception {
112 		Git git = new Git(db);
113 		PushCommand push = git.push();
114 		push.setDryRun(dryRun);
115 		push.setForce(force);
116 		push.setProgressMonitor(new TextProgressMonitor(errw));
117 		push.setReceivePack(receivePack);
118 		push.setRefSpecs(refSpecs);
119 		if (all)
120 			push.setPushAll();
121 		if (tags)
122 			push.setPushTags();
123 		push.setRemote(remote);
124 		push.setThin(thin);
125 		push.setTimeout(timeout);
126 		Iterable<PushResult> results = push.call();
127 		for (PushResult result : results) {
128 			try (ObjectReader reader = db.newObjectReader()) {
129 				printPushResult(reader, result.getURI(), result);
130 			}
131 		}
132 	}
133 
134 	private void printPushResult(final ObjectReader reader, final URIish uri,
135 			final PushResult result) throws IOException {
136 		shownURI = false;
137 		boolean everythingUpToDate = true;
138 
139 		// at first, print up-to-date ones...
140 		for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
141 			if (rru.getStatus() == Status.UP_TO_DATE) {
142 				if (verbose)
143 					printRefUpdateResult(reader, uri, result, rru);
144 			} else
145 				everythingUpToDate = false;
146 		}
147 
148 		for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
149 			// ...then successful updates...
150 			if (rru.getStatus() == Status.OK)
151 				printRefUpdateResult(reader, uri, result, rru);
152 		}
153 
154 		for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
155 			// ...finally, others (problematic)
156 			if (rru.getStatus() != Status.OK
157 					&& rru.getStatus() != Status.UP_TO_DATE)
158 				printRefUpdateResult(reader, uri, result, rru);
159 		}
160 
161 		AbstractFetchCommand.showRemoteMessages(errw, result.getMessages());
162 		if (everythingUpToDate)
163 			outw.println(CLIText.get().everythingUpToDate);
164 	}
165 
166 	private void printRefUpdateResult(final ObjectReader reader,
167 			final URIish uri, final PushResult result, final RemoteRefUpdate rru)
168 			throws IOException {
169 		if (!shownURI) {
170 			shownURI = true;
171 			outw.println(MessageFormat.format(CLIText.get().pushTo, uri));
172 		}
173 
174 		final String remoteName = rru.getRemoteName();
175 		final String srcRef = rru.isDelete() ? null : rru.getSrcRef();
176 
177 		switch (rru.getStatus()) {
178 		case OK:
179 			if (rru.isDelete())
180 				printUpdateLine('-', "[deleted]", null, remoteName, null);
181 			else {
182 				final Ref oldRef = result.getAdvertisedRef(remoteName);
183 				if (oldRef == null) {
184 					final String summary;
185 					if (remoteName.startsWith(Constants.R_TAGS))
186 						summary = "[new tag]";
187 					else
188 						summary = "[new branch]";
189 					printUpdateLine('*', summary, srcRef, remoteName, null);
190 				} else {
191 					boolean fastForward = rru.isFastForward();
192 					final char flag = fastForward ? ' ' : '+';
193 					final String summary = safeAbbreviate(reader, oldRef
194 							.getObjectId())
195 							+ (fastForward ? ".." : "...") //$NON-NLS-1$ //$NON-NLS-2$
196 							+ safeAbbreviate(reader, rru.getNewObjectId());
197 					final String message = fastForward ? null : CLIText.get().forcedUpdate;
198 					printUpdateLine(flag, summary, srcRef, remoteName, message);
199 				}
200 			}
201 			break;
202 
203 		case NON_EXISTING:
204 			printUpdateLine('X', "[no match]", null, remoteName, null);
205 			break;
206 
207 		case REJECTED_NODELETE:
208 			printUpdateLine('!', "[rejected]", null, remoteName,
209 					CLIText.get().remoteSideDoesNotSupportDeletingRefs);
210 			break;
211 
212 		case REJECTED_NONFASTFORWARD:
213 			printUpdateLine('!', "[rejected]", srcRef, remoteName,
214 					CLIText.get().nonFastForward);
215 			break;
216 
217 		case REJECTED_REMOTE_CHANGED:
218 			final String message = MessageFormat.format(
219 					CLIText.get().remoteRefObjectChangedIsNotExpectedOne,
220 					safeAbbreviate(reader, rru.getExpectedOldObjectId()));
221 			printUpdateLine('!', "[rejected]", srcRef, remoteName, message);
222 			break;
223 
224 		case REJECTED_OTHER_REASON:
225 			printUpdateLine('!', "[remote rejected]", srcRef, remoteName, rru
226 					.getMessage());
227 			break;
228 
229 		case UP_TO_DATE:
230 			if (verbose)
231 				printUpdateLine('=', "[up to date]", srcRef, remoteName, null);
232 			break;
233 
234 		case NOT_ATTEMPTED:
235 		case AWAITING_REPORT:
236 			printUpdateLine('?', "[unexpected push-process behavior]", srcRef,
237 					remoteName, rru.getMessage());
238 			break;
239 		}
240 	}
241 
242 	private static String safeAbbreviate(ObjectReader reader, ObjectId id) {
243 		try {
244 			return reader.abbreviate(id).name();
245 		} catch (IOException cannotAbbreviate) {
246 			return id.name();
247 		}
248 	}
249 
250 	private void printUpdateLine(final char flag, final String summary,
251 			final String srcRef, final String destRef, final String message)
252 			throws IOException {
253 		outw.format(" %c %-17s", valueOf(flag), summary); //$NON-NLS-1$
254 
255 		if (srcRef != null)
256 			outw.format(" %s ->", abbreviateRef(srcRef, true)); //$NON-NLS-1$
257 		outw.format(" %s", abbreviateRef(destRef, true)); //$NON-NLS-1$
258 
259 		if (message != null)
260 			outw.format(" (%s)", message); //$NON-NLS-1$
261 
262 		outw.println();
263 	}
264 }