Gerrit API: Allow to get file diff with options

Change-Id: Ia743166df6bc5f7bcaf88d426eba3a4bd1d02379
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2016-02-09 17:33:59 +01:00
parent 0b7c40fa76
commit a021dbf9fe
3 changed files with 103 additions and 0 deletions

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.extensions.api.changes;
import com.google.gerrit.extensions.client.DiffPreferencesInfo.Whitespace;
import com.google.gerrit.extensions.common.DiffInfo;
import com.google.gerrit.extensions.restapi.BinaryResult;
import com.google.gerrit.extensions.restapi.NotImplementedException;
@@ -33,6 +34,57 @@ public interface FileApi {
*/
DiffInfo diff(String base) throws RestApiException;
/**
* Creates a request to retrieve the diff. On the returned request formatting
* options for the diff can be set.
*/
DiffRequest diffRequest() throws RestApiException;
public abstract class DiffRequest {
private String base;
private Integer context;
private Boolean intraline;
private Whitespace whitespace;
public abstract DiffInfo get() throws RestApiException;
public DiffRequest withBase(String base) {
this.base = base;
return this;
}
public DiffRequest withContext(int context) {
this.context = context;
return this;
}
public DiffRequest withIntraline(boolean intraline) {
this.intraline = intraline;
return this;
}
public DiffRequest withWhitespace(Whitespace whitespace) {
this.whitespace = whitespace;
return this;
}
public String getBase() {
return base;
}
public Integer getContext() {
return context;
}
public Boolean getIntraline() {
return intraline;
}
public Whitespace getWhitespace() {
return whitespace;
}
}
/**
* A default implementation which allows source compatibility
* when adding new methods to the interface.
@@ -52,5 +104,10 @@ public interface FileApi {
public DiffInfo diff(String base) throws RestApiException {
throw new NotImplementedException();
}
@Override
public DiffRequest diffRequest() throws RestApiException {
throw new NotImplementedException();
}
}
}

View File

@@ -74,4 +74,35 @@ class FileApiImpl implements FileApi {
throw new RestApiException("Cannot retrieve diff", e);
}
}
@Override
public DiffRequest diffRequest() {
return new DiffRequest() {
@Override
public DiffInfo get() throws RestApiException {
return FileApiImpl.this.get(this);
}
};
}
private DiffInfo get(DiffRequest r) throws RestApiException {
GetDiff diff = getDiff.get();
if (r.getBase() != null) {
diff.setBase(r.getBase());
}
if (r.getContext() != null) {
diff.setContext(r.getContext());
}
if (r.getIntraline() != null) {
diff.setIntraline(r.getIntraline());
}
if (r.getWhitespace() != null) {
diff.setWhitespace(r.getWhitespace());
}
try {
return diff.apply(file).value();
} catch (IOException | InvalidChangeOperationException | OrmException e) {
throw new RestApiException("Cannot retrieve diff", e);
}
}
}

View File

@@ -272,6 +272,21 @@ public class GetDiff implements RestReadView<FileResource> {
return this;
}
public GetDiff setContext(int context) {
this.context = context;
return this;
}
public GetDiff setIntraline(boolean intraline) {
this.intraline = intraline;
return this;
}
public GetDiff setWhitespace(Whitespace whitespace) {
this.whitespace = whitespace;
return this;
}
private static class Content {
final List<ContentEntry> lines;
final SparseFileContent fileA;