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
This commit is contained in:
parent
2e2a3642c3
commit
00f6fe72a7
|
@ -55,6 +55,7 @@
|
||||||
import org.eclipse.jgit.junit.MockSystemReader;
|
import org.eclipse.jgit.junit.MockSystemReader;
|
||||||
import org.eclipse.jgit.merge.MergeConfig;
|
import org.eclipse.jgit.merge.MergeConfig;
|
||||||
import org.eclipse.jgit.storage.file.FileBasedConfig;
|
import org.eclipse.jgit.storage.file.FileBasedConfig;
|
||||||
|
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
|
||||||
import org.eclipse.jgit.transport.RefSpec;
|
import org.eclipse.jgit.transport.RefSpec;
|
||||||
import org.eclipse.jgit.util.FS;
|
import org.eclipse.jgit.util.FS;
|
||||||
import org.eclipse.jgit.util.SystemReader;
|
import org.eclipse.jgit.util.SystemReader;
|
||||||
|
@ -1471,14 +1472,17 @@ public void testCommitTemplateEmptyConfig()
|
||||||
// no values defined nowhere
|
// no values defined nowhere
|
||||||
Config config = new Config(null);
|
Config config = new Config(null);
|
||||||
assertNull(config.get(CommitConfig.KEY).getCommitTemplatePath());
|
assertNull(config.get(CommitConfig.KEY).getCommitTemplatePath());
|
||||||
assertNull(config.get(CommitConfig.KEY).getCommitTemplateContent());
|
assertNull(config.get(CommitConfig.KEY)
|
||||||
|
.getCommitTemplateContent(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCommitTemplateConfig()
|
public void testCommitTemplateConfig()
|
||||||
throws ConfigInvalidException, IOException {
|
throws ConfigInvalidException, IOException {
|
||||||
|
|
||||||
|
File workTree = tmp.newFolder("dummy-worktree");
|
||||||
File tempFile = tmp.newFile("testCommitTemplate-");
|
File tempFile = tmp.newFile("testCommitTemplate-");
|
||||||
|
Repository repo = FileRepositoryBuilder.create(workTree);
|
||||||
String templateContent = "content of the template";
|
String templateContent = "content of the template";
|
||||||
JGitTestUtil.write(tempFile, templateContent);
|
JGitTestUtil.write(tempFile, templateContent);
|
||||||
String expectedTemplatePath = tempFile.getPath();
|
String expectedTemplatePath = tempFile.getPath();
|
||||||
|
@ -1492,7 +1496,32 @@ public void testCommitTemplateConfig()
|
||||||
.getCommitEncoding();
|
.getCommitEncoding();
|
||||||
assertEquals(expectedTemplatePath, templatePath);
|
assertEquals(expectedTemplatePath, templatePath);
|
||||||
assertEquals(templateContent,
|
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",
|
assertNull("no commitEncoding has been set so it must be null",
|
||||||
commitEncoding);
|
commitEncoding);
|
||||||
}
|
}
|
||||||
|
@ -1501,6 +1530,8 @@ public void testCommitTemplateConfig()
|
||||||
public void testCommitTemplateEncoding()
|
public void testCommitTemplateEncoding()
|
||||||
throws ConfigInvalidException, IOException {
|
throws ConfigInvalidException, IOException {
|
||||||
Config config = new Config(null);
|
Config config = new Config(null);
|
||||||
|
File workTree = tmp.newFolder("dummy-worktree");
|
||||||
|
Repository repo = FileRepositoryBuilder.create(workTree);
|
||||||
File tempFile = tmp.newFile("testCommitTemplate-");
|
File tempFile = tmp.newFile("testCommitTemplate-");
|
||||||
String templateContent = "content of the template";
|
String templateContent = "content of the template";
|
||||||
JGitTestUtil.write(tempFile, templateContent);
|
JGitTestUtil.write(tempFile, templateContent);
|
||||||
|
@ -1508,7 +1539,7 @@ public void testCommitTemplateEncoding()
|
||||||
config = parse("[i18n]\n\tcommitEncoding = utf-8\n"
|
config = parse("[i18n]\n\tcommitEncoding = utf-8\n"
|
||||||
+ "[commit]\n\ttemplate = " + expectedTemplatePath + "\n");
|
+ "[commit]\n\ttemplate = " + expectedTemplatePath + "\n");
|
||||||
assertEquals(templateContent,
|
assertEquals(templateContent,
|
||||||
config.get(CommitConfig.KEY).getCommitTemplateContent());
|
config.get(CommitConfig.KEY).getCommitTemplateContent(repo));
|
||||||
String commitEncoding = config.get(CommitConfig.KEY)
|
String commitEncoding = config.get(CommitConfig.KEY)
|
||||||
.getCommitEncoding();
|
.getCommitEncoding();
|
||||||
assertEquals("commitEncoding has been set to utf-8 it must be utf-8",
|
assertEquals("commitEncoding has been set to utf-8 it must be utf-8",
|
||||||
|
@ -1520,6 +1551,8 @@ public void testCommitTemplatePathInHomeDirecory()
|
||||||
throws ConfigInvalidException, IOException {
|
throws ConfigInvalidException, IOException {
|
||||||
Config config = new Config(null);
|
Config config = new Config(null);
|
||||||
File tempFile = tmp.newFile("testCommitTemplate-");
|
File tempFile = tmp.newFile("testCommitTemplate-");
|
||||||
|
File workTree = tmp.newFolder("dummy-worktree");
|
||||||
|
Repository repo = FileRepositoryBuilder.create(workTree);
|
||||||
String templateContent = "content of the template";
|
String templateContent = "content of the template";
|
||||||
JGitTestUtil.write(tempFile, templateContent);
|
JGitTestUtil.write(tempFile, templateContent);
|
||||||
// proper evaluation of the ~/ directory
|
// proper evaluation of the ~/ directory
|
||||||
|
@ -1535,35 +1568,39 @@ public void testCommitTemplatePathInHomeDirecory()
|
||||||
.getCommitTemplatePath();
|
.getCommitTemplatePath();
|
||||||
assertEquals(expectedTemplatePath, templatePath);
|
assertEquals(expectedTemplatePath, templatePath);
|
||||||
assertEquals(templateContent,
|
assertEquals(templateContent,
|
||||||
config.get(CommitConfig.KEY).getCommitTemplateContent());
|
config.get(CommitConfig.KEY).getCommitTemplateContent(repo));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ConfigInvalidException.class)
|
@Test(expected = ConfigInvalidException.class)
|
||||||
public void testCommitTemplateWithInvalidEncoding()
|
public void testCommitTemplateWithInvalidEncoding()
|
||||||
throws ConfigInvalidException, IOException {
|
throws ConfigInvalidException, IOException {
|
||||||
Config config = new Config(null);
|
Config config = new Config(null);
|
||||||
|
File workTree = tmp.newFolder("dummy-worktree");
|
||||||
File tempFile = tmp.newFile("testCommitTemplate-");
|
File tempFile = tmp.newFile("testCommitTemplate-");
|
||||||
|
Repository repo = FileRepositoryBuilder.create(workTree);
|
||||||
String templateContent = "content of the template";
|
String templateContent = "content of the template";
|
||||||
JGitTestUtil.write(tempFile, templateContent);
|
JGitTestUtil.write(tempFile, templateContent);
|
||||||
config = parse("[i18n]\n\tcommitEncoding = invalidEcoding\n"
|
config = parse("[i18n]\n\tcommitEncoding = invalidEcoding\n"
|
||||||
+ "[commit]\n\ttemplate = " + tempFile.getPath() + "\n");
|
+ "[commit]\n\ttemplate = " + tempFile.getPath() + "\n");
|
||||||
config.get(CommitConfig.KEY).getCommitTemplateContent();
|
config.get(CommitConfig.KEY).getCommitTemplateContent(repo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = FileNotFoundException.class)
|
@Test(expected = FileNotFoundException.class)
|
||||||
public void testCommitTemplateWithInvalidPath()
|
public void testCommitTemplateWithInvalidPath()
|
||||||
throws ConfigInvalidException, IOException {
|
throws ConfigInvalidException, IOException {
|
||||||
Config config = new Config(null);
|
Config config = new Config(null);
|
||||||
|
File workTree = tmp.newFolder("dummy-worktree");
|
||||||
File tempFile = tmp.newFile("testCommitTemplate-");
|
File tempFile = tmp.newFile("testCommitTemplate-");
|
||||||
|
Repository repo = FileRepositoryBuilder.create(workTree);
|
||||||
String templateContent = "content of the template";
|
String templateContent = "content of the template";
|
||||||
JGitTestUtil.write(tempFile, templateContent);
|
JGitTestUtil.write(tempFile, templateContent);
|
||||||
// commit message encoding
|
// commit message encoding
|
||||||
String expectedTemplatePath = "nonExistingTemplate";
|
String expectedTemplatePath = "/nonExistingTemplate";
|
||||||
config = parse("[commit]\n\ttemplate = " + expectedTemplatePath + "\n");
|
config = parse("[commit]\n\ttemplate = " + expectedTemplatePath + "\n");
|
||||||
String templatePath = config.get(CommitConfig.KEY)
|
String templatePath = config.get(CommitConfig.KEY)
|
||||||
.getCommitTemplatePath();
|
.getCommitTemplatePath();
|
||||||
assertEquals(expectedTemplatePath, templatePath);
|
assertEquals(expectedTemplatePath, templatePath);
|
||||||
config.get(CommitConfig.KEY).getCommitTemplateContent();
|
config.get(CommitConfig.KEY).getCommitTemplateContent(repo);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void assertValueRoundTrip(String value)
|
private static void assertValueRoundTrip(String value)
|
||||||
|
|
|
@ -18,6 +18,8 @@
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.charset.UnsupportedCharsetException;
|
import java.nio.charset.UnsupportedCharsetException;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
|
import org.eclipse.jgit.annotations.NonNull;
|
||||||
import org.eclipse.jgit.annotations.Nullable;
|
import org.eclipse.jgit.annotations.Nullable;
|
||||||
import org.eclipse.jgit.errors.ConfigInvalidException;
|
import org.eclipse.jgit.errors.ConfigInvalidException;
|
||||||
import org.eclipse.jgit.internal.JGitText;
|
import org.eclipse.jgit.internal.JGitText;
|
||||||
|
@ -77,6 +79,9 @@ public String getCommitEncoding() {
|
||||||
* {@code commit.template}. If no {@code i18n.commitEncoding} is specified,
|
* {@code commit.template}. If no {@code i18n.commitEncoding} is specified,
|
||||||
* UTF-8 fallback is used.
|
* 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.
|
* @return content of the commit template or {@code null} if not present.
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* if the template file can not be read
|
* if the template file can not be read
|
||||||
|
@ -86,7 +91,7 @@ public String getCommitEncoding() {
|
||||||
* if a {@code commitEncoding} is specified and is invalid
|
* if a {@code commitEncoding} is specified and is invalid
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public String getCommitTemplateContent()
|
public String getCommitTemplateContent(@NonNull Repository repository)
|
||||||
throws FileNotFoundException, IOException, ConfigInvalidException {
|
throws FileNotFoundException, IOException, ConfigInvalidException {
|
||||||
|
|
||||||
if (commitTemplatePath == null) {
|
if (commitTemplatePath == null) {
|
||||||
|
@ -94,11 +99,17 @@ public String getCommitTemplateContent()
|
||||||
}
|
}
|
||||||
|
|
||||||
File commitTemplateFile;
|
File commitTemplateFile;
|
||||||
|
FS fileSystem = repository.getFS();
|
||||||
if (commitTemplatePath.startsWith("~/")) { //$NON-NLS-1$
|
if (commitTemplatePath.startsWith("~/")) { //$NON-NLS-1$
|
||||||
commitTemplateFile = FS.DETECTED.resolve(FS.DETECTED.userHome(),
|
commitTemplateFile = fileSystem.resolve(fileSystem.userHome(),
|
||||||
commitTemplatePath.substring(2));
|
commitTemplatePath.substring(2));
|
||||||
} else {
|
} 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();
|
Charset commitMessageEncoding = getEncoding();
|
||||||
|
|
Loading…
Reference in New Issue