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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.FileTreeIterator;
  56. import org.eclipse.jgit.treewalk.TreeWalk;
  57. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  58. import org.eclipse.jgit.treewalk.filter.PathFilter;
  59. /**
  60. * Supplies the content of a file for {@link 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 FileTreeIterator} an optimized version is
  82. * used that doesn't require seeking through a TreeWalk.
  83. *
  84. * @param iterator
  85. * the iterator to obtain source files through.
  86. * @return a content source wrapping the iterator.
  87. */
  88. public static ContentSource create(WorkingTreeIterator iterator) {
  89. return new WorkingTreeSource(iterator);
  90. }
  91. /**
  92. * Determine the size of the object.
  93. *
  94. * @param path
  95. * the path of the file, relative to the root of the repository.
  96. * @param id
  97. * blob id of the file, if known.
  98. * @return the size in bytes.
  99. * @throws IOException
  100. * the file cannot be accessed.
  101. */
  102. public abstract long size(String path, ObjectId id) throws IOException;
  103. /**
  104. * Open the object.
  105. *
  106. * @param path
  107. * the path of the file, relative to the root of the repository.
  108. * @param id
  109. * blob id of the file, if known.
  110. * @return a loader that can supply the content of the file. The loader must
  111. * be used before another loader can be obtained from this same
  112. * source.
  113. * @throws IOException
  114. * the file cannot be accessed.
  115. */
  116. public abstract ObjectLoader open(String path, ObjectId id)
  117. throws IOException;
  118. private static class ObjectReaderSource extends ContentSource {
  119. private final ObjectReader reader;
  120. ObjectReaderSource(ObjectReader reader) {
  121. this.reader = reader;
  122. }
  123. @Override
  124. public long size(String path, ObjectId id) throws IOException {
  125. return reader.getObjectSize(id, Constants.OBJ_BLOB);
  126. }
  127. @Override
  128. public ObjectLoader open(String path, ObjectId id) throws IOException {
  129. return reader.open(id, Constants.OBJ_BLOB);
  130. }
  131. }
  132. private static class WorkingTreeSource extends ContentSource {
  133. private final TreeWalk tw;
  134. private final WorkingTreeIterator iterator;
  135. private String current;
  136. private WorkingTreeIterator ptr;
  137. WorkingTreeSource(WorkingTreeIterator iterator) {
  138. this.tw = new TreeWalk((ObjectReader) null);
  139. this.tw.setRecursive(true);
  140. this.iterator = iterator;
  141. }
  142. @Override
  143. public long size(String path, ObjectId id) throws IOException {
  144. seek(path);
  145. return ptr.getEntryLength();
  146. }
  147. @Override
  148. public ObjectLoader open(String path, ObjectId id) throws IOException {
  149. seek(path);
  150. return new ObjectLoader() {
  151. @Override
  152. public long getSize() {
  153. return ptr.getEntryLength();
  154. }
  155. @Override
  156. public int getType() {
  157. return ptr.getEntryFileMode().getObjectType();
  158. }
  159. @Override
  160. public ObjectStream openStream() throws MissingObjectException,
  161. IOException {
  162. long contentLength = ptr.getEntryContentLength();
  163. InputStream in = ptr.openEntryStream();
  164. in = new BufferedInputStream(in);
  165. return new ObjectStream.Filter(getType(), contentLength, in);
  166. }
  167. @Override
  168. public boolean isLarge() {
  169. return true;
  170. }
  171. @Override
  172. public byte[] getCachedBytes() throws LargeObjectException {
  173. throw new LargeObjectException();
  174. }
  175. };
  176. }
  177. private void seek(String path) throws IOException {
  178. if (!path.equals(current)) {
  179. iterator.reset();
  180. tw.reset();
  181. tw.addTree(iterator);
  182. tw.setFilter(PathFilter.create(path));
  183. current = path;
  184. if (!tw.next())
  185. throw new FileNotFoundException(path);
  186. ptr = tw.getTree(0, WorkingTreeIterator.class);
  187. if (ptr == null)
  188. throw new FileNotFoundException(path);
  189. }
  190. }
  191. }
  192. /** A pair of sources to access the old and new sides of a DiffEntry. */
  193. public static final class Pair {
  194. private final ContentSource oldSource;
  195. private final ContentSource newSource;
  196. /**
  197. * Construct a pair of sources.
  198. *
  199. * @param oldSource
  200. * source to read the old side of a DiffEntry.
  201. * @param newSource
  202. * source to read the new side of a DiffEntry.
  203. */
  204. public Pair(ContentSource oldSource, ContentSource newSource) {
  205. this.oldSource = oldSource;
  206. this.newSource = newSource;
  207. }
  208. /**
  209. * Determine the size of the object.
  210. *
  211. * @param side
  212. * which side of the entry to read (OLD or NEW).
  213. * @param ent
  214. * the entry to examine.
  215. * @return the size in bytes.
  216. * @throws IOException
  217. * the file cannot be accessed.
  218. */
  219. public long size(DiffEntry.Side side, DiffEntry ent) throws IOException {
  220. switch (side) {
  221. case OLD:
  222. return oldSource.size(ent.oldPath, ent.oldId.toObjectId());
  223. case NEW:
  224. return newSource.size(ent.newPath, ent.newId.toObjectId());
  225. default:
  226. throw new IllegalArgumentException();
  227. }
  228. }
  229. /**
  230. * Open the object.
  231. *
  232. * @param side
  233. * which side of the entry to read (OLD or NEW).
  234. * @param ent
  235. * the entry to examine.
  236. * @return a loader that can supply the content of the file. The loader
  237. * must be used before another loader can be obtained from this
  238. * same source.
  239. * @throws IOException
  240. * the file cannot be accessed.
  241. */
  242. public ObjectLoader open(DiffEntry.Side side, DiffEntry ent)
  243. throws IOException {
  244. switch (side) {
  245. case OLD:
  246. return oldSource.open(ent.oldPath, ent.oldId.toObjectId());
  247. case NEW:
  248. return newSource.open(ent.newPath, ent.newId.toObjectId());
  249. default:
  250. throw new IllegalArgumentException();
  251. }
  252. }
  253. }
  254. }