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.

ContentSource.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.diff;
  44. import java.io.BufferedInputStream;
  45. import java.io.FileNotFoundException;
  46. import java.io.IOException;
  47. import java.io.InputStream;
  48. import org.eclipse.jgit.errors.LargeObjectException;
  49. import org.eclipse.jgit.errors.MissingObjectException;
  50. import org.eclipse.jgit.lib.Constants;
  51. import org.eclipse.jgit.lib.ObjectId;
  52. import org.eclipse.jgit.lib.ObjectLoader;
  53. import org.eclipse.jgit.lib.ObjectReader;
  54. import org.eclipse.jgit.lib.ObjectStream;
  55. import org.eclipse.jgit.treewalk.TreeWalk;
  56. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  57. import org.eclipse.jgit.treewalk.filter.PathFilter;
  58. /**
  59. * Supplies the content of a file for
  60. * {@link org.eclipse.jgit.diff.DiffFormatter}.
  61. * <p>
  62. * A content source is not thread-safe. Sources may contain state, including
  63. * information about the last ObjectLoader they returned. Callers must be
  64. * careful to ensure there is no more than one ObjectLoader pending on any
  65. * source, at any time.
  66. */
  67. public abstract class ContentSource {
  68. /**
  69. * Construct a content source for an ObjectReader.
  70. *
  71. * @param reader
  72. * the reader to obtain blobs from.
  73. * @return a source wrapping the reader.
  74. */
  75. public static ContentSource create(ObjectReader reader) {
  76. return new ObjectReaderSource(reader);
  77. }
  78. /**
  79. * Construct a content source for a working directory.
  80. *
  81. * If the iterator is a {@link org.eclipse.jgit.treewalk.FileTreeIterator}
  82. * an optimized version is used that doesn't require seeking through a
  83. * TreeWalk.
  84. *
  85. * @param iterator
  86. * the iterator to obtain source files through.
  87. * @return a content source wrapping the iterator.
  88. */
  89. public static ContentSource create(WorkingTreeIterator iterator) {
  90. return new WorkingTreeSource(iterator);
  91. }
  92. /**
  93. * Determine the size of the object.
  94. *
  95. * @param path
  96. * the path of the file, relative to the root of the repository.
  97. * @param id
  98. * blob id of the file, if known.
  99. * @return the size in bytes.
  100. * @throws java.io.IOException
  101. * the file cannot be accessed.
  102. */
  103. public abstract long size(String path, ObjectId id) throws IOException;
  104. /**
  105. * Open the object.
  106. *
  107. * @param path
  108. * the path of the file, relative to the root of the repository.
  109. * @param id
  110. * blob id of the file, if known.
  111. * @return a loader that can supply the content of the file. The loader must
  112. * be used before another loader can be obtained from this same
  113. * source.
  114. * @throws java.io.IOException
  115. * the file cannot be accessed.
  116. */
  117. public abstract ObjectLoader open(String path, ObjectId id)
  118. throws IOException;
  119. private static class ObjectReaderSource extends ContentSource {
  120. private final ObjectReader reader;
  121. ObjectReaderSource(ObjectReader reader) {
  122. this.reader = reader;
  123. }
  124. @Override
  125. public long size(String path, ObjectId id) throws IOException {
  126. try {
  127. return reader.getObjectSize(id, Constants.OBJ_BLOB);
  128. } catch (MissingObjectException ignore) {
  129. return 0;
  130. }
  131. }
  132. @Override
  133. public ObjectLoader open(String path, ObjectId id) throws IOException {
  134. return reader.open(id, Constants.OBJ_BLOB);
  135. }
  136. }
  137. private static class WorkingTreeSource extends ContentSource {
  138. private final TreeWalk tw;
  139. private final WorkingTreeIterator iterator;
  140. private String current;
  141. WorkingTreeIterator ptr;
  142. WorkingTreeSource(WorkingTreeIterator iterator) {
  143. this.tw = new TreeWalk((ObjectReader) null);
  144. this.tw.setRecursive(true);
  145. this.iterator = iterator;
  146. }
  147. @Override
  148. public long size(String path, ObjectId id) throws IOException {
  149. seek(path);
  150. return ptr.getEntryLength();
  151. }
  152. @Override
  153. public ObjectLoader open(String path, ObjectId id) throws IOException {
  154. seek(path);
  155. long entrySize = ptr.getEntryContentLength();
  156. return new ObjectLoader() {
  157. @Override
  158. public long getSize() {
  159. return entrySize;
  160. }
  161. @Override
  162. public int getType() {
  163. return ptr.getEntryFileMode().getObjectType();
  164. }
  165. @Override
  166. public ObjectStream openStream() throws MissingObjectException,
  167. IOException {
  168. long contentLength = entrySize;
  169. InputStream in = ptr.openEntryStream();
  170. in = new BufferedInputStream(in);
  171. return new ObjectStream.Filter(getType(), contentLength, in);
  172. }
  173. @Override
  174. public boolean isLarge() {
  175. return true;
  176. }
  177. @Override
  178. public byte[] getCachedBytes() throws LargeObjectException {
  179. throw new LargeObjectException();
  180. }
  181. };
  182. }
  183. private void seek(String path) throws IOException {
  184. if (!path.equals(current)) {
  185. iterator.reset();
  186. tw.reset();
  187. tw.addTree(iterator);
  188. tw.setFilter(PathFilter.create(path));
  189. current = path;
  190. if (!tw.next())
  191. throw new FileNotFoundException(path);
  192. ptr = tw.getTree(0, WorkingTreeIterator.class);
  193. if (ptr == null)
  194. throw new FileNotFoundException(path);
  195. }
  196. }
  197. }
  198. /** A pair of sources to access the old and new sides of a DiffEntry. */
  199. public static final class Pair {
  200. private final ContentSource oldSource;
  201. private final ContentSource newSource;
  202. /**
  203. * Construct a pair of sources.
  204. *
  205. * @param oldSource
  206. * source to read the old side of a DiffEntry.
  207. * @param newSource
  208. * source to read the new side of a DiffEntry.
  209. */
  210. public Pair(ContentSource oldSource, ContentSource newSource) {
  211. this.oldSource = oldSource;
  212. this.newSource = newSource;
  213. }
  214. /**
  215. * Determine the size of the object.
  216. *
  217. * @param side
  218. * which side of the entry to read (OLD or NEW).
  219. * @param ent
  220. * the entry to examine.
  221. * @return the size in bytes.
  222. * @throws IOException
  223. * the file cannot be accessed.
  224. */
  225. public long size(DiffEntry.Side side, DiffEntry ent) throws IOException {
  226. switch (side) {
  227. case OLD:
  228. return oldSource.size(ent.oldPath, ent.oldId.toObjectId());
  229. case NEW:
  230. return newSource.size(ent.newPath, ent.newId.toObjectId());
  231. default:
  232. throw new IllegalArgumentException();
  233. }
  234. }
  235. /**
  236. * Open the object.
  237. *
  238. * @param side
  239. * which side of the entry to read (OLD or NEW).
  240. * @param ent
  241. * the entry to examine.
  242. * @return a loader that can supply the content of the file. The loader
  243. * must be used before another loader can be obtained from this
  244. * same source.
  245. * @throws IOException
  246. * the file cannot be accessed.
  247. */
  248. public ObjectLoader open(DiffEntry.Side side, DiffEntry ent)
  249. throws IOException {
  250. switch (side) {
  251. case OLD:
  252. return oldSource.open(ent.oldPath, ent.oldId.toObjectId());
  253. case NEW:
  254. return newSource.open(ent.newPath, ent.newId.toObjectId());
  255. default:
  256. throw new IllegalArgumentException();
  257. }
  258. }
  259. }
  260. }