diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/change/ChangeIT.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/change/ChangeIT.java index 1bde86833a..ead0b063ed 100644 --- a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/change/ChangeIT.java +++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/change/ChangeIT.java @@ -160,6 +160,16 @@ public class ChangeIT extends AbstractDaemonTest { System.setProperty("user.timezone", systemTimeZone); } + @Test + public void reflog() throws Exception { + // Tests are using DfsRepository which does not implement getReflogReader, + // so this will always fail. + // TODO: change this if/when DfsRepository#getReflogReader is implemented. + exception.expect(MethodNotAllowedException.class); + exception.expectMessage("reflog not supported"); + gApi.projects().name(project.get()).branch("master").reflog(); + } + @Test public void get() throws Exception { PushOneCommit.Result r = createChange(); diff --git a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/projects/BranchApi.java b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/projects/BranchApi.java index 995f41ab09..250eca4984 100644 --- a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/projects/BranchApi.java +++ b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/projects/BranchApi.java @@ -17,6 +17,7 @@ package com.google.gerrit.extensions.api.projects; import com.google.gerrit.extensions.restapi.BinaryResult; import com.google.gerrit.extensions.restapi.NotImplementedException; import com.google.gerrit.extensions.restapi.RestApiException; +import java.util.List; public interface BranchApi { BranchApi create(BranchInput in) throws RestApiException; @@ -28,6 +29,8 @@ public interface BranchApi { /** Returns the content of a file from the HEAD revision. */ BinaryResult file(String path) throws RestApiException; + List reflog() throws RestApiException; + /** * A default implementation which allows source compatibility when adding new methods to the * interface. @@ -52,5 +55,10 @@ public interface BranchApi { public BinaryResult file(String path) throws RestApiException { throw new NotImplementedException(); } + + @Override + public List reflog() { + throw new NotImplementedException(); + } } } diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/api/projects/BranchApiImpl.java b/gerrit-server/src/main/java/com/google/gerrit/server/api/projects/BranchApiImpl.java index 2fc78337ee..348b4e8876 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/api/projects/BranchApiImpl.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/api/projects/BranchApiImpl.java @@ -17,6 +17,7 @@ package com.google.gerrit.server.api.projects; import com.google.gerrit.extensions.api.projects.BranchApi; import com.google.gerrit.extensions.api.projects.BranchInfo; import com.google.gerrit.extensions.api.projects.BranchInput; +import com.google.gerrit.extensions.api.projects.ReflogEntryInfo; import com.google.gerrit.extensions.restapi.BinaryResult; import com.google.gerrit.extensions.restapi.IdString; import com.google.gerrit.extensions.restapi.RestApiException; @@ -27,11 +28,13 @@ import com.google.gerrit.server.project.DeleteBranch; import com.google.gerrit.server.project.FileResource; import com.google.gerrit.server.project.FilesCollection; import com.google.gerrit.server.project.GetContent; +import com.google.gerrit.server.project.GetReflog; import com.google.gerrit.server.project.ProjectResource; import com.google.gwtorm.server.OrmException; import com.google.inject.Inject; import com.google.inject.assistedinject.Assisted; import java.io.IOException; +import java.util.List; public class BranchApiImpl implements BranchApi { interface Factory { @@ -43,6 +46,7 @@ public class BranchApiImpl implements BranchApi { private final DeleteBranch deleteBranch; private final FilesCollection filesCollection; private final GetContent getContent; + private final GetReflog getReflog; private final String ref; private final ProjectResource project; @@ -53,6 +57,7 @@ public class BranchApiImpl implements BranchApi { DeleteBranch deleteBranch, FilesCollection filesCollection, GetContent getContent, + GetReflog getReflog, @Assisted ProjectResource project, @Assisted String ref) { this.branches = branches; @@ -60,6 +65,7 @@ public class BranchApiImpl implements BranchApi { this.deleteBranch = deleteBranch; this.filesCollection = filesCollection; this.getContent = getContent; + this.getReflog = getReflog; this.project = project; this.ref = ref; } @@ -102,6 +108,15 @@ public class BranchApiImpl implements BranchApi { } } + @Override + public List reflog() throws RestApiException { + try { + return getReflog.apply(resource()); + } catch (IOException e) { + throw new RestApiException("Cannot retrieve reflog", e); + } + } + private BranchResource resource() throws RestApiException, IOException { return branches.parse(project, IdString.fromDecoded(ref)); } diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/project/GetReflog.java b/gerrit-server/src/main/java/com/google/gerrit/server/project/GetReflog.java index abb7c5fbc8..81f08731be 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/project/GetReflog.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/project/GetReflog.java @@ -17,7 +17,9 @@ package com.google.gerrit.server.project; import com.google.common.collect.Lists; import com.google.gerrit.extensions.api.projects.ReflogEntryInfo; import com.google.gerrit.extensions.restapi.AuthException; +import com.google.gerrit.extensions.restapi.MethodNotAllowedException; import com.google.gerrit.extensions.restapi.ResourceNotFoundException; +import com.google.gerrit.extensions.restapi.RestApiException; import com.google.gerrit.extensions.restapi.RestReadView; import com.google.gerrit.server.CommonConverters; import com.google.gerrit.server.args4j.TimestampHandler; @@ -27,13 +29,16 @@ import java.io.IOException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; -import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.lib.ReflogEntry; import org.eclipse.jgit.lib.ReflogReader; import org.eclipse.jgit.lib.Repository; import org.kohsuke.args4j.Option; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class GetReflog implements RestReadView { + private static final Logger log = LoggerFactory.getLogger(GetReflog.class); + private final GitRepositoryManager repoManager; @Option( @@ -80,14 +85,20 @@ public class GetReflog implements RestReadView { } @Override - public List apply(BranchResource rsrc) - throws AuthException, ResourceNotFoundException, RepositoryNotFoundException, IOException { + public List apply(BranchResource rsrc) throws RestApiException, IOException { if (!rsrc.getControl().isOwner()) { throw new AuthException("not project owner"); } try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) { - ReflogReader r = repo.getReflogReader(rsrc.getRef()); + ReflogReader r; + try { + r = repo.getReflogReader(rsrc.getRef()); + } catch (UnsupportedOperationException e) { + String msg = "reflog not supported on repo " + rsrc.getNameKey().get(); + log.error(msg); + throw new MethodNotAllowedException(msg); + } if (r == null) { throw new ResourceNotFoundException(rsrc.getRef()); }