View Javadoc
1   /*
2    * Copyright (C) 2008, Charles O'Farrell <charleso@charleso.org>
3    * Copyright (C) 2008-2010, Google Inc.
4    * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
5    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
6    * Copyright (C) 2010, Sasa Zivkov <sasa.zivkov@sap.com>
7    * and other copyright owners as documented in the project's IP log.
8    *
9    * This program and the accompanying materials are made available
10   * under the terms of the Eclipse Distribution License v1.0 which
11   * accompanies this distribution, is reproduced below, and is
12   * available at http://www.eclipse.org/org/documents/edl-v10.php
13   *
14   * All rights reserved.
15   *
16   * Redistribution and use in source and binary forms, with or
17   * without modification, are permitted provided that the following
18   * conditions are met:
19   *
20   * - Redistributions of source code must retain the above copyright
21   *   notice, this list of conditions and the following disclaimer.
22   *
23   * - Redistributions in binary form must reproduce the above
24   *   copyright notice, this list of conditions and the following
25   *   disclaimer in the documentation and/or other materials provided
26   *   with the distribution.
27   *
28   * - Neither the name of the Eclipse Foundation, Inc. nor the
29   *   names of its contributors may be used to endorse or promote
30   *   products derived from this software without specific prior
31   *   written permission.
32   *
33   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
34   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
35   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
38   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
40   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
42   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
45   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46   */
47  
48  package org.eclipse.jgit.pgm;
49  
50  import static java.lang.Character.valueOf;
51  
52  import java.io.IOException;
53  import java.text.MessageFormat;
54  
55  import org.eclipse.jgit.lib.Constants;
56  import org.eclipse.jgit.lib.ObjectId;
57  import org.eclipse.jgit.lib.ObjectReader;
58  import org.eclipse.jgit.lib.RefUpdate;
59  import org.eclipse.jgit.pgm.internal.CLIText;
60  import org.eclipse.jgit.transport.FetchResult;
61  import org.eclipse.jgit.transport.TrackingRefUpdate;
62  import org.eclipse.jgit.util.io.ThrowingPrintWriter;
63  import org.kohsuke.args4j.Option;
64  
65  abstract class AbstractFetchCommand extends TextBuiltin {
66  	@Option(name = "--verbose", aliases = { "-v" }, usage = "usage_beMoreVerbose")
67  	private boolean verbose;
68  
69  	/**
70  	 * Show fetch result.
71  	 *
72  	 * @param r
73  	 *            a {@link org.eclipse.jgit.transport.FetchResult} object.
74  	 * @throws java.io.IOException
75  	 *             if any.
76  	 */
77  	protected void showFetchResult(FetchResult r) throws IOException {
78  		try (ObjectReader reader = db.newObjectReader()) {
79  			boolean shownURI = false;
80  			for (TrackingRefUpdate u : r.getTrackingRefUpdates()) {
81  				if (!verbose && u.getResult() == RefUpdate.Result.NO_CHANGE)
82  					continue;
83  
84  				final char type = shortTypeOf(u.getResult());
85  				final String longType = longTypeOf(reader, u);
86  				final String src = abbreviateRef(u.getRemoteName(), false);
87  				final String dst = abbreviateRef(u.getLocalName(), true);
88  
89  				if (!shownURI) {
90  					outw.println(MessageFormat.format(CLIText.get().fromURI,
91  							r.getURI()));
92  					shownURI = true;
93  				}
94  
95  				outw.format(" %c %-17s %-10s -> %s", valueOf(type), longType, //$NON-NLS-1$
96  						src, dst);
97  				outw.println();
98  			}
99  		}
100 		showRemoteMessages(errw, r.getMessages());
101 		for (FetchResult submoduleResult : r.submoduleResults().values()) {
102 			showFetchResult(submoduleResult);
103 		}
104 	}
105 
106 	static void showRemoteMessages(ThrowingPrintWriter writer, String pkt) throws IOException {
107 		while (0 < pkt.length()) {
108 			final int lf = pkt.indexOf('\n');
109 			final int cr = pkt.indexOf('\r');
110 			final int s;
111 			if (0 <= lf && 0 <= cr)
112 				s = Math.min(lf, cr);
113 			else if (0 <= lf)
114 				s = lf;
115 			else if (0 <= cr)
116 				s = cr;
117 			else {
118 				writer.print(MessageFormat.format(CLIText.get().remoteMessage,
119 						pkt));
120 				writer.println();
121 				break;
122 			}
123 
124 			if (pkt.charAt(s) == '\r') {
125 				writer.print(MessageFormat.format(CLIText.get().remoteMessage,
126 						pkt.substring(0, s)));
127 				writer.print('\r');
128 			} else {
129 				writer.print(MessageFormat.format(CLIText.get().remoteMessage,
130 						pkt.substring(0, s)));
131 				writer.println();
132 			}
133 
134 			pkt = pkt.substring(s + 1);
135 		}
136 		writer.flush();
137 	}
138 
139 	private static String longTypeOf(ObjectReader reader,
140 			final TrackingRefUpdate u) {
141 		final RefUpdate.Result r = u.getResult();
142 		if (r == RefUpdate.Result.LOCK_FAILURE)
143 			return "[lock fail]"; //$NON-NLS-1$
144 		if (r == RefUpdate.Result.IO_FAILURE)
145 			return "[i/o error]"; //$NON-NLS-1$
146 		if (r == RefUpdate.Result.REJECTED)
147 			return "[rejected]"; //$NON-NLS-1$
148 		if (ObjectId.zeroId().equals(u.getNewObjectId()))
149 			return "[deleted]"; //$NON-NLS-1$
150 
151 		if (r == RefUpdate.Result.NEW) {
152 			if (u.getRemoteName().startsWith(Constants.R_HEADS))
153 				return "[new branch]"; //$NON-NLS-1$
154 			else if (u.getLocalName().startsWith(Constants.R_TAGS))
155 				return "[new tag]"; //$NON-NLS-1$
156 			return "[new]"; //$NON-NLS-1$
157 		}
158 
159 		if (r == RefUpdate.Result.FORCED) {
160 			final String aOld = safeAbbreviate(reader, u.getOldObjectId());
161 			final String aNew = safeAbbreviate(reader, u.getNewObjectId());
162 			return aOld + "..." + aNew; //$NON-NLS-1$
163 		}
164 
165 		if (r == RefUpdate.Result.FAST_FORWARD) {
166 			final String aOld = safeAbbreviate(reader, u.getOldObjectId());
167 			final String aNew = safeAbbreviate(reader, u.getNewObjectId());
168 			return aOld + ".." + aNew; //$NON-NLS-1$
169 		}
170 
171 		if (r == RefUpdate.Result.NO_CHANGE)
172 			return "[up to date]"; //$NON-NLS-1$
173 		return "[" + r.name() + "]"; //$NON-NLS-1$//$NON-NLS-2$
174 	}
175 
176 	private static String safeAbbreviate(ObjectReader reader, ObjectId id) {
177 		try {
178 			return reader.abbreviate(id).name();
179 		} catch (IOException cannotAbbreviate) {
180 			return id.name();
181 		}
182 	}
183 
184 	private static char shortTypeOf(RefUpdate.Result r) {
185 		if (r == RefUpdate.Result.LOCK_FAILURE)
186 			return '!';
187 		if (r == RefUpdate.Result.IO_FAILURE)
188 			return '!';
189 		if (r == RefUpdate.Result.NEW)
190 			return '*';
191 		if (r == RefUpdate.Result.FORCED)
192 			return '+';
193 		if (r == RefUpdate.Result.FAST_FORWARD)
194 			return ' ';
195 		if (r == RefUpdate.Result.REJECTED)
196 			return '!';
197 		if (r == RefUpdate.Result.NO_CHANGE)
198 			return '=';
199 		return ' ';
200 	}
201 }