PageRenderTime 59ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/main/contrib/NGit/NGit.Diff/ContentSource.cs

https://github.com/jfcantin/monodevelop
C# | 376 lines | 216 code | 44 blank | 116 comment | 8 complexity | a58aa5db320c6e458b05593dc77a8707 MD5 | raw file
  1. /*
  2. This code is derived from jgit (http://eclipse.org/jgit).
  3. Copyright owners are documented in jgit's IP log.
  4. This program and the accompanying materials are made available
  5. under the terms of the Eclipse Distribution License v1.0 which
  6. accompanies this distribution, is reproduced below, and is
  7. available at http://www.eclipse.org/org/documents/edl-v10.php
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or
  10. without modification, are permitted provided that the following
  11. conditions are met:
  12. - Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. - Redistributions in binary form must reproduce the above
  15. copyright notice, this list of conditions and the following
  16. disclaimer in the documentation and/or other materials provided
  17. with the distribution.
  18. - Neither the name of the Eclipse Foundation, Inc. nor the
  19. names of its contributors may be used to endorse or promote
  20. products derived from this software without specific prior
  21. written permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  23. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  24. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  25. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  27. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  29. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  32. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  34. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. using System;
  37. using System.IO;
  38. using NGit;
  39. using NGit.Diff;
  40. using NGit.Errors;
  41. using NGit.Treewalk;
  42. using NGit.Treewalk.Filter;
  43. using Sharpen;
  44. namespace NGit.Diff
  45. {
  46. /// <summary>
  47. /// Supplies the content of a file for
  48. /// <see cref="DiffFormatter">DiffFormatter</see>
  49. /// .
  50. /// A content source is not thread-safe. Sources may contain state, including
  51. /// information about the last ObjectLoader they returned. Callers must be
  52. /// careful to ensure there is no more than one ObjectLoader pending on any
  53. /// source, at any time.
  54. /// </summary>
  55. public abstract class ContentSource
  56. {
  57. /// <summary>Construct a content source for an ObjectReader.</summary>
  58. /// <remarks>Construct a content source for an ObjectReader.</remarks>
  59. /// <param name="reader">the reader to obtain blobs from.</param>
  60. /// <returns>a source wrapping the reader.</returns>
  61. public static ContentSource Create(ObjectReader reader)
  62. {
  63. return new ContentSource.ObjectReaderSource(reader);
  64. }
  65. /// <summary>Construct a content source for a working directory.</summary>
  66. /// <remarks>
  67. /// Construct a content source for a working directory.
  68. /// If the iterator is a
  69. /// <see cref="NGit.Treewalk.FileTreeIterator">NGit.Treewalk.FileTreeIterator</see>
  70. /// an optimized version is
  71. /// used that doesn't require seeking through a TreeWalk.
  72. /// </remarks>
  73. /// <param name="iterator">the iterator to obtain source files through.</param>
  74. /// <returns>a content source wrapping the iterator.</returns>
  75. public static ContentSource Create(WorkingTreeIterator iterator)
  76. {
  77. if (iterator is FileTreeIterator)
  78. {
  79. FileTreeIterator i = (FileTreeIterator)iterator;
  80. return new ContentSource.FileSource(i.GetDirectory());
  81. }
  82. return new ContentSource.WorkingTreeSource(iterator);
  83. }
  84. /// <summary>Determine the size of the object.</summary>
  85. /// <remarks>Determine the size of the object.</remarks>
  86. /// <param name="path">the path of the file, relative to the root of the repository.</param>
  87. /// <param name="id">blob id of the file, if known.</param>
  88. /// <returns>the size in bytes.</returns>
  89. /// <exception cref="System.IO.IOException">the file cannot be accessed.</exception>
  90. public abstract long Size(string path, ObjectId id);
  91. /// <summary>Open the object.</summary>
  92. /// <remarks>Open the object.</remarks>
  93. /// <param name="path">the path of the file, relative to the root of the repository.</param>
  94. /// <param name="id">blob id of the file, if known.</param>
  95. /// <returns>
  96. /// a loader that can supply the content of the file. The loader must
  97. /// be used before another loader can be obtained from this same
  98. /// source.
  99. /// </returns>
  100. /// <exception cref="System.IO.IOException">the file cannot be accessed.</exception>
  101. public abstract ObjectLoader Open(string path, ObjectId id);
  102. private class ObjectReaderSource : ContentSource
  103. {
  104. private readonly ObjectReader reader;
  105. internal ObjectReaderSource(ObjectReader reader)
  106. {
  107. this.reader = reader;
  108. }
  109. /// <exception cref="System.IO.IOException"></exception>
  110. public override long Size(string path, ObjectId id)
  111. {
  112. return reader.GetObjectSize(id, Constants.OBJ_BLOB);
  113. }
  114. /// <exception cref="System.IO.IOException"></exception>
  115. public override ObjectLoader Open(string path, ObjectId id)
  116. {
  117. return reader.Open(id, Constants.OBJ_BLOB);
  118. }
  119. }
  120. private class WorkingTreeSource : ContentSource
  121. {
  122. private readonly TreeWalk tw;
  123. private readonly WorkingTreeIterator iterator;
  124. private string current;
  125. private WorkingTreeIterator ptr;
  126. internal WorkingTreeSource(WorkingTreeIterator iterator)
  127. {
  128. this.tw = new TreeWalk((ObjectReader)null);
  129. this.iterator = iterator;
  130. }
  131. /// <exception cref="System.IO.IOException"></exception>
  132. public override long Size(string path, ObjectId id)
  133. {
  134. Seek(path);
  135. return ptr.GetEntryLength();
  136. }
  137. /// <exception cref="System.IO.IOException"></exception>
  138. public override ObjectLoader Open(string path, ObjectId id)
  139. {
  140. Seek(path);
  141. return new _ObjectLoader_173(this);
  142. }
  143. private sealed class _ObjectLoader_173 : ObjectLoader
  144. {
  145. public _ObjectLoader_173(WorkingTreeSource _enclosing)
  146. {
  147. this._enclosing = _enclosing;
  148. }
  149. public override long GetSize()
  150. {
  151. return this._enclosing.ptr.GetEntryLength();
  152. }
  153. public override int GetType()
  154. {
  155. return this._enclosing.ptr.EntryFileMode.GetObjectType();
  156. }
  157. /// <exception cref="NGit.Errors.MissingObjectException"></exception>
  158. /// <exception cref="System.IO.IOException"></exception>
  159. public override ObjectStream OpenStream()
  160. {
  161. InputStream @in = this._enclosing.ptr.OpenEntryStream();
  162. @in = new BufferedInputStream(@in);
  163. return new ObjectStream.Filter(this.GetType(), this.GetSize(), @in);
  164. }
  165. public override bool IsLarge()
  166. {
  167. return true;
  168. }
  169. /// <exception cref="NGit.Errors.LargeObjectException"></exception>
  170. public override byte[] GetCachedBytes()
  171. {
  172. throw new LargeObjectException();
  173. }
  174. private readonly WorkingTreeSource _enclosing;
  175. }
  176. /// <exception cref="System.IO.IOException"></exception>
  177. private void Seek(string path)
  178. {
  179. if (!path.Equals(current))
  180. {
  181. iterator.Reset();
  182. tw.Reset();
  183. tw.AddTree(iterator);
  184. tw.Filter = PathFilter.Create(path);
  185. current = path;
  186. if (!tw.Next())
  187. {
  188. throw new FileNotFoundException(path);
  189. }
  190. ptr = tw.GetTree<WorkingTreeIterator>(0);
  191. if (ptr == null)
  192. {
  193. throw new FileNotFoundException(path);
  194. }
  195. }
  196. }
  197. }
  198. private class FileSource : ContentSource
  199. {
  200. private readonly FilePath root;
  201. internal FileSource(FilePath root)
  202. {
  203. this.root = root;
  204. }
  205. /// <exception cref="System.IO.IOException"></exception>
  206. public override long Size(string path, ObjectId id)
  207. {
  208. return new FilePath(root, path).Length();
  209. }
  210. /// <exception cref="System.IO.IOException"></exception>
  211. public override ObjectLoader Open(string path, ObjectId id)
  212. {
  213. FilePath p = new FilePath(root, path);
  214. if (!p.IsFile())
  215. {
  216. throw new FileNotFoundException(path);
  217. }
  218. return new _ObjectLoader_237(p);
  219. }
  220. private sealed class _ObjectLoader_237 : ObjectLoader
  221. {
  222. public _ObjectLoader_237(FilePath p)
  223. {
  224. this.p = p;
  225. }
  226. public override long GetSize()
  227. {
  228. return p.Length();
  229. }
  230. public override int GetType()
  231. {
  232. return Constants.OBJ_BLOB;
  233. }
  234. /// <exception cref="NGit.Errors.MissingObjectException"></exception>
  235. /// <exception cref="System.IO.IOException"></exception>
  236. public override ObjectStream OpenStream()
  237. {
  238. FileInputStream @in = new FileInputStream(p);
  239. long sz = @in.GetChannel().Size();
  240. int type = this.GetType();
  241. BufferedInputStream b = new BufferedInputStream(@in);
  242. return new ObjectStream.Filter(type, sz, b);
  243. }
  244. public override bool IsLarge()
  245. {
  246. return true;
  247. }
  248. /// <exception cref="NGit.Errors.LargeObjectException"></exception>
  249. public override byte[] GetCachedBytes()
  250. {
  251. throw new LargeObjectException();
  252. }
  253. private readonly FilePath p;
  254. }
  255. }
  256. /// <summary>A pair of sources to access the old and new sides of a DiffEntry.</summary>
  257. /// <remarks>A pair of sources to access the old and new sides of a DiffEntry.</remarks>
  258. public sealed class Pair
  259. {
  260. private readonly ContentSource oldSource;
  261. private readonly ContentSource newSource;
  262. /// <summary>Construct a pair of sources.</summary>
  263. /// <remarks>Construct a pair of sources.</remarks>
  264. /// <param name="oldSource">source to read the old side of a DiffEntry.</param>
  265. /// <param name="newSource">source to read the new side of a DiffEntry.</param>
  266. public Pair(ContentSource oldSource, ContentSource newSource)
  267. {
  268. this.oldSource = oldSource;
  269. this.newSource = newSource;
  270. }
  271. /// <summary>Determine the size of the object.</summary>
  272. /// <remarks>Determine the size of the object.</remarks>
  273. /// <param name="side">which side of the entry to read (OLD or NEW).</param>
  274. /// <param name="ent">the entry to examine.</param>
  275. /// <returns>the size in bytes.</returns>
  276. /// <exception cref="System.IO.IOException">the file cannot be accessed.</exception>
  277. public long Size(DiffEntry.Side side, DiffEntry ent)
  278. {
  279. switch (side)
  280. {
  281. case DiffEntry.Side.OLD:
  282. {
  283. return oldSource.Size(ent.oldPath, ent.oldId.ToObjectId());
  284. }
  285. case DiffEntry.Side.NEW:
  286. {
  287. return newSource.Size(ent.newPath, ent.newId.ToObjectId());
  288. }
  289. default:
  290. {
  291. throw new ArgumentException();
  292. }
  293. }
  294. }
  295. /// <summary>Open the object.</summary>
  296. /// <remarks>Open the object.</remarks>
  297. /// <param name="side">which side of the entry to read (OLD or NEW).</param>
  298. /// <param name="ent">the entry to examine.</param>
  299. /// <returns>
  300. /// a loader that can supply the content of the file. The loader
  301. /// must be used before another loader can be obtained from this
  302. /// same source.
  303. /// </returns>
  304. /// <exception cref="System.IO.IOException">the file cannot be accessed.</exception>
  305. public ObjectLoader Open(DiffEntry.Side side, DiffEntry ent)
  306. {
  307. switch (side)
  308. {
  309. case DiffEntry.Side.OLD:
  310. {
  311. return oldSource.Open(ent.oldPath, ent.oldId.ToObjectId());
  312. }
  313. case DiffEntry.Side.NEW:
  314. {
  315. return newSource.Open(ent.newPath, ent.newId.ToObjectId());
  316. }
  317. default:
  318. {
  319. throw new ArgumentException();
  320. }
  321. }
  322. }
  323. }
  324. }
  325. }