aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulian Ruppel <julian.ruppel@sap.com>2021-09-18 15:02:03 +0200
committerThomas Wolf <thomas.wolf@paranor.ch>2021-11-22 02:22:03 -0500
commit00f6fe72a744af56364b88fe2b59b8e7ab6d6ce9 (patch)
treea36d61d6fa0241512e13defbf67241a53b9335ec
parent2e2a3642c3775d0cd3c7184fd9b45a53dcb47629 (diff)
downloadjgit-00f6fe72a744af56364b88fe2b59b8e7ab6d6ce9.tar.gz
jgit-00f6fe72a744af56364b88fe2b59b8e7ab6d6ce9.zip
Support commit.template config property
Fixes an issue that commit template file could not be found if it has a relative path instead of absolute path. Relative path is probably common if git config --local is used. Bug: 446355 Change-Id: I8ddf2be672647be825fd9c01af82809d31bb8356
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java51
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitConfig.java17
2 files changed, 58 insertions, 10 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java
index fe3c1db502..9ee54d5b60 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java
@@ -55,6 +55,7 @@ import org.eclipse.jgit.junit.JGitTestUtil;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.merge.MergeConfig;
import org.eclipse.jgit.storage.file.FileBasedConfig;
+import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.SystemReader;
@@ -1471,14 +1472,17 @@ public class ConfigTest {
// no values defined nowhere
Config config = new Config(null);
assertNull(config.get(CommitConfig.KEY).getCommitTemplatePath());
- assertNull(config.get(CommitConfig.KEY).getCommitTemplateContent());
+ assertNull(config.get(CommitConfig.KEY)
+ .getCommitTemplateContent(null));
}
@Test
public void testCommitTemplateConfig()
throws ConfigInvalidException, IOException {
+ File workTree = tmp.newFolder("dummy-worktree");
File tempFile = tmp.newFile("testCommitTemplate-");
+ Repository repo = FileRepositoryBuilder.create(workTree);
String templateContent = "content of the template";
JGitTestUtil.write(tempFile, templateContent);
String expectedTemplatePath = tempFile.getPath();
@@ -1492,7 +1496,32 @@ public class ConfigTest {
.getCommitEncoding();
assertEquals(expectedTemplatePath, templatePath);
assertEquals(templateContent,
- config.get(CommitConfig.KEY).getCommitTemplateContent());
+ config.get(CommitConfig.KEY).getCommitTemplateContent(repo));
+ assertNull("no commitEncoding has been set so it must be null",
+ commitEncoding);
+ }
+
+ @Test
+ public void testCommitTemplateConfigRelativePath()
+ throws ConfigInvalidException, IOException {
+
+ File workTree = tmp.newFolder("dummy-worktree");
+ File tempFile = tmp.newFile("testCommitTemplate-");
+ String templateContent = "content of the template";
+ JGitTestUtil.write(tempFile, templateContent);
+ String expectedTemplatePath = "../" + tempFile.getName();
+
+ Config config = parse(
+ "[commit]\n\ttemplate = " + expectedTemplatePath + "\n");
+
+ String templatePath = config.get(CommitConfig.KEY)
+ .getCommitTemplatePath();
+ String commitEncoding = config.get(CommitConfig.KEY)
+ .getCommitEncoding();
+ assertEquals(expectedTemplatePath, templatePath);
+ assertEquals(templateContent, config.get(CommitConfig.KEY)
+ .getCommitTemplateContent(
+ new RepositoryBuilder().setWorkTree(workTree).build()));
assertNull("no commitEncoding has been set so it must be null",
commitEncoding);
}
@@ -1501,6 +1530,8 @@ public class ConfigTest {
public void testCommitTemplateEncoding()
throws ConfigInvalidException, IOException {
Config config = new Config(null);
+ File workTree = tmp.newFolder("dummy-worktree");
+ Repository repo = FileRepositoryBuilder.create(workTree);
File tempFile = tmp.newFile("testCommitTemplate-");
String templateContent = "content of the template";
JGitTestUtil.write(tempFile, templateContent);
@@ -1508,7 +1539,7 @@ public class ConfigTest {
config = parse("[i18n]\n\tcommitEncoding = utf-8\n"
+ "[commit]\n\ttemplate = " + expectedTemplatePath + "\n");
assertEquals(templateContent,
- config.get(CommitConfig.KEY).getCommitTemplateContent());
+ config.get(CommitConfig.KEY).getCommitTemplateContent(repo));
String commitEncoding = config.get(CommitConfig.KEY)
.getCommitEncoding();
assertEquals("commitEncoding has been set to utf-8 it must be utf-8",
@@ -1520,6 +1551,8 @@ public class ConfigTest {
throws ConfigInvalidException, IOException {
Config config = new Config(null);
File tempFile = tmp.newFile("testCommitTemplate-");
+ File workTree = tmp.newFolder("dummy-worktree");
+ Repository repo = FileRepositoryBuilder.create(workTree);
String templateContent = "content of the template";
JGitTestUtil.write(tempFile, templateContent);
// proper evaluation of the ~/ directory
@@ -1535,35 +1568,39 @@ public class ConfigTest {
.getCommitTemplatePath();
assertEquals(expectedTemplatePath, templatePath);
assertEquals(templateContent,
- config.get(CommitConfig.KEY).getCommitTemplateContent());
+ config.get(CommitConfig.KEY).getCommitTemplateContent(repo));
}
@Test(expected = ConfigInvalidException.class)
public void testCommitTemplateWithInvalidEncoding()
throws ConfigInvalidException, IOException {
Config config = new Config(null);
+ File workTree = tmp.newFolder("dummy-worktree");
File tempFile = tmp.newFile("testCommitTemplate-");
+ Repository repo = FileRepositoryBuilder.create(workTree);
String templateContent = "content of the template";
JGitTestUtil.write(tempFile, templateContent);
config = parse("[i18n]\n\tcommitEncoding = invalidEcoding\n"
+ "[commit]\n\ttemplate = " + tempFile.getPath() + "\n");
- config.get(CommitConfig.KEY).getCommitTemplateContent();
+ config.get(CommitConfig.KEY).getCommitTemplateContent(repo);
}
@Test(expected = FileNotFoundException.class)
public void testCommitTemplateWithInvalidPath()
throws ConfigInvalidException, IOException {
Config config = new Config(null);
+ File workTree = tmp.newFolder("dummy-worktree");
File tempFile = tmp.newFile("testCommitTemplate-");
+ Repository repo = FileRepositoryBuilder.create(workTree);
String templateContent = "content of the template";
JGitTestUtil.write(tempFile, templateContent);
// commit message encoding
- String expectedTemplatePath = "nonExistingTemplate";
+ String expectedTemplatePath = "/nonExistingTemplate";
config = parse("[commit]\n\ttemplate = " + expectedTemplatePath + "\n");
String templatePath = config.get(CommitConfig.KEY)
.getCommitTemplatePath();
assertEquals(expectedTemplatePath, templatePath);
- config.get(CommitConfig.KEY).getCommitTemplateContent();
+ config.get(CommitConfig.KEY).getCommitTemplateContent(repo);
}
private static void assertValueRoundTrip(String value)
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitConfig.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitConfig.java
index e4e7cd6e03..5e70556a3c 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitConfig.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/CommitConfig.java
@@ -18,6 +18,8 @@ import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.text.MessageFormat;
+
+import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.internal.JGitText;
@@ -77,6 +79,9 @@ public class CommitConfig {
* {@code commit.template}. If no {@code i18n.commitEncoding} is specified,
* UTF-8 fallback is used.
*
+ * @param repository
+ * to resolve relative path in local git repo config
+ *
* @return content of the commit template or {@code null} if not present.
* @throws IOException
* if the template file can not be read
@@ -86,7 +91,7 @@ public class CommitConfig {
* if a {@code commitEncoding} is specified and is invalid
*/
@Nullable
- public String getCommitTemplateContent()
+ public String getCommitTemplateContent(@NonNull Repository repository)
throws FileNotFoundException, IOException, ConfigInvalidException {
if (commitTemplatePath == null) {
@@ -94,11 +99,17 @@ public class CommitConfig {
}
File commitTemplateFile;
+ FS fileSystem = repository.getFS();
if (commitTemplatePath.startsWith("~/")) { //$NON-NLS-1$
- commitTemplateFile = FS.DETECTED.resolve(FS.DETECTED.userHome(),
+ commitTemplateFile = fileSystem.resolve(fileSystem.userHome(),
commitTemplatePath.substring(2));
} else {
- commitTemplateFile = FS.DETECTED.resolve(null, commitTemplatePath);
+ commitTemplateFile = fileSystem.resolve(null, commitTemplatePath);
+ }
+ if (!commitTemplateFile.isAbsolute()) {
+ commitTemplateFile = fileSystem.resolve(
+ repository.getWorkTree().getAbsoluteFile(),
+ commitTemplatePath);
}
Charset commitMessageEncoding = getEncoding();