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  	protected void showFetchResult(final FetchResult r) throws IOException {
70  		try (ObjectReader reader = db.newObjectReader()) {
71  			boolean shownURI = false;
72  			for (final TrackingRefUpdate u : r.getTrackingRefUpdates()) {
73  				if (!verbose && u.getResult() == RefUpdate.Result.NO_CHANGE)
74  					continue;
75  
76  				final char type = shortTypeOf(u.getResult());
77  				final String longType = longTypeOf(reader, u);
78  				final String src = abbreviateRef(u.getRemoteName(), false);
79  				final String dst = abbreviateRef(u.getLocalName(), true);
80  
81  				if (!shownURI) {
82  					outw.println(MessageFormat.format(CLIText.get().fromURI,
83  							r.getURI()));
84  					shownURI = true;
85  				}
86  
87  				outw.format(" %c %-17s %-10s -> %s", valueOf(type), longType, //$NON-NLS-1$
88  						src, dst);
89  				outw.println();
90  			}
91  		}
92  		showRemoteMessages(errw, r.getMessages());
93  	}
94  
95  	static void showRemoteMessages(ThrowingPrintWriter writer, String pkt) throws IOException {
96  		while (0 < pkt.length()) {
97  			final int lf = pkt.indexOf('\n');
98  			final int cr = pkt.indexOf('\r');
99  			final int s;
100 			if (0 <= lf && 0 <= cr)
101 				s = Math.min(lf, cr);
102 			else if (0 <= lf)
103 				s = lf;
104 			else if (0 <= cr)
105 				s = cr;
106 			else {
107 				writer.print(MessageFormat.format(CLIText.get().remoteMessage,
108 						pkt));
109 				writer.println();
110 				break;
111 			}
112 
113 			if (pkt.charAt(s) == '\r') {
114 				writer.print(MessageFormat.format(CLIText.get().remoteMessage,
115 						pkt.substring(0, s)));
116 				writer.print('\r');
117 			} else {
118 				writer.print(MessageFormat.format(CLIText.get().remoteMessage,
119 						pkt.substring(0, s)));
120 				writer.println();
121 			}
122 
123 			pkt = pkt.substring(s + 1);
124 		}
125 		writer.flush();
126 	}
127 
128 	private static String longTypeOf(ObjectReader reader,
129 			final TrackingRefUpdate u) {
130 		final RefUpdate.Result r = u.getResult();
131 		if (r == RefUpdate.Result.LOCK_FAILURE)
132 			return "[lock fail]";
133 		if (r == RefUpdate.Result.IO_FAILURE)
134 			return "[i/o error]";
135 		if (r == RefUpdate.Result.REJECTED)
136 			return "[rejected]";
137 		if (ObjectId.zeroId().equals(u.getNewObjectId()))
138 			return "[deleted]";
139 
140 		if (r == RefUpdate.Result.NEW) {
141 			if (u.getRemoteName().startsWith(Constants.R_HEADS))
142 				return "[new branch]";
143 			else if (u.getLocalName().startsWith(Constants.R_TAGS))
144 				return "[new tag]";
145 			return "[new]";
146 		}
147 
148 		if (r == RefUpdate.Result.FORCED) {
149 			final String aOld = safeAbbreviate(reader, u.getOldObjectId());
150 			final String aNew = safeAbbreviate(reader, u.getNewObjectId());
151 			return aOld + "..." + aNew; //$NON-NLS-1$
152 		}
153 
154 		if (r == RefUpdate.Result.FAST_FORWARD) {
155 			final String aOld = safeAbbreviate(reader, u.getOldObjectId());
156 			final String aNew = safeAbbreviate(reader, u.getNewObjectId());
157 			return aOld + ".." + aNew; //$NON-NLS-1$
158 		}
159 
160 		if (r == RefUpdate.Result.NO_CHANGE)
161 			return "[up to date]";
162 		return "[" + r.name() + "]"; //$NON-NLS-1$//$NON-NLS-2$
163 	}
164 
165 	private static String safeAbbreviate(ObjectReader reader, ObjectId id) {
166 		try {
167 			return reader.abbreviate(id).name();
168 		} catch (IOException cannotAbbreviate) {
169 			return id.name();
170 		}
171 	}
172 
173 	private static char shortTypeOf(final RefUpdate.Result r) {
174 		if (r == RefUpdate.Result.LOCK_FAILURE)
175 			return '!';
176 		if (r == RefUpdate.Result.IO_FAILURE)
177 			return '!';
178 		if (r == RefUpdate.Result.NEW)
179 			return '*';
180 		if (r == RefUpdate.Result.FORCED)
181 			return '+';
182 		if (r == RefUpdate.Result.FAST_FORWARD)
183 			return ' ';
184 		if (r == RefUpdate.Result.REJECTED)
185 			return '!';
186 		if (r == RefUpdate.Result.NO_CHANGE)
187 			return '=';
188 		return ' ';
189 	}
190 }