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> and others
7    *
8    * This program and the accompanying materials are made available under the
9    * terms of the Eclipse Distribution License v. 1.0 which is available at
10   * https://www.eclipse.org/org/documents/edl-v10.php.
11   *
12   * SPDX-License-Identifier: BSD-3-Clause
13   */
14  
15  package org.eclipse.jgit.pgm;
16  
17  import static java.lang.Character.valueOf;
18  
19  import java.io.IOException;
20  import java.text.MessageFormat;
21  
22  import org.eclipse.jgit.lib.Constants;
23  import org.eclipse.jgit.lib.ObjectId;
24  import org.eclipse.jgit.lib.ObjectReader;
25  import org.eclipse.jgit.lib.RefUpdate;
26  import org.eclipse.jgit.pgm.internal.CLIText;
27  import org.eclipse.jgit.transport.FetchResult;
28  import org.eclipse.jgit.transport.TrackingRefUpdate;
29  import org.eclipse.jgit.util.io.ThrowingPrintWriter;
30  import org.kohsuke.args4j.Option;
31  
32  abstract class AbstractFetchCommand extends TextBuiltin {
33  	@Option(name = "--verbose", aliases = { "-v" }, usage = "usage_beMoreVerbose")
34  	private boolean verbose;
35  
36  	/**
37  	 * Show fetch result.
38  	 *
39  	 * @param r
40  	 *            a {@link org.eclipse.jgit.transport.FetchResult} object.
41  	 * @throws java.io.IOException
42  	 *             if any.
43  	 */
44  	protected void showFetchResult(FetchResult r) throws IOException {
45  		try (ObjectReader reader = db.newObjectReader()) {
46  			boolean shownURI = false;
47  			for (TrackingRefUpdate u : r.getTrackingRefUpdates()) {
48  				if (!verbose && u.getResult() == RefUpdate.Result.NO_CHANGE)
49  					continue;
50  
51  				final char type = shortTypeOf(u.getResult());
52  				final String longType = longTypeOf(reader, u);
53  				final String src = abbreviateRef(u.getRemoteName(), false);
54  				final String dst = abbreviateRef(u.getLocalName(), true);
55  
56  				if (!shownURI) {
57  					outw.println(MessageFormat.format(CLIText.get().fromURI,
58  							r.getURI()));
59  					shownURI = true;
60  				}
61  
62  				outw.format(" %c %-17s %-10s -> %s", valueOf(type), longType, //$NON-NLS-1$
63  						src, dst);
64  				outw.println();
65  			}
66  		}
67  		showRemoteMessages(errw, r.getMessages());
68  		for (FetchResult submoduleResult : r.submoduleResults().values()) {
69  			showFetchResult(submoduleResult);
70  		}
71  	}
72  
73  	static void showRemoteMessages(ThrowingPrintWriter writer, String pkt) throws IOException {
74  		while (0 < pkt.length()) {
75  			final int lf = pkt.indexOf('\n');
76  			final int cr = pkt.indexOf('\r');
77  			final int s;
78  			if (0 <= lf && 0 <= cr)
79  				s = Math.min(lf, cr);
80  			else if (0 <= lf)
81  				s = lf;
82  			else if (0 <= cr)
83  				s = cr;
84  			else {
85  				writer.print(MessageFormat.format(CLIText.get().remoteMessage,
86  						pkt));
87  				writer.println();
88  				break;
89  			}
90  
91  			if (pkt.charAt(s) == '\r') {
92  				writer.print(MessageFormat.format(CLIText.get().remoteMessage,
93  						pkt.substring(0, s)));
94  				writer.print('\r');
95  			} else {
96  				writer.print(MessageFormat.format(CLIText.get().remoteMessage,
97  						pkt.substring(0, s)));
98  				writer.println();
99  			}
100 
101 			pkt = pkt.substring(s + 1);
102 		}
103 		writer.flush();
104 	}
105 
106 	private static String longTypeOf(ObjectReader reader,
107 			final TrackingRefUpdate u) {
108 		final RefUpdate.Result r = u.getResult();
109 		if (r == RefUpdate.Result.LOCK_FAILURE)
110 			return "[lock fail]"; //$NON-NLS-1$
111 		if (r == RefUpdate.Result.IO_FAILURE)
112 			return "[i/o error]"; //$NON-NLS-1$
113 		if (r == RefUpdate.Result.REJECTED)
114 			return "[rejected]"; //$NON-NLS-1$
115 		if (ObjectId.zeroId().equals(u.getNewObjectId()))
116 			return "[deleted]"; //$NON-NLS-1$
117 
118 		if (r == RefUpdate.Result.NEW) {
119 			if (u.getRemoteName().startsWith(Constants.R_HEADS))
120 				return "[new branch]"; //$NON-NLS-1$
121 			else if (u.getLocalName().startsWith(Constants.R_TAGS))
122 				return "[new tag]"; //$NON-NLS-1$
123 			return "[new]"; //$NON-NLS-1$
124 		}
125 
126 		if (r == RefUpdate.Result.FORCED) {
127 			final String aOld = safeAbbreviate(reader, u.getOldObjectId());
128 			final String aNew = safeAbbreviate(reader, u.getNewObjectId());
129 			return aOld + "..." + aNew; //$NON-NLS-1$
130 		}
131 
132 		if (r == RefUpdate.Result.FAST_FORWARD) {
133 			final String aOld = safeAbbreviate(reader, u.getOldObjectId());
134 			final String aNew = safeAbbreviate(reader, u.getNewObjectId());
135 			return aOld + ".." + aNew; //$NON-NLS-1$
136 		}
137 
138 		if (r == RefUpdate.Result.NO_CHANGE)
139 			return "[up to date]"; //$NON-NLS-1$
140 		return "[" + r.name() + "]"; //$NON-NLS-1$//$NON-NLS-2$
141 	}
142 
143 	private static String safeAbbreviate(ObjectReader reader, ObjectId id) {
144 		try {
145 			return reader.abbreviate(id).name();
146 		} catch (IOException cannotAbbreviate) {
147 			return id.name();
148 		}
149 	}
150 
151 	private static char shortTypeOf(RefUpdate.Result r) {
152 		if (r == RefUpdate.Result.LOCK_FAILURE)
153 			return '!';
154 		if (r == RefUpdate.Result.IO_FAILURE)
155 			return '!';
156 		if (r == RefUpdate.Result.NEW)
157 			return '*';
158 		if (r == RefUpdate.Result.FORCED)
159 			return '+';
160 		if (r == RefUpdate.Result.FAST_FORWARD)
161 			return ' ';
162 		if (r == RefUpdate.Result.REJECTED)
163 			return '!';
164 		if (r == RefUpdate.Result.NO_CHANGE)
165 			return '=';
166 		return ' ';
167 	}
168 }