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.

RepositoryTestCase.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2007-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2006-2007, Shawn O. Pearce <spearce@spearce.org>
  5. * Copyright (C) 2009, Yann Simon <yann.simon.fr@gmail.com> and others
  6. *
  7. * This program and the accompanying materials are made available under the
  8. * terms of the Eclipse Distribution License v. 1.0 which is available at
  9. * https://www.eclipse.org/org/documents/edl-v10.php.
  10. *
  11. * SPDX-License-Identifier: BSD-3-Clause
  12. */
  13. package org.eclipse.jgit.junit;
  14. import static java.nio.charset.StandardCharsets.UTF_8;
  15. import static org.junit.Assert.assertEquals;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.FileNotFoundException;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.io.InputStreamReader;
  22. import java.io.Reader;
  23. import java.nio.file.Path;
  24. import java.time.Instant;
  25. import java.util.Map;
  26. import java.util.concurrent.TimeUnit;
  27. import org.eclipse.jgit.api.Git;
  28. import org.eclipse.jgit.api.errors.GitAPIException;
  29. import org.eclipse.jgit.dircache.DirCacheBuilder;
  30. import org.eclipse.jgit.dircache.DirCacheCheckout;
  31. import org.eclipse.jgit.dircache.DirCacheEntry;
  32. import org.eclipse.jgit.internal.storage.file.FileRepository;
  33. import org.eclipse.jgit.lib.AnyObjectId;
  34. import org.eclipse.jgit.lib.Constants;
  35. import org.eclipse.jgit.lib.FileMode;
  36. import org.eclipse.jgit.lib.ObjectId;
  37. import org.eclipse.jgit.lib.ObjectInserter;
  38. import org.eclipse.jgit.lib.RefUpdate;
  39. import org.eclipse.jgit.lib.Repository;
  40. import org.eclipse.jgit.revwalk.RevCommit;
  41. import org.eclipse.jgit.revwalk.RevWalk;
  42. import org.eclipse.jgit.treewalk.FileTreeIterator;
  43. import org.eclipse.jgit.util.FS;
  44. import org.eclipse.jgit.util.FileUtils;
  45. import org.junit.Before;
  46. /**
  47. * Base class for most JGit unit tests.
  48. *
  49. * Sets up a predefined test repository and has support for creating additional
  50. * repositories and destroying them when the tests are finished.
  51. */
  52. public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase {
  53. /**
  54. * Copy a file
  55. *
  56. * @param src
  57. * @param dst
  58. * @throws IOException
  59. */
  60. protected static void copyFile(File src, File dst)
  61. throws IOException {
  62. try (FileInputStream fis = new FileInputStream(src);
  63. FileOutputStream fos = new FileOutputStream(dst)) {
  64. final byte[] buf = new byte[4096];
  65. int r;
  66. while ((r = fis.read(buf)) > 0) {
  67. fos.write(buf, 0, r);
  68. }
  69. }
  70. }
  71. /**
  72. * Write a trash file
  73. *
  74. * @param name
  75. * @param data
  76. * @return the trash file
  77. * @throws IOException
  78. */
  79. protected File writeTrashFile(String name, String data)
  80. throws IOException {
  81. return JGitTestUtil.writeTrashFile(db, name, data);
  82. }
  83. /**
  84. * Create a symbolic link
  85. *
  86. * @param link
  87. * the path of the symbolic link to create
  88. * @param target
  89. * the target of the symbolic link
  90. * @return the path to the symbolic link
  91. * @throws Exception
  92. * @since 4.2
  93. */
  94. protected Path writeLink(String link, String target)
  95. throws Exception {
  96. return JGitTestUtil.writeLink(db, link, target);
  97. }
  98. /**
  99. * Write a trash file
  100. *
  101. * @param subdir
  102. * @param name
  103. * @param data
  104. * @return the trash file
  105. * @throws IOException
  106. */
  107. protected File writeTrashFile(final String subdir, final String name,
  108. final String data)
  109. throws IOException {
  110. return JGitTestUtil.writeTrashFile(db, subdir, name, data);
  111. }
  112. /**
  113. * Read content of a file
  114. *
  115. * @param name
  116. * @return the file's content
  117. * @throws IOException
  118. */
  119. protected String read(String name) throws IOException {
  120. return JGitTestUtil.read(db, name);
  121. }
  122. /**
  123. * Check if file exists
  124. *
  125. * @param name
  126. * file name
  127. * @return if the file exists
  128. */
  129. protected boolean check(String name) {
  130. return JGitTestUtil.check(db, name);
  131. }
  132. /**
  133. * Delete a trash file
  134. *
  135. * @param name
  136. * file name
  137. * @throws IOException
  138. */
  139. protected void deleteTrashFile(String name) throws IOException {
  140. JGitTestUtil.deleteTrashFile(db, name);
  141. }
  142. /**
  143. * Check content of a file.
  144. *
  145. * @param f
  146. * @param checkData
  147. * expected content
  148. * @throws IOException
  149. */
  150. protected static void checkFile(File f, String checkData)
  151. throws IOException {
  152. try (Reader r = new InputStreamReader(new FileInputStream(f),
  153. UTF_8)) {
  154. if (checkData.length() > 0) {
  155. char[] data = new char[checkData.length()];
  156. assertEquals(data.length, r.read(data));
  157. assertEquals(checkData, new String(data));
  158. }
  159. assertEquals(-1, r.read());
  160. }
  161. }
  162. /** Test repository, initialized for this test case. */
  163. protected FileRepository db;
  164. /** Working directory of {@link #db}. */
  165. protected File trash;
  166. /** {@inheritDoc} */
  167. @Override
  168. @Before
  169. public void setUp() throws Exception {
  170. super.setUp();
  171. db = createWorkRepository();
  172. trash = db.getWorkTree();
  173. }
  174. /**
  175. * Represent the state of the index in one String. This representation is
  176. * useful when writing tests which do assertions on the state of the index.
  177. * By default information about path, mode, stage (if different from 0) is
  178. * included. A bitmask controls which additional info about
  179. * modificationTimes, smudge state and length is included.
  180. * <p>
  181. * The format of the returned string is described with this BNF:
  182. *
  183. * <pre>
  184. * result = ( "[" path mode stage? time? smudge? length? sha1? content? "]" )* .
  185. * mode = ", mode:" number .
  186. * stage = ", stage:" number .
  187. * time = ", time:t" timestamp-index .
  188. * smudge = "" | ", smudged" .
  189. * length = ", length:" number .
  190. * sha1 = ", sha1:" hex-sha1 .
  191. * content = ", content:" blob-data .
  192. * </pre>
  193. *
  194. * 'stage' is only presented when the stage is different from 0. All
  195. * reported time stamps are mapped to strings like "t0", "t1", ... "tn". The
  196. * smallest reported time-stamp will be called "t0". This allows to write
  197. * assertions against the string although the concrete value of the time
  198. * stamps is unknown.
  199. *
  200. * @param includedOptions
  201. * a bitmask constructed out of the constants {@link #MOD_TIME},
  202. * {@link #SMUDGE}, {@link #LENGTH}, {@link #CONTENT_ID} and
  203. * {@link #CONTENT} controlling which info is present in the
  204. * resulting string.
  205. * @return a string encoding the index state
  206. * @throws IllegalStateException
  207. * @throws IOException
  208. */
  209. public String indexState(int includedOptions)
  210. throws IllegalStateException, IOException {
  211. return indexState(db, includedOptions);
  212. }
  213. /**
  214. * Resets the index to represent exactly some filesystem content. E.g. the
  215. * following call will replace the index with the working tree content:
  216. * <p>
  217. * <code>resetIndex(new FileSystemIterator(db))</code>
  218. * <p>
  219. * This method can be used by testcases which first prepare a new commit
  220. * somewhere in the filesystem (e.g. in the working-tree) and then want to
  221. * have an index which matches their prepared content.
  222. *
  223. * @param treeItr
  224. * a {@link org.eclipse.jgit.treewalk.FileTreeIterator} which
  225. * determines which files should go into the new index
  226. * @throws FileNotFoundException
  227. * @throws IOException
  228. */
  229. protected void resetIndex(FileTreeIterator treeItr)
  230. throws FileNotFoundException, IOException {
  231. try (ObjectInserter inserter = db.newObjectInserter()) {
  232. DirCacheBuilder builder = db.lockDirCache().builder();
  233. DirCacheEntry dce;
  234. while (!treeItr.eof()) {
  235. long len = treeItr.getEntryLength();
  236. dce = new DirCacheEntry(treeItr.getEntryPathString());
  237. dce.setFileMode(treeItr.getEntryFileMode());
  238. dce.setLastModified(treeItr.getEntryLastModifiedInstant());
  239. dce.setLength((int) len);
  240. try (FileInputStream in = new FileInputStream(
  241. treeItr.getEntryFile())) {
  242. dce.setObjectId(
  243. inserter.insert(Constants.OBJ_BLOB, len, in));
  244. }
  245. builder.add(dce);
  246. treeItr.next(1);
  247. }
  248. builder.commit();
  249. inserter.flush();
  250. }
  251. }
  252. /**
  253. * Helper method to map arbitrary objects to user-defined names. This can be
  254. * used create short names for objects to produce small and stable debug
  255. * output. It is guaranteed that when you lookup the same object multiple
  256. * times even with different nameTemplates this method will always return
  257. * the same name which was derived from the first nameTemplate.
  258. * nameTemplates can contain "%n" which will be replaced by a running number
  259. * before used as a name.
  260. *
  261. * @param l
  262. * the object to lookup
  263. * @param lookupTable
  264. * a table storing object-name mappings.
  265. * @param nameTemplate
  266. * the name for that object. Can contain "%n" which will be
  267. * replaced by a running number before used as a name. If the
  268. * lookup table already contains the object this parameter will
  269. * be ignored
  270. * @return a name of that object. Is not guaranteed to be unique. Use
  271. * nameTemplates containing "%n" to always have unique names
  272. */
  273. public static String lookup(Object l, String nameTemplate,
  274. Map<Object, String> lookupTable) {
  275. String name = lookupTable.get(l);
  276. if (name == null) {
  277. name = nameTemplate.replaceAll("%n",
  278. Integer.toString(lookupTable.size()));
  279. lookupTable.put(l, name);
  280. }
  281. return name;
  282. }
  283. /**
  284. * Replaces '\' by '/'
  285. *
  286. * @param str
  287. * the string in which backslashes should be replaced
  288. * @return the resulting string with slashes
  289. * @since 4.2
  290. */
  291. public static String slashify(String str) {
  292. str = str.replace('\\', '/');
  293. return str;
  294. }
  295. /**
  296. * Waits until it is guaranteed that a subsequent file modification has a
  297. * younger modification timestamp than the modification timestamp of the
  298. * given file. This is done by touching a temporary file, reading the
  299. * lastmodified attribute and, if needed, sleeping. After sleeping this loop
  300. * starts again until the filesystem timer has advanced enough. The
  301. * temporary file will be created as a sibling of lastFile.
  302. *
  303. * @param lastFile
  304. * the file on which we want to wait until the filesystem timer
  305. * has advanced more than the lastmodification timestamp of this
  306. * file
  307. * @return return the last measured value of the filesystem timer which is
  308. * greater than then the lastmodification time of lastfile.
  309. * @throws InterruptedException
  310. * @throws IOException
  311. */
  312. public static Instant fsTick(File lastFile)
  313. throws InterruptedException,
  314. IOException {
  315. File tmp;
  316. FS fs = FS.DETECTED;
  317. if (lastFile == null) {
  318. lastFile = tmp = File
  319. .createTempFile("fsTickTmpFile", null);
  320. } else {
  321. if (!fs.exists(lastFile)) {
  322. throw new FileNotFoundException(lastFile.getPath());
  323. }
  324. tmp = File.createTempFile("fsTickTmpFile", null,
  325. lastFile.getParentFile());
  326. }
  327. long res = FS.getFileStoreAttributes(tmp.toPath())
  328. .getFsTimestampResolution().toNanos();
  329. long sleepTime = res / 10;
  330. try {
  331. Instant startTime = fs.lastModifiedInstant(lastFile);
  332. Instant actTime = fs.lastModifiedInstant(tmp);
  333. while (actTime.compareTo(startTime) <= 0) {
  334. TimeUnit.NANOSECONDS.sleep(sleepTime);
  335. FileUtils.touch(tmp.toPath());
  336. actTime = fs.lastModifiedInstant(tmp);
  337. }
  338. return actTime;
  339. } finally {
  340. FileUtils.delete(tmp);
  341. }
  342. }
  343. /**
  344. * Create a branch
  345. *
  346. * @param objectId
  347. * @param branchName
  348. * @throws IOException
  349. */
  350. protected void createBranch(ObjectId objectId, String branchName)
  351. throws IOException {
  352. RefUpdate updateRef = db.updateRef(branchName);
  353. updateRef.setNewObjectId(objectId);
  354. updateRef.update();
  355. }
  356. /**
  357. * Checkout a branch
  358. *
  359. * @param branchName
  360. * @throws IllegalStateException
  361. * @throws IOException
  362. */
  363. protected void checkoutBranch(String branchName)
  364. throws IllegalStateException, IOException {
  365. try (RevWalk walk = new RevWalk(db)) {
  366. RevCommit head = walk.parseCommit(db.resolve(Constants.HEAD));
  367. RevCommit branch = walk.parseCommit(db.resolve(branchName));
  368. DirCacheCheckout dco = new DirCacheCheckout(db,
  369. head.getTree().getId(), db.lockDirCache(),
  370. branch.getTree().getId());
  371. dco.setFailOnConflict(true);
  372. dco.checkout();
  373. }
  374. // update the HEAD
  375. RefUpdate refUpdate = db.updateRef(Constants.HEAD);
  376. refUpdate.setRefLogMessage("checkout: moving to " + branchName, false);
  377. refUpdate.link(branchName);
  378. }
  379. /**
  380. * Writes a number of files in the working tree. The first content specified
  381. * will be written into a file named '0', the second into a file named "1"
  382. * and so on. If <code>null</code> is specified as content then this file is
  383. * skipped.
  384. *
  385. * @param ensureDistinctTimestamps
  386. * if set to <code>true</code> then between two write operations
  387. * this method will wait to ensure that the second file will get
  388. * a different lastmodification timestamp than the first file.
  389. * @param contents
  390. * the contents which should be written into the files
  391. * @return the File object associated to the last written file.
  392. * @throws IOException
  393. * @throws InterruptedException
  394. */
  395. protected File writeTrashFiles(boolean ensureDistinctTimestamps,
  396. String... contents)
  397. throws IOException, InterruptedException {
  398. File f = null;
  399. for (int i = 0; i < contents.length; i++)
  400. if (contents[i] != null) {
  401. if (ensureDistinctTimestamps && (f != null))
  402. fsTick(f);
  403. f = writeTrashFile(Integer.toString(i), contents[i]);
  404. }
  405. return f;
  406. }
  407. /**
  408. * Commit a file with the specified contents on the specified branch,
  409. * creating the branch if it didn't exist before.
  410. * <p>
  411. * It switches back to the original branch after the commit if there was
  412. * one.
  413. *
  414. * @param filename
  415. * @param contents
  416. * @param branch
  417. * @return the created commit
  418. */
  419. protected RevCommit commitFile(String filename, String contents, String branch) {
  420. try (Git git = new Git(db)) {
  421. Repository repo = git.getRepository();
  422. String originalBranch = repo.getFullBranch();
  423. boolean empty = repo.resolve(Constants.HEAD) == null;
  424. if (!empty) {
  425. if (repo.findRef(branch) == null)
  426. git.branchCreate().setName(branch).call();
  427. git.checkout().setName(branch).call();
  428. }
  429. writeTrashFile(filename, contents);
  430. git.add().addFilepattern(filename).call();
  431. RevCommit commit = git.commit()
  432. .setMessage(branch + ": " + filename).call();
  433. if (originalBranch != null)
  434. git.checkout().setName(originalBranch).call();
  435. else if (empty)
  436. git.branchCreate().setName(branch).setStartPoint(commit).call();
  437. return commit;
  438. } catch (IOException | GitAPIException e) {
  439. throw new RuntimeException(e);
  440. }
  441. }
  442. /**
  443. * Create <code>DirCacheEntry</code>
  444. *
  445. * @param path
  446. * @param mode
  447. * @return the DirCacheEntry
  448. */
  449. protected DirCacheEntry createEntry(String path, FileMode mode) {
  450. return createEntry(path, mode, DirCacheEntry.STAGE_0, path);
  451. }
  452. /**
  453. * Create <code>DirCacheEntry</code>
  454. *
  455. * @param path
  456. * @param mode
  457. * @param content
  458. * @return the DirCacheEntry
  459. */
  460. protected DirCacheEntry createEntry(final String path, final FileMode mode,
  461. final String content) {
  462. return createEntry(path, mode, DirCacheEntry.STAGE_0, content);
  463. }
  464. /**
  465. * Create <code>DirCacheEntry</code>
  466. *
  467. * @param path
  468. * @param mode
  469. * @param stage
  470. * @param content
  471. * @return the DirCacheEntry
  472. */
  473. protected DirCacheEntry createEntry(final String path, final FileMode mode,
  474. final int stage, final String content) {
  475. final DirCacheEntry entry = new DirCacheEntry(path, stage);
  476. entry.setFileMode(mode);
  477. try (ObjectInserter.Formatter formatter = new ObjectInserter.Formatter()) {
  478. entry.setObjectId(formatter.idFor(
  479. Constants.OBJ_BLOB, Constants.encode(content)));
  480. }
  481. return entry;
  482. }
  483. /**
  484. * Create <code>DirCacheEntry</code>
  485. *
  486. * @param path
  487. * @param objectId
  488. * @return the DirCacheEntry
  489. */
  490. protected DirCacheEntry createGitLink(String path, AnyObjectId objectId) {
  491. final DirCacheEntry entry = new DirCacheEntry(path,
  492. DirCacheEntry.STAGE_0);
  493. entry.setFileMode(FileMode.GITLINK);
  494. entry.setObjectId(objectId);
  495. return entry;
  496. }
  497. /**
  498. * Assert files are equal
  499. *
  500. * @param expected
  501. * @param actual
  502. * @throws IOException
  503. */
  504. public static void assertEqualsFile(File expected, File actual)
  505. throws IOException {
  506. assertEquals(expected.getCanonicalFile(), actual.getCanonicalFile());
  507. }
  508. }