PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/jgit-2.0.0.201206130900-r/org.eclipse.jgit/src/org/eclipse/jgit/diff/ContentSource.java

#
Java | 338 lines | 178 code | 36 blank | 124 comment | 8 complexity | 26e1df3cbb0e78f9e5c22e4bcf507dc8 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. long contentLength = ptr.getEntryContentLength();
  168. InputStream in = ptr.openEntryStream();
  169. in = new BufferedInputStream(in);
  170. return new ObjectStream.Filter(getType(), contentLength, in);
  171. }
  172. @Override
  173. public boolean isLarge() {
  174. return true;
  175. }
  176. @Override
  177. public byte[] getCachedBytes() throws LargeObjectException {
  178. throw new LargeObjectException();
  179. }
  180. };
  181. }
  182. private void seek(String path) throws IOException {
  183. if (!path.equals(current)) {
  184. iterator.reset();
  185. tw.reset();
  186. tw.addTree(iterator);
  187. tw.setFilter(PathFilter.create(path));
  188. current = path;
  189. if (!tw.next())
  190. throw new FileNotFoundException(path);
  191. ptr = tw.getTree(0, WorkingTreeIterator.class);
  192. if (ptr == null)
  193. throw new FileNotFoundException(path);
  194. }
  195. }
  196. }
  197. private static class FileSource extends ContentSource {
  198. private final File root;
  199. FileSource(File root) {
  200. this.root = root;
  201. }
  202. @Override
  203. public long size(String path, ObjectId id) throws IOException {
  204. return new File(root, path).length();
  205. }
  206. @Override
  207. public ObjectLoader open(String path, ObjectId id) throws IOException {
  208. final File p = new File(root, path);
  209. if (!p.isFile())
  210. throw new FileNotFoundException(path);
  211. return new ObjectLoader() {
  212. @Override
  213. public long getSize() {
  214. return p.length();
  215. }
  216. @Override
  217. public int getType() {
  218. return Constants.OBJ_BLOB;
  219. }
  220. @Override
  221. public ObjectStream openStream() throws MissingObjectException,
  222. IOException {
  223. final FileInputStream in = new FileInputStream(p);
  224. final long sz = in.getChannel().size();
  225. final int type = getType();
  226. final BufferedInputStream b = new BufferedInputStream(in);
  227. return new ObjectStream.Filter(type, sz, b);
  228. }
  229. @Override
  230. public boolean isLarge() {
  231. return true;
  232. }
  233. @Override
  234. public byte[] getCachedBytes() throws LargeObjectException {
  235. throw new LargeObjectException();
  236. }
  237. };
  238. }
  239. }
  240. /** A pair of sources to access the old and new sides of a DiffEntry. */
  241. public static final class Pair {
  242. private final ContentSource oldSource;
  243. private final ContentSource newSource;
  244. /**
  245. * Construct a pair of sources.
  246. *
  247. * @param oldSource
  248. * source to read the old side of a DiffEntry.
  249. * @param newSource
  250. * source to read the new side of a DiffEntry.
  251. */
  252. public Pair(ContentSource oldSource, ContentSource newSource) {
  253. this.oldSource = oldSource;
  254. this.newSource = newSource;
  255. }
  256. /**
  257. * Determine the size of the object.
  258. *
  259. * @param side
  260. * which side of the entry to read (OLD or NEW).
  261. * @param ent
  262. * the entry to examine.
  263. * @return the size in bytes.
  264. * @throws IOException
  265. * the file cannot be accessed.
  266. */
  267. public long size(DiffEntry.Side side, DiffEntry ent) throws IOException {
  268. switch (side) {
  269. case OLD:
  270. return oldSource.size(ent.oldPath, ent.oldId.toObjectId());
  271. case NEW:
  272. return newSource.size(ent.newPath, ent.newId.toObjectId());
  273. default:
  274. throw new IllegalArgumentException();
  275. }
  276. }
  277. /**
  278. * Open the object.
  279. *
  280. * @param side
  281. * which side of the entry to read (OLD or NEW).
  282. * @param ent
  283. * the entry to examine.
  284. * @return a loader that can supply the content of the file. The loader
  285. * must be used before another loader can be obtained from this
  286. * same source.
  287. * @throws IOException
  288. * the file cannot be accessed.
  289. */
  290. public ObjectLoader open(DiffEntry.Side side, DiffEntry ent)
  291. throws IOException {
  292. switch (side) {
  293. case OLD:
  294. return oldSource.open(ent.oldPath, ent.oldId.toObjectId());
  295. case NEW:
  296. return newSource.open(ent.newPath, ent.newId.toObjectId());
  297. default:
  298. throw new IllegalArgumentException();
  299. }
  300. }
  301. }
  302. }