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 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 = "--atomic")
86 private boolean atomic;
87
88 @Option(name = "--tags")
89 private boolean tags;
90
91 @Option(name = "--verbose", aliases = { "-v" })
92 private boolean verbose = false;
93
94 @Option(name = "--thin")
95 private boolean thin = Transport.DEFAULT_PUSH_THIN;
96
97 @Option(name = "--no-thin")
98 void nothin(@SuppressWarnings("unused") final boolean ignored) {
99 thin = false;
100 }
101
102 @Option(name = "--force", aliases = { "-f" })
103 private boolean force;
104
105 @Option(name = "--receive-pack", metaVar = "metaVar_path")
106 private String receivePack;
107
108 @Option(name = "--dry-run")
109 private boolean dryRun;
110
111 @Option(name = "--push-option", aliases = { "-t" })
112 private List<String> pushOptions = new ArrayList<>();
113
114 private boolean shownURI;
115
116 @Override
117 protected void run() throws Exception {
118 try (Git git = new Git(db)) {
119 PushCommand push = git.push();
120 push.setDryRun(dryRun);
121 push.setForce(force);
122 push.setProgressMonitor(new TextProgressMonitor(errw));
123 push.setReceivePack(receivePack);
124 push.setRefSpecs(refSpecs);
125 if (all)
126 push.setPushAll();
127 if (tags)
128 push.setPushTags();
129 push.setRemote(remote);
130 push.setThin(thin);
131 push.setAtomic(atomic);
132 push.setTimeout(timeout);
133 if (!pushOptions.isEmpty()) {
134 push.setPushOptions(pushOptions);
135 }
136 Iterable<PushResult> results = push.call();
137 for (PushResult result : results) {
138 try (ObjectReader reader = db.newObjectReader()) {
139 printPushResult(reader, result.getURI(), result);
140 }
141 }
142 }
143 }
144
145 private void printPushResult(final ObjectReader reader, final URIish uri,
146 final PushResult result) throws IOException {
147 shownURI = false;
148 boolean everythingUpToDate = true;
149
150
151 for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
152 if (rru.getStatus() == Status.UP_TO_DATE) {
153 if (verbose)
154 printRefUpdateResult(reader, uri, result, rru);
155 } else
156 everythingUpToDate = false;
157 }
158
159 for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
160
161 if (rru.getStatus() == Status.OK)
162 printRefUpdateResult(reader, uri, result, rru);
163 }
164
165 for (final RemoteRefUpdate rru : result.getRemoteUpdates()) {
166
167 if (rru.getStatus() != Status.OK
168 && rru.getStatus() != Status.UP_TO_DATE)
169 printRefUpdateResult(reader, uri, result, rru);
170 }
171
172 AbstractFetchCommand.showRemoteMessages(errw, result.getMessages());
173 if (everythingUpToDate)
174 outw.println(CLIText.get().everythingUpToDate);
175 }
176
177 private void printRefUpdateResult(final ObjectReader reader,
178 final URIish uri, final PushResult result, final RemoteRefUpdate rru)
179 throws IOException {
180 if (!shownURI) {
181 shownURI = true;
182 outw.println(MessageFormat.format(CLIText.get().pushTo, uri));
183 }
184
185 final String remoteName = rru.getRemoteName();
186 final String srcRef = rru.isDelete() ? null : rru.getSrcRef();
187
188 switch (rru.getStatus()) {
189 case OK:
190 if (rru.isDelete())
191 printUpdateLine('-', "[deleted]", null, remoteName, null);
192 else {
193 final Ref oldRef = result.getAdvertisedRef(remoteName);
194 if (oldRef == null) {
195 final String summary;
196 if (remoteName.startsWith(Constants.R_TAGS))
197 summary = "[new tag]";
198 else
199 summary = "[new branch]";
200 printUpdateLine('*', summary, srcRef, remoteName, null);
201 } else {
202 boolean fastForward = rru.isFastForward();
203 final char flag = fastForward ? ' ' : '+';
204 final String summary = safeAbbreviate(reader, oldRef
205 .getObjectId())
206 + (fastForward ? ".." : "...")
207 + safeAbbreviate(reader, rru.getNewObjectId());
208 final String message = fastForward ? null : CLIText.get().forcedUpdate;
209 printUpdateLine(flag, summary, srcRef, remoteName, message);
210 }
211 }
212 break;
213
214 case NON_EXISTING:
215 printUpdateLine('X', "[no match]", null, remoteName, null);
216 break;
217
218 case REJECTED_NODELETE:
219 printUpdateLine('!', "[rejected]", null, remoteName,
220 CLIText.get().remoteSideDoesNotSupportDeletingRefs);
221 break;
222
223 case REJECTED_NONFASTFORWARD:
224 printUpdateLine('!', "[rejected]", srcRef, remoteName,
225 CLIText.get().nonFastForward);
226 break;
227
228 case REJECTED_REMOTE_CHANGED:
229 final String message = MessageFormat.format(
230 CLIText.get().remoteRefObjectChangedIsNotExpectedOne,
231 safeAbbreviate(reader, rru.getExpectedOldObjectId()));
232 printUpdateLine('!', "[rejected]", srcRef, remoteName, message);
233 break;
234
235 case REJECTED_OTHER_REASON:
236 printUpdateLine('!', "[remote rejected]", srcRef, remoteName, rru
237 .getMessage());
238 break;
239
240 case UP_TO_DATE:
241 if (verbose)
242 printUpdateLine('=', "[up to date]", srcRef, remoteName, null);
243 break;
244
245 case NOT_ATTEMPTED:
246 case AWAITING_REPORT:
247 printUpdateLine('?', "[unexpected push-process behavior]", srcRef,
248 remoteName, rru.getMessage());
249 break;
250 }
251 }
252
253 private static String safeAbbreviate(ObjectReader reader, ObjectId id) {
254 try {
255 return reader.abbreviate(id).name();
256 } catch (IOException cannotAbbreviate) {
257 return id.name();
258 }
259 }
260
261 private void printUpdateLine(final char flag, final String summary,
262 final String srcRef, final String destRef, final String message)
263 throws IOException {
264 outw.format(" %c %-17s", valueOf(flag), summary);
265
266 if (srcRef != null)
267 outw.format(" %s ->", abbreviateRef(srcRef, true));
268 outw.format(" %s", abbreviateRef(destRef, true));
269
270 if (message != null)
271 outw.format(" (%s)", message);
272
273 outw.println();
274 }
275 }