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