From a378ad97e39b4d96e375a2f67e3b141d148123f0 Mon Sep 17 00:00:00 2001 From: Rene Pfeuffer Date: Mon, 26 Jan 2026 13:43:28 +0000 Subject: [PATCH] Fix error in cat command on empty repository --- gradle/changelog/cat_command.yaml | 2 + .../scm/repository/spi/GitCatCommand.java | 5 +- .../GitCatCommand_onEmptyRepositoryTest.java | 52 +++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 gradle/changelog/cat_command.yaml create mode 100644 scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitCatCommand_onEmptyRepositoryTest.java diff --git a/gradle/changelog/cat_command.yaml b/gradle/changelog/cat_command.yaml new file mode 100644 index 0000000000..bfb73f2260 --- /dev/null +++ b/gradle/changelog/cat_command.yaml @@ -0,0 +1,2 @@ +- type: fixed + description: Internal server error for cat command on empty repository diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitCatCommand.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitCatCommand.java index c3ade3a7d5..cd2fb16e59 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitCatCommand.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitCatCommand.java @@ -85,7 +85,10 @@ public class GitCatCommand extends AbstractGitCommand implements CatCommand { org.eclipse.jgit.lib.Repository repo = open(); ObjectId revId = getCommitOrDefault(repo, request.getRevision()); if (revId == null) { - throw notFound(entity("Revision", request.getRevision()).in(repository)); + throw notFound(entity( + "Revision", + request.getRevision() == null ? "default" : request.getRevision() + ).in(repository)); } LOG.debug("loading content for file {} for revision {} in repository {}", request.getPath(), revId, repository); return getLoader(repo, revId, request.getPath()); diff --git a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitCatCommand_onEmptyRepositoryTest.java b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitCatCommand_onEmptyRepositoryTest.java new file mode 100644 index 0000000000..77bc30a0e3 --- /dev/null +++ b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitCatCommand_onEmptyRepositoryTest.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2020 - present Cloudogu GmbH + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Affero General Public License as published by the Free + * Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more + * details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see https://www.gnu.org/licenses/. + */ + +package sonia.scm.repository.spi; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import sonia.scm.NotFoundException; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +public class GitCatCommand_onEmptyRepositoryTest extends AbstractGitCommandTestBase { + + @Rule + public final ExpectedException expectedException = ExpectedException.none(); + + @Test(expected = NotFoundException.class) + public void testCatResult() throws IOException { + CatCommandRequest request = new CatCommandRequest(); + request.setPath("a.txt"); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + new GitCatCommand(createContext(), null).getCatResult(request, baos); + } + + @Test(expected = NotFoundException.class) + public void testStream() throws IOException { + CatCommandRequest request = new CatCommandRequest(); + request.setPath("a.txt"); + new GitCatCommand(createContext(), null).getCatResultStream(request); + } + + @Override + protected String getZippedRepositoryResource() { + return "sonia/scm/repository/spi/scm-git-empty-repo.zip"; + } +}