You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BasePackConnection.java 12KB

Rewrite reference handling to be abstract and accurate This commit actually does three major changes to the way references are handled within JGit. Unfortunately they were easier to do as a single massive commit than to break them up into smaller units. Disambiguate symbolic references: --------------------------------- Reporting a symbolic reference such as HEAD as though it were any other normal reference like refs/heads/master causes subtle programming errors. We have been bitten by this error on several occasions, as have some downstream applications written by myself. Instead of reporting HEAD as a reference whose name differs from its "original name", report it as an actual SymbolicRef object that the application can test the type and examine the target of. With this change, Ref is now an abstract type with different subclasses for the different types. In the classical example of "HEAD" being a symbolic reference to branch "refs/heads/master", the Repository.getAllRefs() method will now return: Map<String, Ref> all = repository.getAllRefs(); SymbolicRef HEAD = (SymbolicRef) all.get("HEAD"); ObjectIdRef master = (ObjectIdRef) all.get("refs/heads/master"); assertSame(master, HEAD.getTarget()); assertSame(master.getObjectId(), HEAD.getObjectId()); assertEquals("HEAD", HEAD.getName()); assertEquals("refs/heads/master", master.getName()); A nice side-effect of this change is the storage type of the symbolic reference is no longer ambiguous with the storge type of the underlying reference it targets. In the above example, if master was only available in the packed-refs file, then the following is also true: assertSame(Ref.Storage.LOOSE, HEAD.getStorage()); assertSame(Ref.Storage.PACKED, master.getStorage()); (Prior to this change we returned the ambiguous storage of LOOSE_PACKED for HEAD, which was confusing since it wasn't actually true on disk). Another nice side-effect of this change is all intermediate symbolic references are preserved, and are therefore visible to the application when they walk the target chain. We can now correctly inspect chains of symbolic references. As a result of this change the Ref.getOrigName() method has been removed from the API. Applications should identify a symbolic reference by testing for isSymbolic() and not by using an arcane string comparsion between properties. Abstract the RefDatabase storage: --------------------------------- RefDatabase is now abstract, similar to ObjectDatabase, and a new concrete implementation called RefDirectory is used for the traditional on-disk storage layout. In the future we plan to support additional implementations, such as a pure in-memory RefDatabase for unit testing purposes. Optimize RefDirectory: ---------------------- The implementation of the in-memory reference cache, reading, and update routines has been completely rewritten. Much of the code was heavily borrowed or cribbed from the prior implementation, so copyright notices have been left intact as much as possible. The RefDirectory cache no longer confuses symbolic references with normal references. This permits the cache to resolve the value of a symbolic reference as late as possible, ensuring it is always current, without needing to maintain reverse pointers. The cache is now 2 sorted RefLists, rather than 3 HashMaps. Using sorted lists allows the implementation to reduce the in-memory footprint when storing many refs. Using specialized types for the elements allows the code to avoid additional map lookups for auxiliary stat information. To improve scan time during getRefs(), the lists are returned via a copy-on-write contract. Most callers of getRefs() do not modify the returned collections, so the copy-on-write semantics improves access on repositories with a large number of packed references. Iterator traversals of the returned Map<String,Ref> are performed using a simple merge-join of the two cache lists, ensuring we can perform the entire traversal in linear time as a function of the number of references: O(PackedRefs + LooseRefs). Scans of the loose reference space to update the cache run in O(LooseRefs log LooseRefs) time, as the directory contents are sorted before being merged against the in-memory cache. Since the majority of stable references are kept packed, there typically are only a handful of reference names to be sorted, so the sorting cost should not be very high. Locking is reduced during getRefs() by taking advantage of the copy-on-write semantics of the improved cache data structure. This permits concurrent readers to pull back references without blocking each other. If there is contention updating the cache during a scan, one or more updates are simply skipped and will get picked up again in a future scan. Writing to the $GIT_DIR/packed-refs during reference delete is now fully atomic. The file is locked, reparsed fresh, and written back out if a change is necessary. This avoids all race conditions with concurrent external updates of the packed-refs file. The RefLogWriter class has been fully folded into RefDirectory and is therefore deleted. Maintaining the reference's log is the responsiblity of the database implementation, and not all implementations will use java.io for access. Future work still remains to be done to abstract the ReflogReader class away from local disk IO. Change-Id: I26b9287c45a4b2d2be35ba2849daa316f5eec85d Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
14 years ago
Rewrite reference handling to be abstract and accurate This commit actually does three major changes to the way references are handled within JGit. Unfortunately they were easier to do as a single massive commit than to break them up into smaller units. Disambiguate symbolic references: --------------------------------- Reporting a symbolic reference such as HEAD as though it were any other normal reference like refs/heads/master causes subtle programming errors. We have been bitten by this error on several occasions, as have some downstream applications written by myself. Instead of reporting HEAD as a reference whose name differs from its "original name", report it as an actual SymbolicRef object that the application can test the type and examine the target of. With this change, Ref is now an abstract type with different subclasses for the different types. In the classical example of "HEAD" being a symbolic reference to branch "refs/heads/master", the Repository.getAllRefs() method will now return: Map<String, Ref> all = repository.getAllRefs(); SymbolicRef HEAD = (SymbolicRef) all.get("HEAD"); ObjectIdRef master = (ObjectIdRef) all.get("refs/heads/master"); assertSame(master, HEAD.getTarget()); assertSame(master.getObjectId(), HEAD.getObjectId()); assertEquals("HEAD", HEAD.getName()); assertEquals("refs/heads/master", master.getName()); A nice side-effect of this change is the storage type of the symbolic reference is no longer ambiguous with the storge type of the underlying reference it targets. In the above example, if master was only available in the packed-refs file, then the following is also true: assertSame(Ref.Storage.LOOSE, HEAD.getStorage()); assertSame(Ref.Storage.PACKED, master.getStorage()); (Prior to this change we returned the ambiguous storage of LOOSE_PACKED for HEAD, which was confusing since it wasn't actually true on disk). Another nice side-effect of this change is all intermediate symbolic references are preserved, and are therefore visible to the application when they walk the target chain. We can now correctly inspect chains of symbolic references. As a result of this change the Ref.getOrigName() method has been removed from the API. Applications should identify a symbolic reference by testing for isSymbolic() and not by using an arcane string comparsion between properties. Abstract the RefDatabase storage: --------------------------------- RefDatabase is now abstract, similar to ObjectDatabase, and a new concrete implementation called RefDirectory is used for the traditional on-disk storage layout. In the future we plan to support additional implementations, such as a pure in-memory RefDatabase for unit testing purposes. Optimize RefDirectory: ---------------------- The implementation of the in-memory reference cache, reading, and update routines has been completely rewritten. Much of the code was heavily borrowed or cribbed from the prior implementation, so copyright notices have been left intact as much as possible. The RefDirectory cache no longer confuses symbolic references with normal references. This permits the cache to resolve the value of a symbolic reference as late as possible, ensuring it is always current, without needing to maintain reverse pointers. The cache is now 2 sorted RefLists, rather than 3 HashMaps. Using sorted lists allows the implementation to reduce the in-memory footprint when storing many refs. Using specialized types for the elements allows the code to avoid additional map lookups for auxiliary stat information. To improve scan time during getRefs(), the lists are returned via a copy-on-write contract. Most callers of getRefs() do not modify the returned collections, so the copy-on-write semantics improves access on repositories with a large number of packed references. Iterator traversals of the returned Map<String,Ref> are performed using a simple merge-join of the two cache lists, ensuring we can perform the entire traversal in linear time as a function of the number of references: O(PackedRefs + LooseRefs). Scans of the loose reference space to update the cache run in O(LooseRefs log LooseRefs) time, as the directory contents are sorted before being merged against the in-memory cache. Since the majority of stable references are kept packed, there typically are only a handful of reference names to be sorted, so the sorting cost should not be very high. Locking is reduced during getRefs() by taking advantage of the copy-on-write semantics of the improved cache data structure. This permits concurrent readers to pull back references without blocking each other. If there is contention updating the cache during a scan, one or more updates are simply skipped and will get picked up again in a future scan. Writing to the $GIT_DIR/packed-refs during reference delete is now fully atomic. The file is locked, reparsed fresh, and written back out if a change is necessary. This avoids all race conditions with concurrent external updates of the packed-refs file. The RefLogWriter class has been fully folded into RefDirectory and is therefore deleted. Maintaining the reference's log is the responsiblity of the database implementation, and not all implementations will use java.io for access. Future work still remains to be done to abstract the ReflogReader class away from local disk IO. Change-Id: I26b9287c45a4b2d2be35ba2849daa316f5eec85d Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
14 years ago
Rewrite reference handling to be abstract and accurate This commit actually does three major changes to the way references are handled within JGit. Unfortunately they were easier to do as a single massive commit than to break them up into smaller units. Disambiguate symbolic references: --------------------------------- Reporting a symbolic reference such as HEAD as though it were any other normal reference like refs/heads/master causes subtle programming errors. We have been bitten by this error on several occasions, as have some downstream applications written by myself. Instead of reporting HEAD as a reference whose name differs from its "original name", report it as an actual SymbolicRef object that the application can test the type and examine the target of. With this change, Ref is now an abstract type with different subclasses for the different types. In the classical example of "HEAD" being a symbolic reference to branch "refs/heads/master", the Repository.getAllRefs() method will now return: Map<String, Ref> all = repository.getAllRefs(); SymbolicRef HEAD = (SymbolicRef) all.get("HEAD"); ObjectIdRef master = (ObjectIdRef) all.get("refs/heads/master"); assertSame(master, HEAD.getTarget()); assertSame(master.getObjectId(), HEAD.getObjectId()); assertEquals("HEAD", HEAD.getName()); assertEquals("refs/heads/master", master.getName()); A nice side-effect of this change is the storage type of the symbolic reference is no longer ambiguous with the storge type of the underlying reference it targets. In the above example, if master was only available in the packed-refs file, then the following is also true: assertSame(Ref.Storage.LOOSE, HEAD.getStorage()); assertSame(Ref.Storage.PACKED, master.getStorage()); (Prior to this change we returned the ambiguous storage of LOOSE_PACKED for HEAD, which was confusing since it wasn't actually true on disk). Another nice side-effect of this change is all intermediate symbolic references are preserved, and are therefore visible to the application when they walk the target chain. We can now correctly inspect chains of symbolic references. As a result of this change the Ref.getOrigName() method has been removed from the API. Applications should identify a symbolic reference by testing for isSymbolic() and not by using an arcane string comparsion between properties. Abstract the RefDatabase storage: --------------------------------- RefDatabase is now abstract, similar to ObjectDatabase, and a new concrete implementation called RefDirectory is used for the traditional on-disk storage layout. In the future we plan to support additional implementations, such as a pure in-memory RefDatabase for unit testing purposes. Optimize RefDirectory: ---------------------- The implementation of the in-memory reference cache, reading, and update routines has been completely rewritten. Much of the code was heavily borrowed or cribbed from the prior implementation, so copyright notices have been left intact as much as possible. The RefDirectory cache no longer confuses symbolic references with normal references. This permits the cache to resolve the value of a symbolic reference as late as possible, ensuring it is always current, without needing to maintain reverse pointers. The cache is now 2 sorted RefLists, rather than 3 HashMaps. Using sorted lists allows the implementation to reduce the in-memory footprint when storing many refs. Using specialized types for the elements allows the code to avoid additional map lookups for auxiliary stat information. To improve scan time during getRefs(), the lists are returned via a copy-on-write contract. Most callers of getRefs() do not modify the returned collections, so the copy-on-write semantics improves access on repositories with a large number of packed references. Iterator traversals of the returned Map<String,Ref> are performed using a simple merge-join of the two cache lists, ensuring we can perform the entire traversal in linear time as a function of the number of references: O(PackedRefs + LooseRefs). Scans of the loose reference space to update the cache run in O(LooseRefs log LooseRefs) time, as the directory contents are sorted before being merged against the in-memory cache. Since the majority of stable references are kept packed, there typically are only a handful of reference names to be sorted, so the sorting cost should not be very high. Locking is reduced during getRefs() by taking advantage of the copy-on-write semantics of the improved cache data structure. This permits concurrent readers to pull back references without blocking each other. If there is contention updating the cache during a scan, one or more updates are simply skipped and will get picked up again in a future scan. Writing to the $GIT_DIR/packed-refs during reference delete is now fully atomic. The file is locked, reparsed fresh, and written back out if a change is necessary. This avoids all race conditions with concurrent external updates of the packed-refs file. The RefLogWriter class has been fully folded into RefDirectory and is therefore deleted. Maintaining the reference's log is the responsiblity of the database implementation, and not all implementations will use java.io for access. Future work still remains to be done to abstract the ReflogReader class away from local disk IO. Change-Id: I26b9287c45a4b2d2be35ba2849daa316f5eec85d Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
14 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright (C) 2008-2010, Google Inc.
  3. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  4. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  5. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.transport;
  47. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_AGENT;
  48. import java.io.EOFException;
  49. import java.io.IOException;
  50. import java.io.InputStream;
  51. import java.io.OutputStream;
  52. import java.text.MessageFormat;
  53. import java.util.Arrays;
  54. import java.util.HashSet;
  55. import java.util.LinkedHashMap;
  56. import java.util.Set;
  57. import org.eclipse.jgit.errors.InvalidObjectIdException;
  58. import org.eclipse.jgit.errors.NoRemoteRepositoryException;
  59. import org.eclipse.jgit.errors.PackProtocolException;
  60. import org.eclipse.jgit.errors.RemoteRepositoryException;
  61. import org.eclipse.jgit.errors.TransportException;
  62. import org.eclipse.jgit.internal.JGitText;
  63. import org.eclipse.jgit.lib.ObjectId;
  64. import org.eclipse.jgit.lib.ObjectIdRef;
  65. import org.eclipse.jgit.lib.Ref;
  66. import org.eclipse.jgit.lib.Repository;
  67. import org.eclipse.jgit.util.io.InterruptTimer;
  68. import org.eclipse.jgit.util.io.TimeoutInputStream;
  69. import org.eclipse.jgit.util.io.TimeoutOutputStream;
  70. /**
  71. * Base helper class for pack-based operations implementations. Provides partial
  72. * implementation of pack-protocol - refs advertising and capabilities support,
  73. * and some other helper methods.
  74. *
  75. * @see BasePackFetchConnection
  76. * @see BasePackPushConnection
  77. */
  78. abstract class BasePackConnection extends BaseConnection {
  79. /** The repository this transport fetches into, or pushes out of. */
  80. protected final Repository local;
  81. /** Remote repository location. */
  82. protected final URIish uri;
  83. /** A transport connected to {@link #uri}. */
  84. protected final Transport transport;
  85. /** Low-level input stream, if a timeout was configured. */
  86. protected TimeoutInputStream timeoutIn;
  87. /** Low-level output stream, if a timeout was configured. */
  88. protected TimeoutOutputStream timeoutOut;
  89. /** Timer to manage {@link #timeoutIn} and {@link #timeoutOut}. */
  90. private InterruptTimer myTimer;
  91. /** Input stream reading from the remote. */
  92. protected InputStream in;
  93. /** Output stream sending to the remote. */
  94. protected OutputStream out;
  95. /** Packet line decoder around {@link #in}. */
  96. protected PacketLineIn pckIn;
  97. /** Packet line encoder around {@link #out}. */
  98. protected PacketLineOut pckOut;
  99. /** Send {@link PacketLineOut#end()} before closing {@link #out}? */
  100. protected boolean outNeedsEnd;
  101. /** True if this is a stateless RPC connection. */
  102. protected boolean statelessRPC;
  103. /** Capability tokens advertised by the remote side. */
  104. private final Set<String> remoteCapablities = new HashSet<>();
  105. /** Extra objects the remote has, but which aren't offered as refs. */
  106. protected final Set<ObjectId> additionalHaves = new HashSet<>();
  107. BasePackConnection(PackTransport packTransport) {
  108. transport = (Transport) packTransport;
  109. local = transport.local;
  110. uri = transport.uri;
  111. }
  112. /**
  113. * Configure this connection with the directional pipes.
  114. *
  115. * @param myIn
  116. * input stream to receive data from the peer. Caller must ensure
  117. * the input is buffered, otherwise read performance may suffer.
  118. * @param myOut
  119. * output stream to transmit data to the peer. Caller must ensure
  120. * the output is buffered, otherwise write performance may
  121. * suffer.
  122. */
  123. protected final void init(InputStream myIn, OutputStream myOut) {
  124. final int timeout = transport.getTimeout();
  125. if (timeout > 0) {
  126. final Thread caller = Thread.currentThread();
  127. if (myTimer == null) {
  128. myTimer = new InterruptTimer(caller.getName() + "-Timer"); //$NON-NLS-1$
  129. }
  130. timeoutIn = new TimeoutInputStream(myIn, myTimer);
  131. timeoutOut = new TimeoutOutputStream(myOut, myTimer);
  132. timeoutIn.setTimeout(timeout * 1000);
  133. timeoutOut.setTimeout(timeout * 1000);
  134. myIn = timeoutIn;
  135. myOut = timeoutOut;
  136. }
  137. in = myIn;
  138. out = myOut;
  139. pckIn = new PacketLineIn(in);
  140. pckOut = new PacketLineOut(out);
  141. outNeedsEnd = true;
  142. }
  143. /**
  144. * Reads the advertised references through the initialized stream.
  145. * <p>
  146. * Subclass implementations may call this method only after setting up the
  147. * input and output streams with {@link #init(InputStream, OutputStream)}.
  148. * <p>
  149. * If any errors occur, this connection is automatically closed by invoking
  150. * {@link #close()} and the exception is wrapped (if necessary) and thrown
  151. * as a {@link org.eclipse.jgit.errors.TransportException}.
  152. *
  153. * @throws org.eclipse.jgit.errors.TransportException
  154. * the reference list could not be scanned.
  155. */
  156. protected void readAdvertisedRefs() throws TransportException {
  157. try {
  158. readAdvertisedRefsImpl();
  159. } catch (TransportException err) {
  160. close();
  161. throw err;
  162. } catch (IOException | RuntimeException err) {
  163. close();
  164. throw new TransportException(err.getMessage(), err);
  165. }
  166. }
  167. private void readAdvertisedRefsImpl() throws IOException {
  168. final LinkedHashMap<String, Ref> avail = new LinkedHashMap<>();
  169. for (;;) {
  170. String line;
  171. try {
  172. line = pckIn.readString();
  173. } catch (EOFException eof) {
  174. if (avail.isEmpty())
  175. throw noRepository();
  176. throw eof;
  177. }
  178. if (line == PacketLineIn.END)
  179. break;
  180. if (line.startsWith("ERR ")) { //$NON-NLS-1$
  181. // This is a customized remote service error.
  182. // Users should be informed about it.
  183. throw new RemoteRepositoryException(uri, line.substring(4));
  184. }
  185. if (avail.isEmpty()) {
  186. final int nul = line.indexOf('\0');
  187. if (nul >= 0) {
  188. // The first line (if any) may contain "hidden"
  189. // capability values after a NUL byte.
  190. remoteCapablities.addAll(
  191. Arrays.asList(line.substring(nul + 1).split(" "))); //$NON-NLS-1$
  192. line = line.substring(0, nul);
  193. }
  194. }
  195. // Expecting to get a line in the form "sha1 refname"
  196. if (line.length() < 41 || line.charAt(40) != ' ') {
  197. throw invalidRefAdvertisementLine(line);
  198. }
  199. String name = line.substring(41, line.length());
  200. if (avail.isEmpty() && name.equals("capabilities^{}")) { //$NON-NLS-1$
  201. // special line from git-receive-pack to show
  202. // capabilities when there are no refs to advertise
  203. continue;
  204. }
  205. final ObjectId id;
  206. try {
  207. id = ObjectId.fromString(line.substring(0, 40));
  208. } catch (InvalidObjectIdException e) {
  209. throw invalidRefAdvertisementLine(line);
  210. }
  211. if (name.equals(".have")) { //$NON-NLS-1$
  212. additionalHaves.add(id);
  213. } else if (name.endsWith("^{}")) { //$NON-NLS-1$
  214. name = name.substring(0, name.length() - 3);
  215. final Ref prior = avail.get(name);
  216. if (prior == null)
  217. throw new PackProtocolException(uri, MessageFormat.format(
  218. JGitText.get().advertisementCameBefore, name, name));
  219. if (prior.getPeeledObjectId() != null)
  220. throw duplicateAdvertisement(name + "^{}"); //$NON-NLS-1$
  221. avail.put(name, new ObjectIdRef.PeeledTag(
  222. Ref.Storage.NETWORK, name, prior.getObjectId(), id));
  223. } else {
  224. final Ref prior = avail.put(name, new ObjectIdRef.PeeledNonTag(
  225. Ref.Storage.NETWORK, name, id));
  226. if (prior != null)
  227. throw duplicateAdvertisement(name);
  228. }
  229. }
  230. available(avail);
  231. }
  232. /**
  233. * Create an exception to indicate problems finding a remote repository. The
  234. * caller is expected to throw the returned exception.
  235. *
  236. * Subclasses may override this method to provide better diagnostics.
  237. *
  238. * @return a TransportException saying a repository cannot be found and
  239. * possibly why.
  240. */
  241. protected TransportException noRepository() {
  242. return new NoRemoteRepositoryException(uri, JGitText.get().notFound);
  243. }
  244. /**
  245. * Whether this option is supported
  246. *
  247. * @param option
  248. * option string
  249. * @return whether this option is supported
  250. */
  251. protected boolean isCapableOf(String option) {
  252. return remoteCapablities.contains(option);
  253. }
  254. /**
  255. * Request capability
  256. *
  257. * @param b
  258. * buffer
  259. * @param option
  260. * option we want
  261. * @return {@code true} if the requested option is supported
  262. */
  263. protected boolean wantCapability(StringBuilder b, String option) {
  264. if (!isCapableOf(option))
  265. return false;
  266. b.append(' ');
  267. b.append(option);
  268. return true;
  269. }
  270. /**
  271. * Add user agent capability
  272. *
  273. * @param b
  274. * a {@link java.lang.StringBuilder} object.
  275. */
  276. protected void addUserAgentCapability(StringBuilder b) {
  277. String a = UserAgent.get();
  278. if (a != null && UserAgent.hasAgent(remoteCapablities)) {
  279. b.append(' ').append(OPTION_AGENT).append('=').append(a);
  280. }
  281. }
  282. /** {@inheritDoc} */
  283. @Override
  284. public String getPeerUserAgent() {
  285. return UserAgent.getAgent(remoteCapablities, super.getPeerUserAgent());
  286. }
  287. private PackProtocolException duplicateAdvertisement(String name) {
  288. return new PackProtocolException(uri, MessageFormat.format(JGitText.get().duplicateAdvertisementsOf, name));
  289. }
  290. private PackProtocolException invalidRefAdvertisementLine(String line) {
  291. return new PackProtocolException(uri, MessageFormat.format(JGitText.get().invalidRefAdvertisementLine, line));
  292. }
  293. /** {@inheritDoc} */
  294. @Override
  295. public void close() {
  296. if (out != null) {
  297. try {
  298. if (outNeedsEnd) {
  299. outNeedsEnd = false;
  300. pckOut.end();
  301. }
  302. out.close();
  303. } catch (IOException err) {
  304. // Ignore any close errors.
  305. } finally {
  306. out = null;
  307. pckOut = null;
  308. }
  309. }
  310. if (in != null) {
  311. try {
  312. in.close();
  313. } catch (IOException err) {
  314. // Ignore any close errors.
  315. } finally {
  316. in = null;
  317. pckIn = null;
  318. }
  319. }
  320. if (myTimer != null) {
  321. try {
  322. myTimer.terminate();
  323. } finally {
  324. myTimer = null;
  325. timeoutIn = null;
  326. timeoutOut = null;
  327. }
  328. }
  329. }
  330. /**
  331. * Tell the peer we are disconnecting, if it cares to know.
  332. */
  333. protected void endOut() {
  334. if (outNeedsEnd && out != null) {
  335. try {
  336. outNeedsEnd = false;
  337. pckOut.end();
  338. } catch (IOException e) {
  339. try {
  340. out.close();
  341. } catch (IOException err) {
  342. // Ignore any close errors.
  343. } finally {
  344. out = null;
  345. pckOut = null;
  346. }
  347. }
  348. }
  349. }
  350. }