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.

GitConstructionTest.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2011, Christian Halstrick <christian.halstrick@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.api;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.fail;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import org.eclipse.jgit.api.ListBranchCommand.ListMode;
  16. import org.eclipse.jgit.api.errors.GitAPIException;
  17. import org.eclipse.jgit.api.errors.JGitInternalException;
  18. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  19. import org.eclipse.jgit.junit.RepositoryTestCase;
  20. import org.eclipse.jgit.lib.Repository;
  21. import org.eclipse.jgit.util.FileUtils;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. public class GitConstructionTest extends RepositoryTestCase {
  25. private Repository bareRepo;
  26. @Override
  27. @Before
  28. public void setUp() throws Exception {
  29. super.setUp();
  30. try (Git git = new Git(db)) {
  31. git.commit().setMessage("initial commit").call();
  32. writeTrashFile("Test.txt", "Hello world");
  33. git.add().addFilepattern("Test.txt").call();
  34. git.commit().setMessage("Initial commit").call();
  35. }
  36. bareRepo = Git.cloneRepository().setBare(true)
  37. .setURI(db.getDirectory().toURI().toString())
  38. .setDirectory(createUniqueTestGitDir(true)).call()
  39. .getRepository();
  40. addRepoToClose(bareRepo);
  41. }
  42. @Test
  43. public void testWrap() throws JGitInternalException, GitAPIException {
  44. Git git = Git.wrap(db);
  45. assertEquals(1, git.branchList().call().size());
  46. git = Git.wrap(bareRepo);
  47. assertEquals(1, git.branchList().setListMode(ListMode.ALL).call()
  48. .size());
  49. try {
  50. Git.wrap(null);
  51. fail("Expected exception has not been thrown");
  52. } catch (NullPointerException e) {
  53. // should not get here
  54. }
  55. }
  56. @Test
  57. public void testOpen() throws IOException, JGitInternalException,
  58. GitAPIException {
  59. Git git = Git.open(db.getDirectory());
  60. assertEquals(1, git.branchList().call().size());
  61. git = Git.open(bareRepo.getDirectory());
  62. assertEquals(1, git.branchList().setListMode(ListMode.ALL).call()
  63. .size());
  64. git = Git.open(db.getWorkTree());
  65. assertEquals(1, git.branchList().setListMode(ListMode.ALL).call()
  66. .size());
  67. try {
  68. Git.open(db.getObjectsDirectory());
  69. fail("Expected exception has not been thrown");
  70. } catch (RepositoryNotFoundException e) {
  71. // should not get here
  72. }
  73. }
  74. @Test
  75. /**
  76. * Tests that a repository with packfiles can be deleted after calling
  77. * Git.close(). On Windows the first try to delete the worktree will fail
  78. * (because file handles on packfiles are still open) but the second
  79. * attempt after a close will succeed.
  80. *
  81. * @throws IOException
  82. * @throws JGitInternalException
  83. * @throws GitAPIException
  84. */
  85. public void testClose() throws IOException, JGitInternalException,
  86. GitAPIException {
  87. File workTree = db.getWorkTree();
  88. Git git = Git.open(workTree);
  89. git.gc().setExpire(null).call();
  90. git.checkout().setName(git.getRepository().resolve("HEAD^").getName())
  91. .call();
  92. try {
  93. FileUtils.delete(workTree, FileUtils.RECURSIVE);
  94. } catch (IOException e) {
  95. git.close();
  96. FileUtils.delete(workTree, FileUtils.RECURSIVE);
  97. }
  98. }
  99. }