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

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