Add REST endpoint to get the reflog of a branch

Change-Id: I9c0b3fa430b46762774bfa43710c3bb01fd2799e
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2014-07-04 12:59:19 +02:00
parent 49098b8717
commit 87504d9a59
3 changed files with 186 additions and 0 deletions

View File

@@ -761,6 +761,77 @@ The content is returned as base64 encoded string.
Ly8gQ29weXJpZ2h0IChDKSAyMDEwIFRoZSBBbmRyb2lkIE9wZW4gU291cmNlIFByb2plY... Ly8gQ29weXJpZ2h0IChDKSAyMDEwIFRoZSBBbmRyb2lkIE9wZW4gU291cmNlIFByb2plY...
---- ----
[[get-reflog]]
=== Get Reflog
--
'GET /projects/link:#project-name[\{project-name\}]/branches/link:#branch-id[\{branch-id\}]/reflog'
--
Gets the reflog of a certain branch.
The caller must be project owner.
.Request
----
GET /projects/gerrit/branches/master/reflog HTTP/1.0
----
As response a list of link:#reflog-entry-info[ReflogEntryInfo] entities
is returned that describe the reflog entries. The reflog entries are
returned in reverse order.
.Response
----
HTTP/1.1 200 OK
Content-Disposition: attachment
Content-Type: application/json;charset=UTF-8
)]}'
[
{
"old_id": "976ced8f4fc0909d7e1584d18455299545881d60",
"new_id": "2eaa94bac536654eb592c941e33b91f925698d16",
"who": {
"name": "Jane Roe",
"email": "jane.roe@example.com",
"date": "2014-06-30 11:53:43.000000000",
"tz": 120
},
"comment": "merged: fast forward"
},
{
"old_id": "c271c6a7161b74f85560c5899c8c73ee89ca5e29",
"new_id": "976ced8f4fc0909d7e1584d18455299545881d60",
"who": {
"name": "John Doe",
"email": "john.doe@example.com",
"date": "2013-10-02 10:45:26.000000000",
"tz": 120
},
"comment": "merged: fast forward"
},
{
"old_id": "0000000000000000000000000000000000000000",
"new_id": "c271c6a7161b74f85560c5899c8c73ee89ca5e29",
"who": {
"name": "John Doe",
"email": "john.doe@example.com",
"date": "2013-09-30 19:08:44.000000000",
"tz": 120
},
"comment": ""
}
]
----
The get reflog endpoint also accepts a limit integer in the `n`
parameter. This limits the results to show the last `n` reflog entries.
Query the last 25 reflog entries.
----
GET /projects/gerrit/branches/master/reflog?n=25 HTTP/1.0
----
[[child-project-endpoints]] [[child-project-endpoints]]
== Child Project Endpoints == Child Project Endpoints
@@ -1554,6 +1625,21 @@ Message that should be used to commit the change of the project parent
in the `project.config` file to the `refs/meta/config` branch. in the `project.config` file to the `refs/meta/config` branch.
|============================= |=============================
[[reflog-entry-info]]
=== ReflogEntryInfo
The `ReflogEntryInfo` entity describes an entry in a reflog.
[options="header",width="50%",cols="1,6"]
|============================
|Field Name |Description
|`old_id` |The old commit ID.
|`new_id` |The new commit ID.
|`who` |
The user performing the change as a
link:rest-api-changes.html#git-person-info[GitPersonInfo] entity.
|`comment` |Comment of the reflog entry.
|============================
[[repository-statistics-info]] [[repository-statistics-info]]
=== RepositoryStatisticsInfo === RepositoryStatisticsInfo
The `RepositoryStatisticsInfo` entity contains information about The `RepositoryStatisticsInfo` entity contains information about

View File

@@ -0,0 +1,99 @@
// Copyright (C) 2014 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.project;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.gerrit.extensions.common.GitPerson;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.inject.Inject;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.ReflogEntry;
import org.eclipse.jgit.lib.ReflogReader;
import org.eclipse.jgit.lib.Repository;
import org.kohsuke.args4j.Option;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
public class GetReflog implements RestReadView<BranchResource> {
private final GitRepositoryManager repoManager;
@Option(name = "--limit", aliases = {"-n"}, metaVar = "CNT",
usage = "maximum number of reflog entries to list")
public GetReflog setLimit(int limit) {
this.limit = limit;
return this;
}
private int limit;
@Inject
public GetReflog(GitRepositoryManager repoManager) {
this.repoManager = repoManager;
}
@Override
public List<ReflogEntryInfo> apply(BranchResource rsrc) throws AuthException,
ResourceNotFoundException, RepositoryNotFoundException, IOException {
if (!rsrc.getControl().isOwner()) {
throw new AuthException("no project owner");
}
Repository repo = repoManager.openRepository(rsrc.getNameKey());
try {
ReflogReader r = repo.getReflogReader(rsrc.getRef());
if (r == null) {
throw new ResourceNotFoundException(rsrc.getRef());
}
List<ReflogEntry> entries =
limit > 0 ? r.getReverseEntries(limit) : r.getReverseEntries();
return Lists.transform(entries, new Function<ReflogEntry, ReflogEntryInfo>() {
@Override
public ReflogEntryInfo apply(ReflogEntry e) {
return new ReflogEntryInfo(e);
}});
} finally {
repo.close();
}
}
public static class ReflogEntryInfo {
public String oldId;
public String newId;
public GitPerson who;
public String comment;
public ReflogEntryInfo(ReflogEntry e) {
oldId = e.getOldId().getName();
newId = e.getNewId().getName();
PersonIdent ident = e.getWho();
who = new GitPerson();
who.name = ident.getName();
who.email = ident.getEmailAddress();
who.date = new Timestamp(ident.getWhen().getTime());
who.tz = ident.getTimeZoneOffset();
comment = e.getComment();
}
}
}

View File

@@ -59,6 +59,7 @@ public class Module extends RestApiModule {
get(BRANCH_KIND).to(GetBranch.class); get(BRANCH_KIND).to(GetBranch.class);
delete(BRANCH_KIND).to(DeleteBranch.class); delete(BRANCH_KIND).to(DeleteBranch.class);
install(new FactoryModuleBuilder().build(CreateBranch.Factory.class)); install(new FactoryModuleBuilder().build(CreateBranch.Factory.class));
get(BRANCH_KIND, "reflog").to(GetReflog.class);
child(BRANCH_KIND, "files").to(FilesCollection.class); child(BRANCH_KIND, "files").to(FilesCollection.class);
get(FILE_KIND, "content").to(GetContent.class); get(FILE_KIND, "content").to(GetContent.class);