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