Support to retrieve the content of a file from a patch set via REST
By GET on /changes/<change-id>/revisions/<revision-id>/files/<patch-id>/content it is now possible to retrieve the content of a certain file from a patch set. The file content is returned as base64 encoded string so that also binary files can be handled. Signed-off-by: Edwin Kempin <edwin.kempin@sap.com> Change-Id: I429ce448c22b7b3b6ffd5503463676a798ea162a
This commit is contained in:
parent
def7f28354
commit
aef44b033d
@ -1847,6 +1847,30 @@ describes the published comment.
|
||||
}
|
||||
----
|
||||
|
||||
[[get-content]]
|
||||
Get Content
|
||||
~~~~~~~~~~~
|
||||
[verse]
|
||||
'GET /changes/link:#change-id[\{change-id\}]/revisions/link:#revision-id[\{revision-id\}]/files/link:#patch-id[\{patch-id\}]/content'
|
||||
|
||||
Gets the content of a file from a certain revision.
|
||||
|
||||
.Request
|
||||
----
|
||||
GET /changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/revisions/674ac754f91e64a0efb8087e59a176484bd534d1/files/gerrit-server%2Fsrc%2Fmain%2Fjava%2Fcom%2Fgoogle%2Fgerrit%2Fserver%2Fproject%2FRefControl.java/content HTTP/1.0
|
||||
----
|
||||
|
||||
The content is returned as base64 encoded string.
|
||||
|
||||
.Response
|
||||
----
|
||||
HTTP/1.1 200 OK
|
||||
Content-Disposition: attachment
|
||||
Content-Type: text/plain;charset=UTF-8
|
||||
|
||||
Ly8gQ29weXJpZ2h0IChDKSAyMDEwIFRoZSBBbmRyb2lkIE9wZW4gU291cmNlIFByb2plY...
|
||||
----
|
||||
|
||||
[[set-reviewed]]
|
||||
Set Reviewed
|
||||
~~~~~~~~~~~~
|
||||
|
@ -39,4 +39,8 @@ public class FileResource implements RestResource {
|
||||
Account.Id getAccountId() {
|
||||
return rev.getAccountId();
|
||||
}
|
||||
|
||||
RevisionResource getRevision() {
|
||||
return rev;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,89 @@
|
||||
// Copyright (C) 2013 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.server.change;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.BaseEncoding;
|
||||
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||
import com.google.gerrit.extensions.restapi.RestReadView;
|
||||
import com.google.gerrit.extensions.restapi.StreamingResponse;
|
||||
import com.google.gerrit.reviewdb.client.Project;
|
||||
import com.google.gerrit.server.git.GitRepositoryManager;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.ObjectLoader;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
import org.eclipse.jgit.treewalk.TreeWalk;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
|
||||
public class GetContent implements RestReadView<FileResource> {
|
||||
private final GitRepositoryManager repoManager;
|
||||
|
||||
@Inject
|
||||
GetContent(GitRepositoryManager repoManager) {
|
||||
this.repoManager = repoManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StreamingResponse apply(FileResource rsrc)
|
||||
throws ResourceNotFoundException, IOException {
|
||||
Project.NameKey project =
|
||||
rsrc.getRevision().getControl().getProject().getNameKey();
|
||||
Repository repo = repoManager.openRepository(project);
|
||||
try {
|
||||
RevWalk rw = new RevWalk(repo);
|
||||
try {
|
||||
RevCommit commit =
|
||||
rw.parseCommit(ObjectId.fromString(rsrc.getRevision().getPatchSet()
|
||||
.getRevision().get()));
|
||||
TreeWalk tw =
|
||||
TreeWalk.forPath(rw.getObjectReader(), rsrc.getPatchKey().get(),
|
||||
commit.getTree().getId());
|
||||
if (tw == null) {
|
||||
throw new ResourceNotFoundException();
|
||||
}
|
||||
try {
|
||||
final ObjectLoader loader = repo.open(tw.getObjectId(0));
|
||||
return new StreamingResponse() {
|
||||
@Override
|
||||
public String getContentType() {
|
||||
return "text/plain;charset=UTF-8";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stream(OutputStream out) throws IOException {
|
||||
OutputStream b64Out = BaseEncoding.base64().encodingStream(
|
||||
new OutputStreamWriter(out, Charsets.UTF_8));
|
||||
loader.copyTo(b64Out);
|
||||
b64Out.close();
|
||||
}
|
||||
};
|
||||
} finally {
|
||||
tw.release();
|
||||
}
|
||||
} finally {
|
||||
rw.release();
|
||||
}
|
||||
} finally {
|
||||
repo.close();
|
||||
}
|
||||
}
|
||||
}
|
@ -84,6 +84,7 @@ public class Module extends RestApiModule {
|
||||
child(REVISION_KIND, "files").to(Files.class);
|
||||
put(FILE_KIND, "reviewed").to(PutReviewed.class);
|
||||
delete(FILE_KIND, "reviewed").to(DeleteReviewed.class);
|
||||
get(FILE_KIND, "content").to(GetContent.class);
|
||||
|
||||
install(new FactoryModule() {
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user