From ca166a0c62af2ea87fdedf2728ac19cb59a12601 Mon Sep 17 00:00:00 2001 From: Ivan Frade Date: Tue, 23 Aug 2022 12:10:27 -0700 Subject: [PATCH] BaseSuperprojectWriter: report invalid paths as manifest errors An invalid path in the manifest (e.g. '.') is reported by DirCache in a runtime exception. In server context this becomes a 500 instead of a user error. Wrap the runtime invalid path exception into a checked ManifestErrorException that caller can handle. Change-Id: I61a2104922765506ae232334891057bb06141d97 --- .../eclipse/jgit/gitrepo/RepoCommandTest.java | 24 +++++++++++++++++++ .../jgit/gitrepo/BareSuperprojectWriter.java | 3 ++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java index 3e6d13a67e..ca6f2e1053 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java @@ -13,6 +13,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.junit.Assume.assumeTrue; @@ -32,6 +33,7 @@ import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.InvalidRefNameException; import org.eclipse.jgit.api.errors.InvalidRemoteException; +import org.eclipse.jgit.gitrepo.RepoCommand.ManifestErrorException; import org.eclipse.jgit.gitrepo.RepoCommand.RemoteFile; import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.junit.JGitTestUtil; @@ -1337,6 +1339,28 @@ public class RepoCommandTest extends RepositoryTestCase { } } + @Test + public void testInvalidPath() throws Exception { + Repository remoteDb = createBareRepository(); + Repository tempDb = createWorkRepository(); + + StringBuilder xmlContent = new StringBuilder(); + xmlContent.append("\n") + .append("") + .append("") + .append("") + .append("").append(""); + JGitTestUtil.writeTrashFile(tempDb, "manifest.xml", + xmlContent.toString()); + + RepoCommand command = new RepoCommand(remoteDb); + command.setPath( + tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml") + .setURI(rootUri).setRecommendShallow(true); + assertThrows(ManifestErrorException.class, () -> command.call()); + } + private void resolveRelativeUris() { // Find the longest common prefix ends with "/" as rootUri. defaultUri = defaultDb.getDirectory().toURI().toString(); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/BareSuperprojectWriter.java b/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/BareSuperprojectWriter.java index e6626aece3..f63cc6d644 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/BareSuperprojectWriter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/BareSuperprojectWriter.java @@ -23,6 +23,7 @@ import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.dircache.DirCacheBuilder; import org.eclipse.jgit.dircache.DirCacheEntry; +import org.eclipse.jgit.dircache.InvalidPathException; import org.eclipse.jgit.gitrepo.RepoCommand.ManifestErrorException; import org.eclipse.jgit.gitrepo.RepoCommand.RemoteFile; import org.eclipse.jgit.gitrepo.RepoCommand.RemoteReader; @@ -138,7 +139,7 @@ class BareSuperprojectWriter { } // In the last try, just propagate the exceptions return commitTreeOnCurrentTip(inserter, rw, treeId); - } catch (IOException | InterruptedException e) { + } catch (IOException | InterruptedException | InvalidPathException e) { throw new ManifestErrorException(e); } } -- 2.39.5