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.

TreeFormatter.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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.lib;
  44. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
  45. import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
  46. import static org.eclipse.jgit.lib.Constants.encode;
  47. import static org.eclipse.jgit.lib.FileMode.GITLINK;
  48. import static org.eclipse.jgit.lib.FileMode.REGULAR_FILE;
  49. import static org.eclipse.jgit.lib.FileMode.TREE;
  50. import java.io.IOException;
  51. import org.eclipse.jgit.errors.CorruptObjectException;
  52. import org.eclipse.jgit.revwalk.RevBlob;
  53. import org.eclipse.jgit.revwalk.RevCommit;
  54. import org.eclipse.jgit.revwalk.RevTree;
  55. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  56. import org.eclipse.jgit.util.TemporaryBuffer;
  57. /**
  58. * Mutable formatter to construct a single tree object.
  59. *
  60. * This formatter does not process subtrees. Callers must handle creating each
  61. * subtree on their own.
  62. *
  63. * To maintain good performance for bulk operations, this formatter does not
  64. * validate its input. Callers are responsible for ensuring the resulting tree
  65. * object is correctly well formed by writing entries in the correct order.
  66. */
  67. public class TreeFormatter {
  68. /**
  69. * Compute the size of a tree entry record.
  70. *
  71. * This method can be used to estimate the correct size of a tree prior to
  72. * allocating a formatter. Getting the size correct at allocation time
  73. * ensures the internal buffer is sized correctly, reducing copying.
  74. *
  75. * @param mode
  76. * the mode the entry will have.
  77. * @param nameLen
  78. * the length of the name, in bytes.
  79. * @return the length of the record.
  80. */
  81. public static int entrySize(FileMode mode, int nameLen) {
  82. return mode.copyToLength() + nameLen + OBJECT_ID_LENGTH + 2;
  83. }
  84. private byte[] buf;
  85. private int ptr;
  86. private TemporaryBuffer.Heap overflowBuffer;
  87. /** Create an empty formatter with a default buffer size. */
  88. public TreeFormatter() {
  89. this(8192);
  90. }
  91. /**
  92. * Create an empty formatter with the specified buffer size.
  93. *
  94. * @param size
  95. * estimated size of the tree, in bytes. Callers can use
  96. * {@link #entrySize(FileMode, int)} to estimate the size of each
  97. * entry in advance of allocating the formatter.
  98. */
  99. public TreeFormatter(int size) {
  100. buf = new byte[size];
  101. }
  102. /**
  103. * Add a link to a submodule commit, mode is {@link FileMode#GITLINK}.
  104. *
  105. * @param name
  106. * name of the entry.
  107. * @param commit
  108. * the ObjectId to store in this entry.
  109. */
  110. public void append(String name, RevCommit commit) {
  111. append(name, GITLINK, commit);
  112. }
  113. /**
  114. * Add a subtree, mode is {@link FileMode#TREE}.
  115. *
  116. * @param name
  117. * name of the entry.
  118. * @param tree
  119. * the ObjectId to store in this entry.
  120. */
  121. public void append(String name, RevTree tree) {
  122. append(name, TREE, tree);
  123. }
  124. /**
  125. * Add a regular file, mode is {@link FileMode#REGULAR_FILE}.
  126. *
  127. * @param name
  128. * name of the entry.
  129. * @param blob
  130. * the ObjectId to store in this entry.
  131. */
  132. public void append(String name, RevBlob blob) {
  133. append(name, REGULAR_FILE, blob);
  134. }
  135. /**
  136. * Append any entry to the tree.
  137. *
  138. * @param name
  139. * name of the entry.
  140. * @param mode
  141. * mode describing the treatment of {@code id}.
  142. * @param id
  143. * the ObjectId to store in this entry.
  144. */
  145. public void append(String name, FileMode mode, AnyObjectId id) {
  146. append(encode(name), mode, id);
  147. }
  148. /**
  149. * Append any entry to the tree.
  150. *
  151. * @param name
  152. * name of the entry. The name should be UTF-8 encoded, but file
  153. * name encoding is not a well defined concept in Git.
  154. * @param mode
  155. * mode describing the treatment of {@code id}.
  156. * @param id
  157. * the ObjectId to store in this entry.
  158. */
  159. public void append(byte[] name, FileMode mode, AnyObjectId id) {
  160. append(name, 0, name.length, mode, id);
  161. }
  162. /**
  163. * Append any entry to the tree.
  164. *
  165. * @param nameBuf
  166. * buffer holding the name of the entry. The name should be UTF-8
  167. * encoded, but file name encoding is not a well defined concept
  168. * in Git.
  169. * @param namePos
  170. * first position within {@code nameBuf} of the name data.
  171. * @param nameLen
  172. * number of bytes from {@code nameBuf} to use as the name.
  173. * @param mode
  174. * mode describing the treatment of {@code id}.
  175. * @param id
  176. * the ObjectId to store in this entry.
  177. */
  178. public void append(byte[] nameBuf, int namePos, int nameLen, FileMode mode,
  179. AnyObjectId id) {
  180. if (fmtBuf(nameBuf, namePos, nameLen, mode)) {
  181. id.copyRawTo(buf, ptr);
  182. ptr += OBJECT_ID_LENGTH;
  183. } else {
  184. try {
  185. fmtOverflowBuffer(nameBuf, namePos, nameLen, mode);
  186. id.copyRawTo(overflowBuffer);
  187. } catch (IOException badBuffer) {
  188. // This should never occur.
  189. throw new RuntimeException(badBuffer);
  190. }
  191. }
  192. }
  193. /**
  194. * Append any entry to the tree.
  195. *
  196. * @param nameBuf
  197. * buffer holding the name of the entry. The name should be UTF-8
  198. * encoded, but file name encoding is not a well defined concept
  199. * in Git.
  200. * @param namePos
  201. * first position within {@code nameBuf} of the name data.
  202. * @param nameLen
  203. * number of bytes from {@code nameBuf} to use as the name.
  204. * @param mode
  205. * mode describing the treatment of {@code id}.
  206. * @param idBuf
  207. * buffer holding the raw ObjectId of the entry.
  208. * @param idPos
  209. * first position within {@code idBuf} to copy the id from.
  210. */
  211. public void append(byte[] nameBuf, int namePos, int nameLen, FileMode mode,
  212. byte[] idBuf, int idPos) {
  213. if (fmtBuf(nameBuf, namePos, nameLen, mode)) {
  214. System.arraycopy(idBuf, idPos, buf, ptr, OBJECT_ID_LENGTH);
  215. ptr += OBJECT_ID_LENGTH;
  216. } else {
  217. try {
  218. fmtOverflowBuffer(nameBuf, namePos, nameLen, mode);
  219. overflowBuffer.write(idBuf, idPos, OBJECT_ID_LENGTH);
  220. } catch (IOException badBuffer) {
  221. // This should never occur.
  222. throw new RuntimeException(badBuffer);
  223. }
  224. }
  225. }
  226. private boolean fmtBuf(byte[] nameBuf, int namePos, int nameLen,
  227. FileMode mode) {
  228. if (buf == null || buf.length < ptr + entrySize(mode, nameLen))
  229. return false;
  230. mode.copyTo(buf, ptr);
  231. ptr += mode.copyToLength();
  232. buf[ptr++] = ' ';
  233. System.arraycopy(nameBuf, namePos, buf, ptr, nameLen);
  234. ptr += nameLen;
  235. buf[ptr++] = 0;
  236. return true;
  237. }
  238. private void fmtOverflowBuffer(byte[] nameBuf, int namePos, int nameLen,
  239. FileMode mode) throws IOException {
  240. if (buf != null) {
  241. overflowBuffer = new TemporaryBuffer.Heap(Integer.MAX_VALUE);
  242. overflowBuffer.write(buf, 0, ptr);
  243. buf = null;
  244. }
  245. mode.copyTo(overflowBuffer);
  246. overflowBuffer.write((byte) ' ');
  247. overflowBuffer.write(nameBuf, namePos, nameLen);
  248. overflowBuffer.write((byte) 0);
  249. }
  250. /**
  251. * Insert this tree and obtain its ObjectId.
  252. *
  253. * @param ins
  254. * the inserter to store the tree.
  255. * @return computed ObjectId of the tree
  256. * @throws IOException
  257. * the tree could not be stored.
  258. */
  259. public ObjectId insertTo(ObjectInserter ins) throws IOException {
  260. if (buf != null)
  261. return ins.insert(OBJ_TREE, buf, 0, ptr);
  262. final long len = overflowBuffer.length();
  263. return ins.insert(OBJ_TREE, len, overflowBuffer.openInputStream());
  264. }
  265. /**
  266. * Compute the ObjectId for this tree
  267. *
  268. * @param ins
  269. * @return ObjectId for this tree
  270. */
  271. public ObjectId computeId(ObjectInserter ins) {
  272. if (buf != null)
  273. return ins.idFor(OBJ_TREE, buf, 0, ptr);
  274. final long len = overflowBuffer.length();
  275. try {
  276. return ins.idFor(OBJ_TREE, len, overflowBuffer.openInputStream());
  277. } catch (IOException e) {
  278. // this should never happen
  279. throw new RuntimeException(e);
  280. }
  281. }
  282. /**
  283. * Copy this formatter's buffer into a byte array.
  284. *
  285. * This method is not efficient, as it needs to create a copy of the
  286. * internal buffer in order to supply an array of the correct size to the
  287. * caller. If the buffer is just to pass to an ObjectInserter, consider
  288. * using {@link ObjectInserter#insert(TreeFormatter)} instead.
  289. *
  290. * @return a copy of this formatter's buffer.
  291. */
  292. public byte[] toByteArray() {
  293. if (buf != null) {
  294. byte[] r = new byte[ptr];
  295. System.arraycopy(buf, 0, r, 0, ptr);
  296. return r;
  297. }
  298. try {
  299. return overflowBuffer.toByteArray();
  300. } catch (IOException err) {
  301. // This should never happen, its read failure on a byte array.
  302. throw new RuntimeException(err);
  303. }
  304. }
  305. @SuppressWarnings("nls")
  306. @Override
  307. public String toString() {
  308. byte[] raw = toByteArray();
  309. CanonicalTreeParser p = new CanonicalTreeParser();
  310. p.reset(raw);
  311. StringBuilder r = new StringBuilder();
  312. r.append("Tree={");
  313. if (!p.eof()) {
  314. r.append('\n');
  315. try {
  316. new ObjectChecker().checkTree(raw);
  317. } catch (CorruptObjectException error) {
  318. r.append("*** ERROR: ").append(error.getMessage()).append("\n");
  319. r.append('\n');
  320. }
  321. }
  322. while (!p.eof()) {
  323. final FileMode mode = p.getEntryFileMode();
  324. r.append(mode);
  325. r.append(' ');
  326. r.append(Constants.typeString(mode.getObjectType()));
  327. r.append(' ');
  328. r.append(p.getEntryObjectId().name());
  329. r.append(' ');
  330. r.append(p.getEntryPathString());
  331. r.append('\n');
  332. p.next();
  333. }
  334. r.append("}");
  335. return r.toString();
  336. }
  337. }