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:
@@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
package com.google.gerrit.extensions.api.changes;
|
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.common.DiffInfo;
|
||||||
import com.google.gerrit.extensions.restapi.BinaryResult;
|
import com.google.gerrit.extensions.restapi.BinaryResult;
|
||||||
import com.google.gerrit.extensions.restapi.NotImplementedException;
|
import com.google.gerrit.extensions.restapi.NotImplementedException;
|
||||||
@@ -33,6 +34,57 @@ public interface FileApi {
|
|||||||
*/
|
*/
|
||||||
DiffInfo diff(String base) throws RestApiException;
|
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
|
* A default implementation which allows source compatibility
|
||||||
* when adding new methods to the interface.
|
* when adding new methods to the interface.
|
||||||
@@ -52,5 +104,10 @@ public interface FileApi {
|
|||||||
public DiffInfo diff(String base) throws RestApiException {
|
public DiffInfo diff(String base) throws RestApiException {
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DiffRequest diffRequest() throws RestApiException {
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,4 +74,35 @@ class FileApiImpl implements FileApi {
|
|||||||
throw new RestApiException("Cannot retrieve diff", e);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -272,6 +272,21 @@ public class GetDiff implements RestReadView<FileResource> {
|
|||||||
return this;
|
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 {
|
private static class Content {
|
||||||
final List<ContentEntry> lines;
|
final List<ContentEntry> lines;
|
||||||
final SparseFileContent fileA;
|
final SparseFileContent fileA;
|
||||||
|
|||||||
Reference in New Issue
Block a user