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.

FileRepositoryBuilderTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (C) 2010, Google Inc. 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.internal.storage.file;
  11. import static java.nio.charset.StandardCharsets.UTF_8;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertNotNull;
  14. import static org.junit.Assert.assertTrue;
  15. import static org.junit.Assert.fail;
  16. import java.io.BufferedWriter;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.nio.file.Files;
  20. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  21. import org.eclipse.jgit.lib.ConfigConstants;
  22. import org.eclipse.jgit.lib.Constants;
  23. import org.eclipse.jgit.lib.Repository;
  24. import org.eclipse.jgit.lib.StoredConfig;
  25. import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
  26. import org.eclipse.jgit.util.FileUtils;
  27. import org.junit.Test;
  28. public class FileRepositoryBuilderTest extends LocalDiskRepositoryTestCase {
  29. @Test
  30. public void testShouldAutomagicallyDetectGitDirectory() throws Exception {
  31. Repository r = createWorkRepository();
  32. File d = new File(r.getDirectory(), "sub-dir");
  33. FileUtils.mkdir(d);
  34. assertEquals(r.getDirectory(), new FileRepositoryBuilder()
  35. .findGitDir(d).getGitDir());
  36. }
  37. @Test
  38. public void emptyRepositoryFormatVersion() throws Exception {
  39. Repository r = createWorkRepository();
  40. StoredConfig config = r.getConfig();
  41. config.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
  42. ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, "");
  43. config.save();
  44. try (FileRepository repo = new FileRepository(r.getDirectory())) {
  45. // Unused
  46. }
  47. }
  48. @Test
  49. public void invalidRepositoryFormatVersion() throws Exception {
  50. Repository r = createWorkRepository();
  51. StoredConfig config = r.getConfig();
  52. config.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
  53. ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, "notanumber");
  54. config.save();
  55. try (FileRepository repo = new FileRepository(r.getDirectory())) {
  56. fail("IllegalArgumentException not thrown");
  57. } catch (IllegalArgumentException e) {
  58. assertNotNull(e.getMessage());
  59. }
  60. }
  61. @Test
  62. public void unknownRepositoryFormatVersion() throws Exception {
  63. Repository r = createWorkRepository();
  64. StoredConfig config = r.getConfig();
  65. config.setLong(ConfigConstants.CONFIG_CORE_SECTION, null,
  66. ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 999999);
  67. config.save();
  68. try (FileRepository repo = new FileRepository(r.getDirectory())) {
  69. fail("IOException not thrown");
  70. } catch (IOException e) {
  71. assertNotNull(e.getMessage());
  72. }
  73. }
  74. @Test
  75. public void absoluteGitDirRef() throws Exception {
  76. Repository repo1 = createWorkRepository();
  77. File dir = createTempDirectory("dir");
  78. File dotGit = new File(dir, Constants.DOT_GIT);
  79. try (BufferedWriter writer = Files.newBufferedWriter(dotGit.toPath(),
  80. UTF_8)) {
  81. writer.append("gitdir: " + repo1.getDirectory().getAbsolutePath());
  82. }
  83. FileRepositoryBuilder builder = new FileRepositoryBuilder();
  84. builder.setWorkTree(dir);
  85. builder.setMustExist(true);
  86. Repository repo2 = builder.build();
  87. assertEquals(repo1.getDirectory().getAbsolutePath(),
  88. repo2.getDirectory().getAbsolutePath());
  89. assertEquals(dir, repo2.getWorkTree());
  90. }
  91. @Test
  92. public void relativeGitDirRef() throws Exception {
  93. Repository repo1 = createWorkRepository();
  94. File dir = new File(repo1.getWorkTree(), "dir");
  95. assertTrue(dir.mkdir());
  96. File dotGit = new File(dir, Constants.DOT_GIT);
  97. try (BufferedWriter writer = Files.newBufferedWriter(dotGit.toPath(),
  98. UTF_8)) {
  99. writer.append("gitdir: ../" + Constants.DOT_GIT);
  100. }
  101. FileRepositoryBuilder builder = new FileRepositoryBuilder();
  102. builder.setWorkTree(dir);
  103. builder.setMustExist(true);
  104. Repository repo2 = builder.build();
  105. // The tmp directory may be a symlink so the actual path
  106. // may not
  107. assertEquals(repo1.getDirectory().getCanonicalPath(),
  108. repo2.getDirectory().getCanonicalPath());
  109. assertEquals(dir, repo2.getWorkTree());
  110. }
  111. @Test
  112. public void scanWithGitDirRef() throws Exception {
  113. Repository repo1 = createWorkRepository();
  114. File dir = createTempDirectory("dir");
  115. File dotGit = new File(dir, Constants.DOT_GIT);
  116. try (BufferedWriter writer = Files.newBufferedWriter(dotGit.toPath(),
  117. UTF_8)) {
  118. writer.append(
  119. "gitdir: " + repo1.getDirectory().getAbsolutePath());
  120. }
  121. FileRepositoryBuilder builder = new FileRepositoryBuilder();
  122. builder.setWorkTree(dir);
  123. builder.findGitDir(dir);
  124. assertEquals(repo1.getDirectory().getAbsolutePath(),
  125. builder.getGitDir().getAbsolutePath());
  126. builder.setMustExist(true);
  127. Repository repo2 = builder.build();
  128. // The tmp directory may be a symlink
  129. assertEquals(repo1.getDirectory().getCanonicalPath(),
  130. repo2.getDirectory().getCanonicalPath());
  131. assertEquals(dir, repo2.getWorkTree());
  132. }
  133. }