use of com.google.startupos.common.repo.Protos.File in project startup-os by google.
the class CodeReviewService method getTextDiff.
@Override
public void getTextDiff(TextDiffRequest req, StreamObserver<TextDiffResponse> responseObserver) {
logger.atInfo().log("TextDiff request\n%s", req);
File file1 = req.getLeftFile();
File file2 = req.getRightFile();
validateFile(file1);
validateFile(file2);
if (!file1.getFilename().equals(file2.getFilename()) || !file1.getRepoId().equals(file2.getRepoId()) || !file1.getWorkspace().equals(file2.getWorkspace())) {
String message = "Files should have the same workspace, repo and filename:\n" + file1 + file2;
responseObserver.onError(Status.NOT_FOUND.withDescription(message).asException());
throw new IllegalArgumentException(message);
}
try {
file1 = getHeadFallbackIfNeeded(file1);
file2 = getHeadFallbackIfNeeded(file2);
Repo repo = getRepo(file1);
if (!repo.fileExists(file1.getCommitId(), file1.getFilename()) && !repo.fileExists(file2.getCommitId(), file2.getFilename())) {
responseObserver.onNext(TextDiffResponse.newBuilder().setTextDiff(TextDiff.getDefaultInstance()).build());
responseObserver.onError(Status.NOT_FOUND.withDescription(String.format("TextDiffRequest: %s", req)).asException());
} else if (!repo.fileExists(file1.getCommitId(), file1.getFilename())) {
String rightText = readTextFile(file2);
String diffString = "@@ -1 +1 @@\n" + addCharToEveryLine(rightText, '+');
responseObserver.onNext(TextDiffResponse.newBuilder().setTextDiff(textDifferencer.getTextDiff("", rightText, diffString)).build());
} else if (!repo.fileExists(file2.getCommitId(), file2.getFilename())) {
String leftText = readTextFile(file1);
String diffString = "@@ -1 +1 @@'n'" + addCharToEveryLine(leftText, '-');
responseObserver.onNext(TextDiffResponse.newBuilder().setTextDiff(textDifferencer.getTextDiff(leftText, "", diffString)).build());
} else {
String leftText = readTextFile(file1);
String rightText = readTextFile(file2);
String diffString = repo.getTextDiff(file1, file2);
responseObserver.onNext(TextDiffResponse.newBuilder().setTextDiff(textDifferencer.getTextDiff(leftText, rightText, diffString)).build());
}
} catch (IOException e) {
e.printStackTrace();
responseObserver.onNext(TextDiffResponse.newBuilder().setTextDiff(TextDiff.getDefaultInstance()).build());
responseObserver.onError(Status.NOT_FOUND.withDescription(String.format("TextDiffRequest: %s", req)).asException());
}
responseObserver.onCompleted();
}
use of com.google.startupos.common.repo.Protos.File in project startup-os by google.
the class CodeReviewService method addWorkspaceAndRepoToCommits.
private ImmutableList<Commit> addWorkspaceAndRepoToCommits(ImmutableList<Commit> commits, String workspace, String repoId) {
ImmutableList.Builder<Commit> result = ImmutableList.builder();
for (Commit commit : commits) {
ImmutableList<File> files = addWorkspaceAndRepoToFiles(commit.getFileList(), workspace, repoId);
commit = commit.toBuilder().clearFile().addAllFile(files).build();
result.add(commit);
}
return result.build();
}
use of com.google.startupos.common.repo.Protos.File in project startup-os by google.
the class CodeReviewService method getDiffFiles.
@Override
public void getDiffFiles(DiffFilesRequest request, StreamObserver<DiffFilesResponse> responseObserver) {
if (request.getWorkspace().isEmpty() || request.getDiffId() <= 0) {
responseObserver.onError(Status.INVALID_ARGUMENT.withDescription("Workspace must be set and diff_id must be > 0.\nrequest:\n" + request).asRuntimeException());
return;
}
logger.atInfo().log("DiffFiles request\n%s", request);
String workspacePath;
String workspace;
String branch;
boolean isHead;
if (workspaceExists(request.getWorkspace())) {
workspacePath = getWorkspacePath(request.getWorkspace());
workspace = request.getWorkspace();
branch = "D" + request.getDiffId();
isHead = false;
} else {
logger.atInfo().log("Workspace %s does not exist. Fallbacking to head.", request.getWorkspace());
workspacePath = fileUtils.joinToAbsolutePath(basePath, "head");
workspace = "";
branch = "remotes/origin/D" + request.getDiffId();
isHead = true;
}
DiffFilesResponse.Builder response = DiffFilesResponse.newBuilder();
try {
fileUtils.listContents(workspacePath).stream().map(path -> fileUtils.joinToAbsolutePath(workspacePath, path)).filter(fileUtils::folderExists).forEach(path -> {
String repoName = Paths.get(path).getFileName().toString();
Repo repo = repoFactory.create(path);
if (!isHead) {
ImmutableList<Commit> commits = addWorkspaceAndRepoToCommits(repo.getCommits(branch), workspace, repoName);
ImmutableList<File> uncommittedFiles = addWorkspaceAndRepoToFiles(repo.getUncommittedFiles(), workspace, repoName);
response.addBranchInfo(BranchInfo.newBuilder().setDiffId(request.getDiffId()).setRepoId(repoName).addAllCommit(commits).addAllUncommittedFile(uncommittedFiles).build());
} else {
ImmutableList<Commit> commits = ImmutableList.of();
if (repo.branchExists(branch)) {
commits = addWorkspaceAndRepoToCommits(repo.getCommits(branch), workspace, repoName);
} else {
logger.atInfo().log("Branch %s does not exist in head", branch);
}
response.addBranchInfo(BranchInfo.newBuilder().setDiffId(request.getDiffId()).setRepoId(repoName).addAllCommit(commits).build());
}
});
} catch (IOException e) {
e.printStackTrace();
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
use of com.google.startupos.common.repo.Protos.File in project startup-os by google.
the class CodeReviewServiceGetDiffFilesTest method testGetDiffFiles_whenFileIsRenamed.
@Test
public void testGetDiffFiles_whenFileIsRenamed() throws IOException {
// rename the file
Files.move(Paths.get(fileUtils.joinToAbsolutePath(repoPath, TEST_FILE)), Paths.get(fileUtils.joinToAbsolutePath(repoPath, "new_filename.txt")), StandardCopyOption.REPLACE_EXISTING);
repo.addFile("new_filename.txt");
repo.addFile(TEST_FILE);
File uncommittedFile = File.newBuilder().setFilename("new_filename.txt").setWorkspace("ws1").setRepoId("startup-os").setAction(File.Action.RENAME).setOriginalFilename("test_file.txt").setFilenameWithRepo("startup-os/new_filename.txt").build();
assertEquals(getExpectedResponseAddingUncommittedFile(uncommittedFile), getResponse());
}
use of com.google.startupos.common.repo.Protos.File in project startup-os by google.
the class TestTool method runGetTextDiff.
public void runGetTextDiff() {
File leftFile = File.newBuilder().setRepoId("startup-os").setCommitId("112da27b321ed6aa2ec1bc91f3918eb41d8a938c").setFilename("WORKSPACE").build();
File rightFile = File.newBuilder().setRepoId("startup-os").setCommitId("dbee600438f322e2db4085b4cd7a27212fe1ef40").setFilename("WORKSPACE").build();
System.out.println(getTextDiff(leftFile, rightFile));
}
Aggregations