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.transport;
46
47 import static java.nio.charset.StandardCharsets.UTF_8;
48 import static org.eclipse.jgit.transport.ReceiveCommand.Result.NOT_ATTEMPTED;
49 import static org.eclipse.jgit.transport.ReceiveCommand.Result.OK;
50 import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_NONFASTFORWARD;
51 import static org.eclipse.jgit.transport.ReceiveCommand.Type.UPDATE_NONFASTFORWARD;
52
53 import java.io.File;
54 import java.io.IOException;
55 import java.io.OutputStreamWriter;
56 import java.io.Writer;
57 import java.text.MessageFormat;
58 import java.util.ArrayList;
59 import java.util.Collection;
60 import java.util.Collections;
61 import java.util.HashMap;
62 import java.util.HashSet;
63 import java.util.Iterator;
64 import java.util.Map;
65 import java.util.Set;
66 import java.util.concurrent.TimeUnit;
67
68 import org.eclipse.jgit.errors.MissingObjectException;
69 import org.eclipse.jgit.errors.NotSupportedException;
70 import org.eclipse.jgit.errors.TransportException;
71 import org.eclipse.jgit.internal.JGitText;
72 import org.eclipse.jgit.internal.storage.file.LockFile;
73 import org.eclipse.jgit.internal.storage.file.PackLock;
74 import org.eclipse.jgit.lib.BatchRefUpdate;
75 import org.eclipse.jgit.lib.BatchingProgressMonitor;
76 import org.eclipse.jgit.lib.Constants;
77 import org.eclipse.jgit.lib.ObjectId;
78 import org.eclipse.jgit.lib.ObjectIdRef;
79 import org.eclipse.jgit.lib.ProgressMonitor;
80 import org.eclipse.jgit.lib.Ref;
81 import org.eclipse.jgit.lib.RefDatabase;
82 import org.eclipse.jgit.revwalk.ObjectWalk;
83 import org.eclipse.jgit.revwalk.RevWalk;
84
85 class FetchProcess {
86
87 private final Transport transport;
88
89
90 private final Collection<RefSpec> toFetch;
91
92
93 private final HashMap<ObjectId, Ref> askFor = new HashMap<>();
94
95
96 private final HashSet<ObjectId> have = new HashSet<>();
97
98
99 private final ArrayList<TrackingRefUpdate> localUpdates = new ArrayList<>();
100
101
102 private final ArrayList<FetchHeadRecord> fetchHeadUpdates = new ArrayList<>();
103
104 private final ArrayList<PackLock> packLocks = new ArrayList<>();
105
106 private FetchConnection conn;
107
108 private Map<String, Ref> localRefs;
109
110 FetchProcess(Transport t, Collection<RefSpec> f) {
111 transport = t;
112 toFetch = f;
113 }
114
115 void execute(ProgressMonitor monitor, FetchResult result)
116 throws NotSupportedException, TransportException {
117 askFor.clear();
118 localUpdates.clear();
119 fetchHeadUpdates.clear();
120 packLocks.clear();
121 localRefs = null;
122
123 try {
124 executeImp(monitor, result);
125 } finally {
126 try {
127 for (PackLock lock : packLocks)
128 lock.unlock();
129 } catch (IOException e) {
130 throw new TransportException(e.getMessage(), e);
131 }
132 }
133 }
134
135 private void executeImp(final ProgressMonitor monitor,
136 final FetchResult result) throws NotSupportedException,
137 TransportException {
138 conn = transport.openFetch();
139 try {
140 result.setAdvertisedRefs(transport.getURI(), conn.getRefsMap());
141 result.peerUserAgent = conn.getPeerUserAgent();
142 final Set<Ref> matched = new HashSet<>();
143 for (RefSpec spec : toFetch) {
144 if (spec.getSource() == null)
145 throw new TransportException(MessageFormat.format(
146 JGitText.get().sourceRefNotSpecifiedForRefspec, spec));
147
148 if (spec.isWildcard())
149 expandWildcard(spec, matched);
150 else
151 expandSingle(spec, matched);
152 }
153
154 Collection<Ref> additionalTags = Collections.<Ref> emptyList();
155 final TagOpt tagopt = transport.getTagOpt();
156 if (tagopt == TagOpt.AUTO_FOLLOW)
157 additionalTags = expandAutoFollowTags();
158 else if (tagopt == TagOpt.FETCH_TAGS)
159 expandFetchTags();
160
161 final boolean includedTags;
162 if (!askFor.isEmpty() && !askForIsComplete()) {
163 fetchObjects(monitor);
164 includedTags = conn.didFetchIncludeTags();
165
166
167
168
169 closeConnection(result);
170 } else {
171 includedTags = false;
172 }
173
174 if (tagopt == TagOpt.AUTO_FOLLOW && !additionalTags.isEmpty()) {
175
176
177
178 have.addAll(askFor.keySet());
179 askFor.clear();
180 for (Ref r : additionalTags) {
181 ObjectId id = r.getPeeledObjectId();
182 if (id == null)
183 id = r.getObjectId();
184 if (transport.local.hasObject(id))
185 wantTag(r);
186 }
187
188 if (!askFor.isEmpty() && (!includedTags || !askForIsComplete())) {
189 reopenConnection();
190 if (!askFor.isEmpty())
191 fetchObjects(monitor);
192 }
193 }
194 } finally {
195 closeConnection(result);
196 }
197
198 BatchRefUpdate batch = transport.local.getRefDatabase()
199 .newBatchUpdate()
200 .setAllowNonFastForwards(true)
201 .setRefLogMessage("fetch", true);
202 try (RevWalkvWalk.html#RevWalk">RevWalk walk = new RevWalk(transport.local)) {
203 if (monitor instanceof BatchingProgressMonitor) {
204 ((BatchingProgressMonitor) monitor).setDelayStart(
205 250, TimeUnit.MILLISECONDS);
206 }
207 if (transport.isRemoveDeletedRefs()) {
208 deleteStaleTrackingRefs(result, batch);
209 }
210 addUpdateBatchCommands(result, batch);
211 for (ReceiveCommand cmd : batch.getCommands()) {
212 cmd.updateType(walk);
213 if (cmd.getType() == UPDATE_NONFASTFORWARD
214 && cmd instanceof TrackingRefUpdate.Command
215 && !((TrackingRefUpdate.Command) cmd).canForceUpdate())
216 cmd.setResult(REJECTED_NONFASTFORWARD);
217 }
218 if (transport.isDryRun()) {
219 for (ReceiveCommand cmd : batch.getCommands()) {
220 if (cmd.getResult() == NOT_ATTEMPTED)
221 cmd.setResult(OK);
222 }
223 } else {
224 batch.execute(walk, monitor);
225 }
226 } catch (TransportException e) {
227 throw e;
228 } catch (IOException err) {
229 throw new TransportException(MessageFormat.format(
230 JGitText.get().failureUpdatingTrackingRef,
231 getFirstFailedRefName(batch), err.getMessage()), err);
232 }
233
234 if (!fetchHeadUpdates.isEmpty()) {
235 try {
236 updateFETCH_HEAD(result);
237 } catch (IOException err) {
238 throw new TransportException(MessageFormat.format(
239 JGitText.get().failureUpdatingFETCH_HEAD, err.getMessage()), err);
240 }
241 }
242 }
243
244 private void addUpdateBatchCommands(FetchResult result,
245 BatchRefUpdate batch) throws TransportException {
246 Map<String, ObjectId> refs = new HashMap<>();
247 for (TrackingRefUpdate u : localUpdates) {
248
249 ObjectId existing = refs.get(u.getLocalName());
250 if (existing == null) {
251 refs.put(u.getLocalName(), u.getNewObjectId());
252 result.add(u);
253 batch.addCommand(u.asReceiveCommand());
254 } else if (!existing.equals(u.getNewObjectId())) {
255 throw new TransportException(MessageFormat
256 .format(JGitText.get().duplicateRef, u.getLocalName()));
257 }
258 }
259 }
260
261 private void fetchObjects(ProgressMonitor monitor)
262 throws TransportException {
263 try {
264 conn.setPackLockMessage("jgit fetch " + transport.uri);
265 conn.fetch(monitor, askFor.values(), have);
266 } finally {
267 packLocks.addAll(conn.getPackLocks());
268 }
269 if (transport.isCheckFetchedObjects()
270 && !conn.didFetchTestConnectivity() && !askForIsComplete())
271 throw new TransportException(transport.getURI(),
272 JGitText.get().peerDidNotSupplyACompleteObjectGraph);
273 }
274
275 private void closeConnection(FetchResult result) {
276 if (conn != null) {
277 conn.close();
278 result.addMessages(conn.getMessages());
279 conn = null;
280 }
281 }
282
283 private void reopenConnection() throws NotSupportedException,
284 TransportException {
285 if (conn != null)
286 return;
287
288 conn = transport.openFetch();
289
290
291
292
293
294
295
296
297
298 final HashMap<ObjectId, Ref> avail = new HashMap<>();
299 for (Ref r : conn.getRefs())
300 avail.put(r.getObjectId(), r);
301
302 final Collection<Ref> wants = new ArrayList<>(askFor.values());
303 askFor.clear();
304 for (Ref want : wants) {
305 final Ref newRef = avail.get(want.getObjectId());
306 if (newRef != null) {
307 askFor.put(newRef.getObjectId(), newRef);
308 } else {
309 removeFetchHeadRecord(want.getObjectId());
310 removeTrackingRefUpdate(want.getObjectId());
311 }
312 }
313 }
314
315 private void removeTrackingRefUpdate(ObjectId want) {
316 final Iterator<TrackingRefUpdate> i = localUpdates.iterator();
317 while (i.hasNext()) {
318 final TrackingRefUpdate u = i.next();
319 if (u.getNewObjectId().equals(want))
320 i.remove();
321 }
322 }
323
324 private void removeFetchHeadRecord(ObjectId want) {
325 final Iterator<FetchHeadRecord> i = fetchHeadUpdates.iterator();
326 while (i.hasNext()) {
327 final FetchHeadRecord fh = i.next();
328 if (fh.newValue.equals(want))
329 i.remove();
330 }
331 }
332
333 private void updateFETCH_HEAD(FetchResult result) throws IOException {
334 File meta = transport.local.getDirectory();
335 if (meta == null)
336 return;
337 final LockFileorage/file/LockFile.html#LockFile">LockFile lock = new LockFile(new File(meta, "FETCH_HEAD"));
338 try {
339 if (lock.lock()) {
340 try (Writer w = new OutputStreamWriter(
341 lock.getOutputStream(), UTF_8)) {
342 for (FetchHeadRecord h : fetchHeadUpdates) {
343 h.write(w);
344 result.add(h);
345 }
346 }
347 lock.commit();
348 }
349 } finally {
350 lock.unlock();
351 }
352 }
353
354 private boolean askForIsComplete() throws TransportException {
355 try {
356 try (ObjectWalkectWalk.html#ObjectWalk">ObjectWalk ow = new ObjectWalk(transport.local)) {
357 for (ObjectId want : askFor.keySet())
358 ow.markStart(ow.parseAny(want));
359 for (Ref ref : localRefs().values())
360 ow.markUninteresting(ow.parseAny(ref.getObjectId()));
361 ow.checkConnectivity();
362 }
363 return true;
364 } catch (MissingObjectException e) {
365 return false;
366 } catch (IOException e) {
367 throw new TransportException(JGitText.get().unableToCheckConnectivity, e);
368 }
369 }
370
371 private void expandWildcard(RefSpec spec, Set<Ref> matched)
372 throws TransportException {
373 for (Ref src : conn.getRefs()) {
374 if (spec.matchSource(src) && matched.add(src))
375 want(src, spec.expandFromSource(src));
376 }
377 }
378
379 private void expandSingle(RefSpec spec, Set<Ref> matched)
380 throws TransportException {
381 String want = spec.getSource();
382 if (ObjectId.isId(want)) {
383 want(ObjectId.fromString(want));
384 return;
385 }
386
387 Ref src = conn.getRef(want);
388 if (src == null) {
389 throw new TransportException(MessageFormat.format(JGitText.get().remoteDoesNotHaveSpec, want));
390 }
391 if (matched.add(src)) {
392 want(src, spec);
393 }
394 }
395
396 private Collection<Ref> expandAutoFollowTags() throws TransportException {
397 final Collection<Ref> additionalTags = new ArrayList<>();
398 final Map<String, Ref> haveRefs = localRefs();
399 for (Ref r : conn.getRefs()) {
400 if (!isTag(r))
401 continue;
402
403 Ref local = haveRefs.get(r.getName());
404 if (local != null)
405
406
407 continue;
408
409 ObjectId obj = r.getPeeledObjectId();
410 if (obj == null)
411 obj = r.getObjectId();
412
413 if (askFor.containsKey(obj) || transport.local.hasObject(obj))
414 wantTag(r);
415 else
416 additionalTags.add(r);
417 }
418 return additionalTags;
419 }
420
421 private void expandFetchTags() throws TransportException {
422 final Map<String, Ref> haveRefs = localRefs();
423 for (Ref r : conn.getRefs()) {
424 if (!isTag(r)) {
425 continue;
426 }
427 ObjectId id = r.getObjectId();
428 if (id == null) {
429 continue;
430 }
431 final Ref local = haveRefs.get(r.getName());
432 if (local == null || !id.equals(local.getObjectId())) {
433 wantTag(r);
434 }
435 }
436 }
437
438 private void wantTag(Ref r) throws TransportException {
439 want(r, new RefSpec().setSource(r.getName())
440 .setDestination(r.getName()).setForceUpdate(true));
441 }
442
443 private void want(Ref src, RefSpec spec)
444 throws TransportException {
445 final ObjectId newId = src.getObjectId();
446 if (newId == null) {
447 throw new NullPointerException(MessageFormat.format(
448 JGitText.get().transportProvidedRefWithNoObjectId,
449 src.getName()));
450 }
451 if (spec.getDestination() != null) {
452 final TrackingRefUpdate tru = createUpdate(spec, newId);
453 if (newId.equals(tru.getOldObjectId()))
454 return;
455 localUpdates.add(tru);
456 }
457
458 askFor.put(newId, src);
459
460 final FetchHeadRecordadRecord.html#FetchHeadRecord">FetchHeadRecord fhr = new FetchHeadRecord();
461 fhr.newValue = newId;
462 fhr.notForMerge = spec.getDestination() != null;
463 fhr.sourceName = src.getName();
464 fhr.sourceURI = transport.getURI();
465 fetchHeadUpdates.add(fhr);
466 }
467
468 private void want(ObjectId id) {
469 askFor.put(id,
470 new ObjectIdRef.Unpeeled(Ref.Storage.NETWORK, id.name(), id));
471 }
472
473 private TrackingRefUpdate createUpdate(RefSpec spec, ObjectId newId)
474 throws TransportException {
475 Ref ref = localRefs().get(spec.getDestination());
476 ObjectId oldId = ref != null && ref.getObjectId() != null
477 ? ref.getObjectId()
478 : ObjectId.zeroId();
479 return new TrackingRefUpdate(
480 spec.isForceUpdate(),
481 spec.getSource(),
482 spec.getDestination(),
483 oldId,
484 newId);
485 }
486
487 private Map<String, Ref> localRefs() throws TransportException {
488 if (localRefs == null) {
489 try {
490 localRefs = transport.local.getRefDatabase()
491 .getRefs(RefDatabase.ALL);
492 } catch (IOException err) {
493 throw new TransportException(JGitText.get().cannotListRefs, err);
494 }
495 }
496 return localRefs;
497 }
498
499 private void deleteStaleTrackingRefs(FetchResult result,
500 BatchRefUpdate batch) throws IOException {
501 Set<Ref> processed = new HashSet<>();
502 for (Ref ref : localRefs().values()) {
503 if (ref.isSymbolic()) {
504 continue;
505 }
506 String refname = ref.getName();
507 for (RefSpec spec : toFetch) {
508 if (spec.matchDestination(refname)) {
509 RefSpec s = spec.expandFromDestination(refname);
510 if (result.getAdvertisedRef(s.getSource()) == null
511 && processed.add(ref)) {
512 deleteTrackingRef(result, batch, s, ref);
513 }
514 }
515 }
516 }
517 }
518
519 private void deleteTrackingRef(final FetchResult result,
520 final BatchRefUpdate batch, final RefSpec spec, final Ref localRef) {
521 if (localRef.getObjectId() == null)
522 return;
523 TrackingRefUpdate update = new TrackingRefUpdate(
524 true,
525 spec.getSource(),
526 localRef.getName(),
527 localRef.getObjectId(),
528 ObjectId.zeroId());
529 result.add(update);
530 batch.addCommand(update.asReceiveCommand());
531 }
532
533 private static boolean isTag(Ref r) {
534 return isTag(r.getName());
535 }
536
537 private static boolean isTag(String name) {
538 return name.startsWith(Constants.R_TAGS);
539 }
540
541 private static String getFirstFailedRefName(BatchRefUpdate batch) {
542 for (ReceiveCommand cmd : batch.getCommands()) {
543 if (cmd.getResult() != ReceiveCommand.Result.OK)
544 return cmd.getRefName();
545 }
546 return "";
547 }
548 }