選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

UnpackedObjectTest.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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.internal.storage.file;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import java.io.ByteArrayInputStream;
  50. import java.io.ByteArrayOutputStream;
  51. import java.io.File;
  52. import java.io.FileInputStream;
  53. import java.io.FileOutputStream;
  54. import java.io.IOException;
  55. import java.io.InputStream;
  56. import java.text.MessageFormat;
  57. import java.util.Arrays;
  58. import java.util.zip.DeflaterOutputStream;
  59. import org.eclipse.jgit.errors.CorruptObjectException;
  60. import org.eclipse.jgit.errors.LargeObjectException;
  61. import org.eclipse.jgit.internal.JGitText;
  62. import org.eclipse.jgit.junit.JGitTestUtil;
  63. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  64. import org.eclipse.jgit.junit.TestRng;
  65. import org.eclipse.jgit.lib.Constants;
  66. import org.eclipse.jgit.lib.ObjectId;
  67. import org.eclipse.jgit.lib.ObjectInserter;
  68. import org.eclipse.jgit.lib.ObjectLoader;
  69. import org.eclipse.jgit.lib.ObjectStream;
  70. import org.eclipse.jgit.storage.file.WindowCacheConfig;
  71. import org.eclipse.jgit.util.FileUtils;
  72. import org.eclipse.jgit.util.IO;
  73. import org.junit.After;
  74. import org.junit.Before;
  75. import org.junit.Test;
  76. public class UnpackedObjectTest extends LocalDiskRepositoryTestCase {
  77. private int streamThreshold = 16 * 1024;
  78. private TestRng rng;
  79. private FileRepository repo;
  80. private WindowCursor wc;
  81. private TestRng getRng() {
  82. if (rng == null)
  83. rng = new TestRng(JGitTestUtil.getName());
  84. return rng;
  85. }
  86. @Before
  87. public void setUp() throws Exception {
  88. super.setUp();
  89. WindowCacheConfig cfg = new WindowCacheConfig();
  90. cfg.setStreamFileThreshold(streamThreshold);
  91. cfg.install();
  92. repo = createBareRepository();
  93. wc = (WindowCursor) repo.newObjectReader();
  94. }
  95. @After
  96. public void tearDown() throws Exception {
  97. if (wc != null)
  98. wc.close();
  99. new WindowCacheConfig().install();
  100. super.tearDown();
  101. }
  102. @Test
  103. public void testStandardFormat_SmallObject() throws Exception {
  104. final int type = Constants.OBJ_BLOB;
  105. byte[] data = getRng().nextBytes(300);
  106. byte[] gz = compressStandardFormat(type, data);
  107. ObjectId id = ObjectId.zeroId();
  108. ObjectLoader ol = UnpackedObject.open(new ByteArrayInputStream(gz),
  109. path(id), id, wc);
  110. assertNotNull("created loader", ol);
  111. assertEquals(type, ol.getType());
  112. assertEquals(data.length, ol.getSize());
  113. assertFalse("is not large", ol.isLarge());
  114. assertTrue("same content", Arrays.equals(data, ol.getCachedBytes()));
  115. ObjectStream in = ol.openStream();
  116. assertNotNull("have stream", in);
  117. assertEquals(type, in.getType());
  118. assertEquals(data.length, in.getSize());
  119. byte[] data2 = new byte[data.length];
  120. IO.readFully(in, data2, 0, data.length);
  121. assertTrue("same content", Arrays.equals(data2, data));
  122. assertEquals("stream at EOF", -1, in.read());
  123. in.close();
  124. }
  125. @Test
  126. public void testStandardFormat_LargeObject() throws Exception {
  127. final int type = Constants.OBJ_BLOB;
  128. byte[] data = getRng().nextBytes(streamThreshold + 5);
  129. ObjectId id = getId(type, data);
  130. write(id, compressStandardFormat(type, data));
  131. ObjectLoader ol;
  132. {
  133. FileInputStream fs = new FileInputStream(path(id));
  134. try {
  135. ol = UnpackedObject.open(fs, path(id), id, wc);
  136. } finally {
  137. fs.close();
  138. }
  139. }
  140. assertNotNull("created loader", ol);
  141. assertEquals(type, ol.getType());
  142. assertEquals(data.length, ol.getSize());
  143. assertTrue("is large", ol.isLarge());
  144. try {
  145. ol.getCachedBytes();
  146. fail("Should have thrown LargeObjectException");
  147. } catch (LargeObjectException tooBig) {
  148. assertEquals(MessageFormat.format(
  149. JGitText.get().largeObjectException, id.name()), tooBig
  150. .getMessage());
  151. }
  152. ObjectStream in = ol.openStream();
  153. assertNotNull("have stream", in);
  154. assertEquals(type, in.getType());
  155. assertEquals(data.length, in.getSize());
  156. byte[] data2 = new byte[data.length];
  157. IO.readFully(in, data2, 0, data.length);
  158. assertTrue("same content", Arrays.equals(data2, data));
  159. assertEquals("stream at EOF", -1, in.read());
  160. in.close();
  161. }
  162. @Test
  163. public void testStandardFormat_NegativeSize() throws Exception {
  164. ObjectId id = ObjectId.zeroId();
  165. byte[] data = getRng().nextBytes(300);
  166. try {
  167. byte[] gz = compressStandardFormat("blob", "-1", data);
  168. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  169. fail("Did not throw CorruptObjectException");
  170. } catch (CorruptObjectException coe) {
  171. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  172. id.name(), JGitText.get().corruptObjectNegativeSize), coe
  173. .getMessage());
  174. }
  175. }
  176. @Test
  177. public void testStandardFormat_InvalidType() throws Exception {
  178. ObjectId id = ObjectId.zeroId();
  179. byte[] data = getRng().nextBytes(300);
  180. try {
  181. byte[] gz = compressStandardFormat("not.a.type", "1", data);
  182. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  183. fail("Did not throw CorruptObjectException");
  184. } catch (CorruptObjectException coe) {
  185. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  186. id.name(), JGitText.get().corruptObjectInvalidType), coe
  187. .getMessage());
  188. }
  189. }
  190. @Test
  191. public void testStandardFormat_NoHeader() throws Exception {
  192. ObjectId id = ObjectId.zeroId();
  193. byte[] data = {};
  194. try {
  195. byte[] gz = compressStandardFormat("", "", data);
  196. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  197. fail("Did not throw CorruptObjectException");
  198. } catch (CorruptObjectException coe) {
  199. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  200. id.name(), JGitText.get().corruptObjectNoHeader), coe
  201. .getMessage());
  202. }
  203. }
  204. @Test
  205. public void testStandardFormat_GarbageAfterSize() throws Exception {
  206. ObjectId id = ObjectId.zeroId();
  207. byte[] data = getRng().nextBytes(300);
  208. try {
  209. byte[] gz = compressStandardFormat("blob", "1foo", data);
  210. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  211. fail("Did not throw CorruptObjectException");
  212. } catch (CorruptObjectException coe) {
  213. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  214. id.name(), JGitText.get().corruptObjectGarbageAfterSize),
  215. coe.getMessage());
  216. }
  217. }
  218. @Test
  219. public void testStandardFormat_SmallObject_CorruptZLibStream()
  220. throws Exception {
  221. ObjectId id = ObjectId.zeroId();
  222. byte[] data = getRng().nextBytes(300);
  223. try {
  224. byte[] gz = compressStandardFormat(Constants.OBJ_BLOB, data);
  225. for (int i = 5; i < gz.length; i++)
  226. gz[i] = 0;
  227. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  228. fail("Did not throw CorruptObjectException");
  229. } catch (CorruptObjectException coe) {
  230. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  231. id.name(), JGitText.get().corruptObjectBadStream), coe
  232. .getMessage());
  233. }
  234. }
  235. @Test
  236. public void testStandardFormat_SmallObject_TruncatedZLibStream()
  237. throws Exception {
  238. ObjectId id = ObjectId.zeroId();
  239. byte[] data = getRng().nextBytes(300);
  240. try {
  241. byte[] gz = compressStandardFormat(Constants.OBJ_BLOB, data);
  242. byte[] tr = new byte[gz.length - 1];
  243. System.arraycopy(gz, 0, tr, 0, tr.length);
  244. UnpackedObject.open(new ByteArrayInputStream(tr), path(id), id, wc);
  245. fail("Did not throw CorruptObjectException");
  246. } catch (CorruptObjectException coe) {
  247. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  248. id.name(), JGitText.get().corruptObjectBadStream), coe
  249. .getMessage());
  250. }
  251. }
  252. @Test
  253. public void testStandardFormat_SmallObject_TrailingGarbage()
  254. throws Exception {
  255. ObjectId id = ObjectId.zeroId();
  256. byte[] data = getRng().nextBytes(300);
  257. try {
  258. byte[] gz = compressStandardFormat(Constants.OBJ_BLOB, data);
  259. byte[] tr = new byte[gz.length + 1];
  260. System.arraycopy(gz, 0, tr, 0, gz.length);
  261. UnpackedObject.open(new ByteArrayInputStream(tr), path(id), id, wc);
  262. fail("Did not throw CorruptObjectException");
  263. } catch (CorruptObjectException coe) {
  264. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  265. id.name(), JGitText.get().corruptObjectBadStream), coe
  266. .getMessage());
  267. }
  268. }
  269. @Test
  270. public void testStandardFormat_LargeObject_CorruptZLibStream()
  271. throws Exception {
  272. final int type = Constants.OBJ_BLOB;
  273. byte[] data = getRng().nextBytes(streamThreshold + 5);
  274. ObjectId id = getId(type, data);
  275. byte[] gz = compressStandardFormat(type, data);
  276. gz[gz.length - 1] = 0;
  277. gz[gz.length - 2] = 0;
  278. write(id, gz);
  279. ObjectLoader ol;
  280. {
  281. FileInputStream fs = new FileInputStream(path(id));
  282. try {
  283. ol = UnpackedObject.open(fs, path(id), id, wc);
  284. } finally {
  285. fs.close();
  286. }
  287. }
  288. try {
  289. byte[] tmp = new byte[data.length];
  290. InputStream in = ol.openStream();
  291. try {
  292. IO.readFully(in, tmp, 0, tmp.length);
  293. } finally {
  294. in.close();
  295. }
  296. fail("Did not throw CorruptObjectException");
  297. } catch (CorruptObjectException coe) {
  298. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  299. id.name(), JGitText.get().corruptObjectBadStream), coe
  300. .getMessage());
  301. }
  302. }
  303. @Test
  304. public void testStandardFormat_LargeObject_TruncatedZLibStream()
  305. throws Exception {
  306. final int type = Constants.OBJ_BLOB;
  307. byte[] data = getRng().nextBytes(streamThreshold + 5);
  308. ObjectId id = getId(type, data);
  309. byte[] gz = compressStandardFormat(type, data);
  310. byte[] tr = new byte[gz.length - 1];
  311. System.arraycopy(gz, 0, tr, 0, tr.length);
  312. write(id, tr);
  313. ObjectLoader ol;
  314. {
  315. FileInputStream fs = new FileInputStream(path(id));
  316. try {
  317. ol = UnpackedObject.open(fs, path(id), id, wc);
  318. } finally {
  319. fs.close();
  320. }
  321. }
  322. byte[] tmp = new byte[data.length];
  323. InputStream in = ol.openStream();
  324. IO.readFully(in, tmp, 0, tmp.length);
  325. try {
  326. in.close();
  327. fail("close did not throw CorruptObjectException");
  328. } catch (CorruptObjectException coe) {
  329. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  330. id.name(), JGitText.get().corruptObjectBadStream), coe
  331. .getMessage());
  332. }
  333. }
  334. @Test
  335. public void testStandardFormat_LargeObject_TrailingGarbage()
  336. throws Exception {
  337. final int type = Constants.OBJ_BLOB;
  338. byte[] data = getRng().nextBytes(streamThreshold + 5);
  339. ObjectId id = getId(type, data);
  340. byte[] gz = compressStandardFormat(type, data);
  341. byte[] tr = new byte[gz.length + 1];
  342. System.arraycopy(gz, 0, tr, 0, gz.length);
  343. write(id, tr);
  344. ObjectLoader ol;
  345. {
  346. FileInputStream fs = new FileInputStream(path(id));
  347. try {
  348. ol = UnpackedObject.open(fs, path(id), id, wc);
  349. } finally {
  350. fs.close();
  351. }
  352. }
  353. byte[] tmp = new byte[data.length];
  354. InputStream in = ol.openStream();
  355. IO.readFully(in, tmp, 0, tmp.length);
  356. try {
  357. in.close();
  358. fail("close did not throw CorruptObjectException");
  359. } catch (CorruptObjectException coe) {
  360. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  361. id.name(), JGitText.get().corruptObjectBadStream), coe
  362. .getMessage());
  363. }
  364. }
  365. @Test
  366. public void testPackFormat_SmallObject() throws Exception {
  367. final int type = Constants.OBJ_BLOB;
  368. byte[] data = getRng().nextBytes(300);
  369. byte[] gz = compressPackFormat(type, data);
  370. ObjectId id = ObjectId.zeroId();
  371. ObjectLoader ol = UnpackedObject.open(new ByteArrayInputStream(gz),
  372. path(id), id, wc);
  373. assertNotNull("created loader", ol);
  374. assertEquals(type, ol.getType());
  375. assertEquals(data.length, ol.getSize());
  376. assertFalse("is not large", ol.isLarge());
  377. assertTrue("same content", Arrays.equals(data, ol.getCachedBytes()));
  378. ObjectStream in = ol.openStream();
  379. assertNotNull("have stream", in);
  380. assertEquals(type, in.getType());
  381. assertEquals(data.length, in.getSize());
  382. byte[] data2 = new byte[data.length];
  383. IO.readFully(in, data2, 0, data.length);
  384. assertTrue("same content", Arrays.equals(data, ol.getCachedBytes()));
  385. in.close();
  386. }
  387. @Test
  388. public void testPackFormat_LargeObject() throws Exception {
  389. final int type = Constants.OBJ_BLOB;
  390. byte[] data = getRng().nextBytes(streamThreshold + 5);
  391. ObjectId id = getId(type, data);
  392. write(id, compressPackFormat(type, data));
  393. ObjectLoader ol;
  394. {
  395. FileInputStream fs = new FileInputStream(path(id));
  396. try {
  397. ol = UnpackedObject.open(fs, path(id), id, wc);
  398. } finally {
  399. fs.close();
  400. }
  401. }
  402. assertNotNull("created loader", ol);
  403. assertEquals(type, ol.getType());
  404. assertEquals(data.length, ol.getSize());
  405. assertTrue("is large", ol.isLarge());
  406. try {
  407. ol.getCachedBytes();
  408. fail("Should have thrown LargeObjectException");
  409. } catch (LargeObjectException tooBig) {
  410. assertEquals(MessageFormat.format(
  411. JGitText.get().largeObjectException, id.name()), tooBig
  412. .getMessage());
  413. }
  414. ObjectStream in = ol.openStream();
  415. assertNotNull("have stream", in);
  416. assertEquals(type, in.getType());
  417. assertEquals(data.length, in.getSize());
  418. byte[] data2 = new byte[data.length];
  419. IO.readFully(in, data2, 0, data.length);
  420. assertTrue("same content", Arrays.equals(data2, data));
  421. assertEquals("stream at EOF", -1, in.read());
  422. in.close();
  423. }
  424. @Test
  425. public void testPackFormat_DeltaNotAllowed() throws Exception {
  426. ObjectId id = ObjectId.zeroId();
  427. byte[] data = getRng().nextBytes(300);
  428. try {
  429. byte[] gz = compressPackFormat(Constants.OBJ_OFS_DELTA, data);
  430. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  431. fail("Did not throw CorruptObjectException");
  432. } catch (CorruptObjectException coe) {
  433. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  434. id.name(), JGitText.get().corruptObjectInvalidType), coe
  435. .getMessage());
  436. }
  437. try {
  438. byte[] gz = compressPackFormat(Constants.OBJ_REF_DELTA, data);
  439. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  440. fail("Did not throw CorruptObjectException");
  441. } catch (CorruptObjectException coe) {
  442. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  443. id.name(), JGitText.get().corruptObjectInvalidType), coe
  444. .getMessage());
  445. }
  446. try {
  447. byte[] gz = compressPackFormat(Constants.OBJ_TYPE_5, data);
  448. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  449. fail("Did not throw CorruptObjectException");
  450. } catch (CorruptObjectException coe) {
  451. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  452. id.name(), JGitText.get().corruptObjectInvalidType), coe
  453. .getMessage());
  454. }
  455. try {
  456. byte[] gz = compressPackFormat(Constants.OBJ_EXT, data);
  457. UnpackedObject.open(new ByteArrayInputStream(gz), path(id), id, wc);
  458. fail("Did not throw CorruptObjectException");
  459. } catch (CorruptObjectException coe) {
  460. assertEquals(MessageFormat.format(JGitText.get().objectIsCorrupt,
  461. id.name(), JGitText.get().corruptObjectInvalidType), coe
  462. .getMessage());
  463. }
  464. }
  465. private static byte[] compressStandardFormat(int type, byte[] data)
  466. throws IOException {
  467. String typeString = Constants.typeString(type);
  468. String length = String.valueOf(data.length);
  469. return compressStandardFormat(typeString, length, data);
  470. }
  471. private static byte[] compressStandardFormat(String type, String length,
  472. byte[] data) throws IOException {
  473. ByteArrayOutputStream out = new ByteArrayOutputStream();
  474. DeflaterOutputStream d = new DeflaterOutputStream(out);
  475. d.write(Constants.encodeASCII(type));
  476. d.write(' ');
  477. d.write(Constants.encodeASCII(length));
  478. d.write(0);
  479. d.write(data);
  480. d.finish();
  481. return out.toByteArray();
  482. }
  483. private static byte[] compressPackFormat(int type, byte[] data)
  484. throws IOException {
  485. byte[] hdr = new byte[64];
  486. int rawLength = data.length;
  487. int nextLength = rawLength >>> 4;
  488. hdr[0] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (type << 4) | (rawLength & 0x0F));
  489. rawLength = nextLength;
  490. int n = 1;
  491. while (rawLength > 0) {
  492. nextLength >>>= 7;
  493. hdr[n++] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (rawLength & 0x7F));
  494. rawLength = nextLength;
  495. }
  496. final ByteArrayOutputStream out = new ByteArrayOutputStream();
  497. out.write(hdr, 0, n);
  498. DeflaterOutputStream d = new DeflaterOutputStream(out);
  499. d.write(data);
  500. d.finish();
  501. return out.toByteArray();
  502. }
  503. private File path(ObjectId id) {
  504. return repo.getObjectDatabase().fileFor(id);
  505. }
  506. private void write(ObjectId id, byte[] data) throws IOException {
  507. File path = path(id);
  508. FileUtils.mkdirs(path.getParentFile());
  509. FileOutputStream out = new FileOutputStream(path);
  510. try {
  511. out.write(data);
  512. } finally {
  513. out.close();
  514. }
  515. }
  516. private ObjectId getId(int type, byte[] data) {
  517. try (ObjectInserter.Formatter formatter = new ObjectInserter.Formatter()) {
  518. return formatter.idFor(type, data);
  519. }
  520. }
  521. }