1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
71
72
73
74
75
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,
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]";
144 if (r == RefUpdate.Result.IO_FAILURE)
145 return "[i/o error]";
146 if (r == RefUpdate.Result.REJECTED)
147 return "[rejected]";
148 if (ObjectId.zeroId().equals(u.getNewObjectId()))
149 return "[deleted]";
150
151 if (r == RefUpdate.Result.NEW) {
152 if (u.getRemoteName().startsWith(Constants.R_HEADS))
153 return "[new branch]";
154 else if (u.getLocalName().startsWith(Constants.R_TAGS))
155 return "[new tag]";
156 return "[new]";
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;
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;
169 }
170
171 if (r == RefUpdate.Result.NO_CHANGE)
172 return "[up to date]";
173 return "[" + r.name() + "]";
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 }