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 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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.File;
  46. import java.io.FileInputStream;
  47. import java.io.FileNotFoundException;
  48. import java.io.IOException;
  49. import java.io.InputStream;
  50. import org.eclipse.jgit.errors.LargeObjectException;
  51. import org.eclipse.jgit.errors.MissingObjectException;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.lib.ObjectId;
  54. import org.eclipse.jgit.lib.ObjectLoader;
  55. import org.eclipse.jgit.lib.ObjectReader;
  56. import org.eclipse.jgit.lib.ObjectStream;
  57. import org.eclipse.jgit.treewalk.FileTreeIterator;
  58. import org.eclipse.jgit.treewalk.TreeWalk;
  59. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  60. import org.eclipse.jgit.treewalk.filter.PathFilter;
  61. /**
  62. * Supplies the content of a file for {@link DiffFormatter}.
  63. *
  64. * A content source is not thread-safe. Sources may contain state, including
  65. * information about the last ObjectLoader they returned. Callers must be
  66. * careful to ensure there is no more than one ObjectLoader pending on any
  67. * source, at any time.
  68. */
  69. public abstract class ContentSource {
  70. /**
  71. * Construct a content source for an ObjectReader.
  72. *
  73. * @param reader
  74. * the reader to obtain blobs from.
  75. * @return a source wrapping the reader.
  76. */
  77. public static ContentSource create(ObjectReader reader) {
  78. return new ObjectReaderSource(reader);
  79. }
  80. /**
  81. * Construct a content source for a working directory.
  82. *
  83. * If the iterator is a {@link FileTreeIterator} an optimized version is
  84. * used that doesn't require seeking through a TreeWalk.
  85. *
  86. * @param iterator
  87. * the iterator to obtain source files through.
  88. * @return a content source wrapping the iterator.
  89. */
  90. public static ContentSource create(WorkingTreeIterator iterator) {
  91. if (iterator instanceof FileTreeIterator) {
  92. FileTreeIterator i = (FileTreeIterator) iterator;
  93. return new FileSource(i.getDirectory());
  94. }
  95. return new WorkingTreeSource(iterator);
  96. }
  97. /**
  98. * Determine the size of the object.
  99. *
  100. * @param path
  101. * the path of the file, relative to the root of the repository.
  102. * @param id
  103. * blob id of the file, if known.
  104. * @return the size in bytes.
  105. * @throws IOException
  106. * the file cannot be accessed.
  107. */
  108. public abstract long size(String path, ObjectId id) throws IOException;
  109. /**
  110. * Open the object.
  111. *
  112. * @param path
  113. * the path of the file, relative to the root of the repository.
  114. * @param id
  115. * blob id of the file, if known.
  116. * @return a loader that can supply the content of the file. The loader must
  117. * be used before another loader can be obtained from this same
  118. * source.
  119. * @throws IOException
  120. * the file cannot be accessed.
  121. */
  122. public abstract ObjectLoader open(String path, ObjectId id)
  123. throws IOException;
  124. private static class ObjectReaderSource extends ContentSource {
  125. private final ObjectReader reader;
  126. ObjectReaderSource(ObjectReader reader) {
  127. this.reader = reader;
  128. }
  129. @Override
  130. public long size(String path, ObjectId id) throws IOException {
  131. return reader.getObjectSize(id, Constants.OBJ_BLOB);
  132. }
  133. @Override
  134. public ObjectLoader open(String path, ObjectId id) throws IOException {
  135. return reader.open(id, Constants.OBJ_BLOB);
  136. }
  137. }
  138. private static class WorkingTreeSource extends ContentSource {
  139. private final TreeWalk tw;
  140. private final WorkingTreeIterator iterator;
  141. private String current;
  142. private WorkingTreeIterator ptr;
  143. WorkingTreeSource(WorkingTreeIterator iterator) {
  144. this.tw = new TreeWalk((ObjectReader) null);
  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. return new ObjectLoader() {
  156. @Override
  157. public long getSize() {
  158. return ptr.getEntryLength();
  159. }
  160. @Override
  161. public int getType() {
  162. return ptr.getEntryFileMode().getObjectType();
  163. }
  164. @Override
  165. public ObjectStream openStream() throws MissingObjectException,
  166. IOException {
  167. InputStream in = ptr.openEntryStream();
  168. in = new BufferedInputStream(in);
  169. return new ObjectStream.Filter(getType(), getSize(), in);
  170. }
  171. @Override
  172. public boolean isLarge() {
  173. return true;
  174. }
  175. @Override
  176. public byte[] getCachedBytes() throws LargeObjectException {
  177. throw new LargeObjectException();
  178. }
  179. };
  180. }
  181. private void seek(String path) throws IOException {
  182. if (!path.equals(current)) {
  183. iterator.reset();
  184. tw.reset();
  185. tw.addTree(iterator);
  186. tw.setFilter(PathFilter.create(path));
  187. current = path;
  188. if (!tw.next())
  189. throw new FileNotFoundException(path);
  190. ptr = tw.getTree(0, WorkingTreeIterator.class);
  191. if (ptr == null)
  192. throw new FileNotFoundException(path);
  193. }
  194. }
  195. }
  196. private static class FileSource extends ContentSource {
  197. private final File root;
  198. FileSource(File root) {
  199. this.root = root;
  200. }
  201. @Override
  202. public long size(String path, ObjectId id) throws IOException {
  203. return new File(root, path).length();
  204. }
  205. @Override
  206. public ObjectLoader open(String path, ObjectId id) throws IOException {
  207. final File p = new File(root, path);
  208. if (!p.isFile())
  209. throw new FileNotFoundException(path);
  210. return new ObjectLoader() {
  211. @Override
  212. public long getSize() {
  213. return p.length();
  214. }
  215. @Override
  216. public int getType() {
  217. return Constants.OBJ_BLOB;
  218. }
  219. @Override
  220. public ObjectStream openStream() throws MissingObjectException,
  221. IOException {
  222. final FileInputStream in = new FileInputStream(p);
  223. final long sz = in.getChannel().size();
  224. final int type = getType();
  225. final BufferedInputStream b = new BufferedInputStream(in);
  226. return new ObjectStream.Filter(type, sz, b);
  227. }
  228. @Override
  229. public boolean isLarge() {
  230. return true;
  231. }
  232. @Override
  233. public byte[] getCachedBytes() throws LargeObjectException {
  234. throw new LargeObjectException();
  235. }
  236. };
  237. }
  238. }
  239. /** A pair of sources to access the old and new sides of a DiffEntry. */
  240. public static final class Pair {
  241. private final ContentSource oldSource;
  242. private final ContentSource newSource;
  243. /**
  244. * Construct a pair of sources.
  245. *
  246. * @param oldSource
  247. * source to read the old side of a DiffEntry.
  248. * @param newSource
  249. * source to read the new side of a DiffEntry.
  250. */
  251. public Pair(ContentSource oldSource, ContentSource newSource) {
  252. this.oldSource = oldSource;
  253. this.newSource = newSource;
  254. }
  255. /**
  256. * Determine the size of the object.
  257. *
  258. * @param side
  259. * which side of the entry to read (OLD or NEW).
  260. * @param ent
  261. * the entry to examine.
  262. * @return the size in bytes.
  263. * @throws IOException
  264. * the file cannot be accessed.
  265. */
  266. public long size(DiffEntry.Side side, DiffEntry ent) throws IOException {
  267. switch (side) {
  268. case OLD:
  269. return oldSource.size(ent.oldPath, ent.oldId.toObjectId());
  270. case NEW:
  271. return newSource.size(ent.newPath, ent.newId.toObjectId());
  272. default:
  273. throw new IllegalArgumentException();
  274. }
  275. }
  276. /**
  277. * Open the object.
  278. *
  279. * @param side
  280. * which side of the entry to read (OLD or NEW).
  281. * @param ent
  282. * the entry to examine.
  283. * @return a loader that can supply the content of the file. The loader
  284. * must be used before another loader can be obtained from this
  285. * same source.
  286. * @throws IOException
  287. * the file cannot be accessed.
  288. */
  289. public ObjectLoader open(DiffEntry.Side side, DiffEntry ent)
  290. throws IOException {
  291. switch (side) {
  292. case OLD:
  293. return oldSource.open(ent.oldPath, ent.oldId.toObjectId());
  294. case NEW:
  295. return newSource.open(ent.newPath, ent.newId.toObjectId());
  296. default:
  297. throw new IllegalArgumentException();
  298. }
  299. }
  300. }
  301. }