Search in sources :

Example 1 with Repo

use of com.google.startupos.common.repo.Repo in project startup-os by google.

the class TestTool method run.

void run(String[] args) throws Exception {
    if (args.length > 0) {
        String command = args[0];
        Repo repo = repoFactory.create(System.getenv("BUILD_WORKSPACE_DIRECTORY"));
        if (command.equals("switchBranch")) {
            String branch = args[1];
            repo.switchBranch(branch);
        } else if (command.equals("getCommits")) {
            String branch = args[1];
            for (Commit commit : repo.getCommits(branch)) {
                System.out.println();
                System.out.println(commit);
            }
        } else if (command.equals("getFilesInCommit")) {
            String commitId = args[1];
            for (File file : repo.getFilesInCommit(commitId)) {
                System.out.println();
                System.out.println(file);
            }
        } else if (command.equals("getUncommittedFiles")) {
            for (File file : repo.getUncommittedFiles()) {
                System.out.println(file);
            }
        } else if (command.equals("merge")) {
            String branch = args[1];
            repo.merge(branch);
        } else if (command.equals("isMerged")) {
            String branch = args[1];
            repo.isMerged(branch);
        } else if (command.equals("removeBranch")) {
            String branch = args[1];
            repo.removeBranch(branch);
        } else if (command.equals("listBranches")) {
            for (String branch : repo.listBranches()) {
                System.out.println(branch);
            }
        } else if (command.equals("getTextDiff")) {
            File file1 = File.newBuilder().setCommitId(args[1]).setFilename("tools/aa/AaModule.java").build();
            File file2 = File.newBuilder().setCommitId(args[2]).setFilename("tools/aa/AaModule.java").build();
            System.out.println(repo.getTextDiff(file1, file2));
        } else if (command.equals("getFileContents")) {
            System.out.println(repo.getFileContents(args[1], args[2]));
        } else {
            System.out.println("Please specify command");
        }
    }
}
Also used : Commit(com.google.startupos.common.repo.Protos.Commit) Repo(com.google.startupos.common.repo.Repo) File(com.google.startupos.common.repo.Protos.File)

Example 2 with Repo

use of com.google.startupos.common.repo.Repo in project startup-os by google.

the class CodeReviewService method readWorkspaceFile.

private String readWorkspaceFile(File file) throws IOException {
    if (file.getCommitId().isEmpty()) {
        // It's a file in the local filesystem (not in a repo)
        String filePath = fileUtils.joinToAbsolutePath(basePath, "ws", file.getWorkspace(), file.getRepoId(), file.getFilename());
        return fileUtils.readFile(filePath);
    } else {
        // It's a file in a repo
        String repoPath = fileUtils.joinToAbsolutePath(basePath, "ws", file.getWorkspace(), file.getRepoId());
        Repo repo = repoFactory.create(repoPath);
        return repo.getFileContents(file.getCommitId(), file.getFilename());
    }
}
Also used : Repo(com.google.startupos.common.repo.Repo)

Example 3 with Repo

use of com.google.startupos.common.repo.Repo 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();
}
Also used : Repo(com.google.startupos.common.repo.Repo) IOException(java.io.IOException) File(com.google.startupos.common.repo.Protos.File)

Example 4 with Repo

use of com.google.startupos.common.repo.Repo 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();
}
Also used : DiffFilesResponse(com.google.startupos.tools.reviewer.local_server.service.Protos.DiffFilesResponse) Commit(com.google.startupos.common.repo.Protos.Commit) Repo(com.google.startupos.common.repo.Repo) IOException(java.io.IOException) File(com.google.startupos.common.repo.Protos.File)

Aggregations

Repo (com.google.startupos.common.repo.Repo)4 File (com.google.startupos.common.repo.Protos.File)3 Commit (com.google.startupos.common.repo.Protos.Commit)2 IOException (java.io.IOException)2 DiffFilesResponse (com.google.startupos.tools.reviewer.local_server.service.Protos.DiffFilesResponse)1